aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/dist.cc
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@cct.lsu.edu>2007-08-21 18:52:00 +0000
committerErik Schnetter <schnetter@cct.lsu.edu>2007-08-21 18:52:00 +0000
commit28eaf1591b3673641eaa3c7046e8b937edfb221b (patch)
tree8717378af9c6ee7f885484376887a6781e2ed784 /Carpet/CarpetLib/src/dist.cc
parentb13e77bac146afa79d7855f62bd0433e0b4bb4ed (diff)
CarpetLib: Add support for OpenMP
Add #pragma omp statements for loops in reduction and prolongation operators. Change loop control variables to signed types. Add functions to determine the number of active threads. Add a parameter to set the number of threads if desired. darcs-hash:20070821185237-dae7b-56827b72a69b5fa1b3d1316379a0f155696b4cb2.gz
Diffstat (limited to 'Carpet/CarpetLib/src/dist.cc')
-rw-r--r--Carpet/CarpetLib/src/dist.cc44
1 files changed, 44 insertions, 0 deletions
diff --git a/Carpet/CarpetLib/src/dist.cc b/Carpet/CarpetLib/src/dist.cc
index 79871f3f0..1af4bcf38 100644
--- a/Carpet/CarpetLib/src/dist.cc
+++ b/Carpet/CarpetLib/src/dist.cc
@@ -1,5 +1,8 @@
#include <cassert>
+#ifdef _OPENMP
+# include <omp.h>
+#endif
#include <mpi.h>
#include "cctk.h"
@@ -63,4 +66,45 @@ namespace dist {
}
}
+ // Local number of threads
+ int num_threads_worker ()
+ {
+ DECLARE_CCTK_PARAMETERS;
+ int num_threads_;
+#ifdef _OPENMP
+ if (num_threads > 0) {
+ // Set number of threads which should be used
+ // TODO: do this at startup, not in this routine
+ omp_set_num_threads (num_threads);
+ }
+#pragma omp parallel
+ {
+#pragma omp single nowait
+ {
+ num_threads_ = omp_get_num_threads();
+ }
+ }
+#else
+ if (num_threads > 0 and num_threads != 1) {
+ CCTK_WARN (CCTK_WARN_ABORT,
+ "OpenMP is not enabled. Cannot set the number of threads.");
+ }
+ num_threads_ = 1;
+#endif
+ assert (num_threads_ >= 1);
+ return num_threads_;
+ }
+
+ // Global number of threads
+ int total_num_threads_worker ()
+ {
+ int total_num_threads_;
+ int const mynthreads = num_threads();
+ MPI_Allreduce
+ (const_cast <int *> (& mynthreads), & total_num_threads_, 1, MPI_INT,
+ MPI_SUM, comm());
+ assert (total_num_threads_ >= size());
+ return total_num_threads_;
+ }
+
} // namespace dist