alpaka
Abstraction Library for Parallel Kernel Acceleration
MemFenceCpu.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 #include <atomic>
11 
12 namespace alpaka
13 {
14  //! The default CPU memory fence.
15  class MemFenceCpu : public concepts::Implements<ConceptMemFence, MemFenceCpu>
16  {
17  };
18 
19  namespace trait
20  {
21  template<typename TMemScope>
22  struct MemFence<MemFenceCpu, TMemScope>
23  {
24  static auto mem_fence(MemFenceCpu const&, TMemScope const&)
25  {
26  /*
27  * Intuitively, std::atomic_thread_fence creates a fence on the block level.
28  *
29  * Creating a block fence is enough for the whole device because the blocks are executed serially. By
30  * definition of fences, preceding blocks don't have a guarantee to see the results of this block's
31  * STORE operations (only that they will be ordered correctly); the following blocks see the results
32  * once they start. Consider the following code:
33  *
34  * int x = 1;
35  * int y = 2;
36  *
37  * void foo()
38  * {
39  * x = 10;
40  * alpaka::mem_fence(acc, memory_scope::device);
41  * y = 20;
42  * }
43  *
44  * void bar()
45  * {
46  * auto b = y;
47  * alpaka::mem_fence(acc, memory_scope::device);
48  * auto a = x;
49  * }
50  *
51  * The following are all valid outcomes:
52  * a == 1 && b == 2
53  * a == 10 && b == 2
54  * a == 10 && b == 20
55  */
56 
57  std::atomic_thread_fence(std::memory_order_acq_rel);
58  }
59  };
60  } // namespace trait
61 } // namespace alpaka
The default CPU memory fence.
Definition: MemFenceCpu.hpp:16
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(MemFenceCpu const &, TMemScope const &)
Definition: MemFenceCpu.hpp:24
The mem_fence trait.
Definition: Traits.hpp:39