diff --git a/include/openmc/openmp_interface.h b/include/openmc/openmp_interface.h index b489df0c9ad..fe517415baa 100644 --- a/include/openmc/openmp_interface.h +++ b/include/openmc/openmp_interface.h @@ -7,6 +7,27 @@ namespace openmc { +//============================================================================== +//! Accessor functions related to number of threads and thread number +//============================================================================== +inline int num_threads() +{ +#ifdef _OPENMP + return omp_get_max_threads(); +#else + return 1; +#endif +} + +inline int thread_num() +{ +#ifdef _OPENMP + return omp_get_thread_num(); +#else + return 0; +#endif +} + //============================================================================== //! An object used to prevent concurrent access to a piece of data. // diff --git a/src/initialize.cpp b/src/initialize.cpp index cafcd582846..9e3c886039c 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -23,6 +23,7 @@ #include "openmc/message_passing.h" #include "openmc/mgxs_interface.h" #include "openmc/nuclide.h" +#include "openmc/openmp_interface.h" #include "openmc/output.h" #include "openmc/plot.h" #include "openmc/random_lcg.h" @@ -63,13 +64,7 @@ int openmc_init(int argc, char* argv[], const void* intracomm) return err; #ifdef LIBMESH - -#ifdef _OPENMP - int n_threads = omp_get_max_threads(); -#else - int n_threads = 1; -#endif - + const int n_threads = num_threads(); // initialize libMesh if it hasn't been initialized already // (if initialized externally, the libmesh_init object needs to be provided // also) diff --git a/src/mesh.cpp b/src/mesh.cpp index ea66552de40..34510b614e4 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -8,9 +8,7 @@ #ifdef OPENMC_MPI #include "mpi.h" #endif -#ifdef _OPENMP -#include -#endif + #include "xtensor/xbuilder.hpp" #include "xtensor/xeval.hpp" #include "xtensor/xmath.hpp" @@ -27,6 +25,7 @@ #include "openmc/hdf5_interface.h" #include "openmc/memory.h" #include "openmc/message_passing.h" +#include "openmc/openmp_interface.h" #include "openmc/random_dist.h" #include "openmc/search.h" #include "openmc/settings.h" @@ -2715,13 +2714,7 @@ void LibMesh::initialize() libMesh::ExplicitSystem& eq_sys = equation_systems_->add_system(eq_system_name_); -#ifdef _OPENMP - int n_threads = omp_get_max_threads(); -#else - int n_threads = 1; -#endif - - for (int i = 0; i < n_threads; i++) { + for (int i = 0; i < num_threads(); i++) { pl_.emplace_back(m_->sub_point_locator()); pl_.back()->set_contains_point_tol(FP_COINCIDENT); pl_.back()->enable_out_of_mesh_mode(); @@ -2896,13 +2889,7 @@ int LibMesh::get_bin(Position r) const return -1; } -#ifdef _OPENMP - int thread_num = omp_get_thread_num(); -#else - int thread_num = 0; -#endif - - const auto& point_locator = pl_.at(thread_num); + const auto& point_locator = pl_.at(thread_num()); const auto elem_ptr = (*point_locator)(p); return elem_ptr ? get_bin_from_element(elem_ptr) : -1; diff --git a/src/plot.cpp b/src/plot.cpp index e2f07c47cf0..8d995b0a9a2 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -23,6 +23,7 @@ #include "openmc/material.h" #include "openmc/mesh.h" #include "openmc/message_passing.h" +#include "openmc/openmp_interface.h" #include "openmc/output.h" #include "openmc/particle.h" #include "openmc/progress_bar.h" @@ -1218,11 +1219,7 @@ void ProjectionPlot::create_output() const * Note that a vector of vectors is required rather than a 2-tensor, * since the stack size varies within each column. */ -#ifdef _OPENMP - const int n_threads = omp_get_max_threads(); -#else - const int n_threads = 1; -#endif + const int n_threads = num_threads(); std::vector>> this_line_segments( n_threads); for (int t = 0; t < n_threads; ++t) { @@ -1234,14 +1231,8 @@ void ProjectionPlot::create_output() const #pragma omp parallel { - -#ifdef _OPENMP - const int n_threads = omp_get_max_threads(); - const int tid = omp_get_thread_num(); -#else - int n_threads = 1; - int tid = 0; -#endif + const int n_threads = num_threads(); + const int tid = thread_num(); SourceSite s; // Where particle starts from (camera) s.E = 1; diff --git a/src/volume_calc.cpp b/src/volume_calc.cpp index 8cd697a9d84..859a0d5ac1b 100644 --- a/src/volume_calc.cpp +++ b/src/volume_calc.cpp @@ -10,18 +10,16 @@ #include "openmc/message_passing.h" #include "openmc/mgxs_interface.h" #include "openmc/nuclide.h" +#include "openmc/openmp_interface.h" #include "openmc/output.h" #include "openmc/random_lcg.h" #include "openmc/settings.h" #include "openmc/timer.h" #include "openmc/xml_interface.h" -#include -#ifdef _OPENMP -#include -#endif #include "xtensor/xadapt.hpp" #include "xtensor/xview.hpp" +#include #include // for copy #include // for pow, sqrt @@ -205,12 +203,7 @@ vector VolumeCalculation::execute() const // At this point, each thread has its own pair of index/hits lists and we // now need to reduce them. OpenMP is not nearly smart enough to do this // on its own, so we have to manually reduce them - -#ifdef _OPENMP - int n_threads = omp_get_num_threads(); -#else - int n_threads = 1; -#endif + const int n_threads = num_threads(); #pragma omp for ordered schedule(static) for (int i = 0; i < n_threads; ++i) {