TTK
Loading...
Searching...
No Matches
OpenMP.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef TTK_ENABLE_OPENMP
4#include <omp.h>
5#endif // TTK_ENABLE_OPENMP
6
7#if defined(__GNUC__) && !defined(__clang__)
8#include <parallel/algorithm>
9#endif
10
11#ifdef TTK_ENABLE_OPENMP
12namespace ttk {
16 class ParallelGuard {
17 int oldThreadNumber_;
18
19 public:
20 ParallelGuard(const int nThreads)
21 : oldThreadNumber_{omp_get_max_threads()} {
22 omp_set_num_threads(nThreads);
23 }
24 ~ParallelGuard() {
25 omp_set_num_threads(oldThreadNumber_);
26 }
27 };
28} // namespace ttk
29#endif // TTK_ENABLE_OPENMP
30
39#if defined(_GLIBCXX_PARALLEL_FEATURES_H) && defined(TTK_ENABLE_OPENMP)
40#define TTK_PSORT(NTHREADS, ...) \
41 { \
42 ttk::ParallelGuard pg{NTHREADS}; \
43 __gnu_parallel::sort(__VA_ARGS__); \
44 }
45#else
46#define TTK_PSORT(NTHREADS, ...) std::sort(__VA_ARGS__);
47#endif // _GLIBCXX_PARALLEL_FEATURES_H && TTK_ENABLE_OPENMP
48
49namespace ttk {
53 class Lock {
54#ifdef TTK_ENABLE_OPENMP
55 public:
56 Lock() {
57 omp_init_lock(&this->lock_);
58 }
59 ~Lock() {
60 omp_destroy_lock(&this->lock_);
61 }
62 inline void lock() {
63 omp_set_lock(&this->lock_);
64 }
65 inline void unlock() {
66 omp_unset_lock(&this->lock_);
67 }
68 Lock(const Lock &) = delete;
69 Lock(Lock &&) = delete;
70 Lock &operator=(const Lock &) = delete;
71 Lock &operator=(Lock &&) = delete;
72
73 private:
74 omp_lock_t lock_{};
75#else
76 public:
77 inline void lock() {
78 }
79 inline void unlock() {
80 }
81#endif // TTK_ENABLE_OPENMP
82 };
83} // namespace ttk
RAII wrapper around OpenMP lock.
Definition OpenMP.h:53
void unlock()
Definition OpenMP.h:79
void lock()
Definition OpenMP.h:77
The Topology ToolKit.