Loading...
Searching...
No Matches
gpr::kokkos_environment Namespace Reference

Classes

class  DeviceEnvironments
 Environment data of one configuration set, in execution memory. More...

Typedefs

using ExecutionSpace = kd::exec_space
using MemorySpace = kd::mem_space
template<class T>
using DeviceArray = Kokkos::View<T*, MemorySpace>
using Index = Kokkos::IndexType<std::size_t>

Functions

DeviceEnvironments upload (const gpr::detail::EnvironmentBatchView &source)
 Copy the environments of source to execution memory.
std::size_t pairsPerChunk (const DeviceEnvironments &left_environments, const DeviceEnvironments &right_environments, int channel_count, std::size_t scratch_bytes)
Eigen::VectorXd assemble (const DeviceEnvironments &left_environments, const DeviceEnvironments &right_environments, const gpr::detail::EnvironmentPairBatch &batch, std::size_t scratch_bytes)

Variables

constexpr std::size_t default_scratch_bytes = std::size_t(256) << 20

Typedef Documentation

◆ DeviceArray

template<class T>
using gpr::kokkos_environment::DeviceArray = Kokkos::View<T*, MemorySpace>

Definition at line 22 of file EnvironmentKokkosAssembly.cpp.

◆ ExecutionSpace

Definition at line 19 of file EnvironmentKokkosAssembly.cpp.

◆ Index

using gpr::kokkos_environment::Index = Kokkos::IndexType<std::size_t>

Definition at line 23 of file EnvironmentKokkosAssembly.cpp.

◆ MemorySpace

using gpr::kokkos_environment::MemorySpace = kd::mem_space

Definition at line 20 of file EnvironmentKokkosAssembly.cpp.

Function Documentation

◆ assemble()

Eigen::VectorXd gpr::kokkos_environment::assemble ( const DeviceEnvironments & left,
const DeviceEnvironments & right,
const gpr::detail::EnvironmentPairBatch & batch,
std::size_t scratch_bytes = default_scratch_bytes )
nodiscard

Covariance blocks of the pairs in batch, one (1 + dl) x (1 + dr) column-major block per pair and channel. batch names host index arrays; its environment views are replaced by left and right, which live in execution memory. The pairs run in chunks of pairsPerChunk(scratch_bytes) and each chunk's blocks are copied to the host after its last kernel. Every work item has one writer and a fixed summation order, so the result does not depend on the chunk size.

Definition at line 137 of file EnvironmentKokkosAssembly.cpp.

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}
const detail::EnvironmentStorage * storage() const noexcept
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)

◆ pairsPerChunk()

std::size_t gpr::kokkos_environment::pairsPerChunk ( const DeviceEnvironments & left,
const DeviceEnvironments & right,
int channel_count,
std::size_t scratch_bytes )
nodiscard

Pairs assemble() processes at once so that their scratch fits in scratch_bytes; at least one. A pair's scratch grows with the number of central atoms squared, so the batch is never allocated whole.

Definition at line 118 of file EnvironmentKokkosAssembly.cpp.

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}

◆ upload()

DeviceEnvironments gpr::kokkos_environment::upload ( const gpr::detail::EnvironmentBatchView & source)
nodiscard

Copy the environments of source to execution memory.

Definition at line 93 of file EnvironmentKokkosAssembly.cpp.

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}
Environment data of one configuration set, in execution memory.

Variable Documentation

◆ default_scratch_bytes

std::size_t gpr::kokkos_environment::default_scratch_bytes = std::size_t(256) << 20
inlineconstexpr

Execution memory, in bytes, that one call to assemble() may use for its per-pair scratch (jets, weighted Jacobians, Gram matrices and blocks).

Definition at line 70 of file EnvironmentKokkosAssembly.h.