Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
threadManager_OpenMP.cpp
Go to the documentation of this file.
1 
14 #include "threadManager_OpenMP.h"
15 
16 #include <omp.h>
17 
18 #include "communicator.h"
19 #include "bridgeIO.h"
20 using Bridge::vout;
21 
22 //====================================================================
23 // initialization of static member variables.
24 
28 
29 const std::string ThreadManager_OpenMP::class_name = "ThreadManager_OpenMP";
30 
31 //====================================================================
32 void ThreadManager_OpenMP::init(int nthread)
33 {
35 
36  vout.general(m_vl, "%s: initialization\n", class_name.c_str());
37 
38  int nthread_env = 0;
39 
40 #pragma omp parallel
41  {
42  if (omp_get_thread_num() == 0) {
43  nthread_env = omp_get_num_threads();
44  }
45  }
46 
47 
48  if ((nthread == nthread_env) || (nthread == 0)) {
49  m_Nthread = nthread_env;
50  } else {
51  vout.general(m_vl, " Number of threads(env) = %d\n", nthread_env);
52  vout.general(m_vl, " Number of threads(input) = %d\n", nthread);
53  vout.general(m_vl, " resetting Number of threads.\n");
54  omp_set_num_threads(nthread);
55  m_Nthread = nthread;
56  }
57 
58  vout.general(m_vl, " Number of thread = %d\n", m_Nthread);
59 
61 }
62 
63 
64 //====================================================================
66 {
67  vout.paranoiac(m_vl, "%s: finalize.\n", class_name.c_str());
68 }
69 
70 
71 //====================================================================
73 {
74  return omp_get_num_threads();
75 }
76 
77 
78 //====================================================================
80 {
81  return omp_get_thread_num();
82 }
83 
84 
85 //====================================================================
87 {
88  int nth = get_num_threads();
89 
90  barrier(nth);
91 }
92 
93 
94 //====================================================================
96 {
97 #pragma omp barrier
98 }
99 
100 
101 //====================================================================
103 {
104 #pragma omp barrier
105 
106 #pragma omp master
107  {
109  }
110 
111 #pragma omp barrier
112 }
113 
114 
115 //====================================================================
117  const int ith, const int nth)
118 {
119  m_darray_reduction[ith] = a;
120  barrier(nth);
121 #pragma omp barrier
122  // #pragma omp flush (m_darray_reduction)
123 
124 #pragma omp master
125  {
126  double b = 0.0;
127 
128 #pragma omp flush (m_darray_reduction)
129  for (int i = 0; i < nth; ++i) {
130  b += m_darray_reduction[i];
131  }
132  // m_darray_reduction[0] = Communicator::reduce_sum(b);
134 
135 #pragma omp flush (m_darray_reduction)
136  m_darray_reduction[0] = b;
137  // #pragma omp flush (m_darray_reduction)
138  }
139 
140  // barrier(nth);
141 #pragma omp barrier
142 
143  // #pragma omp flush (m_darray_reduction)
144  a = m_darray_reduction[0];
145 
146 #pragma omp barrier
147 }
148 
149 
150 //====================================================================
151 void ThreadManager_OpenMP::assert_single_thread(const std::string& name)
152 {
153  int nth = get_num_threads();
154 
155  if (nth != 1) {
156 #pragma omp master
157  {
158  vout.crucial(m_vl, "\n");
159  vout.crucial(m_vl, "##### Caution #####\n");
160  vout.crucial(m_vl, "Single-thread %s is called in parallel region.\n", name.c_str());
161  vout.crucial(m_vl, "Current number of thread = %d.\n", nth);
162  }
163 #pragma omp barrier
164 
165  abort();
166  }
167 }
168 
169 
170 //====================================================================
171 //============================================================END=====
static void barrier(const int nth)
barrier among threads inside a node.
static int m_Nthread
number of threads.
BridgeIO vout
Definition: bridgeIO.cpp:207
static int get_num_threads()
returns available number of threads.
void general(const char *format,...)
Definition: bridgeIO.cpp:38
static Bridge::VerboseLevel Vlevel()
static void wait()
barrier among threads inside a node.
static valarray< double > m_darray_reduction
static void reduce_sum_global(double &value, const int ith, const int nth)
global reduction with summation: value is assumed thread local.
static int get_thread_id()
returns thread id.
static const std::string class_name
static void sync_barrier_all()
barrier among all the threads and nodes.
static void finalize()
finalization.
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:62
static void init(int nthread)
setup: called in main only once.
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
static Bridge::VerboseLevel m_vl
verbose level.
VerboseLevel
Definition: bridgeIO.h:25
static int reduce_sum(int count, double *recv_buf, double *send_buf, int pattern=0)
make a global sum of an array of double over the communicator. pattern specifies the dimensions to be...
static int sync()
synchronize within small world.
static void assert_single_thread(const std::string &classname)
assert currently running on single thread.