PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
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 MPI, false otherwise
38static bool s_pism_finalize_mpi = false;
39
40#if (Pism_USE_YAC == 1)
41//! true if PISM initialized YAC, false otherwise
42static bool s_pism_yac_initialized = false;
43
44static void pism_yac_error_handler(MPI_Comm /* unused */, const char *msg, const char *source,
45 int line) {
46 throw pism::RuntimeError::formatted(pism::ErrorLocation(source, line), "YAC error: %s", msg);
47}
48#endif
49
50void initialize(int argc, char **argv, const char *help) {
51
52 // Initialize MPI only if it was not done yet
53 {
54 int flag = 0;
55 MPI_Initialized(&flag);
56
57 if (flag == 0) {
58 MPI_Init(&argc, &argv);
60 } else {
61 s_pism_finalize_mpi = false;
62 }
63 }
64
65#if (Pism_USE_YAC == 1)
66 // YAXT
67 {
68 int yaxt_initialized = pism_yaxt_initialized();
69 if (yaxt_initialized != 1) {
70 pism_yaxt_initialize(MPI_COMM_WORLD);
71 }
72 }
73
74 // YAC
75 if (not s_pism_yac_initialized) {
76 int yac_comp_id;
77 const char *start_datetime = "1850-01-01T00:00:00";
78 const char *end_datetime = "1850-12-31T00:00:00";
79
80 yac_cinit();
81
82 yac_set_abort_handler((yac_abort_func)pism_yac_error_handler);
83
84 yac_cdef_calendar(YAC_YEAR_OF_365_DAYS);
85 yac_cdef_datetime(start_datetime, end_datetime);
86
87 yac_cdef_comp("pism", &yac_comp_id);
88 yac_cget_comp_comm(yac_comp_id, &PETSC_COMM_WORLD);
89
90 s_pism_yac_initialized = true;
91 }
92#endif
93
94 // PETSc
95 {
96 PetscBool initialized = PETSC_FALSE;
97 PetscErrorCode ierr = PetscInitialized(&initialized);
98 PISM_CHK(ierr, "PetscInitialized");
99
100 if (initialized == PETSC_FALSE) {
101 ierr = PetscInitialize(&argc, &argv, NULL, help);
102 PISM_CHK(ierr, "PetscInitialize");
103
104 if (ierr != 0) {
105 printf("PETSc initialization failed. Aborting...\n");
106 MPI_Abort(MPI_COMM_WORLD, -1);
107 }
108 }
109 }
110}
111
112void initialize(const char *help) {
113 std::vector<char *> argv = { nullptr };
114 initialize(0, argv.data(), help);
115}
116
117void initialize_options(const std::vector<std::string> &args) {
118 int argc = (int) args.size();
119 std::vector<const char*> argv(argc + 1);
120
121 for (int i = 0; i < argc; ++i) {
122 argv[i] = args[i].c_str();
123 }
124 argv[argc] = nullptr;
125
126 // Note: this const_cast is needed for compatibility with earlier PETSc versions.
127 PetscOptionsInsertArgs(NULL, argc, const_cast<char**>(argv.data()));
128}
129
130
131void finalize() {
132
133 // PETSc
134 {
135 PetscBool petsc_initialized = PETSC_FALSE;
136 PetscErrorCode ierr = PetscInitialized(&petsc_initialized);
137 CHKERRCONTINUE(ierr);
138
139 if (petsc_initialized == PETSC_TRUE) {
140 // there is nothing we can do if this fails
141 ierr = PetscFinalize();
142 CHKERRCONTINUE(ierr);
143 }
144 }
145
146#if (Pism_USE_YAC == 1)
147 // YAC
148 if (s_pism_yac_initialized) {
149 yac_cfinalize();
150 }
151
152 // YAXT
153 {
154 int yaxt_initialized = pism_yaxt_initialized();
155 int yaxt_finalized = pism_yaxt_finalized();
156 if (yaxt_initialized == 1 and yaxt_finalized != 1) {
158 }
159 }
160#endif
161
162 // MPI
164 MPI_Finalize();
165 }
166}
167
168}
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)
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)