Serac  0.1
Serac is an implicit thermal strucural mechanics simulation code.
application_manager.cpp
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Serac Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
7 #include "serac/infrastructure/application_manager.hpp"
8 
9 #ifdef WIN32
10 #include <windows.h>
11 #include <tchar.h>
12 #else
13 #include <unistd.h>
14 #include <limits.h>
15 #endif
16 
17 #include <string.h>
18 #include <csignal>
19 #include <cstdlib>
20 
21 #include "mfem.hpp"
22 
23 #include "serac/serac_config.hpp"
24 
25 #ifdef SERAC_USE_PETSC
26 #include "petsc.h" // for PetscPopSignalHandler
27 #endif
28 
32 
33 namespace serac {
38 void finalizer();
39 } // namespace serac
40 
41 namespace {
42 void signalHandler(int signal)
43 {
44  std::cerr << "[SIGNAL]: Received signal " << signal << " (" << strsignal(signal) << "), exiting" << std::endl;
46  exit(1);
47 }
48 } // namespace
49 
50 namespace serac {
51 
52 void finalizer()
53 {
54  if (axom::slic::isInitialized()) {
55  serac::logger::flush();
56  serac::logger::finalize();
57  }
58 
59 #ifdef SERAC_USE_PETSC
60 #ifdef SERAC_USE_SLEPC
61  mfem::MFEMFinalizeSlepc();
62 #else
63  mfem::MFEMFinalizePetsc();
64 #endif
65 #endif
66 
68 
69  int mpi_initialized = 0;
70  MPI_Initialized(&mpi_initialized);
71  int mpi_finalized = 0;
72  MPI_Finalized(&mpi_finalized);
73  if (mpi_initialized && !mpi_finalized) {
74  MPI_Finalize();
75  }
76 
78 }
79 
80 ApplicationManager::ApplicationManager(int argc, char* argv[], MPI_Comm comm) : comm_(comm)
81 {
82  // Initialize MPI
83  if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
84  std::cerr << "Failed to initialize MPI" << std::endl;
85  exit(1);
86  }
87 
88  // Initialize SLIC logger
89  if (!logger::initialize(comm_)) {
90  std::cerr << "Failed to initialize SLIC logger" << std::endl;
91  exit(1);
92  }
93 
94  // Start the profiler (no-op if not enabled)
95  profiling::initialize(comm_);
96 
97  mfem::Hypre::Init();
98 
99 #ifdef SERAC_USE_SUNDIALS
100  mfem::Sundials::Init();
101 #endif
102 
103 #ifdef SERAC_USE_PETSC
104 #ifdef SERAC_USE_SLEPC
105  mfem::MFEMInitializeSlepc(&argc, &argv);
106 #else
107  mfem::MFEMInitializePetsc(&argc, &argv);
108 #endif
109  PetscPopSignalHandler();
110 #endif
111 
112  // Initialize GPU (no-op if not enabled/available)
114 
115  // Register signal handlers
116  std::signal(SIGABRT, signalHandler);
117  std::signal(SIGINT, signalHandler);
118  std::signal(SIGSEGV, signalHandler);
119  std::signal(SIGTERM, signalHandler);
120 }
121 
123 
124 } // namespace serac
This file contains the interface used for initializing/terminating any hardware accelerator-related f...
ApplicationManager(int argc, char *argv[], MPI_Comm comm=MPI_COMM_WORLD)
Initialize MPI, signal handling, logging, profiling, hypre, sundials, petsc, and slepc.
~ApplicationManager()
Calls serac::finalizer.
This file contains the all the necessary functions and macros required for logging as well as a helpe...
void initializeDevice()
Initializes the device (GPU)
Definition: accelerator.cpp:24
void terminateDevice()
Cleans up the device, if applicable.
Definition: accelerator.cpp:33
void finalize()
Concludes performance monitoring and writes collected data to a file.
Definition: profiling.cpp:59
void initialize([[maybe_unused]] MPI_Comm comm, [[maybe_unused]] std::string options)
Initializes performance monitoring using the Caliper and Adiak libraries.
Definition: profiling.cpp:24
Accelerator functionality.
Definition: serac.cpp:36
void finalizer()
Destroy MPI, signal handling, logging, profiling, hypre, sundials, petsc, and slepc....
Various helper functions and macros for profiling using Caliper.