Loading...
Searching...
No Matches
gpr::linalg::KokkosMolecularPosterior::Impl Struct Reference

Public Member Functions

PairTopology topology () const
 Impl (const AtomsConfiguration &configuration, double far)
V::matrix upload (const EigenMatrix &host, const char *label)
V::vector upload (const Eigen::VectorXd &host, const char *label)
bool describe (DescriptorRows &out, const EigenMatrix &input, const Eigen::VectorXd &channels)
void release ()

Public Attributes

Exec exec
AtomsConfiguration conf
Cell cell
double far_radius
double cutoff
double width
double nugget = 0.0
int moving
int types
Kokkos::View< Pair *, V::memory_space > pairs
V::ivector first
V::ivector second
V::ivector pair_type
V::ivector atom_offsets
V::ivector atom_pairs
V::ivector shared_pair
V::matrix frozen
V::matrix covariance
V::matrix cross
V::vector targets
V::vector noise
V::vector lengths
V::vector prior
Eigen::VectorXd target_witness
Eigen::VectorXd noise_witness
Eigen::VectorXd length_witness
double configured_jitter = 0.0
DescriptorRows training
DescriptorRows query
kokkos_joint::PairTypeIndex< Exec > pair_index
kokkos_joint::JointHyper< Exec > hyper
KokkosMolecularStats stats
detail::TransferCounters transfers
Posterior posterior
std::shared_ptr< const KokkosFactor > factor
bool predicted = false
bool predicted_variance = false
bool predicted_stable_variance = false

Detailed Description

Definition at line 220 of file KokkosMolecularPosterior.cpp.

Constructor & Destructor Documentation

◆ Impl()

gpr::linalg::KokkosMolecularPosterior::Impl::Impl ( const AtomsConfiguration & configuration,
double far )
inline

Definition at line 249 of file KokkosMolecularPosterior.cpp.

