KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
openmp_utils.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ \.
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 
13 #ifndef KRATOS_OPENMP_UTILS_H
14 #define KRATOS_OPENMP_UTILS_H
15 
16 #include <stdio.h>
17 #include <vector>
18 #include <iostream>
19 #ifdef KRATOS_SMP_OPENMP
20 #include <omp.h>
21 #else
22 #include <ctime>
23 #endif
24 #include "parallel_utilities.h"
25 
26 namespace Kratos
27 {
30 
33 
35 
43 {
44 public:
45 
48 
50 
53  typedef std::vector<int> PartitionVector;
54 
58 
60 
64  KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"ParallelUtilities\" instead") static inline int GetNumThreads()
65  {
67  }
68 
70 
74  {
75 #ifdef _OPENMP
76  return omp_get_num_threads();
77 #else
78  return 1;
79 #endif
80  }
81 
83 
86  KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"ParallelUtilities\" instead") static int GetNumberOfProcessors()
87  {
89  }
90 
92 
95  static int IsDynamic()
96  {
97 #ifdef _OPENMP
98  return omp_get_dynamic();
99 #else
100  return 0;
101 #endif
102  }
103 
105 
108  static inline int ThisThread()
109  {
110 #ifdef _OPENMP
111  return omp_get_thread_num();
112 #else
113  return 0;
114 #endif
115  }
116 
118 
122  static inline int IsInParallel()
123  {
124 #ifdef _OPENMP
125  return omp_in_parallel();
126 #else
127  return 0;
128 #endif
129  }
130 
132 
137  KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"utilities/builtin_timer.h\" instead") static double GetCurrentTime()
138  {
139 #ifndef _OPENMP
140  return std::clock()/static_cast<double>(CLOCKS_PER_SEC);
141 #else
142  return omp_get_wtime();
143 #endif
144  }
145 
147 
157  KRATOS_DEPRECATED_MESSAGE("This is legacy, please use the \"ParallelUtilities\" instead")
158  static inline void DivideInPartitions(
159  const int NumTerms,
160  const int NumThreads,
161  PartitionVector& Partitions)
162  {
163  Partitions.resize(NumThreads + 1);
164  int PartitionSize = NumTerms / NumThreads;
165  Partitions[0] = 0;
166  Partitions[NumThreads] = NumTerms;
167  for(int i = 1; i < NumThreads; i++)
168  Partitions[i] = Partitions[i-1] + PartitionSize ;
169  }
170 
172 
177  template< class TVector >
178  KRATOS_DEPRECATED_MESSAGE("This is legacy, please use the \"ParallelUtilities\" instead")
179  static void PartitionedIterators(TVector& rVector,
180  typename TVector::iterator& rBegin,
181  typename TVector::iterator& rEnd)
182  {
183 #ifdef _OPENMP
184  int NumTerms = rVector.size();
185  int ThreadNum = omp_get_thread_num();
186  int NumThreads = omp_get_max_threads();
187  int PartitionSize = NumTerms / NumThreads;
188  // Set Partition start
189  rBegin = rVector.begin() + ThreadNum * PartitionSize;
190  // Partition ends after 'PartitionSize' terms, except if this is the last partition
191  if ( (ThreadNum + 1) != NumThreads )
192  rEnd = rBegin + PartitionSize;
193  else
194  rEnd = rVector.end();
195 #else
196  rBegin = rVector.begin();
197  rEnd = rVector.end();
198 #endif
199  }
200 
202 
209  KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"ParallelUtilities\" instead") static inline void SetNumThreads(int NumThreads = 1)
210  {
212  }
213 
217  static inline void PrintOMPInfo()
218  {
219 #ifdef _OPENMP
220 
221  int nthreads,tid, procs, maxt, inpar, dynamic, nested;
222 
223  /* Start parallel region */
224 
225 #pragma omp parallel private(nthreads, tid)
226  {
227  /* Obtain thread number */
228  tid = omp_get_thread_num();
229 
230  /* Only master thread does this */
231  if (tid == 0)
232  {
233  printf(" Thread %d getting environment info...\n", tid);
234 
235  /* Get environment information */
236  procs = omp_get_num_procs();
237  nthreads = omp_get_num_threads();
238  maxt = omp_get_max_threads();
239  inpar = omp_in_parallel();
240  //omp_set_dynamic(true);
241  dynamic = omp_get_dynamic();
242  //omp_set_nested(true);
243  nested = omp_get_nested();
244 
245  /* Print environment information */
246  printf( " | ------------ OMP IN USE --------- |\n");
247  printf( " | Machine number of processors = %d |\n", procs);
248  printf( " | Number of threads set = %d |\n", nthreads);
249  printf( " | Max threads in use = %d |\n", maxt);
250  printf( " | In parallel? = %d |\n", inpar);
251  printf( " | Dynamic threads enabled? = %d |\n", dynamic);
252  printf( " | Nested parallelism supported? = %d |\n", nested);
253  printf( " | --------------------------------- |\n");
254 
255 
256  if( procs < nthreads )
257  std::cout<<" ( WARNING: Maximimun number of threads is EXCEEDED )"<<std::endl;
258 
259  }
260 
261  }
262 
263 #endif
264  }
265 
266  template<class T>
267  KRATOS_DEPRECATED_MESSAGE("This is legacy, please use the \"ParallelUtilities\" instead")
268  static inline void CreatePartition(unsigned int number_of_threads, const int number_of_rows, T& partitions)
269  {
270  partitions.resize(number_of_threads+1);
271  int partition_size = number_of_rows / number_of_threads;
272  partitions[0] = 0;
273  partitions[number_of_threads] = number_of_rows;
274  for(unsigned int i = 1; i<number_of_threads; i++)
275  partitions[i] = partitions[i-1] + partition_size ;
276  }
277 
279 };
280 
282 
284 }
285 
286 #endif /* KRATOS_OPENMP_UTILS_H */
287 
Implements basic tasks for OpenMP parallelism and suitable scalar alternatives.
Definition: openmp_utils.h:43
static void DivideInPartitions(const int NumTerms, const int NumThreads, PartitionVector &Partitions)
Divide an array of length NumTerms between NumThreads threads.
Definition: openmp_utils.h:158
static int GetCurrentNumberOfThreads()
Wrapper for omp_get_num_threads().
Definition: openmp_utils.h:73
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"ParallelUtilities\" instead") static inline int GetNumThreads()
Wrapper for omp_get_max_threads().
Definition: openmp_utils.h:64
static int IsInParallel()
Wrapper for omp_in_parallel().
Definition: openmp_utils.h:122
KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"ParallelUtilities\" instead") static int GetNumberOfProcessors()
Wrapper for omp_get_num_procs().
Definition: openmp_utils.h:86
KRATOS_DEPRECATED_MESSAGE("This is legacy version, please use the \"utilities/builtin_timer.h\" instead") static double GetCurrentTime()
Timing routine.
Definition: openmp_utils.h:137
static void PartitionedIterators(TVector &rVector, typename TVector::iterator &rBegin, typename TVector::iterator &rEnd)
Generate a partition for an std::vector-like array, providing iterators to the begin and end position...
Definition: openmp_utils.h:179
static int IsDynamic()
Wrapper for omp_get_dynamic().
Definition: openmp_utils.h:95
std::vector< int > PartitionVector
Vector type for the output of DivideInPartitions method.
Definition: openmp_utils.h:53
static void SetNumThreads(const int NumThreads)
Sets the current number of threads.
Definition: parallel_utilities.cpp:49
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
static int GetNumProcs()
Returns the number of processors available to this device This can include the multiple threads per p...
Definition: parallel_utilities.cpp:69
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
tuple const
Definition: ode_solve.py:403
integer i
Definition: TensorModule.f:17