Loading...
Searching...
No Matches
KokkosAuditedCopy.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5// Every Kokkos::deep_copy in the library goes through audited_deep_copy, so
6// the transfer audit (kokkosTransferAuditStats) sees each copy. A raw
7// Kokkos::deep_copy elsewhere would leave a transfer pin reading zero for a
8// copy that happened. The KokkosResidentOperation tests catch such a copy
9// through the copies Kokkos itself reports, and
10// scripts/check_kokkos_deep_copy.py refuses the spelling outside this header.
11
12#include <Kokkos_Core.hpp>
13#include <cstddef>
14#include <type_traits>
15
16namespace gpr::linalg::detail {
17
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;
23
28enum class KokkosCopyContext { Unaudited, Audited, ThirdParty };
29
30KokkosCopyContext kokkosCopyContext() noexcept;
31
32class KokkosCopyScope {
33public:
34 explicit KokkosCopyScope(KokkosCopyContext context) noexcept;
35 ~KokkosCopyScope();
36 KokkosCopyScope(const KokkosCopyScope&) = delete;
37 KokkosCopyScope& operator=(const KokkosCopyScope&) = delete;
38
39private:
40 KokkosCopyContext previous_;
41};
42
43template <class MemorySpace>
44inline constexpr bool is_host_space_v =
45 std::is_same_v<std::remove_cv_t<MemorySpace>, Kokkos::HostSpace>;
46
50enum class CopyRole { Internal, ToExecution, ToHost };
51
52template <class View>
53inline std::size_t view_bytes(const View& view)
54{
55 return view.span() * sizeof(typename View::value_type);
56}
57
58template <class DestinationView, class SourceView>
59inline void record_audited_copy(std::size_t bytes, CopyRole role)
60{
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);
71 }
72 recordKokkosDeepCopy(source_host, destination_host, bytes);
73}
74
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,
79 CopyRole role)
80{
81 record_audited_copy<DestinationView, SourceView>(bytes, role);
82 const KokkosCopyScope audited(KokkosCopyContext::Audited);
83 Kokkos::deep_copy(destination, source);
84}
85
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,
91 CopyRole role)
92{
93 record_audited_copy<DestinationView, SourceView>(bytes, role);
94 const KokkosCopyScope audited(KokkosCopyContext::Audited);
95 Kokkos::deep_copy(execution, destination, source);
96}
97
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)
103{
104 audited_deep_copy(execution, destination, source, view_bytes(destination),
105 role);
106}
107
110template <class ExecutionSpace, class DestinationView, class Value,
111 std::enable_if_t<!Kokkos::is_view_v<Value>, int> = 0>
112inline void audited_deep_copy(const ExecutionSpace& execution,
113 const DestinationView& destination,
114 const Value& value)
115{
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);
120}
121
122} // namespace gpr::linalg::detail