Loading...
Searching...
No Matches
BackendContext.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4#include "BackendContext.h"
5
6#include <stdexcept>
7
8namespace gpr::linalg {
9
11 std::shared_ptr<const MpiCommunicator> communication)
12 : communication_(std::move(communication))
13{
14 if (!communication_)
15 throw std::invalid_argument("backend context requires communication");
16}
17
19{
20 if (communication_) return communication_->view();
21#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
22 int initialized = 0, finalized = 0;
23 MPI_Initialized(&initialized);
24 MPI_Finalized(&finalized);
25 if (initialized && !finalized) return MpiContext::world();
26#endif
27 return MpiContext::local();
28}
29
31{
32#ifdef GPR_LINALG_SCALAPACK
33 if (distributed_ && distributed_->grid_generation() > 0)
34 return distributed_->node_ranks();
35#endif
36 return mpi().nprocs;
37}
38
39#ifdef GPR_LINALG_SCALAPACK
40DistributedState& BackendContext::distributed() const
41{
42 if (!distributed_) {
43 if (!communication_) {
44 int initialized = 0;
45 MPI_Initialized(&initialized);
46 if (!initialized) {
47 // The compatibility runtime owns MPI until process exit.
48 // Its factor storage is separate from this workspace.
50 runtime.initialize();
51 communication_ = runtime.communication();
52 } else {
53 communication_ =
54 std::make_shared<MpiCommunicator>(MPI_COMM_WORLD);
55 }
56 }
57 distributed_ = std::make_shared<DistributedState>(communication_);
58 }
59 return *distributed_;
60}
61#endif
62
63} // namespace gpr::linalg
static DistributedState & instance()
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
STL namespace.
static MpiContext local() noexcept
Definition MpiContext.h:75