AutoPas  3.0.0
Loading...
Searching...
No Matches
WrapOpenMP.h
Go to the documentation of this file.
1
16#pragma once
17
18#if defined(AUTOPAS_USE_OPENMP)
19#include <omp.h>
20
21#include <cstddef> // for size_t
22#include <vector>
23#else
24#include "ExceptionHandler.h"
25#endif
26
27namespace autopas {
28
29#if defined(AUTOPAS_USE_OPENMP)
30
35#define AUTOPAS_DO_PRAGMA(x) _Pragma(#x)
36
43#define AUTOPAS_OPENMP(args) AUTOPAS_DO_PRAGMA(omp args)
44
49inline int autopas_get_thread_num() { return omp_get_thread_num(); }
50
55inline int autopas_get_num_threads() { return omp_get_num_threads(); }
56
61inline int autopas_get_max_threads() { return omp_get_max_threads(); }
62
67inline void autopas_set_num_threads(int n) { omp_set_num_threads(n); }
68
72class AutoPasLock {
73 public:
77 AutoPasLock() { omp_init_lock(&_lock); }
78
82 AutoPasLock(AutoPasLock &&) noexcept { omp_init_lock(&_lock); }
83
87 AutoPasLock(const AutoPasLock &) { omp_init_lock(&_lock); }
88
94
98 ~AutoPasLock() { omp_destroy_lock(&_lock); }
99
103 void lock() { omp_set_lock(&_lock); }
104
108 void unlock() { omp_unset_lock(&_lock); }
109
110 private:
111 omp_lock_t _lock;
112};
113
117// reduction for merging vectors: {1,2} + {2,3} -> {1,2,2,3}
118#pragma omp declare reduction(vecMerge : std::vector<size_t> : omp_out.insert(omp_out.end(), omp_in.begin(), omp_in.end()))
119#pragma omp declare reduction(vecMerge : std::vector<double> : omp_out.insert(omp_out.end(), omp_in.begin(), omp_in.end()))
120
121#else
122
126#define AUTOPAS_OPENMP(args)
127
132inline int autopas_get_thread_num() { return 0; }
133
138inline int autopas_get_num_threads() { return 1; }
139
144inline int autopas_get_max_threads() { return 1; }
145
150inline void autopas_set_num_threads(int /* n */) {}
151
156 public:
160 AutoPasLock() { _locked = false; }
161
165 AutoPasLock(AutoPasLock &&) noexcept { _locked = false; }
166
170 AutoPasLock(AutoPasLock &) { _locked = false; }
171
177
182 if (_locked) {
183 utils::ExceptionHandler::exception("AutoPasLocked destroyed in locked state.");
184 }
185 }
186
190 void lock() {
191 if (_locked) {
192 utils::ExceptionHandler::exception("Tried to acquire a locked lock.");
193 }
194 _locked = true;
195 }
196
200 void unlock() {
201 if (not _locked) {
202 utils::ExceptionHandler::exception("Tried to release an unlocked lock.");
203 }
204 _locked = false;
205 }
206
207 private:
208 // true if locked, false if unlocked
209 bool _locked;
210};
211
212#endif
213
214// These properties are needed because we use AutoPasLock in vectors on which we call resize().
215static_assert(std::is_default_constructible_v<AutoPasLock>, "AutoPasLock needs to be default constructible!");
216static_assert(std::is_move_constructible_v<AutoPasLock>, "AutoPasLock needs to be move constructible!");
217
218} // namespace autopas
AutoPasLock for the sequential case.
Definition: WrapOpenMP.h:155
AutoPasLock(AutoPasLock &)
Copy constructor.
Definition: WrapOpenMP.h:170
AutoPasLock()
Default constructor.
Definition: WrapOpenMP.h:160
AutoPasLock(AutoPasLock &&) noexcept
Move Constructor.
Definition: WrapOpenMP.h:165
AutoPasLock & operator=(AutoPasLock)=delete
Assignment operator.
~AutoPasLock()
Destructor.
Definition: WrapOpenMP.h:181
void unlock()
Release the lock.
Definition: WrapOpenMP.h:200
void lock()
Acquire the lock.
Definition: WrapOpenMP.h:190
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
int autopas_get_num_threads()
Dummy for omp_get_num_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:138
void autopas_set_num_threads(int)
Wrapper for omp_set_num_threads().
Definition: WrapOpenMP.h:150
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132