OGS
LinearSolverLibrarySetup.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <memory>
7
8#include "BaseLib/Error.h"
9#include "BaseLib/Logging.h"
11
12// All concrete LinearSolverLibrarySetup implementations are in the detail
13// namespace. They should not be instantiated manually, but only via
14// LinearSolverLibrarySetup::create().
15#if defined(USE_PETSC)
16#include <mpi.h>
17#include <petsc.h>
18
19#include "BaseLib/MPI.h"
20
22{
25{
26 LinearSolverLibrarySetupPETSc(int argc, char* argv[])
27 {
28 char help[] = "ogs6 with PETSc \n";
29 PETSC_COMM_WORLD = BaseLib::MPI::OGS_COMM_WORLD;
30 PetscInitialize(&argc, &argv, nullptr, help);
31 MPI_Comm_set_errhandler(PETSC_COMM_WORLD, MPI_ERRORS_RETURN);
32 }
33
39};
41} // namespace ApplicationsLib::detail
42#elif defined(USE_LIS)
45{
46struct LinearSolverLibrarySetupLis final
48{
49 LinearSolverLibrarySetupLis(int argc, char* argv[])
50 {
51 lis_initialize(&argc, &argv);
52 }
53
54 ~LinearSolverLibrarySetupLis()
55 {
57 lis_finalize();
58 }
59};
60using LinearSolverLibrarySetupImpl = LinearSolverLibrarySetupLis;
61} // namespace ApplicationsLib::detail
62#else
64{
65struct LinearSolverLibrarySetupEigen final
66 : public ApplicationsLib::LinearSolverLibrarySetup
67{
68 LinearSolverLibrarySetupEigen(int /*argc*/, char* /*argv*/[])
69 {
70#ifdef _OPENMP
71 const char* omp_num_threads_env = std::getenv("OMP_NUM_THREADS");
72 if (omp_num_threads_env)
73 {
74 INFO("OMP_NUM_THREADS is set to: {:s}", omp_num_threads_env);
75 }
76 else
77 {
78 WARN("OMP_NUM_THREADS is not set, falling back to: {:d}",
79 omp_get_max_threads());
80 }
81#endif
82 INFO("Eigen use {:d} threads", Eigen::nbThreads());
83 }
84 ~LinearSolverLibrarySetupEigen() override
85 {
87 }
88};
89using LinearSolverLibrarySetupImpl = LinearSolverLibrarySetupEigen;
90} // namespace ApplicationsLib::detail
91#endif
92
93namespace ApplicationsLib
94{
96{
97 DBUG("Tearing down linear solver library setup.");
98}
99
100std::shared_ptr<LinearSolverLibrarySetup> LinearSolverLibrarySetup::create(
101 int argc, char* argv[])
102{
103 static std::weak_ptr<LinearSolverLibrarySetup> instance;
104 static std::mutex mutex;
105
106 // Lock to avoid multiple initializations.
107 std::lock_guard lock{mutex};
108
109 std::shared_ptr lsls = instance.lock();
110
111 if (!lsls)
112 {
113 DBUG("Initializing linear solver library for the first time");
114 lsls =
115 std::make_shared<detail::LinearSolverLibrarySetupImpl>(argc, argv);
116
117 // Necessary such that the internally cached ptr and the returned ptr
118 // both point to the same object, which might be returned from
119 // subsequent create() calls.
120 instance = lsls;
121 }
122 else
123 {
124 DBUG("Initializing linear solver library for the {}th time",
125 lsls.use_count());
126#if defined(USE_PETSC) || defined(USE_LIS)
127 // PETSc and Lis parse commandline arguments. Therefore, we exclude them
128 // for the time being such that nobody can accidentally pass
129 // different arguments to different linear solver library setups.
130 OGS_FATAL(
131 "Reusing a LinearSolverLibrarySetup has not been defined for "
132 "PETSc or LIS. In the present build configuration you cannot run "
133 "two separate OGS simulations in the same process.");
134#else
135 // No initialization needed for Eigen, just return the internally cached
136 // ptr.
137#endif
138 }
139
140 // Consistency check that the internally cached ptr and the returned ptr
141 // both point to the same object.
142 // C++26 will have owner_equal()
143 assert(!instance.owner_before(lsls) && !lsls.owner_before(instance));
144
145 return lsls;
146}
147} // namespace ApplicationsLib
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
LinearSolverLibrarySetupPETSc LinearSolverLibrarySetupImpl
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
void cleanupGlobalMatrixProviders()
OGS_EXPORT_SYMBOL static std::shared_ptr< LinearSolverLibrarySetup > create(int argc, char *argv[])