Consolidating number of threads and thread number queries into the openmp interface header (#2809)

This commit is contained in:
Patrick Shriwise 2023-12-18 21:31:29 -06:00 committed by GitHub
parent fb65dfd53c
commit 53363da3cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 47 deletions

View file

@ -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.
//

View file

@ -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)

View file

@ -8,9 +8,7 @@
#ifdef OPENMC_MPI
#include "mpi.h"
#endif
#ifdef _OPENMP
#include <omp.h>
#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<libMesh::ExplicitSystem>(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;

View file

@ -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<std::vector<std::vector<TrackSegment>>> 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;

View file

@ -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 <fmt/core.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#include "xtensor/xadapt.hpp"
#include "xtensor/xview.hpp"
#include <fmt/core.h>
#include <algorithm> // for copy
#include <cmath> // for pow, sqrt
@ -205,12 +203,7 @@ vector<VolumeCalculation::Result> 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) {