Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ‘const’ in Neighbor Table Value Types #1796

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions include/gridtools/storage/data_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,38 +173,52 @@ namespace gridtools {
auto const_host_view() const { return const_target_view(); }
};

template <class Traits, class T, class Info, class Kind, bool IsHostRefrenceable>
class data_store<Traits, T const, Info, Kind, true, IsHostRefrenceable>
: public base<Traits, T const, Info, Kind> {

template <class>
struct is_host_refrenceable : std::bool_constant<IsHostRefrenceable> {};

template <class Initializer, std::enable_if_t<!is_host_refrenceable<Initializer>::value, int> = 0>
void init(Initializer const &initializer) {
auto host_ptr = std::make_unique<T[]>(this->info().length());
initializer(host_ptr.get(), typename data_store::layout_t(), this->info());
traits::update_target<Traits>(this->raw_target_ptr(), host_ptr.get(), this->info().length());
}
template <class Traits, class T, class Info, class Kind>
class data_store<Traits, T const, Info, Kind, true, false> : public base<Traits, T const, Info, Kind> {
std::unique_ptr<T[]> m_host_ptr;

template <class Initializer, std::enable_if_t<is_host_refrenceable<Initializer>::value, int> = 0>
void init(Initializer const &initializer) {
initializer(this->raw_target_ptr(), typename data_store::layout_t(), this->info());
public:
template <class Halos>
data_store(std::string, Info, Halos const &, uninitialized const &) = delete;

template <class Initializer, class Halos>
data_store(std::string name, Info info, Halos const &halos, Initializer const &initializer)
: data_store::base(std::move(name), std::move(info), halos),
m_host_ptr(std::make_unique<T[]>(this->info().length())) {
initializer(m_host_ptr.get(), typename data_store::layout_t(), this->info());
traits::update_target<Traits>(this->raw_target_ptr(), m_host_ptr.get(), this->info().length());
}

T const *get_target_ptr() const { return this->raw_target_ptr(); }
T const *get_const_target_ptr() const { return this->raw_target_ptr(); }
auto target_view() const { return traits::make_target_view<Traits>(get_target_ptr(), this->info()); }
auto const_target_view() const { return target_view(); }

T const *get_host_ptr() const { return m_host_ptr.get(); }
T const *get_const_host_ptr() const { return get_host_ptr(); }
auto host_view() const { return make_host_view(get_host_ptr(), this->info()); }
auto const_host_view() const { return host_view(); }
};

template <class Traits, class T, class Info, class Kind>
class data_store<Traits, T const, Info, Kind, true, true> : public base<Traits, T const, Info, Kind> {
public:
template <class Halos>
data_store(std::string, Info, Halos const &, uninitialized const &) = delete;

template <class Initializer, class Halos>
data_store(std::string name, Info info, Halos const &halos, Initializer const &initializer)
: base<Traits, T const, Info, Kind>(std::move(name), std::move(info), halos) {
init(initializer);
initializer(this->raw_target_ptr(), typename data_store::layout_t(), this->info());
}
T const *get_target_ptr() const { return this->raw_target_ptr(); }
T const *get_const_target_ptr() const { return get_target_ptr(); }
auto target_view() const { return traits::make_target_view<Traits>(get_target_ptr(), this->info()); }
auto get_const_target_ptr() const { return get_target_ptr(); }
auto const_target_view() const { return target_view(); }
T const *get_host_ptr() { return get_target_ptr(); }
T const *get_const_host_ptr() { return get_target_ptr(); }
auto host_view() const { return target_view(); }
auto const_host_view() const { return target_view(); }
};

template <class>
Expand Down
4 changes: 2 additions & 2 deletions tests/include/fn_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ namespace gridtools {
}

auto v2e_table() const {
return storage::builder<StorageTraits>.dimensions(nvertices(), max_v2e_neighbors_t()).template type<int>().initializer(v2e_initializer()).unknown_id().build();
return storage::builder<StorageTraits>.dimensions(nvertices(), max_v2e_neighbors_t()).template type<int const>().initializer(v2e_initializer()).unknown_id().build();
}

auto e2v_table() const {
return storage::builder<StorageTraits>.dimensions(nedges(), max_e2v_neighbors_t()).template type<int>().initializer(e2v_initializer()).unknown_id().build();
return storage::builder<StorageTraits>.dimensions(nedges(), max_e2v_neighbors_t()).template type<int const>().initializer(e2v_initializer()).unknown_id().build();
}
};

Expand Down
Loading