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 #if defined USE_OPENMP
17 #include <omp.h>
18 #include "bgqthread.h"
19 #endif
20 
21 #include "communicator.h"
22 #include "bridgeIO.h"
23 using Bridge::vout;
24 
25 //====================================================================
26 // initialization of static member variables.
27 
31 
32 //====================================================================
33 void ThreadManager_OpenMP::init(int nthread)
34 {
36 
37  vout.general(m_vl, "ThreadManager_OpenMP being setup.\n");
38 
39 #if defined USE_OPENMP
40  int nthread_env = 0;
41 #pragma omp parallel
42  {
43  if (omp_get_thread_num() == 0) {
44  nthread_env = omp_get_num_threads();
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.crucial(m_vl, " Number of thread = %d\n", m_Nthread);
59 
61 
62  // setup of BG/Q thread library
63  BGQThread_Init();
64 
65  /*
66  int Nthread_BGQTh = BGQThread_GetNumBarrier();
67  vout.general(m_vl,
68  " BGQ thread lib initialized: number of barrier objects = %d.\n",
69  Nthread_BGQTh);
70 
71  if(m_Nthread > Nthread_BGQTh){
72  vout.crucial(m_vl," Number of thread is too large.\n");
73  abort();
74  }
75  */
76 
77 #else
78  // OpenMP not used
79 
80  m_Nthread = 1;
81  vout.crucial(m_vl, " Number of thread = %d\n", m_Nthread);
82  vout.crucial(m_vl, " OpenMP not used.\n");
83 #endif
84 }
85 
86 
87 //====================================================================
89 {
90  vout.general(m_vl, "Thread manager says good-bye.\n");
91 }
92 
93 
94 //====================================================================
96 {
97 #if defined USE_OPENMP
98  return omp_get_num_threads();
99 
100 #else
101  return 1;
102 #endif
103 }
104 
105 
106 //====================================================================
108 {
109 #if defined USE_OPENMP
110  return omp_get_thread_num();
111 
112 #else
113  return 0;
114 #endif
115 }
116 
117 
118 //====================================================================
120 {
121 #if defined USE_OPENMP
122  int nth = get_num_threads();
123  barrier(nth);
124 #endif
125 }
126 
127 
128 //====================================================================
130 {
131 #if defined USE_OPENMP
132  //#pragma omp barrier
133 
134  // using BGQ thread
135  BGQThread_Barrier(0, nth);
136 #endif
137 }
138 
139 
140 //====================================================================
142 {
143 #pragma omp barrier
144 
145 #pragma omp master
146  {
148  }
149 
150 #pragma omp barrier
151 }
152 
153 
154 //====================================================================
156  const int ith, const int nth)
157 {
158 #if defined USE_OPENMP
159  // using BGQ thread
160 
161  /*
162  double b = BGQThread_GatherDouble(a, 0, ith, nth);
163 
164  if(ith == 0) b = Communicator::reduce_sum(b);
165 
166  barrier(nth);
167 
168  a = BGQThread_ScatterDouble(b, 0, ith, nth);
169  */
170 
171  /*
172  // OpenMP native reduction
173  m_darray_reduction[ith] = a;
174  // barrier(nth);
175  #pragma omp barrier
176 
177  #pragma omp flush (m_darray_reduction)
178  #pragma omp master
179  {
180  #pragma omp flush (m_darray_reduction)
181  double b = 0.0;
182  for(int i = 0; i < nth; ++i){
183  b += m_darray_reduction[i];
184  }
185  // m_darray_reduction[0] = Communicator::reduce_sum(b);
186  b = Communicator::reduce_sum(b);
187  m_darray_reduction[0] = b;
188  #pragma omp flush (m_darray_reduction)
189  }
190 
191  // barrier(nth);
192  #pragma omp barrier
193 
194  #pragma omp flush (m_darray_reduction)
195  //#pragma omp flush (m_darray_reduction)
196  a = m_darray_reduction[0];
197  */
198 
199  // OpenMP native reduction
200  m_darray_reduction[ith] = a;
201  barrier(nth);
202  // #pragma omp barrier
203 #pragma omp flush (m_darray_reduction)
204 
205 #pragma omp master
206  {
207 #pragma omp flush (m_darray_reduction)
208  double b = 0.0;
209  for (int i = 0; i < nth; ++i) {
210  b += m_darray_reduction[i];
211  }
212  // m_darray_reduction[0] = Communicator::reduce_sum(b);
214  m_darray_reduction[0] = b;
215 #pragma omp flush (m_darray_reduction)
216  }
217 
218  barrier(nth);
219  // #pragma omp barrier
220 
221 #pragma omp flush (m_darray_reduction)
222  a = m_darray_reduction[0];
223 
224 #else
226 #endif
227 }
228 
229 
230 //====================================================================
231 //============================================================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 void sync_barrier_all()
barrier among all the threads and nodes.
static void finalize()
finalization.
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.