ryujin 2.1.1 revision d0a94ad2ccc0c4c2e8c2485c52b06b90e2fc9853
openmp.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2020 - 2023 by the ryujin authors
4//
5
6#pragma once
7
8#include <compile_time_options.h>
9
10#include <deal.II/base/config.h>
11
12#ifdef WITH_OPENMP
13#include <omp.h>
14#endif
15
16#include <atomic>
17#include <future>
18
40
47#define RYUJIN_PRAGMA(x) _Pragma(#x)
48
54#define RYUJIN_PARALLEL_REGION_BEGIN \
55 RYUJIN_PRAGMA(omp parallel default(shared)) \
56 {
57
63#define RYUJIN_PARALLEL_REGION_END }
64
70#define RYUJIN_OMP_FOR RYUJIN_PRAGMA(omp for)
71
79#define RYUJIN_OMP_FOR_NOWAIT RYUJIN_PRAGMA(omp for nowait)
80
86#define RYUJIN_OMP_BARRIER RYUJIN_PRAGMA(omp barrier)
87
93#define RYUJIN_OMP_CRITICAL RYUJIN_PRAGMA(omp critical)
94
100#define RYUJIN_OMP_SINGLE RYUJIN_PRAGMA(omp single)
101
116#define RYUJIN_LIKELY(x) (__builtin_expect(!!(x), 1))
117
132#define RYUJIN_UNLIKELY(x) (__builtin_expect(!!(x), 0))
133
134namespace ryujin
135{
142 {
143 public:
144 SynchronizationDispatch(const std::function<void()> &async_payload)
145 : async_payload_(async_payload)
146 , n_threads_ready_(0)
147 {
148 }
149
151 {
152 /* Executes in serial, non thread-parallel context: */
153
154 if (payload_status_.valid()) {
155 payload_status_.wait();
156 } else {
157 async_payload_();
158 }
159 }
160
161#ifdef ASYNC_MPI_EXCHANGE
162 DEAL_II_ALWAYS_INLINE inline void check(bool &thread_ready,
163 const bool condition)
164 {
165 /* Executes in concurrent, thread-parallel context: */
166 if (RYUJIN_UNLIKELY(thread_ready == false && condition)) {
167 thread_ready = true;
168#ifdef WITH_OPENMP
169 if (++n_threads_ready_ == omp_get_num_threads())
170#endif
171 payload_status_ = std::async(std::launch::async, async_payload_);
172 }
173 }
174#else
175 DEAL_II_ALWAYS_INLINE inline void check(bool &, const bool) {}
176#endif
177
178 private:
179 const std::function<void()> async_payload_;
180 std::future<void> payload_status_;
181 std::atomic_int n_threads_ready_;
182 };
183} // namespace ryujin
184
SynchronizationDispatch(const std::function< void()> &async_payload)
Definition: openmp.h:144
DEAL_II_ALWAYS_INLINE void check(bool &, const bool)
Definition: openmp.h:175
#define RYUJIN_UNLIKELY(x)
Definition: openmp.h:132