CbmRoot
Loading...
Searching...
No Matches
OpenMP.h
Go to the documentation of this file.
1/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Felix Weiglhofer [committer] */
4#ifndef CBM_ALGO_BASE_COMPAT_OPENMP_H
5#define CBM_ALGO_BASE_COMPAT_OPENMP_H
6
7#include "BuildInfo.h"
8
9#ifdef HAVE_OMP
10#include <omp.h>
11#endif
12
13#define CBM_PRAGMA(...) _Pragma(#__VA_ARGS__)
14
15// OpenMP parallel for
16// If OpenMP is not available, this macro expands to nothing
17//
18// Hiding the pragma in a macro isn't technically necessary, as the compiler will ignore it if OpenMP is not available.
19// But it slightly increases readability as it's indented to the same level as the code it applies to.
20//
21// Accepts the same arguments as the OpenMP parallel for pragma.
22#ifdef HAVE_OMP
23#define CBM_PARALLEL_FOR(...) CBM_PRAGMA(omp parallel for __VA_ARGS__)
24#else
25#define CBM_PARALLEL_FOR(...)
26#endif
27
28// OpenMP parallel
29#ifdef HAVE_OMP
30#define CBM_PARALLEL(...) CBM_PRAGMA(omp parallel __VA_ARGS__)
31#else
32#define CBM_PARALLEL(...)
33#endif
34
35// generic omp pragma for other commands
36#ifdef HAVE_OMP
37#define CBM_OMP(...) CBM_PRAGMA(omp __VA_ARGS__)
38#else
39#define CBM_OMP(...)
40#endif
41
43{
44
45#ifndef HAVE_OMP
46 inline int GetMaxThreads() { return 1; }
47 inline int GetThreadNum() { return 0; }
48 inline int GetNumThreads() { return 1; }
49 inline void SetNumThreads(int) {}
50#else
51 inline int GetMaxThreads() { return omp_get_max_threads(); }
52 inline int GetThreadNum() { return omp_get_thread_num(); }
53 inline int GetNumThreads() { return omp_get_num_threads(); }
54 inline void SetNumThreads(int n) { omp_set_num_threads(n); }
55#endif
56
57} // namespace cbm::algo::openmp
58
59#endif // CBM_ALGO_BASE_COMPAT_OPENMP_H
int GetNumThreads()
Definition OpenMP.h:48
int GetMaxThreads()
Definition OpenMP.h:46
void SetNumThreads(int)
Definition OpenMP.h:49
int GetThreadNum()
Definition OpenMP.h:47