12#include <Kokkos_Core.hpp>
16namespace gpr::linalg::detail {
18void recordKokkosDeepCopy(
bool source_host,
bool destination_host,
19 std::size_t bytes)
noexcept;
20void recordKokkosMirrorCreate(std::size_t bytes)
noexcept;
21void recordKokkosBoundaryToExecution(std::size_t bytes)
noexcept;
22void recordKokkosBoundaryToHost(std::size_t bytes)
noexcept;
28enum class KokkosCopyContext { Unaudited, Audited, ThirdParty };
30KokkosCopyContext kokkosCopyContext() noexcept;
32class KokkosCopyScope {
34 explicit KokkosCopyScope(KokkosCopyContext context)
noexcept;
36 KokkosCopyScope(
const KokkosCopyScope&) =
delete;
37 KokkosCopyScope& operator=(
const KokkosCopyScope&) =
delete;
40 KokkosCopyContext previous_;
43template <
class MemorySpace>
44inline constexpr bool is_host_space_v =
45 std::is_same_v<std::remove_cv_t<MemorySpace>, Kokkos::HostSpace>;
50enum class CopyRole { Internal, ToExecution, ToHost };
53inline std::size_t view_bytes(
const View& view)
55 return view.span() *
sizeof(
typename View::value_type);
58template <
class DestinationView,
class SourceView>
59inline void record_audited_copy(std::size_t bytes, CopyRole role)
61 constexpr bool source_host =
62 is_host_space_v<typename SourceView::memory_space>;
63 constexpr bool destination_host =
64 is_host_space_v<typename DestinationView::memory_space>;
65 if (role == CopyRole::ToExecution) {
66 recordKokkosBoundaryToExecution(bytes);
67 if (source_host && destination_host) recordKokkosMirrorCreate(bytes);
68 }
else if (role == CopyRole::ToHost) {
69 recordKokkosBoundaryToHost(bytes);
70 if (source_host && destination_host) recordKokkosMirrorCreate(bytes);
72 recordKokkosDeepCopy(source_host, destination_host, bytes);
75template <
class DestinationView,
class SourceView,
76 std::enable_if_t<Kokkos::is_view_v<SourceView>,
int> = 0>
77inline void audited_deep_copy(
const DestinationView& destination,
78 const SourceView& source, std::size_t bytes,
81 record_audited_copy<DestinationView, SourceView>(bytes, role);
82 const KokkosCopyScope audited(KokkosCopyContext::Audited);
83 Kokkos::deep_copy(destination, source);
86template <
class ExecutionSpace,
class DestinationView,
class SourceView,
87 std::enable_if_t<Kokkos::is_view_v<SourceView>,
int> = 0>
88inline void audited_deep_copy(
const ExecutionSpace& execution,
89 const DestinationView& destination,
90 const SourceView& source, std::size_t bytes,
93 record_audited_copy<DestinationView, SourceView>(bytes, role);
94 const KokkosCopyScope audited(KokkosCopyContext::Audited);
95 Kokkos::deep_copy(execution, destination, source);
98template <
class ExecutionSpace,
class DestinationView,
class SourceView,
99 std::enable_if_t<Kokkos::is_view_v<SourceView>,
int> = 0>
100inline void audited_deep_copy(
const ExecutionSpace& execution,
101 const DestinationView& destination,
102 const SourceView& source, CopyRole role)
104 audited_deep_copy(execution, destination, source, view_bytes(destination),
111 std::enable_if_t<!Kokkos::is_view_v<Value>,
int> = 0>
112inline void audited_deep_copy(
const ExecutionSpace& execution,
113 const DestinationView& destination,
116 record_audited_copy<DestinationView, DestinationView>(
117 view_bytes(destination), CopyRole::Internal);
118 const KokkosCopyScope audited(KokkosCopyContext::Audited);
119 Kokkos::deep_copy(execution, destination, value);
kd::exec_space ExecutionSpace
std::vector< Real > Value