alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
scope.hpp
Go to the documentation of this file.
1/* Copyright 2025 Mehmet Yusufoglu, René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include <string>
8
9/**
10 * @brief Provides scopes for atomic and memory fence operations, analogous to NVIDIA CUDA's atomic and fence scopes.
11 *
12 * This namespace defines the visibility scopes for atomic operations and memory fences,
13 * which control the visibility of memory operations across threads, blocks, and devices.
14 * The provided scopes are:
15 * - Block: Visibility within a thread block.
16 * - Device: Visibility across all thread blocks on the same device.
17 * - System: System-wide visibility, mapped to the strongest available atomic/fence by the backend.
18 *
19 * @see alpaka::onAcc::atomicAdd, alpaka::onAcc::memFence
20 */
21namespace alpaka::onAcc
22{
23 namespace scope
24 {
25 /**
26 * @brief Base tag for scope types.
27 *
28 * This tag can be used to constrain APIs that accept only valid scopes.
29 */
30 struct ScopeTag
31 {
32 };
33
34 /**
35 * @brief Scope for atomic and fence operations visible only within the same thread block.
36 *
37 * When used with atomic operations (e.g., atomicAdd), only threads within the same block
38 * will see the updated value. When used with threadFence, it ensures that all writes
39 * from the current thread are visible to all other threads in the same block.
40 *
41 * @note Analogous to CUDA's `atomicAdd_block` and `threadFence_block`.
42 */
43 struct Block : ScopeTag
44 {
45 static std::string getName()
46 {
47 return "Block";
48 }
49 };
50
51 inline constexpr Block block{};
52
53 /**
54 * @brief Scope for atomic and fence operations visible across all thread blocks on the same device.
55 *
56 * When used with atomic operations, all threads on the same device will see the updated value.
57 * When used with threadFence, it ensures that all writes from the current thread are visible
58 * to all other threads on the same device.
59 *
60 * @note This scope is stronger than Block but weaker than System.
61 */
63 {
64 static std::string getName()
65 {
66 return "Device";
67 }
68 };
69
70 inline constexpr Device device{};
71
72 /**
73 * @brief Scope for atomic and fence operations with system-wide visibility.
74 *
75 * When used with atomic operations, all threads in the system (potentially across multiple devices)
76 * will see the updated value. When used with threadFence, it ensures that all writes from the current
77 * thread are visible to all other threads in the system.
78 *
79 * @attention System operations are only visible to other threads of the same device kind.
80 * Operations executed on a host compute device will not be visible to threads in, for example, CUDA/HIP or
81 * oneAPI kernels, and vice versa.
82 *
83 * @note This is the strongest scope, analogous to CUDA's `atomicAdd_system` and the strongest fence.
84 */
86 {
87 static std::string getName()
88 {
89 return "System";
90 }
91 };
92
93 inline constexpr System system{};
94 } // namespace scope
95
96 namespace concepts
97 {
98 template<typename T>
99 concept Scope = std::derived_from<T, scope::ScopeTag>;
100 } // namespace concepts
101
102} // namespace alpaka::onAcc
constexpr Block block
Definition scope.hpp:51
constexpr Device device
Definition scope.hpp:70
constexpr System system
Definition scope.hpp:93
functionality which is usable on the accelerator compute device from within a kernel.
Definition executor.hpp:38
Scope for atomic and fence operations visible only within the same thread block.
Definition scope.hpp:44
static std::string getName()
Definition scope.hpp:45
Scope for atomic and fence operations visible across all thread blocks on the same device.
Definition scope.hpp:63
static std::string getName()
Definition scope.hpp:64
Base tag for scope types.
Definition scope.hpp:31
Scope for atomic and fence operations with system-wide visibility.
Definition scope.hpp:86
static std::string getName()
Definition scope.hpp:87