Hermes
defs.h
Go to the documentation of this file.
1 
32 #ifndef HERMES_COMMON_DEFS_H
33 #define HERMES_COMMON_DEFS_H
34 
35 // *********************************************************************************************************************
36 // CUDA SUPPORT
37 // *********************************************************************************************************************
38 #if defined(ENABLE_CUDA)
39 
40 #include <cuda_runtime.h>
41 
43 #define HERMES_HOST_FUNCTION __host__
45 #define HERMES_DEVICE_CALLABLE __device__ __host__
47 #define HERMES_DEVICE_FUNCTION __device__
49 #define HERMES_DEVICE_ENABLED __CUDA_ARCH__
53 #define HERMES_CUDA_KERNEL(NAME) __global__ void NAME ## _k
55 #define HERMES_CUDA_CODE(CODE) {CODE}
56 
57 #else
58 
59 #define HERMES_HOST_FUNCTION
60 #define HERMES_DEVICE_CALLABLE
61 #define HERMES_DEVICE_FUNCTION
62 #define HERMES_CUDA_CODE(CODE)
63 
64 #endif
65 
66 #include <cstdint>
67 #include <type_traits>
68 #include <string>
69 // *********************************************************************************************************************
70 // DATA TYPES
71 // *********************************************************************************************************************
72 #ifdef HERMES_USE_DOUBLE_AS_DEFAULT
73 using real_t = double;
74 #else
75 using real_t = float;
76 #endif
77 
78 using f32 = float;
79 using f64 = double;
80 
81 using i8 = int8_t;
82 using i16 = int16_t;
83 using i32 = int32_t;
84 using i64 = int64_t;
85 
86 using u8 = uint8_t;
87 using u16 = uint16_t;
88 using u32 = uint32_t;
89 using u64 = uint64_t;
90 
91 using ulong = unsigned long;
92 using uint = unsigned int;
93 using ushort = unsigned short;
94 using uchar = unsigned char;
95 
96 using byte = uint8_t;
97 
98 namespace hermes {
99 
101 enum class DataType : u8 {
102  I8 = 0,
103  I16 = 1,
104  I32 = 2,
105  I64 = 3,
106  U8 = 4,
107  U16 = 5,
108  U32 = 6,
109  U64 = 7,
110  F16 = 8,
111  F32 = 9,
112  F64 = 10,
113  CUSTOM = 11
114 };
115 
117 class DataTypes {
118 public:
123 #define MATCH_TYPE(Type) \
124  if((u8)DataType::Type == index) \
125  return DataType::Type;
126  MATCH_TYPE(I8)
127  MATCH_TYPE(I16)
128  MATCH_TYPE(I32)
129  MATCH_TYPE(I64)
130  MATCH_TYPE(U8)
131  MATCH_TYPE(U16)
132  MATCH_TYPE(U32)
133  MATCH_TYPE(U64)
134  MATCH_TYPE(F32)
135  MATCH_TYPE(F64)
136  return DataType::CUSTOM;
137 #undef MATCH_TYPE
138  }
142  template<typename T>
144 #define MATCH_TYPE(Type, R) \
145  if(std::is_same_v<T, Type>) \
146  return DataType::R;
147  MATCH_TYPE(i8, I8)
148  MATCH_TYPE(i16, I16)
149  MATCH_TYPE(i32, I32)
150  MATCH_TYPE(i64, I64)
151  MATCH_TYPE(u8, U8)
152  MATCH_TYPE(u16, U16)
153  MATCH_TYPE(u32, U32)
154  MATCH_TYPE(u64, U64)
155  MATCH_TYPE(f32, F32)
156  MATCH_TYPE(f64, F64)
157  return DataType::CUSTOM;
158 #undef MATCH_TYPE
159  }
163  static u32 typeSize(DataType type) {
164 #define TYPE_SIZE(Size, Type) \
165  if(DataType::Type == type) \
166  return Size;
167  TYPE_SIZE(sizeof(i8), I8)
168  TYPE_SIZE(sizeof(i16), I16)
169  TYPE_SIZE(sizeof(i32), I32)
170  TYPE_SIZE(sizeof(i64), I64)
171  TYPE_SIZE(sizeof(u8), U8)
172  TYPE_SIZE(sizeof(u16), U16)
173  TYPE_SIZE(sizeof(u32), U32)
174  TYPE_SIZE(sizeof(u64), U64)
175  TYPE_SIZE(sizeof(f32), F32)
176  TYPE_SIZE(sizeof(f64), F64)
177  return 0;
178 #undef TYPE_SIZE
179  }
183  static std::string typeName(DataType type) {
184 #define DATA_TYPE_NAME(Type) \
185  if(DataType::Type == type) \
186  return #Type;
187  DATA_TYPE_NAME(I8)
188  DATA_TYPE_NAME(I16)
189  DATA_TYPE_NAME(I32)
190  DATA_TYPE_NAME(I64)
191  DATA_TYPE_NAME(U8)
192  DATA_TYPE_NAME(U16)
193  DATA_TYPE_NAME(U32)
194  DATA_TYPE_NAME(U64)
195  DATA_TYPE_NAME(F16)
196  DATA_TYPE_NAME(F32)
197  DATA_TYPE_NAME(F64)
198  DATA_TYPE_NAME(CUSTOM)
199  return "CUSTOM";
200 #undef DATA_TYPE_NAME
201  }
202 };
204 enum class MemoryLocation {
205  DEVICE,
206  HOST,
207  UNIFIED
208 };
212 inline std::string memoryLocationName(MemoryLocation location) {
213 #define ENUM_NAME(E) \
214  if(MemoryLocation::E == location) \
215  return #E;
216  ENUM_NAME(DEVICE)
217  ENUM_NAME(HOST)
218  ENUM_NAME(UNIFIED)
219  return "CUSTOM";
220 #undef ENUM_NAME
221 }
222 
223 // *********************************************************************************************************************
224 // IO
225 // *********************************************************************************************************************
230 inline std::ostream &operator<<(std::ostream &o, MemoryLocation location) {
232  return o;
233 }
234 
235 } // namespace hermes
236 
237 #endif
238 
DataType set of auxiliary functions.
Definition: defs.h:117
static std::string typeName(DataType type)
Gets DataType string name.
Definition: defs.h:183
static u32 typeSize(DataType type)
Computes number of bytes from DataType.
Definition: defs.h:163
static HERMES_DEVICE_CALLABLE DataType typeFrom(u8 index)
Translates DataType from identifier number.
Definition: defs.h:122
static HERMES_DEVICE_CALLABLE DataType typeFrom()
Translates template type T to DataType.
Definition: defs.h:143
std::string memoryLocationName(MemoryLocation location)
Gets MemoryLocation value string name.
Definition: defs.h:212
DataType
Enum class for integral types.
Definition: defs.h:101
@ F64
f64 type identifier
@ U64
u64 type identifier
@ F32
f32 type identifier
@ F16
f16 type identifier
@ I8
i8 type identifier
@ U8
u8 type identifier
@ CUSTOM
unidentified type
@ I16
i16 type identifier
@ U32
u32 type identifier
@ I32
i32 type identifier
@ I64
i64 type identifier
@ U16
u16 type identifier
MemoryLocation
Specifies where memory is stored.
Definition: defs.h:204
@ UNIFIED
unified memory
int8_t i8
8 bit size integer type
Definition: defs.h:81
uint64_t u64
64 bit size unsigned integer type
Definition: defs.h:89
float real_t
default floating point type
Definition: defs.h:75
unsigned long ulong
unsigned long type
Definition: defs.h:91
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45
unsigned int uint
unsigned int type
Definition: defs.h:92
uint16_t u16
16 bit size unsigned integer type
Definition: defs.h:87
uint32_t u32
32 bit size unsigned integer type
Definition: defs.h:88
int64_t i64
64 bit size integer type
Definition: defs.h:84
int16_t i16
16 bit size integer type
Definition: defs.h:82
uint8_t u8
8 bit size unsigned integer type
Definition: defs.h:86
double f64
64 bit size floating point type
Definition: defs.h:79
float f32
32 bit size floating point type
Definition: defs.h:78
unsigned char uchar
unsigned char type
Definition: defs.h:94
unsigned short ushort
unsigned short type
Definition: defs.h:93
int32_t i32
32 bit size integer type
Definition: defs.h:83
@ location
logs code location