core
Shared MPI block decomposition + asynchronous ghost-layer exchange (header-only C++20)
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1// core — shared Kokkos device-memory abstraction for the suite's portable path.
2//
3// One place defines the execution/memory spaces, the 1D device array the halo exchange operates on
4// (it gathers/scatters by flat x-fastest local index, so a contiguous per-cell field is a View<T>),
5// and the 3D structured field (x-fastest => Kokkos::LayoutLeft). Including this header requires
6// Kokkos (build with -DPECLET_CORE_ENABLE_KOKKOS=ON); the CPU and legacy-CUDA paths do NOT pull it
7// in.
8#ifndef PECLET_CORE_COMMON_VIEW_HPP
9#define PECLET_CORE_COMMON_VIEW_HPP
10
11#include <Kokkos_Core.hpp>
12#include <string>
13#include <utility>
14#include <vector>
15
17
18namespace peclet::core {
19
20using ExecSpace = Kokkos::DefaultExecutionSpace;
21using MemSpace = ExecSpace::memory_space;
22
25template <class T>
26using View = Kokkos::View<T*, MemSpace>;
27
30template <class T>
31using HostView = decltype(Kokkos::create_mirror_view(std::declval<View<T>>()));
32
34using IndexView = Kokkos::View<Index*, MemSpace>;
35
39template <class T>
40using Field3D = Kokkos::View<T***, Kokkos::LayoutLeft, MemSpace>;
41
43template <class T>
44inline View<T> toDevice(const std::vector<T>& h, const std::string& label) {
45 View<T> d(Kokkos::view_alloc(label, Kokkos::WithoutInitializing), h.size());
46 if (!h.empty()) {
47 Kokkos::View<const T*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>> hv(h.data(),
48 h.size());
49 Kokkos::deep_copy(d, hv);
50 }
51 return d;
52}
53
62template <class V>
63std::vector<std::remove_const_t<typename V::value_type>> toVector(const V& view) {
64 using T = std::remove_const_t<typename V::value_type>;
65 std::vector<T> out(view.size());
66 if (view.size()) {
67 // `view` may be a STRIDED subview (e.g. [0:numReal] of a larger LayoutLeft device View once the
68 // SoA has grown past numReal): a cross-space (device->host) deep_copy of a strided layout has no
69 // valid copy mechanism and aborts. Repack into a CONTIGUOUS buffer in the SOURCE space first
70 // (same-space deep_copy handles the strided->contiguous gather via a kernel), then hop to host.
71 // A no-op memcpy when `view` is already contiguous. Only extent(0) is a runtime dimension for the
72 // suite's particle getters (float* / float*[N]).
73 Kokkos::View<typename V::non_const_data_type, typename V::memory_space> contig(
74 Kokkos::view_alloc(Kokkos::WithoutInitializing, "peclet::core::toVector::contig"),
75 view.extent(0));
76 Kokkos::deep_copy(contig, view);
77 auto host = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), contig);
78 Kokkos::LayoutRight layout;
79 for (std::size_t i = 0; i < V::rank; ++i)
80 layout.dimension[i] = view.extent(i);
81 Kokkos::View<typename V::non_const_data_type, Kokkos::LayoutRight, Kokkos::HostSpace,
82 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
83 dst(out.data(), layout);
84 Kokkos::deep_copy(dst, host);
85 }
86 return out;
87}
88
89} // namespace peclet::core
90
91#endif // PECLET_CORE_COMMON_VIEW_HPP
decltype(Kokkos::create_mirror_view(std::declval< View< T > >())) HostView
Host-accessible mirror of View<T> (identical to View<T> on host backends).
Definition view.hpp:31
View< T > toDevice(const std::vector< T > &h, const std::string &label)
Upload a host std::vector into a freshly-sized device View (empty vector => empty view).
Definition view.hpp:44
ExecSpace::memory_space MemSpace
Definition view.hpp:21
Kokkos::View< T *, MemSpace > View
1D device array.
Definition view.hpp:26
Kokkos::DefaultExecutionSpace ExecSpace
Definition view.hpp:20
Kokkos::View< Index *, MemSpace > IndexView
Device array of grid/particle indices (the matched send/recv/self-copy lists).
Definition view.hpp:34
Kokkos::View< T ***, Kokkos::LayoutLeft, MemSpace > Field3D
3D structured field in the suite's x-fastest convention: LayoutLeft makes the leftmost (x) index cont...
Definition view.hpp:40
std::vector< std::remove_const_t< typename V::value_type > > toVector(const V &view)
Copy a (host- or device-resident) Kokkos View of any rank into a contiguous host std::vector,...
Definition view.hpp:63