Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
application_manager.cpp
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Smith Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
7 #include "smith/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 "smith/smith_config.hpp"
24 
25 #ifdef SMITH_USE_PETSC
26 #include "petsc.h" // for PetscPopSignalHandler
27 #endif
28 
33 
34 namespace smith {
39 void finalizer();
40 } // namespace smith
41 
42 namespace {
43 void signalHandler(int signal)
44 {
45  std::cerr << "[SIGNAL]: Received signal " << signal << " (" << strsignal(signal) << "), exiting" << std::endl;
47  exit(1);
48 }
49 } // namespace
50 
51 namespace smith {
52 
53 void finalizer()
54 {
55  if (axom::slic::isInitialized()) {
56  smith::logger::flush();
57  smith::logger::finalize();
58  }
59 
60 #ifdef SMITH_USE_PETSC
61 #ifdef SMITH_USE_SLEPC
62  mfem::MFEMFinalizeSlepc();
63 #else
64  mfem::MFEMFinalizePetsc();
65 #endif
66 #endif
67 
68 #ifdef SMITH_USE_SUNDIALS
69  mfem::Sundials::Finalize();
70 #endif
71 
73 
74  int mpi_initialized = 0;
75  MPI_Initialized(&mpi_initialized);
76  int mpi_finalized = 0;
77  MPI_Finalized(&mpi_finalized);
78  if (mpi_initialized && !mpi_finalized) {
79  MPI_Finalize();
80  }
81 
83 }
84 
85 ApplicationManager::ApplicationManager(int argc, char* argv[], MPI_Comm comm, bool doesPrintRunInfo,
86  ExecutionSpace exec_space)
87  : comm_(comm)
88 {
89  // Initialize MPI
90  if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
91  std::cerr << "Failed to initialize MPI" << std::endl;
92  exit(1);
93  }
94 
95  // Initialize SLIC logger
96  if (!logger::initialize(comm_)) {
97  std::cerr << "Failed to initialize SLIC logger" << std::endl;
98  exit(1);
99  }
100 
101  if (doesPrintRunInfo) {
102  printRunInfo();
103  }
104 
105  // Start the profiler (no-op if not enabled)
106  profiling::initialize(comm_);
107 
108  mfem::Hypre::Init();
109 
110 #ifdef SMITH_USE_SUNDIALS
111  mfem::Sundials::Init();
112 #endif
113 
114 #ifdef SMITH_USE_PETSC
115  // PETSc tries to parse all command line options, but Smith applications
116  // may have others intended for MPI or the application itself.
117  // Silence the PETSc warning that there are leftover options it doesn't
118  // know.
119  PetscOptionsSetValue(NULL, "-options_left", "no");
120 #ifdef SMITH_USE_SLEPC
121  mfem::MFEMInitializeSlepc(&argc, &argv);
122 #else
123  mfem::MFEMInitializePetsc(&argc, &argv);
124 #endif
125  PetscPopSignalHandler();
126 #endif
127 
128  // Initialize GPU (no-op if not enabled/available)
129  accelerator::initializeDevice(exec_space);
130 
131  // Register signal handlers
132  std::signal(SIGABRT, signalHandler);
133  std::signal(SIGINT, signalHandler);
134  std::signal(SIGSEGV, signalHandler);
135  std::signal(SIGTERM, signalHandler);
136 }
137 
139 
140 } // namespace smith
This file contains the interface used for retrieving information about how the driver is configured.
This file contains the interface used for initializing/terminating any hardware accelerator-related f...
~ApplicationManager()
Calls smith::finalizer.
ApplicationManager(int argc, char *argv[], MPI_Comm comm=MPI_COMM_WORLD, bool doesPrintRunInfo=true, ExecutionSpace exec_space=ExecutionSpace::CPU)
Initialize MPI, signal handling, logging, profiling, hypre, sundials, petsc, and slepc.
This file contains the all the necessary functions and macros required for logging as well as a helpe...
void initializeDevice(ExecutionSpace exec_space)
Initializes the device (GPU)
Definition: accelerator.cpp:24
void terminateDevice()
Cleans up the device, if applicable.
Definition: accelerator.cpp:43
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
void finalize()
Concludes performance monitoring and writes collected data to a file.
Definition: profiling.cpp:59
Accelerator functionality.
Definition: smith.cpp:36
void finalizer()
Destroy MPI, signal handling, logging, profiling, hypre, sundials, petsc, and slepc....
ExecutionSpace
enum used for signalling whether or not to perform certain calculations on the CPU or GPU
Definition: accelerator.hpp:88
void printRunInfo()
Outputs basic run information to the screen.
Definition: about.cpp:194
Various helper functions and macros for profiling using Caliper.