Skip to content

Commit

Permalink
Merge pull request #6441 from nilsvu/fix_nvcc
Browse files Browse the repository at this point in the history
Fix compiling StaticCache with nvcc
  • Loading branch information
nilsdeppe authored Jan 17, 2025
2 parents 918181b + 4ba6b5e commit 6fc8fdb
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 136 deletions.
4 changes: 2 additions & 2 deletions src/Domain/Creators/TimeDependentOptions/ShapeMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ FromVolumeFileShapeSize<Object>::FromVolumeFileShapeSize(
std::string subfile_name)
: FromVolumeFile(std::move(h5_filename), std::move(subfile_name)),
transition_ends_at_cube(transition_ends_at_cube_in) {
const auto shape_fot_map =
retrieve_function_of_time({"Shape" + name(Object)}, std::nullopt);
const auto shape_fot_map = retrieve_function_of_time(
{std::string{"Shape" + name(Object)}}, std::nullopt);
const auto& shape_fot = shape_fot_map.at("Shape" + name(Object));

const double initial_time = shape_fot->time_bounds()[0];
Expand Down
248 changes: 114 additions & 134 deletions src/Utilities/StaticCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ class StaticCache {
std::tuple<size_t, tmpl::list<IntegralConstants...>> parameter0) const {
// note that the act of assigning to the specified function pointer type
// fixes the template arguments that need to be inferred.
static const std::array<const T& (StaticCache<Generator, T, Ranges...>::*)()
const,
static const std::array<const T& (StaticCache::*)() const,
sizeof...(IntegralConstants)>
cache{{&StaticCache<Generator, T, Ranges...>::unwrap_cache_combined<
IntegralConstantValues..., IntegralConstants>...}};
cache{{&StaticCache::unwrap_cache_combined<IntegralConstantValues...,
IntegralConstants>...}};
// The array `cache` holds pointers to member functions, so we dereference
// the pointer and invoke it on `this`.
return (this->*gsl::at(cache, std::get<0>(parameter0)))();
Expand All @@ -173,41 +172,39 @@ class StaticCache {
const T& unwrap_cache_combined(
std::tuple<size_t, tmpl::list<IntegralConstants0...>> parameter0,
std::tuple<size_t, tmpl::list<IntegralConstants1...>> parameter1) const {
constexpr size_t num0 = sizeof...(IntegralConstants0);
constexpr size_t num1 = sizeof...(IntegralConstants1);
constexpr size_t total_size = num0 * num1;
// note that the act of assigning to the specified function pointer type
// fixes the template arguments that need to be inferred.
static const std::array<
const T& (StaticCache<Generator, T, Ranges...>::*)() const,
sizeof...(IntegralConstants0) * sizeof...(IntegralConstants1)>
static const std::array<const T& (StaticCache::*)() const, total_size>
cache = []() {
std::array<const T& (StaticCache<Generator, T, Ranges...>::*)() const,
sizeof...(IntegralConstants0) *
sizeof...(IntegralConstants1)>
result;
std::array<const T& (StaticCache::*)() const, total_size> result;
size_t counter1 = 0;
const auto helper = [&counter1,
&result]<typename IntegralConstant1>() {
const auto helper1 = [&counter1,
&result]<typename IntegralConstant1>() {
size_t counter0 = 0;
// Note: Left-to-right ordering is guaranteed by the comma
// operator, otherwise we'd need to use another
// EXPAND_PACK_LEFT_TO_RIGHT
(((result[counter0 + sizeof...(IntegralConstants0) * counter1] =
&StaticCache<Generator, T, Ranges...>::unwrap_cache_combined<
IntegralConstantValues..., IntegralConstants0,
IntegralConstant1>),
++counter0),
...);
const auto helper0 = [&counter0, &counter1,
&result]<typename IntegralConstant0>() {
result[counter0 + num0 * counter1] =
&StaticCache::unwrap_cache_combined<IntegralConstantValues...,
IntegralConstant0,
IntegralConstant1>;
++counter0;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper0.template operator()<IntegralConstants0>());
++counter1;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper.template operator()<IntegralConstants1>());
helper1.template operator()<IntegralConstants1>());
return result;
}();

// The array `cache` holds pointers to member functions, so we dereference
// the pointer and invoke it on `this`.
return (this->*gsl::at(cache, std::get<0>(parameter0) +
sizeof...(IntegralConstants0) *
std::get<0>(parameter1)))();
num0 * std::get<0>(parameter1)))();
}

template <typename... IntegralConstantValues, typename... IntegralConstants0,
Expand All @@ -216,42 +213,36 @@ class StaticCache {
std::tuple<size_t, tmpl::list<IntegralConstants0...>> parameter0,
std::tuple<size_t, tmpl::list<IntegralConstants1...>> parameter1,
std::tuple<size_t, tmpl::list<IntegralConstants2...>> parameter2) const {
constexpr size_t num0 = sizeof...(IntegralConstants0);
constexpr size_t num1 = sizeof...(IntegralConstants1);
constexpr size_t num2 = sizeof...(IntegralConstants2);
constexpr size_t total_size = num0 * num1 * num2;
// note that the act of assigning to the specified function pointer type
// fixes the template arguments that need to be inferred.
static const std::array<
const T& (StaticCache<Generator, T, Ranges...>::*)() const,
sizeof...(IntegralConstants0) * sizeof...(IntegralConstants1) *
sizeof...(IntegralConstants2)>
static const std::array<const T& (StaticCache::*)() const, total_size>
cache = []() {
std::array<const T& (StaticCache<Generator, T, Ranges...>::*)() const,
sizeof...(IntegralConstants0) *
sizeof...(IntegralConstants1) *
sizeof...(IntegralConstants2)>
result;
std::array<const T& (StaticCache::*)() const, total_size> result;
size_t counter2 = 0;
const auto helper2 = [&counter2,
&result]<typename IntegralConstant2>() {
size_t counter1 = 0;
const auto helper = [&counter1, &counter2,
&result]<typename IntegralConstant1>() {
const auto helper1 = [&counter1, &counter2,
&result]<typename IntegralConstant1>() {
size_t counter0 = 0;
// Note: Left-to-right ordering is guaranteed by the comma
// operator, otherwise we'd need to use another
// EXPAND_PACK_LEFT_TO_RIGHT
(((result[counter0 +
sizeof...(IntegralConstants0) *
(counter1 +
sizeof...(IntegralConstants1) * counter2)] =
&StaticCache<Generator, T, Ranges...>::
unwrap_cache_combined<
IntegralConstantValues..., IntegralConstants0,
IntegralConstant1, IntegralConstant2>),
++counter0),
...);
const auto helper0 = [&counter0, &counter1, &counter2,
&result]<typename IntegralConstant0>() {
result[counter0 + num0 * (counter1 + num1 * counter2)] =
&StaticCache::unwrap_cache_combined<
IntegralConstantValues..., IntegralConstant0,
IntegralConstant1, IntegralConstant2>;
++counter0;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper0.template operator()<IntegralConstants0>());
++counter1;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper.template operator()<IntegralConstants1>());
helper1.template operator()<IntegralConstants1>());
++counter2;
};
EXPAND_PACK_LEFT_TO_RIGHT(
Expand All @@ -261,11 +252,10 @@ class StaticCache {

// The array `cache` holds pointers to member functions, so we dereference
// the pointer and invoke it on `this`.
return (this->*gsl::at(cache, std::get<0>(parameter0) +
sizeof...(IntegralConstants0) *
(std::get<0>(parameter1) +
sizeof...(IntegralConstants1) *
std::get<0>(parameter2))))();
return (
this->*gsl::at(cache, std::get<0>(parameter0) +
num0 * (std::get<0>(parameter1) +
num1 * std::get<0>(parameter2))))();
}

template <typename... IntegralConstantValues, typename... IntegralConstants0,
Expand All @@ -276,48 +266,44 @@ class StaticCache {
std::tuple<size_t, tmpl::list<IntegralConstants1...>> parameter1,
std::tuple<size_t, tmpl::list<IntegralConstants2...>> parameter2,
std::tuple<size_t, tmpl::list<IntegralConstants3...>> parameter3) const {
constexpr size_t num0 = sizeof...(IntegralConstants0);
constexpr size_t num1 = sizeof...(IntegralConstants1);
constexpr size_t num2 = sizeof...(IntegralConstants2);
constexpr size_t num3 = sizeof...(IntegralConstants3);
constexpr size_t total_size = num0 * num1 * num2 * num3;
// note that the act of assigning to the specified function pointer type
// fixes the template arguments that need to be inferred.
static const std::array<
const T& (StaticCache<Generator, T, Ranges...>::*)() const,
sizeof...(IntegralConstants0) * sizeof...(IntegralConstants1) *
sizeof...(IntegralConstants2) * sizeof...(IntegralConstants3)>
static const std::array<const T& (StaticCache::*)() const, total_size>
cache = []() {
std::array<
const T& (StaticCache<Generator, T, Ranges...>::*)() const,
sizeof...(IntegralConstants0) * sizeof...(IntegralConstants1) *
sizeof...(IntegralConstants2) * sizeof...(IntegralConstants3)>
result;
std::array<const T& (StaticCache::*)() const, total_size> result;
size_t counter3 = 0;
const auto helper3 = [&counter3,
&result]<typename IntegralConstant3>() {
size_t counter2 = 0;
const auto helper2 = [&counter2, &counter3,
&result]<typename IntegralConstant2>() {
size_t counter1 = 0;
const auto helper = [&counter1, &counter2, &counter3,
&result]<typename IntegralConstant1>() {
const auto helper1 = [&counter1, &counter2, &counter3,
&result]<typename IntegralConstant1>() {
size_t counter0 = 0;
// Note: Left-to-right ordering is guaranteed by the comma
// operator, otherwise we'd need to use another
// EXPAND_PACK_LEFT_TO_RIGHT
(((result[counter0 +
sizeof...(IntegralConstants0) *
(counter1 +
sizeof...(IntegralConstants1) *
(counter2 + sizeof...(IntegralConstants2) *
counter3))] =
&StaticCache<Generator, T, Ranges...>::
unwrap_cache_combined<
IntegralConstantValues..., IntegralConstants0,
IntegralConstant1, IntegralConstant2,
IntegralConstant3>),
++counter0),
...);
const auto helper0 = [&counter0, &counter1, &counter2,
&counter3,
&result]<typename IntegralConstant0>() {
result[counter0 +
num0 *
(counter1 + num1 * (counter2 + num2 * counter3))] =
&StaticCache::unwrap_cache_combined<
IntegralConstantValues..., IntegralConstant0,
IntegralConstant1, IntegralConstant2,
IntegralConstant3>;
++counter0;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper0.template operator()<IntegralConstants0>());
++counter1;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper.template operator()<IntegralConstants1>());
helper1.template operator()<IntegralConstants1>());
++counter2;
};
EXPAND_PACK_LEFT_TO_RIGHT(
Expand All @@ -329,16 +315,14 @@ class StaticCache {
return result;
}();

// The array `cache` holds pointers to member functions, so we dereference
// the pointer and invoke it on `this`.
// The array `cache` holds pointers to member functions, so we
// dereference the pointer and invoke it on `this`.
return (
this->*gsl::at(cache, std::get<0>(parameter0) +
sizeof...(IntegralConstants0) *
(std::get<0>(parameter1) +
sizeof...(IntegralConstants1) *
(std::get<0>(parameter2) +
sizeof...(IntegralConstants2) *
std::get<0>(parameter3)))))();
this->*gsl::at(cache,
std::get<0>(parameter0) +
num0 * (std::get<0>(parameter1) +
num1 * (std::get<0>(parameter2) +
num2 * std::get<0>(parameter3)))))();
}

template <typename... IntegralConstantValues, typename... IntegralConstants0,
Expand All @@ -352,19 +336,18 @@ class StaticCache {
std::tuple<size_t, tmpl::list<IntegralConstants3...>> parameter3,
std::tuple<size_t, tmpl::list<IntegralConstants4...>> parameter4,
const Args&... parameters) const {
constexpr size_t num0 = sizeof...(IntegralConstants0);
constexpr size_t num1 = sizeof...(IntegralConstants1);
constexpr size_t num2 = sizeof...(IntegralConstants2);
constexpr size_t num3 = sizeof...(IntegralConstants3);
constexpr size_t num4 = sizeof...(IntegralConstants4);
constexpr size_t total_size = num0 * num1 * num2 * num3 * num4;
// note that the act of assigning to the specified function pointer type
// fixes the template arguments that need to be inferred.
static const std::array<
const T& (StaticCache<Generator, T, Ranges...>::*)(Args...) const,
sizeof...(IntegralConstants0) * sizeof...(IntegralConstants1) *
sizeof...(IntegralConstants2) * sizeof...(IntegralConstants3) *
sizeof...(IntegralConstants3)>
static const std::array<const T& (StaticCache::*)(Args...) const,
total_size>
cache = []() {
std::array<
const T& (StaticCache<Generator, T, Ranges...>::*)(Args...) const,
sizeof...(IntegralConstants0) * sizeof...(IntegralConstants1) *
sizeof...(IntegralConstants2) *
sizeof...(IntegralConstants3) * sizeof...(IntegralConstants3)>
std::array<const T& (StaticCache::*)(Args...) const, total_size>
result;
size_t counter4 = 0;
const auto helper4 = [&counter4,
Expand All @@ -376,32 +359,30 @@ class StaticCache {
const auto helper2 = [&counter2, &counter3, &counter4,
&result]<typename IntegralConstant2>() {
size_t counter1 = 0;
const auto helper = [&counter1, &counter2, &counter3, &counter4,
&result]<typename IntegralConstant1>() {
const auto helper1 = [&counter1, &counter2, &counter3,
&counter4,
&result]<typename IntegralConstant1>() {
size_t counter0 = 0;
// Note: Left-to-right ordering is guaranteed by the comma
// operator, otherwise we'd need to use another
// EXPAND_PACK_LEFT_TO_RIGHT
(((result[counter0 +
sizeof...(IntegralConstants0) *
(counter1 +
sizeof...(IntegralConstants1) *
(counter2 +
sizeof...(IntegralConstants2) *
(counter3 +
sizeof...(IntegralConstants3) *
counter4)))] =
&StaticCache<Generator, T, Ranges...>::
unwrap_cache_combined<
IntegralConstantValues..., IntegralConstants0,
IntegralConstant1, IntegralConstant2,
IntegralConstant3, IntegralConstant4>),
++counter0),
...);
const auto helper0 = [&counter0, &counter1, &counter2,
&counter3, &counter4,
&result]<typename IntegralConstant0>() {
result[counter0 +
num0 *
(counter1 +
num1 * (counter2 +
num2 * (counter3 + num3 * counter4)))] =
&StaticCache::unwrap_cache_combined<
IntegralConstantValues..., IntegralConstant0,
IntegralConstant1, IntegralConstant2,
IntegralConstant3, IntegralConstant4>;
++counter0;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper0.template operator()<IntegralConstants0>());
++counter1;
};
EXPAND_PACK_LEFT_TO_RIGHT(
helper.template operator()<IntegralConstants1>());
helper1.template operator()<IntegralConstants1>());
++counter2;
};
EXPAND_PACK_LEFT_TO_RIGHT(
Expand All @@ -419,16 +400,15 @@ class StaticCache {

// The array `cache` holds pointers to member functions, so we dereference
// the pointer and invoke it on `this`.
return (this->*gsl::at(cache,
std::get<0>(parameter0) +
sizeof...(IntegralConstants0) *
(std::get<0>(parameter1) +
sizeof...(IntegralConstants1) *
(std::get<0>(parameter2) +
sizeof...(IntegralConstants2) *
(std::get<0>(parameter3) +
sizeof...(IntegralConstants3) *
std::get<0>(parameter4))))))(
return (
this->*gsl::at(
cache,
std::get<0>(parameter0) +
num0 *
(std::get<0>(parameter1) +
num1 * (std::get<0>(parameter2) +
num2 * (std::get<0>(parameter3) +
num3 * std::get<0>(parameter4))))))(
parameters...);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ > 10 && __GNUC__ < 14
Expand Down

0 comments on commit 6fc8fdb

Please sign in to comment.