alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
MemFenceCpu.hpp
Go to the documentation of this file.
1/* Copyright 2022 Jan Stephan, Bernhard Manfred Gruber, Tapish Narwal
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
10
11#include <atomic>
12
13namespace alpaka
14{
15 //! The default CPU memory fence.
16 class MemFenceCpu : public interface::Implements<ConceptMemFence, MemFenceCpu>
17 {
18 };
19
20 namespace trait
21 {
22 template<>
23 struct MemFenceDefaultOrder<MemFenceCpu>
24 {
25 using type = mem_order::AcqRel;
26 static constexpr auto value = mem_order::acq_rel;
27 };
28
29 template<MemoryOrder TMemOrder, MemoryScope TMemScope>
30 struct MemFence<MemFenceCpu, TMemOrder, TMemScope>
31
32 {
33 static auto mem_fence(MemFenceCpu const&, TMemOrder order, TMemScope const&)
34 {
35 /*
36 * Intuitively, std::atomic_thread_fence creates a fence on the block level.
37 *
38 * Creating a block fence is enough for the whole device because the blocks are executed serially. By
39 * definition of fences, preceding blocks don't have a guarantee to see the results of this block's
40 * STORE operations (only that they will be ordered correctly); the following blocks see the results
41 * once they start. Consider the following code:
42 *
43 * int x = 1;
44 * int y = 2;
45 *
46 * void foo()
47 * {
48 * x = 10;
49 * alpaka::mem_fence(acc, memory_scope::device);
50 * y = 20;
51 * }
52 *
53 * void bar()
54 * {
55 * auto b = y;
56 * alpaka::mem_fence(acc, memory_scope::device);
57 * auto a = x;
58 * }
59 *
60 * The following are all valid outcomes:
61 * a == 1 && b == 2
62 * a == 10 && b == 2
63 * a == 10 && b == 20
64 */
65
66 std::atomic_thread_fence(MemOrderStl::get(order));
67 }
68 };
69 } // namespace trait
70} // namespace alpaka
The default CPU memory fence.
static constexpr AcqRel acq_rel
The alpaka accelerator library.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_ACC auto mem_fence(TMemFence const &fence, TMemOrder order, TMemScope const &scope) -> void
Issues memory fence instructions.
Definition Traits.hpp:80
static constexpr auto get(TMemOrder)
Tag used in class inheritance hierarchies that describes that a specific interface (TInterface) is im...
Definition Interface.hpp:15