https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseInit.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#include "MooseInit.h"
11#include "ParallelUniqueId.h"
12#include "Factory.h"
13#include "ActionFactory.h"
14#include "Executioner.h"
15#include "MooseRandom.h"
16
17#include "libmesh/petsc_solver_exception.h"
18#include "libmesh/reference_counter.h"
19
20// PETSc
21#include "petscsys.h"
22
23#ifdef LIBMESH_HAVE_OPENMP
24#include <omp.h>
25#endif
26
27#ifdef MOOSE_LIBTORCH_ENABLED
28#include <ATen/Parallel.h>
29#endif
30
31#include <unistd.h>
32#include <signal.h>
33#include <charconv>
34
35#ifdef PETSC_HAVE_CUDA
36#include "cuda_runtime.h"
37#endif
38
39void
40SigHandler(int signum)
41{
43 return;
44}
45
46void
48{
49 signal(SIGUSR1, SigHandler);
50}
51
52int
54{
55 static constexpr std::array local_rank_vars{
56 "OMPI_COMM_WORLD_LOCAL_RANK", // Open MPI
57 "MV2_COMM_WORLD_LOCAL_RANK", // MVAPICH2
58 "MPI_LOCALRANKID", // Intel MPI / some MPICH environments
59 "PMI_LOCAL_RANK", // PMI-based launchers
60 "PALS_LOCAL_RANKID", // HPE Cray PALS
61 "SLURM_LOCALID", // Slurm
62 "FLUX_TASK_LOCAL_ID" // Flux
63 };
64
65 for (const char * variable : local_rank_vars)
66 {
67 const char * value = std::getenv(variable);
68 if (!value)
69 continue;
70
71 int rank;
72 const char * end = value + std::char_traits<char>::length(value);
73 const auto [ptr, error] = std::from_chars(value, end, rank);
74
75 if (error != std::errc{} || ptr != end || rank < 0)
76 throw std::runtime_error(std::string("Invalid local rank in ") + variable + ": " + value);
77
78 return rank;
79 }
80
81 // Not launched through a recognized MPI/process-manager environment.
82 return 0;
83}
84
85#ifdef PETSC_HAVE_CUDA
86void
88{
89 int local_rank = getLocalRank();
90
111 int num_gpus = 0;
112 cudaGetDeviceCount(&num_gpus);
113
114 if (num_gpus)
115 {
116 int device = local_rank % num_gpus;
117 cudaSetDevice(device);
118 }
119}
120#endif
121
123{
124#ifdef PETSC_HAVE_CUDA
126#endif
127}
128
129MooseInit::MooseInit(int argc, char * argv[], MPI_Comm COMM_WORLD_IN)
130 : MooseDeviceInit(), LibMeshInit(argc, argv, COMM_WORLD_IN)
131{
132 LibmeshPetscCallA(COMM_WORLD_IN, PetscPopSignalHandler()); // get rid of PETSc error handler
133
134// Set the number of OpenMP threads to the same as the number of threads libMesh is going to use
135#ifdef LIBMESH_HAVE_OPENMP
136 omp_set_num_threads(libMesh::n_threads());
137#endif
138
139#ifdef MOOSE_LIBTORCH_ENABLED
140 at::set_num_threads(libMesh::n_threads());
141 at::set_num_interop_threads(libMesh::n_threads());
142#endif
143
144 if (!libMesh::on_command_line("--enable-refcount-printing"))
146
148
149 // Make sure that any calls to the global random number generator are consistent among processes
151
153
154#ifdef MOOSE_KOKKOS_ENABLED
155 initKokkos();
156#endif
157}
void SigHandler(int signum)
Definition MooseInit.C:40
int getLocalRank()
Definition MooseInit.C:53
void initializeCuda()
Definition MooseInit.C:87
void RegisterSigHandler()
Definition MooseInit.C:47
Initialize GPU runtime.
Definition MooseInit.h:21
MooseInit(int argc, char *argv[], MPI_Comm COMM_WORLD_IN=MPI_COMM_WORLD)
Definition MooseInit.C:129
void initKokkos()
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition MooseRandom.h:44
static void initialize()
Must be called by main thread before any threaded computation! Do NOT call in a worker thread!
static void disable_print_counter_info()
volatile std::sig_atomic_t interrupt_signal_number
Used by the signal handler to determine if we should write a checkpoint file out at any point during ...
Definition Moose.C:928
bool on_command_line(std::string arg)
unsigned int n_threads()