C++ mpi module for stochmagnet_main Package
OMP_Thread.h
1 #ifndef OMP_Thread_H
2 #define OMP_Thread_H
3 
4 //base class header
5 #include "CORE_Object.h"
6 
7 //runner option
8 #include "CORE_Run.h"
9 
10 //OMP pragmas
11 #include "openMP.h"
12 
13 
24 class OMP_Thread : public CORE_Object {
25  // ATTRIBUTES
26 
27 private:
28 
29  static tInteger MAX_THREADS_NUMBER;
30 
31  // ASSOCIATIONS
32 
33 
34 protected:
35  // METHODS
36 
37  // CONSTRUCTORS
38 
40  OMP_Thread(void) {
41  }
42 
43 
44 
45  // DESTRUCTORS
46 
47 
50  virtual ~OMP_Thread(void) {
51  }
52 
53 
54 public:
55  // NEW methods
59  static inline CORE_UniquePointer<OMP_Thread> New() {
60  CORE_UniquePointer<OMP_Thread> p(new OMP_Thread(),OMP_Thread::Delete());
61  return p;
62  }
63 
77  virtual tMemSize getMemorySize() const {
78  return sizeof(*this)+getContentsMemorySize();
79  }
80 
81  // SET methods
82 public:
86  static inline void SetThreadsNumber(const tIndex& nThreads) {
87  if ((nThreads>GetMaxThreadsNumber() || (nThreads==0))) OMP_SET_THREADS_NUMBER(GetMaxThreadsNumber());
88  else OMP_SET_THREADS_NUMBER(nThreads);
89  tInteger nUsedThreads=GetThreadsNumber();
90  if (nUsedThreads<nThreads) {
91  std::cout<<"Warning: OpenMP threads number wanted: "<<nThreads<<", only "<<GetThreadsNumber()<<" available ! ";
92  }
93 #ifdef DEBUG
94  if (nUsedThreads>1) {
96  std::cout<<"Warning: memory checked is forced to be disabled because of multi-threadings ";
97  }
99  }
100 #endif
101  }
102 
106  static inline tBoolean IsOpenMPMultiThreaded() {
107  return OMP_IS_ENABLED();
108  }
109 
110 
111  // GET methods
112 
113 
117  static inline tInteger GetThreadId() {
118  return OMP_GET_THREAD_ID();
119  }
120 
124  static inline tInteger GetThreadsNumber() {
125  tInteger n=1;
126  OMP_PARALLEL_SHARED((n)) {
127  if (OMP_GET_THREAD_ID()==0) {
128  n=OMP_GET_THREADS_NUMBER();
129  }
130  }
131  return n;
132  }
133 
137  static inline const tInteger& GetMaxThreadsNumber() {
138  return MAX_THREADS_NUMBER;
139  }
140 
141 
145  static tULLInt GetTime() {
146  return OMP_GET_TIME();
147  }
151  static inline tULLInt GetDuration(const tULLInt& startTime) {
152  return GetTime()-startTime;
153  }
154 
159  static tInteger SetThreadsOptions(const CORE_OptionsList& options) {
160 
161  tInteger nThreads=1;
162  if (!options.getOptionValue("threads-number",nThreads)) {
163  nThreads=1;
164  }
165  SetThreadsNumber(nThreads);
166 
167  return nThreads;
168 
169  }
170 
171 
172 };
173 
174 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
abstract base class for most classes.
Definition: CORE_Object.h:65
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
static void DisableMemoryStack()
disable the memory stack
Definition: CORE_Object.h:200
static tBoolean IsMemoryStackEnabled()
return trur if the memory stack is enabled
Definition: CORE_Object.h:209
This class is an list of options.
Definition: CORE_OptionsList.h:36
tBoolean getOptionValue(const tString &optName, tString &optValue) const
get the option value
Definition: CORE_OptionsList.h:334
This class describes an Open MP process.
Definition: OMP_Thread.h:24
static tInteger SetThreadsOptions(const CORE_OptionsList &options)
set the threads data from the options
Definition: OMP_Thread.h:159
static CORE_UniquePointer< OMP_Thread > New()
create an instance of thread
Definition: OMP_Thread.h:59
static const tInteger & GetMaxThreadsNumber()
get the max threads number
Definition: OMP_Thread.h:137
static tBoolean IsOpenMPMultiThreaded()
return true if the program is multithreaded by OpenMP
Definition: OMP_Thread.h:106
virtual ~OMP_Thread(void)
destroy an object.
Definition: OMP_Thread.h:50
static void SetThreadsNumber(const tIndex &nThreads)
set the number of threads
Definition: OMP_Thread.h:86
static tInteger GetThreadId()
get the thread id
Definition: OMP_Thread.h:117
static tULLInt GetTime()
get time
Definition: OMP_Thread.h:145
static tULLInt GetDuration(const tULLInt &startTime)
get duration
Definition: OMP_Thread.h:151
OMP_Thread(void)
create an object
Definition: OMP_Thread.h:40
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: OMP_Thread.h:77
static tInteger GetThreadsNumber()
get the threads number
Definition: OMP_Thread.h:124