Assemble anchor, cross, and query blocks separately. The anchor block becomes the factor storage without retaining a full joint matrix.
406{
407 valid_ = false;
408 factor_.reset();
409 if (!validOnAllRanks(
410 anchors.rows() > 0 && query.rows() > 0 && anchors.cols() > 0 &&
411 anchors.cols() == query.cols() && anchors.allFinite() &&
412 query.allFinite() &&
413 anchor_noise.size() == anchors.rows() * (anchors.cols() + 1) &&
414 anchor_noise.allFinite() && (anchor_noise.array() >= 0.0).all(),
415 execution))
416 return false;
417 anchor_rows_ = anchors.rows();
418 query_rows_ = query.rows();
419 dimensions_ = anchors.cols();
420#ifdef GPR_LINALG_SCALAPACK
421 if (execution) {
422
423
424 const Eigen::Index channels = dimensions_ + 1;
425 const Eigen::Index order = anchor_rows_ * channels;
426 if (order > std::numeric_limits<int>::max()) return false;
430 execution->initialize(static_cast<int>(channels));
431 int descriptor[9];
432 auto local = execution->alloc_local(
433 static_cast<int>(order), static_cast<int>(order), descriptor);
434 if (local.empty()) local.resize(1, 0.0);
435 int valid = 1;
436 try {
438 "projection anchor assembly");
439 const auto prepared_tiles =
440 kernel.prepareJointCovarianceTiles(anchors);
441 const bool whole_tiles = execution->block_size() == channels;
442 for (Eigen::Index i = 0; i < anchor_rows_; ++i) {
443 for (Eigen::Index j = 0; j <= i; ++j) {
444 const int row_start = static_cast<int>(i * channels);
445 const int col_start = static_cast<int>(j * channels);
446 bool owned =
447 execution->owns_global_entry(row_start, col_start);
448 if (!whole_tiles && !owned)
449 for (Eigen::Index a = 0; a < channels && !owned; ++a)
450 for (Eigen::Index b = 0; b < channels; ++b)
451 if (execution->owns_global_entry(
452 row_start + a, col_start + b)) {
453 owned = true;
454 break;
455 }
456 if (!owned) continue;
457 const Eigen::MatrixXd tile =
458 prepared_tiles
459 ? prepared_tiles(i, j)
460 : kernel.jointEnergyGradientCrossCovariance(
461 anchors.middleRows(i, 1),
462 anchors.middleRows(j, 1));
463 if (tile.rows() != channels || tile.cols() != channels ||
464 !tile.allFinite()) {
465 valid = 0;
466 continue;
467 }
468 if (whole_tiles) {
469
470
471
472 const int local_row = execution->local_index_for_global(
473 row_start, execution->block_size(),
474 execution->myrow(), execution->nprow());
475 const int local_col = execution->local_index_for_global(
476 col_start, execution->block_size(),
477 execution->mycol(), execution->npcol());
478 for (Eigen::Index b = 0; b < channels; ++b) {
479 double* column =
480 local.data() + local_row +
481 static_cast<std::size_t>(local_col + b) *
482 descriptor[8];
483 for (Eigen::Index a = i == j ? b : 0; a < channels;
484 ++a)
485 column[a] =
486 tile(a, b) +
487 (i == j && a == b
488 ? anchor_noise(a * anchor_rows_ + i)
489 : 0.0);
490 }
491 continue;
492 }
493 for (Eigen::Index a = 0; a < channels; ++a)
494 for (Eigen::Index b = 0; b < channels; ++b) {
495 const int row = row_start + a, col = col_start + b;
496 if (row < col ||
497 !execution->owns_global_entry(row, col))
498 continue;
499 const int local_row =
500 execution->local_index_for_global(
501 row, execution->block_size(),
502 execution->myrow(), execution->nprow());
503 const int local_col =
504 execution->local_index_for_global(
505 col, execution->block_size(),
506 execution->mycol(), execution->npcol());
507 local[local_row +
508 static_cast<std::size_t>(local_col) *
509 descriptor[8]] =
510 tile(a, b) +
511 (row == col ? anchor_noise(a * anchor_rows_ + i)
512 : 0.0);
513 }
514 }
515 }
516 } catch (const std::exception&) {
517 valid = 0;
518 }
519 if (execution->nprocs() > 1)
520 MPI_Allreduce(MPI_IN_PLACE, &valid, 1, MPI_INT, MPI_MIN,
521 execution->world_comm());
522 if (!valid) return false;
523 factor_ = std::make_unique<Factor>();
524 factor_->observation_major = true;
525 factor_->distributed =
526 std::make_unique<linalg::ScalapackPDPotrfInverse>(execution);
527 {
529 "projection factor");
530 if (!factor_->distributed->factorise(std::move(local), descriptor,
531 static_cast<int>(order)))
532 return false;
533 }
534 Eigen::MatrixXd aq, qq;
535 try {
537 "projection cross covariance");
538 aq = kernel.jointEnergyGradientCrossCovariance(anchors, query);
539 qq = kernel.jointEnergyGradientCovariance(query);
540 const Eigen::Index query_order = query_rows_ * channels;
541 valid = aq.rows() == order && aq.cols() == query_order &&
542 qq.rows() == query_order && qq.cols() == query_order &&
543 aq.allFinite() && qq.allFinite();
544 } catch (const std::exception&) {
545 valid = 0;
546 }
547 if (execution->nprocs() > 1)
548 MPI_Allreduce(MPI_IN_PLACE, &valid, 1, MPI_INT, MPI_MIN,
549 execution->world_comm());
550 if (!valid) return false;
551 Eigen::MatrixXd ordered(aq.rows(), aq.cols());
552 for (Eigen::Index row = 0; row < order; ++row)
553 ordered.row(row) =
554 aq.row((row % channels) * anchor_rows_ + row / channels);
555 {
557 "projection solve");
558 ordered = factor_->distributed->solve_mat(ordered);
559 }
560 weights_.resize(aq.rows(), aq.cols());
561 for (Eigen::Index row = 0; row < order; ++row)
562 weights_.row((row % channels) * anchor_rows_ + row / channels) =
563 ordered.row(row);
564 {
566 "projection Schur complement");
567 covariance = conditionalCovariance(qq, aq, weights_);
568 }
569 valid_ = covariance.allFinite() && weights_.allFinite();
570 return valid_;
571 }
572#endif
573 return prepareBlocks(
574 kernel.jointEnergyGradientCovariance(anchors),
575 kernel.jointEnergyGradientCrossCovariance(anchors, query),
576 kernel.jointEnergyGradientCovariance(query), anchor_noise, covariance,
577 execution);
578}
static double projection_cross_seconds
static double projection_schur_seconds
static long projection_max_anchor_rows
static int projection_count
static double projection_factor_seconds
static double projection_assembly_seconds
static double projection_solve_seconds