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");
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");
104 detail::copy(source.values, atoms * source.features,
"gpr values");
105 result->jacobians = detail::copy(
106 source.jacobians, atoms * source.dimensions * source.features,
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();
120 int channel_count, std::size_t scratch_bytes)
122 if (left_environments.
empty() || right_environments.
empty() ||
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)));
139 const gpr::detail::EnvironmentPairBatch& batch,
140 std::size_t scratch_bytes)
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");
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);
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);
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,
189 right_indices.data(),
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()});
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(
211 Kokkos::subview(blocks, std::make_pair(std::size_t(0), length)),
212 length *
sizeof(
double), kd::CopyRole::ToHost);