Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
threadManager_OpenMP.cpp
Go to the documentation of this file.
1 
15 
16 #include <omp.h>
17 
19 #include "IO/bridgeIO.h"
20 using Bridge::vout;
21 
22 //====================================================================
23 // initialization of static member variables.
24 
27 std::vector<double> ThreadManager_OpenMP::m_darray_reduction(0);
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 Nthread = get_num_threads();
89 
90  barrier(Nthread);
91 }
92 
93 
94 //====================================================================
96 {
97 #pragma omp barrier
98 }
99 
100 
101 //====================================================================
103 {
104 #pragma omp barrier
105 #pragma omp master
106  {
108  }
109 #pragma omp barrier
110 }
111 
112 
113 //====================================================================
115  const int i_thread, const int Nthread)
116 {
117  m_darray_reduction[i_thread] = a;
118  barrier(Nthread);
119 
120 #pragma omp barrier
121 #pragma omp master
122  {
123  double b = 0.0;
124 
125 #ifdef NECSX
126 #pragma omp flush
127 #else
128 #pragma omp flush (m_darray_reduction)
129 #endif
130 
131  for (int i = 0; i < Nthread; ++i) {
132  b += m_darray_reduction[i];
133  }
135 
136 #ifdef NECSX
137 #pragma omp flush
138 #else
139 #pragma omp flush (m_darray_reduction)
140 #endif
141 
142  m_darray_reduction[0] = b;
143  }
144 
145 #pragma omp barrier
146 
147  a = m_darray_reduction[0];
148 
149 #pragma omp barrier
150 }
151 
152 
153 //====================================================================
154 void ThreadManager_OpenMP::assert_single_thread(const std::string& name)
155 {
156  int Nthread = get_num_threads();
157 
158  if (Nthread != 1) {
159  vout.crucial(m_vl, "\n");
160  vout.crucial(m_vl, "##### Caution #####\n");
161  vout.crucial(m_vl, "Single-thread %s is called in parallel region.\n", name.c_str());
162  vout.crucial(m_vl, "Current number of thread = %d.\n", Nthread);
163 
164  exit(EXIT_FAILURE);
165  }
166 }
167 
168 
169 //====================================================================
170 //============================================================END=====
static int m_Nthread
number of threads.
BridgeIO vout
Definition: bridgeIO.cpp:495
static int get_num_threads()
returns available number of threads.
void general(const char *format,...)
Definition: bridgeIO.cpp:195
static Bridge::VerboseLevel Vlevel()
static void wait()
barrier among threads inside a node.
static int get_thread_id()
returns thread id.
static void init(int Nthread)
setup: called in main only once.
static const std::string class_name
static void barrier(const int Nthread)
barrier among threads inside a node.
static void reduce_sum_global(double &value, const int i_thread, const int Nthread)
global reduction with summation: value is assumed thread local.
static void sync_barrier_all()
barrier among all the threads and nodes.
static void finalize()
finalization.
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:229
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
static Bridge::VerboseLevel m_vl
verbose level.
static std::vector< double > m_darray_reduction
VerboseLevel
Definition: bridgeIO.h:42
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 &class_name)
assert currently running on single thread.