Loading...
Searching...
No Matches
EnvironmentKokkosAssembly.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
5
6#include <Kokkos_Core.hpp>
7#include <algorithm>
8#include <cstddef>
9#include <stdexcept>
10#include <string>
11#include <utility>
12
14
16
17namespace kd = ::gpr::linalg::detail;
18
19using ExecutionSpace = kd::exec_space;
20using MemorySpace = kd::mem_space;
21template <class T>
22using DeviceArray = Kokkos::View<T*, MemorySpace>;
23using Index = Kokkos::IndexType<std::size_t>;
24
25namespace detail {
26
27struct EnvironmentStorage {
28 DeviceArray<int> species;
29 DeviceArray<double> values, jacobians, inverse_variance;
30 gpr::detail::EnvironmentBatchView view{};
31};
32
33// Named, not anonymous: the CUDA compiler rejects extended lambdas in
34// functions with internal linkage.
35namespace impl {
36
37template <class T>
38DeviceArray<T> copy(const T* source, std::size_t count,
39 const std::string& label)
40{
41 DeviceArray<T> device(label, count);
42 Kokkos::View<const T*, Kokkos::HostSpace,
43 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
44 host(source, count);
45 kd::audited_deep_copy(device, host, count * sizeof(T),
46 kd::CopyRole::ToExecution);
47 return device;
48}
49
50template <class Work>
51void execute(std::size_t count, const Work& work)
52{
53 Kokkos::parallel_for(
54 "gpr environment work",
55 Kokkos::RangePolicy<ExecutionSpace, Index>(0, count),
56 KOKKOS_LAMBDA(const std::size_t index) { work(index); });
57 Kokkos::fence();
58}
59
62struct GramEntries {
63 gpr::detail::EnvironmentPairBatch batch;
64 const double* weighted;
65 double* grams;
66
67 KOKKOS_FUNCTION void operator()(std::size_t index) const
68 {
69 const auto left = batch.left, right = batch.right;
70 const std::size_t dl = left.dimensions, dr = right.dimensions;
71 const std::size_t entries = dl * dr;
72 const std::size_t matrix = index / entries, entry = index % entries;
73 const std::size_t i = entry % dl, j = entry / dl;
74 const std::size_t a = matrix % left.atoms;
75 const std::size_t pair = matrix / (left.atoms * batch.channel_count);
76 const double* A =
77 left.jacobians +
78 (std::size_t(batch.left_indices[pair]) * left.atoms + a) * dl *
79 left.features;
80 const double* W = weighted + matrix * dr * right.features;
81 double value = 0.0;
82 for (int f = 0; f < left.features; ++f)
83 value += A[f * dl + i] * W[f * dr + j];
84 grams[index] = value;
85 }
86};
87
88} // namespace impl
89
90using namespace impl;
91} // namespace detail
92
93DeviceEnvironments upload(const gpr::detail::EnvironmentBatchView& source)
94{
95 if (source.configurations <= 0 || source.atoms <= 0 ||
96 source.dimensions <= 0 || source.features <= 0 || !source.species ||
97 !source.values || !source.jacobians || !source.inverse_variance)
98 throw std::invalid_argument("invalid environment view");
99 kd::ensure_kokkos();
100 const std::size_t atoms = std::size_t(source.configurations) * source.atoms;
101 auto result = std::make_shared<detail::EnvironmentStorage>();
102 result->species = detail::copy(source.species, atoms, "gpr species");
103 result->values =
104 detail::copy(source.values, atoms * source.features, "gpr values");
105 result->jacobians = detail::copy(
106 source.jacobians, atoms * source.dimensions * source.features,
107 "gpr jacobians");
108 result->inverse_variance =
109 detail::copy(source.inverse_variance, atoms, "gpr inverse variance");
110 result->view = source;
111 result->view.species = result->species.data();
112 result->view.values = result->values.data();
113 result->view.jacobians = result->jacobians.data();
114 result->view.inverse_variance = result->inverse_variance.data();
115 return DeviceEnvironments(std::move(result));
116}
117
118std::size_t pairsPerChunk(const DeviceEnvironments& left_environments,
119 const DeviceEnvironments& right_environments,
120 int channel_count, std::size_t scratch_bytes)
121{
122 if (left_environments.empty() || right_environments.empty() ||
123 channel_count <= 0)
124 throw std::invalid_argument("invalid environment chunk shape");
125 const auto& left = left_environments.storage()->view;
126 const auto& right = right_environments.storage()->view;
127 const std::size_t al = left.atoms, ar = right.atoms;
128 const std::size_t dl = left.dimensions, dr = right.dimensions;
129 const std::size_t features = left.features;
130 const std::size_t nc = channel_count;
131 const std::size_t doubles = al * ar * (2 + dl + dr) +
132 nc * al * dr * features + nc * al * dl * dr +
133 nc * (1 + dl) * (1 + dr);
134 return std::max<std::size_t>(1, scratch_bytes / (doubles * sizeof(double)));
135}
136
137Eigen::VectorXd assemble(const DeviceEnvironments& left_environments,
138 const DeviceEnvironments& right_environments,
139 const gpr::detail::EnvironmentPairBatch& batch,
140 std::size_t scratch_bytes)
141{
142 if (left_environments.empty() || right_environments.empty())
143 throw std::invalid_argument("empty device environments");
144 const auto& left = *left_environments.storage();
145 const auto& right = *right_environments.storage();
146 if (batch.pair_count == 0) return Eigen::VectorXd(0);
147 if (!batch.left_indices || !batch.right_indices || !batch.channels ||
148 batch.channel_count <= 0 || left.view.features != right.view.features)
149 throw std::invalid_argument("invalid environment pairs");
150 for (std::size_t pair = 0; pair < batch.pair_count; ++pair)
151 if (batch.left_indices[pair] < 0 ||
152 batch.left_indices[pair] >= left.view.configurations ||
153 batch.right_indices[pair] < 0 ||
154 batch.right_indices[pair] >= right.view.configurations)
155 throw std::invalid_argument("environment pair index is invalid");
156
157 const std::size_t al = left.view.atoms, ar = right.view.atoms;
158 const std::size_t dl = left.view.dimensions, dr = right.view.dimensions;
159 const std::size_t features = left.view.features;
160 const std::size_t nc = batch.channel_count, count = batch.pair_count;
161 const std::size_t jet_size = al * ar * (2 + dl + dr);
162 const std::size_t weighted_size = nc * al * dr * features;
163 const std::size_t gram_size = nc * al * dl * dr;
164 const std::size_t block_size = nc * (1 + dl) * (1 + dr);
165
166 // Every work item reads one pair and writes one entry, so a chunk of
167 // pairs gives the bits the whole batch would. The scratch holds one
168 // chunk and is reused; only the finished blocks cross to the host.
169 const std::size_t chunk =
170 std::min(count, pairsPerChunk(left_environments, right_environments,
171 batch.channel_count, scratch_bytes));
172 const auto channels = detail::copy(batch.channels, nc, "gpr channels");
173 DeviceArray<double> jets(std::string("gpr jets"), chunk * jet_size);
174 DeviceArray<double> weighted(std::string("gpr weighted"),
175 chunk * weighted_size);
176 DeviceArray<double> grams(std::string("gpr grams"), chunk * gram_size);
177 DeviceArray<double> blocks(std::string("gpr blocks"), chunk * block_size);
178
179 Eigen::VectorXd result(static_cast<Eigen::Index>(count * block_size));
180 for (std::size_t first = 0; first < count; first += chunk) {
181 const std::size_t pairs = std::min(chunk, count - first);
182 const auto left_indices =
183 detail::copy(batch.left_indices + first, pairs, "gpr left indices");
184 const auto right_indices = detail::copy(batch.right_indices + first,
185 pairs, "gpr right indices");
186 const gpr::detail::EnvironmentPairBatch device{left.view,
187 right.view,
188 left_indices.data(),
189 right_indices.data(),
190 channels.data(),
191 batch.channel_count,
192 pairs,
193 batch.magnitude};
194 detail::execute(pairs * al * ar,
195 gpr::detail::EnvironmentPairJets{device, jets.data()});
196 detail::execute(pairs * weighted_size,
197 gpr::detail::EnvironmentWeightedJacobians{
198 device, jets.data(), weighted.data()});
199 detail::execute(
200 pairs * gram_size,
201 detail::GramEntries{device, weighted.data(), grams.data()});
202 detail::execute(pairs * block_size,
203 gpr::detail::EnvironmentBlockEntries{
204 device, jets.data(), grams.data(), blocks.data()});
205 const std::size_t length = pairs * block_size;
206 Kokkos::View<double*, Kokkos::HostSpace,
207 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
208 host(result.data() + first * block_size, length);
209 kd::audited_deep_copy(
210 host,
211 Kokkos::subview(blocks, std::make_pair(std::size_t(0), length)),
212 length * sizeof(double), kd::CopyRole::ToHost);
213 }
214 return result;
215}
216
217} // namespace gpr::kokkos_environment
Species-environment covariance blocks assembled as Kokkos kernels.
Environment data of one configuration set, in execution memory.
const detail::EnvironmentStorage * storage() const noexcept
DeviceEnvironments upload(const gpr::detail::EnvironmentBatchView &source)
Copy the environments of source to execution memory.
Kokkos::View< T *, MemorySpace > DeviceArray
std::size_t pairsPerChunk(const DeviceEnvironments &left_environments, const DeviceEnvironments &right_environments, int channel_count, std::size_t scratch_bytes)
Kokkos::IndexType< std::size_t > Index
Eigen::VectorXd assemble(const DeviceEnvironments &left_environments, const DeviceEnvironments &right_environments, const gpr::detail::EnvironmentPairBatch &batch, std::size_t scratch_bytes)