PISM, A Parallel Ice Sheet Model 2.3.2-fa1174726 committed by Constantine Khrulev on 2026-04-08
Loading...
Searching...
No Matches
pism_initialization.cc
Go to the documentation of this file.
1/* Copyright (C) 2026 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <mpi.h>
21#include <petscsys.h>
22
23#include "pism/util/pism_initialization.hh"
24
25#include "pism/util/error_handling.hh"
26
27#if (Pism_USE_YAC == 1)
28#include "pism/util/yaxt_wrapper.h"
29
30extern "C" {
31#include "yac.h"
32}
33#endif
34
35namespace pism {
36
37//! true if PISM should finalize PETSc, false otherwise
38static bool s_pism_finalize_petsc = false;
39
40//! true if PISM should finalize MPI, false otherwise
41static bool s_pism_finalize_mpi = false;
42
43#if (Pism_USE_YAC == 1)
44//! true if PISM initialized YAC, false otherwise
45static bool s_pism_yac_initialized = false;
46
47static void pism_yac_error_handler(MPI_Comm /* unused */, const char *msg, const char *source,
48 int line) {
49 throw pism::RuntimeError::formatted(pism::ErrorLocation(source, line), "YAC error: %s", msg);
50}
51#endif
52
53void initialize(int argc, char **argv, const char *help) {
54
55 // Initialize MPI only if it was not done yet
56 {
57 int flag = 0;
58 MPI_Initialized(&flag);
59
60 if (flag == 0) {
61 MPI_Init(&argc, &argv);
63 } else {
64 s_pism_finalize_mpi = false;
65 }
66 }
67
68 PetscBool petsc_initialized = PETSC_FALSE;
69 {
70 PetscErrorCode ierr = PetscInitialized(&petsc_initialized);
71 PISM_CHK(ierr, "PetscInitialized");
72 }
73
74
75#if (Pism_USE_YAC == 1)
76 // YAXT
77 {
78 int yaxt_initialized = pism_yaxt_initialized();
79 if (yaxt_initialized != 1) {
80 pism_yaxt_initialize(MPI_COMM_WORLD);
81 }
82 }
83
84 // YAC
85 if (not s_pism_yac_initialized) {
86 int yac_comp_id;
87 const char *start_datetime = "1850-01-01T00:00:00";
88 const char *end_datetime = "1850-12-31T00:00:00";
89
90 yac_cinit();
91
92 yac_set_abort_handler((yac_abort_func)pism_yac_error_handler);
93
94 yac_cdef_calendar(YAC_YEAR_OF_365_DAYS);
95 yac_cdef_datetime(start_datetime, end_datetime);
96
97 yac_cdef_comp("pism", &yac_comp_id);
98
99 // make sure we don't mess with PETSC_COMM_WORLD if someone else initialized PETSc
100 // already:
101 if (petsc_initialized == PETSC_FALSE) {
102 yac_cget_comp_comm(yac_comp_id, &PETSC_COMM_WORLD);
103 }
104
105 s_pism_yac_initialized = true;
106 }
107#endif
108
109 // PETSc
110 {
111 if (petsc_initialized == PETSC_FALSE) {
112 PetscErrorCode ierr = PetscInitialize(&argc, &argv, NULL, help);
113 PISM_CHK(ierr, "PetscInitialize");
114
115 if (ierr != 0) {
116 printf("PETSc initialization failed. Aborting...\n");
117 MPI_Abort(MPI_COMM_WORLD, -1);
118 }
120 } else {
121 s_pism_finalize_petsc = false;
122 }
123 }
124}
125
126void initialize(const char *help) {
127 std::vector<char *> argv = { nullptr };
128 initialize(0, argv.data(), help);
129}
130
131void initialize_options(const std::vector<std::string> &args) {
132 int argc = (int) args.size();
133 std::vector<const char*> argv(argc + 1);
134
135 for (int i = 0; i < argc; ++i) {
136 argv[i] = args[i].c_str();
137 }
138 argv[argc] = nullptr;
139
140 // Note: this const_cast is needed for compatibility with earlier PETSc versions.
141 PetscOptionsInsertArgs(NULL, argc, const_cast<char**>(argv.data()));
142}
143
144
145void finalize() {
146
147 // PETSc
148 {
149 PetscBool petsc_initialized = PETSC_FALSE;
150 PetscErrorCode ierr = PetscInitialized(&petsc_initialized);
151 CHKERRCONTINUE(ierr);
152
153 if (petsc_initialized == PETSC_TRUE and s_pism_finalize_petsc) {
154 // there is nothing we can do if this fails
155 ierr = PetscFinalize();
156 CHKERRCONTINUE(ierr);
157 }
158 }
159
160#if (Pism_USE_YAC == 1)
161 // YAC
162 if (s_pism_yac_initialized) {
163 yac_cfinalize();
164 }
165
166 // YAXT
167 {
168 int yaxt_initialized = pism_yaxt_initialized();
169 int yaxt_finalized = pism_yaxt_finalized();
170 if (yaxt_initialized == 1 and yaxt_finalized != 1) {
172 }
173 }
174#endif
175
176 // MPI
178 MPI_Finalize();
179 }
180}
181
182}
static char help[]
Definition btutest.cc:22
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
#define PISM_CHK(errcode, name)
void initialize(int argc, char **argv, const char *help)
void initialize_options(const std::vector< std::string > &args)
static bool s_pism_finalize_petsc
true if PISM should finalize PETSc, false otherwise
std::string printf(const char *format,...)
static bool s_pism_finalize_mpi
true if PISM should finalize MPI, false otherwise
void pism_yaxt_initialize(MPI_Comm default_comm)
int pism_yaxt_initialized(void)
int pism_yaxt_finalized(void)
void pism_yaxt_finalize(void)