GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmOmp.h
Go to the documentation of this file.
1/************************************************************************
2**
3** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4** All rights reserved.
5**
6** This file is part of the "GeMA" software. It's use should respect
7** the terms in the license agreement that can be found together
8** with this source code.
9** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11** A PARTICULAR PURPOSE.
12**
13************************************************************************/
14
26#ifndef _GEMA_OPENMP_H_
27#define _GEMA_OPENMP_H_
28
29#ifdef _OPENMP
30#include <omp.h>
31#endif
32
33#include <assert.h>
34
38inline void GmOmpInit(int maxThreads)
39{
40 assert(maxThreads > 0);
41#ifdef _OPENMP
42 omp_set_num_threads(maxThreads);
43 omp_set_dynamic(0);
44#endif
45}
46
53inline int GmOmpAdjustNumThreads(int nt)
54{
55#ifdef _OPENMP
56 int mt = omp_get_max_threads();
57 assert(mt > 0);
58 return (nt > 0 && nt <= mt) ? nt : mt;
59#else
60 return 1;
61#endif
62}
63
68inline int GmOmpThreadNum()
69{
70#ifdef _OPENMP
71 return omp_get_thread_num();
72#else
73 return 0;
74#endif
75}
76
77
80{
81public:
82 GmOmpTempUpdateNumThreads(int nthreads) {
83#ifdef _OPENMP
84 _oldNumThreads = omp_get_max_threads(); omp_set_num_threads(nthreads);
85#endif
86 }
88#ifdef _OPENMP
89 omp_set_num_threads(_oldNumThreads);
90#endif
91 }
92private:
93 int _oldNumThreads;
94};
95
96
97#ifdef _OPENMP
98#define GM_OMP_ASSERT_NO_DYNAMIC assert(omp_get_dynamic() == 0)
99#else
100#define GM_OMP_ASSERT_NO_DYNAMIC ((void)0)
101#endif
102
103#endif
A RAII class used to temporarily adjust the number of OMP threads in effect.
Definition gmOmp.h:80
void GmOmpInit(int maxThreads)
Initializes OpenMP to use the given maximum number of threads. It also disables dynamic scheduling.
Definition gmOmp.h:38
int GmOmpAdjustNumThreads(int nt)
Adjusts the given number of threads. If nt <= 0 or if nt > maximum number of omp threads,...
Definition gmOmp.h:53
int GmOmpThreadNum()
Returns the current Open Mp thread number (omp_get_thread_num()).
Definition gmOmp.h:68