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
167
168
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");
175 chunk * weighted_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)