Loading...
Searching...
No Matches
CollectiveAssembly.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <Eigen/Core>
6#include <algorithm>
7#include <exception>
8#include <limits>
9#include <stdexcept>
10
11#include "MpiCollectives.h"
12
13namespace gpr::linalg {
14
18template <class Matrices, class Body>
19void assembleCollectively(const MpiContext& context, Matrices& result,
20 const Body& body)
21{
22 std::exception_ptr failure;
23 try {
24 body();
25 } catch (...) {
26 failure = std::current_exception();
27 }
28#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
29 if (context.is_distributed()) {
30 int failed = failure ? 1 : 0;
31 if (MPI_Allreduce(MPI_IN_PLACE, &failed, 1, MPI_INT, MPI_MAX,
32 context.comm) != MPI_SUCCESS)
33 throw std::runtime_error("could not share assembly status");
34 if (failed && !failure)
35 throw std::runtime_error("assembly failed on another rank");
36 }
37#endif
38 if (failure) std::rethrow_exception(failure);
39 if (!context.is_distributed()) return;
40 constexpr Eigen::Index maximum = std::numeric_limits<int>::max();
41 for (auto& matrix: result)
42 for (Eigen::Index offset = 0; offset < matrix.size();) {
43 const int entries =
44 static_cast<int>(std::min(maximum, matrix.size() - offset));
45 disjoint_sum_bcast(matrix.data() + offset, entries, context);
46 offset += entries;
47 }
48}
49
50} // namespace gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void disjoint_sum_bcast(double *data, int count, const MpiContext &context)
void assembleCollectively(const MpiContext &context, Matrices &result, const Body &body)
bool is_distributed() const noexcept
Definition MpiContext.h:67