openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
backend.cc
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
9 #include "backend.h"
10 #include "basics.h"
11 
12 #include <cstdlib>
13 #include <cstring>
14 #include <string>
15 
16 namespace opencarp {
17 
18 namespace {
19 
20 bool petsc_running = false;
22 bool mpi_started_here = false;
23 
24 void stop_if_flavor_is_missing()
25 {
26  const std::string flavor = param_globals::flavor != nullptr ? param_globals::flavor : "";
27 #ifndef WITH_GINKGO
28  if (flavor == "ginkgo") {
29  fprintf(stderr, "\n*** The flavor \"ginkgo\" is not available, openCARP was built without the Ginkgo backend.\n\n");
30  exit(EXIT_FAILURE);
31  }
32 #endif
33 #ifndef WITH_PETSC
34  if (!flavor.empty() && flavor != "ginkgo") {
35  fprintf(stderr, "\n*** The flavor \"%s\" is not available, openCARP was built with only the Ginkgo backend.\n\n",
36  flavor.c_str());
37  exit(EXIT_FAILURE);
38  }
39 #endif
40 }
41 
42 } // namespace
43 
45 {
46 #ifdef WITH_PETSC
47  return param_globals::flavor != nullptr && std::string{param_globals::flavor} == "ginkgo";
48 #else
49  return true;
50 #endif
51 }
52 
54 {
55  return petsc_running;
56 }
57 
58 void initialize_backend(int *argc, char **argv, char *rc, char *help_msg)
59 {
60  stop_if_flavor_is_missing();
61 
62  if (!ginkgo_selected()) {
63 #ifdef WITH_PETSC
64  initialize_PETSc(argc, argv, rc, help_msg);
65 #endif
66  return;
67  }
68 
69  int delimiter = 1;
70  while (delimiter < *argc && strcmp(argv[delimiter], DELIMIT_OPTS) != 0) delimiter++;
71  const bool has_petsc_options = delimiter + 1 < *argc;
72  *argc = delimiter;
73 
74  int initialized = 0;
75  MPI_Initialized(&initialized);
76  if (!initialized) {
77  // the level PETSc asks for: the OpenMP kernels are threaded, MPI is only used outside of them
78  int provided = 0;
79  MPI_Init_thread(nullptr, nullptr, MPI_THREAD_FUNNELED, &provided);
80  mpi_started_here = true;
81  }
82 
83  if (has_petsc_options)
84  log_msg(NULL, 3, 0, "The options after \"%s\" are PETSc options, which the Ginkgo backend ignores.", DELIMIT_OPTS);
85 }
86 
88 {
89 #ifdef WITH_PETSC
90  if (petsc_running) {
91  finalize_PETSc();
92  return;
93  }
94 #endif
95  if (mpi_started_here) {
96  mpi_started_here = false;
97  MPI_Finalize();
98  }
99 }
100 
102 {
104  exit(code);
105 }
106 
107 #ifdef WITH_PETSC
108 void set_petsc_active(bool active)
109 {
110  petsc_running = active;
111 }
112 #endif
113 
114 } // namespace opencarp
Starts and stops the linear algebra backend the flavor parameter selects.
#define DELIMIT_OPTS
separates the openCARP options from the PETSc options on the command line
Definition: backend.h:21
Basic utility structs and functions, mostly IO related.
bool petsc_active()
Definition: backend.cc:53
void finalize_backend_and_exit(int code)
Definition: backend.cc:101
void finalize_backend()
Definition: backend.cc:87
void initialize_backend(int *argc, char **argv, char *rc, char *help_msg)
Definition: backend.cc:58
bool ginkgo_selected()
Definition: backend.cc:44
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:55