250 : conf(configuration),
251 cell(configuration.cell),
252 far_radius(far),
253 cutoff(conf.pair_cutoff),
254 width(conf.pair_cutoff_width),
255 moving(conf.atoms_mov.type.size()),
256 types(conf.n_pt),
257 posterior(
258 exec,
259 [](const Exec& ex, const Posterior::matrix_type& view) {
260 KernelWall wall(KernelWall::Kind::Factor);
261 auto work = view;
262 return detail::kokkosFactorLower(ex, work);
263 },
264 [](const Exec& ex, const Posterior::matrix_type& L,
265 const Posterior::matrix_type& B, bool transpose) {
266 KernelWall wall(KernelWall::Kind::Solve);
267 auto work = B;
268 detail::solve_lower(ex, L, work, transpose);
269 },
270 &transfers)
271 {
272 if (!(far >= 0.0) || !std::isfinite(far) || !(cutoff >= 0.0) ||
273 !std::isfinite(cutoff) || !(width >= 0.0) ||
274 !std::isfinite(width) || (far > 0.0 && cutoff > 0.0) ||
275 moving <= 0 || types < 0)
276 throw std::invalid_argument(
277 "invalid molecular descriptor configuration");
278 if (cutoff > 0.0) {
279 if (width == 0.0) width = std::min(1.0, .2 * cutoff);
280 if (width >= cutoff)
281 throw std::invalid_argument(
282 "cutoff shell must be smaller than its radius");
283 }
284 const int n_frozen = conf.atoms_froz_active.type.size();
285 if (conf.atoms_froz_active.positions.size() != 3 * n_frozen ||
286 !conf.atoms_froz_active.positions.allFinite())
287 throw std::invalid_argument(
288 "frozen coordinates have incompatible dimensions");
289 EigenMatrix frozen_host(n_frozen, 3);
290 for (int i = 0; i < n_frozen; ++i)
291 for (int d = 0; d < 3; ++d)
292 frozen_host(i, d) =
293 conf.atoms_froz_active.positions.data()[3 * i + d];
294 frozen = upload(frozen_host, "gpr frozen positions");
295 std::vector<Pair> vocabulary;
296 for (int a = 0; a < moving; ++a) {
297 std::vector<Eigen::Matrix3d> tensor(types, Eigen::Matrix3d::Zero());
298 if (far > 0.0 && (conf.atoms_mov.positions.size() != 3 * moving ||
299 !conf.atoms_mov.positions.allFinite()))
300 throw std::invalid_argument(
301 "far field requires finite reference positions");
302 for (int b = a + 1; b < moving + n_frozen; ++b) {
303 const int at = conf.atoms_mov.type(a);
304 const int bt = b < moving
305 ? conf.atoms_mov.type(b)
306 : conf.atoms_froz_active.type(b - moving);
307 if (at < 0 || bt < 0 || at >= conf.pairtype.rows() ||
308 bt >= conf.pairtype.cols())
309 throw std::invalid_argument(
310 "atom type is outside the pair vocabulary");
311 const int type = conf.pairtype(at, bt);
312 if (type < 0 || type >= types) continue;
313 if (far > 0.0 && b >= moving) {
314 double dr[3];
315 for (int d = 0; d < 3; ++d)
316 dr[d] = conf.atoms_mov.positions.data()[3 * a + d] -
317 frozen_host(b - moving, d);
318 if (!cell.wrap(dr))
319 throw std::invalid_argument(
320 "far field periodic image overflow");
321 const Eigen::Vector3d r(dr[0], dr[1], dr[2]);
322 const double r2 = r.squaredNorm();
323 if (r2 >= far * far) {
324 tensor[type] += r * r.transpose() / (r2 * r2 * r2);
325 continue;
326 }
327 }
328 vocabulary.push_back(Pair{a, b, type});
329 }
330 for (int t = 0; t < types; ++t) {
331 if (tensor[t].isZero(0.0)) continue;
332 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eig(
333 tensor[t]);
334 if (eig.info() != Eigen::Success ||
335 eig.eigenvalues().minCoeff() <
336 -64 * std::numeric_limits<double>::epsilon() *
337 tensor[t].norm())
338 throw std::runtime_error(
339 "far field tensor is not positive semidefinite");
340 for (int d = 0; d < 3; ++d) {
341 Pair p{a, moving + n_frozen + 3 * t + d, t, true};
342 const auto slope =
343 (std::sqrt(std::max(0.0, eig.eigenvalues()(d))) *
344 eig.eigenvectors().col(d))
345 .eval();
346 for (int c = 0; c < 3; ++c) {
347 p.centre[c] =
348 conf.atoms_mov.positions.data()[3 * a + c];
349 p.slope[c] = slope(c);
350 }
351 vocabulary.push_back(p);
352 }
353 }
354 }
355 pairs = decltype(pairs)("gpr pair vocabulary", vocabulary.size());
356 auto ph = Kokkos::create_mirror_view(pairs);
357 first = V::ivector("gpr pair first", vocabulary.size());
358 second = V::ivector("gpr pair second", vocabulary.size());
359 pair_type = V::ivector("gpr pair type", vocabulary.size());
360 auto f = Kokkos::create_mirror_view(first),
361 s = Kokkos::create_mirror_view(second),
362 t = Kokkos::create_mirror_view(pair_type);
363 for (std::size_t p = 0; p < vocabulary.size(); ++p) {
364 ph(p) = vocabulary[p];
365 f(p) = ph(p).first;
366 s(p) = ph(p).second;
367 t(p) = ph(p).type;
368 }
369 detail::audited_deep_copy(exec, pairs, ph,
370 detail::CopyRole::ToExecution);
371 detail::audited_deep_copy(exec, first, f,
372 detail::CopyRole::ToExecution);
373 detail::audited_deep_copy(exec, second, s,
374 detail::CopyRole::ToExecution);
375 detail::audited_deep_copy(exec, pair_type, t,
376 detail::CopyRole::ToExecution);
377 // A Cartesian slope of a pair is nonzero only for its own atoms.
378 // The pairs of each moving atom and the pair of two moving atoms
379 // let a matrix entry visit those alone.
380 std::vector<std::vector<int>> of_atom(moving);
381 std::vector<int> shared(static_cast<std::size_t>(moving) * moving, -1);
382 for (std::size_t p = 0; p < vocabulary.size(); ++p) {
383 const int a = vocabulary[p].first, b = vocabulary[p].second;
384 of_atom[a].push_back(static_cast<int>(p));
385 if (b < moving) {
386 of_atom[b].push_back(static_cast<int>(p));
387 shared[static_cast<std::size_t>(a) * moving + b] =
388 static_cast<int>(p);
389 shared[static_cast<std::size_t>(b) * moving + a] =
390 static_cast<int>(p);
391 }
392 }
393 // By pair type, then by pair: the order in which the sums over all
394 // pairs add the terms of one atom.
395 for (auto& list: of_atom)
396 std::stable_sort(list.begin(), list.end(), [&](int l, int r) {
397 return vocabulary[l].type < vocabulary[r].type;
398 });
399 std::size_t adjacency = 0;
400 for (const auto& list: of_atom)
401 adjacency += list.size();
402 atom_offsets = V::ivector("gpr atom pair offsets", moving + 1);
403 atom_pairs = V::ivector("gpr atom pairs", adjacency);
404 shared_pair = V::ivector("gpr shared pair", shared.size());
405 auto oh = Kokkos::create_mirror_view(atom_offsets),
406 ah = Kokkos::create_mirror_view(atom_pairs),
407 sh = Kokkos::create_mirror_view(shared_pair);
408 int position = 0;
409 for (int a = 0; a < moving; ++a) {
410 oh(a) = position;
411 for (const int pair: of_atom[a])
412 ah(position++) = pair;
413 }
414 oh(moving) = position;
415 for (std::size_t k = 0; k < shared.size(); ++k)
416 sh(k) = shared[k];
417 detail::audited_deep_copy(exec, atom_offsets, oh,
418 detail::CopyRole::ToExecution);
419 detail::audited_deep_copy(exec, atom_pairs, ah,
420 detail::CopyRole::ToExecution);
421 detail::audited_deep_copy(exec, shared_pair, sh,
422 detail::CopyRole::ToExecution);
423 exec.fence("gpr upload fixed pair vocabulary");
424 stats.input_bytes +=
425 vocabulary.size() * (sizeof(Pair) + 3 * sizeof(int)) +
426 (moving + 1 + adjacency + shared.size()) * sizeof(int);
427 pair_index =
429 }
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
PairTypeIndex< ExecSpace > build_pair_type_index(const ExecSpace &exec, const typename Views< ExecSpace >::const_ivector &pair_type, int n_types)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
Kokkos::View< Pair *, V::memory_space > pairs
V::matrix upload(const EigenMatrix &host, const char *label)

