kokkos-execution 0.0.1
Loading...
Searching...
No Matches
counter.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_TESTS_UTILS_FUNCTORS_COUNTER_HPP
2#define KOKKOS_EXECUTION_TESTS_UTILS_FUNCTORS_COUNTER_HPP
3
4#include "Kokkos_Macros.hpp"
5#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
6import kokkos.core;
7#else
8# include "Kokkos_Core.hpp"
9#endif
10
12
14struct Counter {
15 static inline std::atomic<unsigned int> default_constructions{0};
16 static inline std::atomic<unsigned int> destructions{0};
17 static inline std::atomic<unsigned int> copy_constructions{0};
18 static inline std::atomic<unsigned int> copy_assignments{0};
19 static inline std::atomic<unsigned int> move_constructions{0};
20 static inline std::atomic<unsigned int> move_assignments{0};
21
22 static inline std::atomic<unsigned int> next_id{0};
23
24 unsigned int id{0};
25
26 Counter() noexcept
27 : id(next_id.fetch_add(1, std::memory_order_relaxed)) {
28 default_constructions.fetch_add(1, std::memory_order_relaxed);
29 }
30
31 ~Counter() noexcept {
32 destructions.fetch_add(1, std::memory_order_relaxed);
33 }
34
35 Counter(const Counter&) noexcept
36 : id(next_id.fetch_add(1, std::memory_order_relaxed)) {
37 copy_constructions.fetch_add(1, std::memory_order_relaxed);
38 }
39
40 Counter& operator=(const Counter&) noexcept {
41 copy_assignments.fetch_add(1, std::memory_order_relaxed);
42 return *this;
43 }
44
45 Counter(Counter&&) noexcept
46 : id(next_id.fetch_add(1, std::memory_order_relaxed)) {
47 move_constructions.fetch_add(1, std::memory_order_relaxed);
48 }
49
50 Counter& operator=(Counter&&) noexcept {
51 move_assignments.fetch_add(1, std::memory_order_relaxed);
52 return *this;
53 }
54
55 template <typename... Args>
56 KOKKOS_FUNCTION void operator()(Args&&...) const noexcept {
57 }
58
59 static void reset() {
61 destructions = 0;
66 }
67};
68
70struct CounterRef {
71 struct counters {
72 unsigned int constructions = 0;
73 unsigned int destructions = 0;
74 unsigned int copy_constructions = 0;
75 unsigned int copy_assignments = 0;
76 unsigned int move_constructions = 0;
77 unsigned int move_assignments = 0;
78 };
79
80 counters& m_counts; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
81
82 explicit constexpr CounterRef(counters& counts) noexcept
83 : m_counts(counts) {
85 }
86
87 constexpr ~CounterRef() noexcept {
89 }
90
91 constexpr CounterRef(const CounterRef& other) noexcept
92 : m_counts(other.m_counts) {
94 }
95
96 constexpr CounterRef& operator=(const CounterRef& other) noexcept {
97 m_counts = other.m_counts;
99 return *this;
100 }
101
102 constexpr CounterRef(CounterRef&& other) noexcept
103 : m_counts(other.m_counts) {
105 }
106
107 constexpr CounterRef& operator=(CounterRef&& other) noexcept {
108 m_counts = other.m_counts;
110 return *this;
111 }
112};
113
114} // namespace Tests::Utils::Functors
115
116#endif // KOKKOS_EXECUTION_TESTS_UTILS_FUNCTORS_COUNTER_HPP
constexpr CounterRef(counters &counts) noexcept
Definition counter.hpp:82
constexpr CounterRef(const CounterRef &other) noexcept
Definition counter.hpp:91
constexpr CounterRef & operator=(const CounterRef &other) noexcept
Definition counter.hpp:96
constexpr CounterRef(CounterRef &&other) noexcept
Definition counter.hpp:102
constexpr ~CounterRef() noexcept
Definition counter.hpp:87
constexpr CounterRef & operator=(CounterRef &&other) noexcept
Definition counter.hpp:107
static std::atomic< unsigned int > move_assignments
Definition counter.hpp:20
static std::atomic< unsigned int > copy_constructions
Definition counter.hpp:17
Counter & operator=(Counter &&) noexcept
Definition counter.hpp:50
static std::atomic< unsigned int > destructions
Definition counter.hpp:16
void operator()(Args &&...) const noexcept
Definition counter.hpp:56
Counter(Counter &&) noexcept
Definition counter.hpp:45
static std::atomic< unsigned int > next_id
Definition counter.hpp:22
static std::atomic< unsigned int > default_constructions
Definition counter.hpp:15
static std::atomic< unsigned int > move_constructions
Definition counter.hpp:19
static std::atomic< unsigned int > copy_assignments
Definition counter.hpp:18
Counter & operator=(const Counter &) noexcept
Definition counter.hpp:40
Counter(const Counter &) noexcept
Definition counter.hpp:35