From 8e0b6160751398985d464e9e7b843b4d803895ed Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Mon, 4 Nov 2019 22:35:05 -0500 Subject: [PATCH] Create use_all_threads variable instead of n_threads --- include/openmc/capi.h | 2 +- openmc/capi/core.py | 2 +- openmc/cmfd.py | 23 +++++++++++------------ src/cmfd_solver.cpp | 18 ++++++------------ 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 0acc29e780..dbdf713973 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -138,7 +138,7 @@ extern "C" { const int* indices, int n_elements, int dim, double spectral, const int* cmfd_indices, - const int* map, int n_threads); + const int* map, bool use_all_threads); //! Runs a Gauss Seidel linear solver to solve CMFD matrix equations //! linear solver diff --git a/openmc/capi/core.py b/openmc/capi/core.py index fe911d3a52..546121c98c 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -47,7 +47,7 @@ _dll.openmc_get_keff.argtypes = [POINTER(c_double*2)] _dll.openmc_get_keff.restype = c_int _dll.openmc_get_keff.errcheck = _error_handler _init_linsolver_argtypes = [_array_1d_int, c_int, _array_1d_int, c_int, c_int, - c_double, _array_1d_int, _array_1d_int, c_int] + c_double, _array_1d_int, _array_1d_int, c_bool] _dll.openmc_initialize_linsolver.argtypes = _init_linsolver_argtypes _dll.openmc_initialize_linsolver.restype = None _dll.openmc_is_statepoint_batch.restype = c_bool diff --git a/openmc/cmfd.py b/openmc/cmfd.py index c711c9b468..445eee7f4a 100644 --- a/openmc/cmfd.py +++ b/openmc/cmfd.py @@ -295,6 +295,8 @@ class CMFDRun(object): Time for building CMFD matrices, in seconds time_cmfdsolve : float Time for solving CMFD matrix equations, in seconds + use_all_threads : bool + Whether to use all threads allocated to OpenMC for CMFD solver intracomm : mpi4py.MPI.Intracomm or None MPI intercommunicator for running MPI commands @@ -328,7 +330,7 @@ class CMFDRun(object): self._window_type = 'none' self._window_size = 10 self._intracomm = None - self._n_threads = 1 + self._use_all_threads = False # External variables used during runtime but users cannot control self._set_reference_params = False @@ -494,8 +496,8 @@ class CMFDRun(object): return self._indices @property - def n_threads(self): - return self._n_threads + def use_all_threads(self): + return self._use_all_threads @property def cmfd_src(self): @@ -683,11 +685,10 @@ class CMFDRun(object): check_length('Gauss-Seidel tolerance', gauss_seidel_tolerance, 2) self._gauss_seidel_tolerance = gauss_seidel_tolerance - @n_threads.setter - def n_threads(self, n_threads): - check_type('CMFD number of threads', n_threads, Integral) - check_greater_than('CMFD number of threads', n_threads, 0) - self._n_threads = n_threads + @use_all_threads.setter + def use_all_threads(self, use_all_threads): + check_type('CMFD use all threads', use_all_threads, bool) + self._use_all_threads = use_all_threads def run(self, **kwargs): """Run OpenMC with coarse mesh finite difference acceleration @@ -704,8 +705,7 @@ class CMFDRun(object): """ with self.run_in_memory(**kwargs): for _ in self.iter_batches(): - print('done') - #pass + pass @contextmanager def run_in_memory(self, **kwargs): @@ -800,7 +800,6 @@ class CMFDRun(object): # Run next batch status = openmc.capi.next_batch() - print('2') # Perform CMFD calculations self._execute_cmfd() @@ -918,7 +917,7 @@ class CMFDRun(object): args = temp_loss.indptr, len(temp_loss.indptr), \ temp_loss.indices, len(temp_loss.indices), n, \ - self._spectral, self._indices, coremap, self._n_threads + self._spectral, self._indices, coremap, self._use_all_threads return openmc.capi._dll.openmc_initialize_linsolver(*args) def _write_cmfd_output(self): diff --git a/src/cmfd_solver.cpp b/src/cmfd_solver.cpp index 40be440d5f..6c432f595c 100644 --- a/src/cmfd_solver.cpp +++ b/src/cmfd_solver.cpp @@ -32,9 +32,7 @@ int nx, ny, nz, ng; xt::xtensor indexmap; -int n_threads; - -int n_threads_reset; +int use_all_threads; } // namespace cmfd @@ -108,7 +106,7 @@ int cmfd_linsolver_1g(const double* A_data, const double* b, double* x, for (int irb = 0; irb < 2; irb++) { // Loop around matrix rows - #pragma omp parallel for reduction (+:err) num_threads(cmfd::n_threads) + #pragma omp parallel for reduction (+:err) if(cmfd::use_all_threads) for (int irow = 0; irow < cmfd::dim; irow++) { int g, i, j, k; matrix_to_indices(irow, g, i, j, k); @@ -175,7 +173,7 @@ int cmfd_linsolver_2g(const double* A_data, const double* b, double* x, for (int irb = 0; irb < 2; irb++) { // Loop around matrix rows - #pragma omp parallel for reduction (+:err) num_threads(cmfd::n_threads) + #pragma omp parallel for reduction (+:err) if(cmfd::use_all_threads) for (int irow = 0; irow < cmfd::dim; irow+=2) { int g, i, j, k; matrix_to_indices(irow, g, i, j, k); @@ -311,7 +309,7 @@ extern "C" void openmc_initialize_linsolver(const int* indptr, int len_indptr, const int* indices, int n_elements, int dim, double spectral, const int* cmfd_indices, - const int* map, int n_threads) + const int* map, bool use_all_threads) { // Store elements of indptr for (int i = 0; i < len_indptr; i++) @@ -339,12 +337,8 @@ void openmc_initialize_linsolver(const int* indptr, int len_indptr, set_indexmap(map); } -#ifdef _OPENMP - // Set number of threads to run CMFD solver on and store number of threads - // to reset to after solver finishes executing - cmfd::n_threads = n_threads; - cmfd::n_threads_reset = omp_get_max_threads(); -#endif + // Use all threads allocated to OpenMC simulation to run CMFD solver + cmfd::use_all_threads = use_all_threads; } //==============================================================================