Member Function Documentation

◆ describe()

bool gpr::linalg::KokkosMolecularPosterior::Impl::describe ( DescriptorRows & out,
const EigenMatrix & input,
const Eigen::VectorXd & channels )
inline

Definition at line 451 of file KokkosMolecularPosterior.cpp.

453 {
454 if (identical(out.witness, input) && sameVector(out.indices, channels))
455 return false;
456 if (input.cols() < 3 * moving || channels.size() != input.rows() ||
457 !input.allFinite())
458 throw std::invalid_argument(
459 "molecular observation rows have incompatible dimensions");
460 exec.fence("gpr recycle molecular descriptors");
461 std::map<std::vector<double>, int> unique;
462 std::vector<std::vector<double>> positions;
463 std::vector<int> mapping(input.rows());
464 for (Eigen::Index row = 0; row < input.rows(); ++row) {
465 if (!std::isfinite(channels(row)) || channels(row) < 0 ||
466 channels(row) > 3 * moving ||
467 std::floor(channels(row)) != channels(row))
468 throw std::invalid_argument(
469 "invalid Cartesian observation channel");
470 std::vector<double> key(3 * moving);
471 for (int c = 0; c < 3 * moving; ++c)
472 key[c] = input(row, c);
473 auto [it, inserted] = unique.emplace(key, positions.size());
474 if (inserted) positions.push_back(std::move(key));
475 mapping[row] = it->second;
476 }
477 EigenMatrix host(positions.size(), 3 * moving);
478 for (std::size_t r = 0; r < positions.size(); ++r)
479 for (int c = 0; c < 3 * moving; ++c)
480 host(r, c) = positions[r][c];
481 const auto xyz = upload(host, "gpr compact coordinates");
482 out.channel = V::ivector("gpr observation channel", input.rows());
483 out.geometry = V::ivector("gpr observation geometry", input.rows());
484 auto ch = Kokkos::create_mirror_view(out.channel),
485 map = Kokkos::create_mirror_view(out.geometry);
486 for (Eigen::Index r = 0; r < input.rows(); ++r) {
487 ch(r) = channels(r);
488 map(r) = mapping[r];
489 }
490 detail::audited_deep_copy(exec, out.channel, ch,
491 detail::CopyRole::ToExecution);
492 detail::audited_deep_copy(exec, out.geometry, map,
493 detail::CopyRole::ToExecution);
494 stats.input_bytes += 2 * input.rows() * sizeof(int);
495 out.q = V::matrix("gpr inverse distances", positions.size(),
496 pairs.extent(0));
497 out.spatial = Spatial("gpr pair Cartesian slopes", positions.size(),
498 pairs.extent(0), 3);
499 const auto q = out.q;
500 const auto spatial = out.spatial;
501 const auto vocabulary = pairs;
502 const auto fixed = frozen;
503 const auto periodic = cell;
504 const int nm = moving;
505 const double R = cutoff, shell = width;
506 Kokkos::View<int, V::memory_space> invalid("gpr descriptor status");
507 Kokkos::parallel_for(
508 "gpr molecular descriptors",
509 Kokkos::MDRangePolicy<Exec, Kokkos::Rank<2>>(
510 exec, {0, 0}, {positions.size(), pairs.extent(0)}),
511 DescriptorKernel{xyz, q, spatial, vocabulary, fixed, periodic, nm,
512 R, shell, invalid});
513 int status = 0;
514 Kokkos::View<int, Kokkos::HostSpace,
515 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
516 host_status(&status);
517 detail::audited_deep_copy(exec, host_status, invalid,
518 detail::CopyRole::ToHost);
519 exec.fence("gpr molecular descriptor validation");
520 stats.output_bytes += sizeof(int);
521 if (status)
522 throw std::invalid_argument(
523 "molecular descriptor has a coincident pair or invalid "
524 "periodic image");
525 out.witness = input;
526 out.indices = channels;
527 ++stats.descriptor_builds;
528 stats.descriptor_entries += 4 * q.size();
529 return true;
530 }
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
int status

