diff --git a/docs/source/io_formats/settings.rst b/docs/source/io_formats/settings.rst index e5c0ca160..9c68a9730 100644 --- a/docs/source/io_formats/settings.rst +++ b/docs/source/io_formats/settings.rst @@ -730,15 +730,6 @@ sections. *Default*: 10 K ---------------------- -```` Element ---------------------- - -The ```` element indicates the number of OpenMP threads to be used for -a simulation. It has no attributes and accepts a positive integer value. - - *Default*: None (Determined by environment variable :envvar:`OMP_NUM_THREADS`) - .. _trace: ------------------- diff --git a/include/openmc/simulation.h b/include/openmc/simulation.h index 3eb5b1f05..8d43108f1 100644 --- a/include/openmc/simulation.h +++ b/include/openmc/simulation.h @@ -44,12 +44,8 @@ extern std::vector work_index; // Threadprivate variables extern "C" bool trace; //!< flag to show debug information -#ifdef _OPENMP -extern "C" int n_threads; //!< number of OpenMP threads -extern "C" int thread_id; //!< ID of a given thread -#endif -#pragma omp threadprivate(current_work, thread_id, trace) +#pragma omp threadprivate(current_work, trace) } // namespace simulation diff --git a/openmc/settings.py b/openmc/settings.py index 4be2eaaa3..f348bc6cb 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -130,8 +130,6 @@ class Settings(object): range. 'multipole' is a boolean indicating whether or not the windowed multipole method should be used to evaluate resolved resonance cross sections. - threads : int - Number of OpenMP threads trace : tuple or list Show detailed information about a single particle, indicated by three integers: the batch number, generation number, and particle number @@ -197,7 +195,6 @@ class Settings(object): self._statepoint = {} self._sourcepoint = {} - self._threads = None self._no_reduce = None self._verbosity = None @@ -312,10 +309,6 @@ class Settings(object): def statepoint(self): return self._statepoint - @property - def threads(self): - return self._threads - @property def no_reduce(self): return self._no_reduce @@ -623,12 +616,6 @@ class Settings(object): self._temperature = temperature - @threads.setter - def threads(self, threads): - cv.check_type('number of threads', threads, Integral) - cv.check_greater_than('number of threads', threads, 0) - self._threads = threads - @trace.setter def trace(self, trace): cv.check_type('trace', trace, Iterable, Integral) @@ -885,11 +872,6 @@ class Settings(object): else: element.text = str(value) - def _create_threads_subelement(self, root): - if self._threads is not None: - element = ET.SubElement(root, "threads") - element.text = str(self._threads) - def _create_trace_subelement(self, root): if self._trace is not None: element = ET.SubElement(root, "trace") @@ -980,7 +962,6 @@ class Settings(object): self._create_entropy_mesh_subelement(root_element) self._create_trigger_subelement(root_element) self._create_no_reduce_subelement(root_element) - self._create_threads_subelement(root_element) self._create_verbosity_subelement(root_element) self._create_tabular_legendre_subelements(root_element) self._create_temperature_subelements(root_element) diff --git a/src/bank.cpp b/src/bank.cpp index fc48a7761..a80cfbf37 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -31,7 +31,7 @@ std::vector master_fission_bank; void free_memory_bank() { simulation::source_bank.clear(); -#pragma omp parallel + #pragma omp parallel { simulation::fission_bank.clear(); } diff --git a/src/eigenvalue.cpp b/src/eigenvalue.cpp index 170195529..f5ed493fb 100644 --- a/src/eigenvalue.cpp +++ b/src/eigenvalue.cpp @@ -20,6 +20,10 @@ #include "openmc/timer.h" #include "openmc/tallies/tally.h" +#ifdef _OPENMP +#include +#endif + #include // for min #include #include // for sqrt, abs, pow @@ -348,11 +352,13 @@ void calculate_average_keff() #ifdef _OPENMP void join_bank_from_threads() { + int n_threads = omp_get_max_threads(); + #pragma omp parallel { // Copy thread fission bank sites to one shared copy #pragma omp for ordered schedule(static) - for (int i = 0; i < simulation::n_threads; ++i) { + for (int i = 0; i < n_threads; ++i) { #pragma omp ordered { std::copy( @@ -367,7 +373,7 @@ void join_bank_from_threads() #pragma omp barrier // Now copy the shared fission bank sites back to the master thread's copy. - if (simulation::thread_id == 0) { + if (omp_get_thread_num() == 0) { simulation::fission_bank = simulation::master_fission_bank; simulation::master_fission_bank.clear(); } else { diff --git a/src/initialize.cpp b/src/initialize.cpp index 3aa31184f..61390651f 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -196,13 +196,13 @@ parse_command_line(int argc, char* argv[]) #ifdef _OPENMP // Read and set number of OpenMP threads - simulation::n_threads = std::stoi(argv[i]); - if (simulation::n_threads < 1) { + int n_threads = std::stoi(argv[i]); + if (n_threads < 1) { std::string msg {"Number of threads must be positive."}; strcpy(openmc_err_msg, msg.c_str()); return OPENMC_E_INVALID_ARGUMENT; } - omp_set_num_threads(simulation::n_threads); + omp_set_num_threads(n_threads); #else if (mpi::master) warning("Ignoring number of threads specified on command line."); diff --git a/src/output.cpp b/src/output.cpp index 92ad14e9d..82aaca989 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -11,7 +11,9 @@ #include #include // for pair +#ifdef _OPENMP #include +#endif #include "xtensor/xview.hpp" #include "openmc/capi.h" diff --git a/src/particle.cpp b/src/particle.cpp index 92045e2cd..ab0f6effb 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -590,7 +590,7 @@ Particle::mark_as_lost(const char* message) // Increment number of lost particles alive_ = false; -#pragma omp atomic + #pragma omp atomic simulation::n_lost_particles += 1; // Count the total number of simulated particles (on this processor) @@ -615,7 +615,7 @@ Particle::write_restart() const filename << settings::path_output << "particle_" << simulation::current_batch << '_' << id_ << ".h5"; -#pragma omp critical (WriteParticleRestart) + #pragma omp critical (WriteParticleRestart) { // Create file hid_t file_id = file_open(filename.str(), 'w'); diff --git a/src/physics_mg.cpp b/src/physics_mg.cpp index 2a6b700f0..84dfa9cde 100644 --- a/src/physics_mg.cpp +++ b/src/physics_mg.cpp @@ -174,22 +174,19 @@ absorption(Particle* p) { if (settings::survival_biasing) { // Determine weight absorbed in survival biasing - p->wgt_absorb_ = p->wgt_ * - p->macro_xs_.absorption / p->macro_xs_.total; + p->wgt_absorb_ = p->wgt_ * p->macro_xs_.absorption / p->macro_xs_.total; // Adjust weight of particle by the probability of absorption p->wgt_ -= p->wgt_absorb_; p->wgt_last_ = p->wgt_; // Score implicit absorpion estimate of keff -#pragma omp atomic - global_tally_absorption += p->wgt_absorb_ * - p->macro_xs_.nu_fission / - p->macro_xs_.absorption; + #pragma omp atomic + global_tally_absorption += p->wgt_absorb_ * p->macro_xs_.nu_fission / + p->macro_xs_.absorption; } else { - if (p->macro_xs_.absorption > - prn() * p->macro_xs_.total) { -#pragma omp atomic + if (p->macro_xs_.absorption > prn() * p->macro_xs_.total) { + #pragma omp atomic global_tally_absorption += p->wgt_ * p->macro_xs_.nu_fission / p->macro_xs_.absorption; p->alive_ = false; diff --git a/src/random_lcg.cpp b/src/random_lcg.cpp index cf5d7e34b..5002b4279 100644 --- a/src/random_lcg.cpp +++ b/src/random_lcg.cpp @@ -141,7 +141,7 @@ extern "C" void openmc_set_seed(int64_t new_seed) { seed = new_seed; -#pragma omp parallel + #pragma omp parallel { for (int i = 0; i < N_STREAMS; i++) { prn_seed[i] = seed + i; diff --git a/src/settings.cpp b/src/settings.cpp index dfa5ebb7e..4c041a080 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -383,20 +383,8 @@ void read_settings_xml() // Number of OpenMP threads if (check_for_node(root, "threads")) { -#ifdef _OPENMP - if (simulation::n_threads == 0) { - simulation::n_threads = std::stoi(get_node_value(root, "threads")); - if (simulation::n_threads < 1) { - std::stringstream msg; - msg << "Invalid number of threads: " << simulation::n_threads; - fatal_error(msg); - } - omp_set_num_threads(simulation::n_threads); - } -#else - if (mpi::master) warning("OpenMC was not compiled with OpenMP support; " - "ignoring number of threads."); -#endif + if (mpi::master) warning("The element has been deprecated. Use " + "the OMP_NUM_THREADS environment variable to set the number of threads."); } // ========================================================================== diff --git a/src/simulation.cpp b/src/simulation.cpp index fca0c8528..ea313e94c 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -20,7 +20,9 @@ #include "openmc/tallies/tally.h" #include "openmc/tallies/trigger.h" +#ifdef _OPENMP #include +#endif #include "xtensor/xview.hpp" #include @@ -59,7 +61,7 @@ int openmc_simulation_init() calculate_work(); // Allocate array for matching filter bins -#pragma omp parallel + #pragma omp parallel { simulation::filter_matches.resize(model::tally_filters.size()); } @@ -136,7 +138,7 @@ int openmc_simulation_finalize() // Write tally results to tallies.out if (settings::output_tallies && mpi::master) write_tallies(); -#pragma omp parallel + #pragma omp parallel { simulation::filter_matches.clear(); } @@ -186,7 +188,7 @@ int openmc_next_batch(int* status) // ==================================================================== // LOOP OVER PARTICLES -#pragma omp parallel for schedule(runtime) + #pragma omp parallel for schedule(runtime) for (int64_t i_work = 1; i_work <= simulation::work; ++i_work) { simulation::current_work = i_work; @@ -250,10 +252,6 @@ std::vector work_index; // Threadprivate variables bool trace; //!< flag to show debug information -#ifdef _OPENMP -int n_threads {-1}; //!< number of OpenMP threads -int thread_id; //!< ID of a given thread -#endif } // namespace simulation @@ -273,15 +271,13 @@ void allocate_banks() // a generation, there is also a 'master_fission_bank' that is used to // collect the sites from each thread. - simulation::n_threads = omp_get_max_threads(); - -#pragma omp parallel + #pragma omp parallel { - simulation::thread_id = omp_get_thread_num(); - if (simulation::thread_id == 0) { + if (omp_get_thread_num() == 0) { simulation::fission_bank.reserve(3*simulation::work); } else { - simulation::fission_bank.reserve(3*simulation::work / simulation::n_threads); + int n_threads = omp_get_num_threads(); + simulation::fission_bank.reserve(3*simulation::work / n_threads); } } simulation::master_fission_bank.reserve(3*simulation::work); @@ -403,9 +399,9 @@ void finalize_generation() auto& gt = simulation::global_tallies; // Update global tallies with the omp private accumulation variables -#pragma omp parallel + #pragma omp parallel { -#pragma omp critical(increment_global_tallies) + #pragma omp critical(increment_global_tallies) { if (settings::run_mode == RUN_MODE_EIGENVALUE) { gt(K_COLLISION, RESULT_VALUE) += global_tally_collision; diff --git a/tests/unit_tests/test_settings.py b/tests/unit_tests/test_settings.py index 697eb095f..e42f6240f 100644 --- a/tests/unit_tests/test_settings.py +++ b/tests/unit_tests/test_settings.py @@ -36,7 +36,6 @@ def test_export_to_xml(run_in_tmpdir): s.tabular_legendre = {'enable': True, 'num_points': 50} s.temperature = {'default': 293.6, 'method': 'interpolation', 'multipole': True, 'range': (200., 1000.)} - s.threads = 8 s.trace = (10, 1, 20) s.track = [1, 1, 1, 2, 1, 1] s.ufs_mesh = mesh