Skip to content

Commit

Permalink
Consolidating number of threads and thread number queries into the op…
Browse files Browse the repository at this point in the history
…enmp interface header (#2809)
  • Loading branch information
pshriwise authored Dec 19, 2023
1 parent fb65dfd commit 53363da
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 47 deletions.
21 changes: 21 additions & 0 deletions include/openmc/openmp_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
9 changes: 2 additions & 7 deletions src/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 4 additions & 17 deletions src/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 4 additions & 13 deletions src/plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
13 changes: 3 additions & 10 deletions src/volume_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 53363da

Please sign in to comment.