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 
69 
70  int mpi_initialized = 0;
71  MPI_Initialized(&mpi_initialized);
72  int mpi_finalized = 0;
73  MPI_Finalized(&mpi_finalized);
74  if (mpi_initialized && !mpi_finalized) {
75  MPI_Finalize();
76  }
77 
79 }
80 
81 ApplicationManager::ApplicationManager(int argc, char* argv[], MPI_Comm comm, bool doesPrintRunInfo) : comm_(comm)
82 {
83  // Initialize MPI
84  if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
85  std::cerr << "Failed to initialize MPI" << std::endl;
86  exit(1);
87  }
88 
89  // Initialize SLIC logger
90  if (!logger::initialize(comm_)) {
91  std::cerr << "Failed to initialize SLIC logger" << std::endl;
92  exit(1);
93  }
94 
95  if (doesPrintRunInfo) {
96  printRunInfo();
97  }
98 
99  // Start the profiler (no-op if not enabled)
100  profiling::initialize(comm_);
101 
102  mfem::Hypre::Init();
103 
104 #ifdef SMITH_USE_SUNDIALS
105  mfem::Sundials::Init();
106 #endif
107 
108 #ifdef SMITH_USE_PETSC
109  // PETSc tries to parse all command line options, but Smith applications
110  // may have others intended for MPI or the application itself.
111  // Silence the PETSc warning that there are leftover options it doesn't
112  // know.
113  PetscOptionsSetValue(NULL, "-options_left", "no");
114 #ifdef SMITH_USE_SLEPC
115  mfem::MFEMInitializeSlepc(&argc, &argv);
116 #else
117  mfem::MFEMInitializePetsc(&argc, &argv);
118 #endif
119  PetscPopSignalHandler();
120 #endif
121 
122  // Initialize GPU (no-op if not enabled/available)
124 
125  // Register signal handlers
126  std::signal(SIGABRT, signalHandler);
127  std::signal(SIGINT, signalHandler);
128  std::signal(SIGSEGV, signalHandler);
129  std::signal(SIGTERM, signalHandler);
130 }
131 
133 
134 } // 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)
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()
Initializes the device (GPU)
Definition: accelerator.cpp:24
void terminateDevice()
Cleans up the device, if applicable.
Definition: accelerator.cpp:33
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....
void printRunInfo()
Outputs basic run information to the screen.
Definition: about.cpp:194
Various helper functions and macros for profiling using Caliper.