◆ release()

void gpr::linalg::KokkosMolecularPosterior::Impl::release ( )
inline

Definition at line 531 of file KokkosMolecularPosterior.cpp.

532 {
533 factor.reset();
534 training = DescriptorRows{};
535 query = DescriptorRows{};
536 pairs = {};
537 first = {};
538 second = {};
539 pair_type = {};
540 frozen = {};
541 covariance = {};
542 cross = {};
543 targets = {};
544 noise = {};
545 lengths = {};
546 prior = {};
547 pair_index = {};
548 hyper = {};
549 }

◆ topology()

PairTopology gpr::linalg::KokkosMolecularPosterior::Impl::topology ( ) const
inline

Definition at line 229 of file KokkosMolecularPosterior.cpp.

230 {
231 return {first, second, atom_offsets,
233 }

◆ upload() [1/2]

V::vector gpr::linalg::KokkosMolecularPosterior::Impl::upload ( const Eigen::VectorXd & host,
const char * label )
inline

Definition at line 441 of file KokkosMolecularPosterior.cpp.

442 {
443 V::vector result(std::string(label), host.size());
444 detail::audited_deep_copy(exec, result, detail::wrap(host),
445 host.size() * sizeof(double),
446 detail::CopyRole::ToExecution);
447 exec.fence("gpr molecular vector input");
448 stats.input_bytes += host.size() * sizeof(double);
449 return result;
450 }

◆ upload() [2/2]

V::matrix gpr::linalg::KokkosMolecularPosterior::Impl::upload ( const EigenMatrix & host,
const char * label )
inline

Definition at line 431 of file KokkosMolecularPosterior.cpp.

432 {
433 V::matrix result(std::string(label), host.rows(), host.cols());
434 detail::audited_deep_copy(exec, result, detail::wrap(host),
435 host.size() * sizeof(double),
436 detail::CopyRole::ToExecution);
437 exec.fence("gpr molecular matrix input");
438 stats.input_bytes += host.size() * sizeof(double);
439 return result;
440 }

Member Data Documentation

◆ atom_offsets

V::ivector gpr::linalg::KokkosMolecularPosterior::Impl::atom_offsets

Definition at line 228 of file KokkosMolecularPosterior.cpp.

◆ atom_pairs

V::ivector gpr::linalg::KokkosMolecularPosterior::Impl::atom_pairs

Definition at line 228 of file KokkosMolecularPosterior.cpp.

◆ cell

Cell gpr::linalg::KokkosMolecularPosterior::Impl::cell

Definition at line 223 of file KokkosMolecularPosterior.cpp.

◆ conf

AtomsConfiguration gpr::linalg::KokkosMolecularPosterior::Impl::conf

Definition at line 222 of file KokkosMolecularPosterior.cpp.

◆ configured_jitter

double gpr::linalg::KokkosMolecularPosterior::Impl::configured_jitter = 0.0

Definition at line 237 of file KokkosMolecularPosterior.cpp.

◆ covariance

V::matrix gpr::linalg::KokkosMolecularPosterior::Impl::covariance

Definition at line 234 of file KokkosMolecularPosterior.cpp.

◆ cross

V::matrix gpr::linalg::KokkosMolecularPosterior::Impl::cross

Definition at line 234 of file KokkosMolecularPosterior.cpp.

◆ cutoff

double gpr::linalg::KokkosMolecularPosterior::Impl::cutoff

Definition at line 224 of file KokkosMolecularPosterior.cpp.

◆ exec

Exec gpr::linalg::KokkosMolecularPosterior::Impl::exec

Definition at line 221 of file KokkosMolecularPosterior.cpp.

◆ factor

std::shared_ptr<const KokkosFactor> gpr::linalg::KokkosMolecularPosterior::Impl::factor

Definition at line 244 of file KokkosMolecularPosterior.cpp.

◆ far_radius

double gpr::linalg::KokkosMolecularPosterior::Impl::far_radius

Definition at line 224 of file KokkosMolecularPosterior.cpp.

◆ first

V::ivector gpr::linalg::KokkosMolecularPosterior::Impl::first

Definition at line 227 of file KokkosMolecularPosterior.cpp.

◆ frozen

V::matrix gpr::linalg::KokkosMolecularPosterior::Impl::frozen

Definition at line 234 of file KokkosMolecularPosterior.cpp.

◆ hyper

kokkos_joint::JointHyper<Exec> gpr::linalg::KokkosMolecularPosterior::Impl::hyper

Definition at line 240 of file KokkosMolecularPosterior.cpp.

◆ length_witness

Eigen::VectorXd gpr::linalg::KokkosMolecularPosterior::Impl::length_witness

Definition at line 236 of file KokkosMolecularPosterior.cpp.

◆ lengths

V::vector gpr::linalg::KokkosMolecularPosterior::Impl::lengths

Definition at line 235 of file KokkosMolecularPosterior.cpp.

◆ moving

int gpr::linalg::KokkosMolecularPosterior::Impl::moving

Definition at line 225 of file KokkosMolecularPosterior.cpp.

◆ noise

V::vector gpr::linalg::KokkosMolecularPosterior::Impl::noise

Definition at line 235 of file KokkosMolecularPosterior.cpp.

◆ noise_witness

Eigen::VectorXd gpr::linalg::KokkosMolecularPosterior::Impl::noise_witness

Definition at line 236 of file KokkosMolecularPosterior.cpp.

◆ nugget

double gpr::linalg::KokkosMolecularPosterior::Impl::nugget = 0.0

Definition at line 224 of file KokkosMolecularPosterior.cpp.

◆ pair_index

kokkos_joint::PairTypeIndex<Exec> gpr::linalg::KokkosMolecularPosterior::Impl::pair_index

Definition at line 239 of file KokkosMolecularPosterior.cpp.

◆ pair_type

V::ivector gpr::linalg::KokkosMolecularPosterior::Impl::pair_type

Definition at line 227 of file KokkosMolecularPosterior.cpp.

◆ pairs

Kokkos::View<Pair*, V::memory_space> gpr::linalg::KokkosMolecularPosterior::Impl::pairs

Definition at line 226 of file KokkosMolecularPosterior.cpp.

◆ posterior

Posterior gpr::linalg::KokkosMolecularPosterior::Impl::posterior

Definition at line 243 of file KokkosMolecularPosterior.cpp.

◆ predicted

bool gpr::linalg::KokkosMolecularPosterior::Impl::predicted = false

Definition at line 245 of file KokkosMolecularPosterior.cpp.

◆ predicted_stable_variance

bool gpr::linalg::KokkosMolecularPosterior::Impl::predicted_stable_variance = false

Definition at line 247 of file KokkosMolecularPosterior.cpp.

◆ predicted_variance

bool gpr::linalg::KokkosMolecularPosterior::Impl::predicted_variance = false

Definition at line 246 of file KokkosMolecularPosterior.cpp.

◆ prior

V::vector gpr::linalg::KokkosMolecularPosterior::Impl::prior

Definition at line 235 of file KokkosMolecularPosterior.cpp.

◆ query

DescriptorRows gpr::linalg::KokkosMolecularPosterior::Impl::query

Definition at line 238 of file KokkosMolecularPosterior.cpp.

◆ second

V::ivector gpr::linalg::KokkosMolecularPosterior::Impl::second

Definition at line 227 of file KokkosMolecularPosterior.cpp.

◆ shared_pair

V::ivector gpr::linalg::KokkosMolecularPosterior::Impl::shared_pair

Definition at line 228 of file KokkosMolecularPosterior.cpp.

◆ stats

KokkosMolecularStats gpr::linalg::KokkosMolecularPosterior::Impl::stats

Definition at line 241 of file KokkosMolecularPosterior.cpp.

◆ target_witness

Eigen::VectorXd gpr::linalg::KokkosMolecularPosterior::Impl::target_witness

Definition at line 236 of file KokkosMolecularPosterior.cpp.

◆ targets

V::vector gpr::linalg::KokkosMolecularPosterior::Impl::targets

Definition at line 235 of file KokkosMolecularPosterior.cpp.

◆ training

DescriptorRows gpr::linalg::KokkosMolecularPosterior::Impl::training

Definition at line 238 of file KokkosMolecularPosterior.cpp.

◆ transfers

detail::TransferCounters gpr::linalg::KokkosMolecularPosterior::Impl::transfers

Definition at line 242 of file KokkosMolecularPosterior.cpp.

◆ types

int gpr::linalg::KokkosMolecularPosterior::Impl::types

Definition at line 225 of file KokkosMolecularPosterior.cpp.

◆ width

double gpr::linalg::KokkosMolecularPosterior::Impl::width

Definition at line 224 of file KokkosMolecularPosterior.cpp.


The documentation for this struct was generated from the following file: