OpenRAND  0.9
OpenRAND: A C++ Library for Reproducible Random Number Generation in Parallel Computing Environments
util.h
1 // @HEADER
2 // *******************************************************************************
3 // OpenRAND *
4 // A Performance Portable, Reproducible Random Number Generation Library *
5 // *
6 // Copyright (c) 2023, Michigan State University *
7 // *
8 // Permission is hereby granted, free of charge, to any person obtaining a copy *
9 // of this software and associated documentation files (the "Software"), to deal *
10 // in the Software without restriction, including without limitation the rights *
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
12 // copies of the Software, and to permit persons to whom the Software is *
13 // furnished to do so, subject to the following conditions: *
14 // *
15 // The above copyright notice and this permission notice shall be included in *
16 // all copies or substantial portions of the Software. *
17 // *
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
24 // SOFTWARE. *
25 //********************************************************************************
26 // @HEADER
27 
28 #ifndef OPENRAND_UTIL_H_
29 #define OPENRAND_UTIL_H_
30 
31 #include <cmath>
32 #include <cstdint>
33 #include <type_traits>
34 
35 // Detects Nvidia and AMD devices for now
36 #ifdef __CUDA_ARCH__
37 #define OPENRAND_DEVICE __host__ __device__
38 #elif defined(__HIP_DEVICE_COMPILE__)
39 #define OPENRAND_DEVICE __device__ __host__
40 #else
41 #define OPENRAND_DEVICE
42 #endif
43 
44 namespace openrand {
45 
46 // NOTE: nvcc compiler replaces floating point variants with cuda built-in
47 // versions
48 
49 constexpr uint32_t DEFAULT_GLOBAL_SEED =
50  0xAAAAAAAA; // equal number of 0 and 1 bits
51 
52 template <typename T>
53 inline OPENRAND_DEVICE T sin(T x) {
54  if constexpr (std::is_same_v<T, float>)
55  return sinf(x);
56  else if constexpr (std::is_same_v<T, double>)
57  return std::sin(x);
58 }
59 
60 template <typename T>
61 inline OPENRAND_DEVICE T cos(T x) {
62  if constexpr (std::is_same_v<T, float>)
63  return cosf(x);
64  else if constexpr (std::is_same_v<T, double>)
65  return std::cos(x);
66 }
67 
68 template <typename T>
69 inline OPENRAND_DEVICE T log(T x) {
70  if constexpr (std::is_same_v<T, float>)
71  return logf(x);
72  else if constexpr (std::is_same_v<T, double>)
73  return std::log(x);
74 }
75 
76 template <typename T>
77 inline OPENRAND_DEVICE T sqrt(T x) {
78  if constexpr (std::is_same_v<T, float>)
79  return sqrtf(x);
80  else if constexpr (std::is_same_v<T, double>)
81  return std::sqrt(x);
82 }
83 
84 template <typename T>
85 struct vec2 {
86  T x, y;
87 };
88 
89 template <typename T>
90 struct vec3 {
91  T x, y, z;
92 };
93 
94 template <typename T>
95 struct vec4 {
96  T x, y, z, w;
97 };
98 
99 // for GPU, better to be explicit about the type and size
100 using uint2 = vec2<uint32_t>;
101 using uint3 = vec3<uint32_t>;
102 using uint4 = vec4<uint32_t>;
103 
104 using float2 = vec2<float>;
105 using float3 = vec3<float>;
106 using float4 = vec4<float>;
107 
108 using double2 = vec2<double>;
109 using double3 = vec3<double>;
110 using double4 = vec4<double>;
111 
112 // CRTP: helper struct to check if Derived has internal counter
113 // that enables O(1) state forwarding
114 template <typename T, typename = std::void_t<>>
115 struct has_counter : std::false_type {};
116 
117 template <typename T>
118 struct has_counter<T, std::void_t<decltype(std::declval<T>()._ctr)>>
119  : std::true_type {};
120 
121 } // namespace openrand
122 
123 #endif
Definition: util.h:115
Definition: util.h:85
Definition: util.h:90
Definition: util.h:95