Contract a joint weight L R^T through its thin factors. Each callback returns one geometry's [E; G] rows and the same number of factor columns. Left geometries belong to one rank; right factors stream in bounded batches. Affine charts use their fixed Jacobian; other chart geometries retain the general joint-weight contraction.
532{
533 struct ProjectedFactor {
534 Eigen::VectorXd energy;
535 Eigen::MatrixXd gradient;
536 };
537 Eigen::MatrixXd contributions;
538 std::array<Eigen::MatrixXd, 0> no_tensors;
540 if (affine_metric_.size() == 0)
541 throw std::invalid_argument(
542 "factored contraction requires an affine chart");
543 if (!left_factors || !right_factors || context.nprocs <= 0 ||
544 context.rank < 0 || context.rank >= context.nprocs)
545 throw std::invalid_argument(
546 "invalid factored contraction ownership");
547 const auto points = evaluatePoints(positions);
548 const Eigen::Index rows = positions.rows();
549 const Eigen::Index dimensions = positions.cols();
550 const Eigen::Index first_owned = rows * context.rank / context.nprocs;
551 const Eigen::Index end_owned =
552 rows * (context.rank + 1) / context.nprocs;
553 const Eigen::Index owned = end_owned - first_owned;
554 contributions = Eigen::MatrixXd::Zero(rows, 4);
555 if (!owned) return;
556 Eigen::Index factor_columns = -1;
558 Eigen::Index row) {
559 const Eigen::MatrixXd factor = factors(row, 1);
560 if (factor.rows() != dimensions + 1 ||
561 (factor_columns >= 0 && factor.cols() != factor_columns) ||
562 !factor.allFinite())
563 throw std::invalid_argument("invalid joint weight factor rows");
564 factor_columns = factor.cols();
565 ProjectedFactor result;
566 result.energy = factor.row(0).transpose();
567 result.gradient.noalias() =
568 points[static_cast<std::size_t>(row)].jacobian *
569 factor.bottomRows(dimensions);
570 return result;
571 };
572 std::vector<ProjectedFactor> left;
573 left.reserve(static_cast<std::size_t>(owned));
574 for (Eigen::Index row = first_owned; row < end_owned; ++row)
575 left.push_back(project(left_factors, row));
576 std::vector<std::array<CompensatedSum, 2>> sums(
577 static_cast<std::size_t>(owned));
578 const double a = 1.0 / (length_scale_ * length_scale_);
579 constexpr Eigen::Index batch_rows = 16;
581 static_cast<std::size_t>(owned * rows), std::memory_order_relaxed);
582 for (Eigen::Index first = 0; first < rows; first += batch_rows) {
583 const Eigen::Index count = std::min(batch_rows, rows - first);
584 std::vector<ProjectedFactor> right;
585 right.reserve(static_cast<std::size_t>(count));
586 for (Eigen::Index row = first; row < first + count; ++row)
587 right.push_back(project(right_factors, row));
588 execution.forEach(
589 static_cast<std::size_t>(owned), [&](std::size_t offset) {
590 const Eigen::Index i =
591 first_owned + static_cast<Eigen::Index>(offset);
592 const auto& l = left[offset];
593 auto& magnitude = sums[offset][0];
594 auto& length = sums[offset][1];
595 Eigen::VectorXd p(factor_columns),
z(factor_columns);
596 for (Eigen::Index row = 0; row < count; ++row) {
597 const Eigen::Index j = first + row;
598 const auto& r = right[static_cast<std::size_t>(row)];
599 const Eigen::VectorXd delta =
600 points[static_cast<std::size_t>(i)].coordinates -
601 points[static_cast<std::size_t>(j)].coordinates;
602 const double r2 = delta.squaredNorm();
603 const double kernel =
604 magnitude_variance_ * std::exp(-0.5 * a * r2);
605 const double s = a * r2;
606 p.noalias() = l.gradient.transpose() * delta;
607 z.noalias() = r.gradient.transpose() * delta;
608 const auto ee =
609 compensatedProductSum(l.energy, r.energy);
610 const auto eg = compensatedProductSum(l.energy,
z);
611 const auto ge = compensatedProductSum(p, r.energy);
612 const auto gg =
613 compensatedProductSum(l.gradient, r.gradient);
614 const auto outer = compensatedProductSum(p,
z);
615 const auto add = [](CompensatedSum& target,
616 const CompensatedSum& value,
617 const auto& scale) {
618 target.add(scale(value.sum));
619 target.add(scale(value.correction));
620 };
621
622
623
624
625 add(magnitude, ee,
626 [&](
double x) {
return kernel *
x; });
627 add(magnitude, eg,
628 [&](
double x) {
return kernel * (a *
x); });
629 add(magnitude, ge,
630 [&](
double x) {
return -kernel * (a *
x); });
631 add(magnitude, gg,
632 [&](
double x) {
return kernel * (a *
x); });
633 add(magnitude, outer,
634 [&](
double x) {
return -kernel * (a * (a *
x)); });
635 add(length, ee,
636 [&](
double x) {
return kernel * (s *
x); });
637 add(length, eg, [&](
double x) {
638 return kernel * (a * ((s - 2.0) *
x));
639 });
640 add(length, ge, [&](
double x) {
641 return -kernel * (a * ((s - 2.0) *
x));
642 });
643 add(length, gg, [&](
double x) {
644 return kernel * (a * ((s - 2.0) *
x));
645 });
646 add(length, outer, [&](
double x) {
647 return -kernel * (a * (a * ((s - 4.0) *
x)));
648 });
649 }
650 });
651 }
652 for (Eigen::Index offset = 0; offset < owned; ++offset) {
653 const auto& sum = sums[static_cast<std::size_t>(offset)];
654 contributions.row(first_owned + offset) << sum[0].sum,
655 sum[0].correction, sum[1].sum, sum[1].correction;
656 }
657 });
659 context);
660 Eigen::Vector2d result;
661 for (Eigen::Index parameter = 0; parameter < 2; ++parameter) {
662 CompensatedSum total;
663 for (Eigen::Index row = 0; row < contributions.rows(); ++row) {
664 total.add(contributions(row, 2 * parameter));
665 total.add(contributions(row, 2 * parameter + 1));
666 }
667 result(parameter) = total.sum + total.correction;
668 }
669 return result;
670}
std::function< Eigen::MatrixXd(Eigen::Index, Eigen::Index)> JointWeightRows
void ordered_sum_bcast(double *data, int count, const MpiContext &context)
static std::atomic_size_t chart_assembly_tiles