alpaka
Abstraction Library for Parallel Kernel Acceleration
MemFenceOmp2Threads.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Jan Stephan, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
9 
10 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_OMP2_ENABLED
11 
12 # if _OPENMP < 200203
13 # error If ALPAKA_ACC_CPU_B_SEQ_T_OMP2_ENABLED is set, the compiler has to support OpenMP 2.0 or higher!
14 # endif
15 
16 namespace alpaka
17 {
18  //! The CPU OpenMP 2.0 block memory fence.
19  class MemFenceOmp2Threads : public concepts::Implements<ConceptMemFence, MemFenceOmp2Threads>
20  {
21  };
22 
23  namespace trait
24  {
25  template<typename TMemScope>
26  struct MemFence<MemFenceOmp2Threads, TMemScope>
27  {
28  static auto mem_fence(MemFenceOmp2Threads const&, TMemScope const&)
29  {
30  /*
31  * Intuitively, this pragma creates a fence on the block level.
32  *
33  * Creating a block fence is enough for the whole device because the blocks are executed serially. By
34  * definition of fences, preceding blocks don't have a guarantee to see the results of this block's
35  * STORE operations (only that they will be ordered correctly); the following blocks see the results
36  * once they start. Consider the following code:
37  *
38  * int x = 1;
39  * int y = 2;
40  *
41  * void foo()
42  * {
43  * x = 10;
44  * alpaka::mem_fence(acc, memory_scope::device);
45  * y = 20;
46  * }
47  *
48  * void bar()
49  * {
50  * auto b = y;
51  * alpaka::mem_fence(acc, memory_scope::device);
52  * auto a = x;
53  * }
54  *
55  * The following are all valid outcomes:
56  * a == 1 && b == 2
57  * a == 10 && b == 2
58  * a == 10 && b == 20
59  */
60 # pragma omp flush
61 # ifdef _MSC_VER
62  ; // MSVC needs an empty statement here or it diagnoses a syntax error
63 # endif
64  }
65  };
66  } // namespace trait
67 } // namespace alpaka
68 #endif
The CPU OpenMP 2.0 block memory fence.
The alpaka accelerator library.
Tag used in class inheritance hierarchies that describes that a specific concept (TConcept) is implem...
Definition: Concepts.hpp:15
static auto mem_fence(MemFenceOmp2Threads const &, TMemScope const &)
The mem_fence trait.
Definition: Traits.hpp:39