PeriDyno 1.0.0
Loading...
Searching...
No Matches
Typedef.inl
Go to the documentation of this file.
1#include <list>
2#include <vector>
3#include <map>
4#include <memory>
5#include <string>
6#include <algorithm>
7
8#include <assert.h>
9#include <stdio.h>
10#ifdef CUDA_BACKEND
11#include <cuda_runtime.h>
12#include <device_launch_parameters.h>
13#include <vector_types.h>
14#include <vector_functions.h>
15#endif // CUDA_BACKEDN
16#include <iostream>
17#include <stdexcept>
18#include <limits>
19
20#ifdef PRECISION_FLOAT
21typedef float Real;
22#else
23typedef double Real;
24#endif
25
26namespace dyno {
27
28 using uint = unsigned int;
29
30 using uchar = unsigned char;
31 using uint64 = unsigned long long;
32 using int64 = signed long long;
33
34#define INVALID -1
35#ifndef M_PI
36#define M_PI 3.14159265358979323846
37#endif
38#ifndef M_E
39#define M_E 2.71828182845904523536
40#endif
41
42 constexpr Real EPSILON = std::numeric_limits<Real>::epsilon();
43 constexpr Real REAL_EPSILON = (std::numeric_limits<Real>::epsilon)();
45 constexpr Real REAL_MAX = (std::numeric_limits<Real>::max)();
46 constexpr Real REAL_MIN = (std::numeric_limits<Real>::min)();
47 constexpr Real REAL_INF = (std::numeric_limits<Real>::infinity)();
48 constexpr uint BLOCK_SIZE = 64;
49
50
51#ifdef CUDA_BACKEND
52 static uint iDivUp(uint a, uint b)
53 {
54 return (a % b != 0) ? (a / b + 1) : (a / b);
55 }
56
57 // compute grid and thread block size for a given number of elements
58 static uint cudaGridSize(uint totalSize, uint blockSize)
59 {
60 int dim = iDivUp(totalSize, blockSize);
61 return dim == 0 ? 1 : dim;
62 }
63
64 static uint3 cudaGridSize2D(uint2 totalSize, uint blockSize)
65 {
66 uint3 gridDims;
67 gridDims.x = iDivUp(totalSize.x, blockSize);
68 gridDims.y = iDivUp(totalSize.y, blockSize);
69
70 gridDims.x = gridDims.x == 0 ? 1 : gridDims.x;
71 gridDims.y = gridDims.y == 0 ? 1 : gridDims.y;
72 gridDims.z = 1;
73
74 return gridDims;
75 }
76
77 static uint3 cudaGridSize3D(uint3 totalSize, uint blockSize)
78 {
79 uint3 gridDims;
80 gridDims.x = iDivUp(totalSize.x, blockSize);
81 gridDims.y = iDivUp(totalSize.y, blockSize);
82 gridDims.z = iDivUp(totalSize.z, blockSize);
83
84 gridDims.x = gridDims.x == 0 ? 1 : gridDims.x;
85 gridDims.y = gridDims.y == 0 ? 1 : gridDims.y;
86 gridDims.z = gridDims.z == 0 ? 1 : gridDims.z;
87
88 return gridDims;
89 }
90
91 static uint3 cudaGridSize3D(uint3 totalSize, uint3 blockSize)
92 {
93 uint3 gridDims;
94 gridDims.x = iDivUp(totalSize.x, blockSize.x);
95 gridDims.y = iDivUp(totalSize.y, blockSize.y);
96 gridDims.z = iDivUp(totalSize.z, blockSize.z);
97
98 gridDims.x = gridDims.x == 0 ? 1 : gridDims.x;
99 gridDims.y = gridDims.y == 0 ? 1 : gridDims.y;
100 gridDims.z = gridDims.z == 0 ? 1 : gridDims.z;
101
102 return gridDims;
103 }
104
108 static inline void checkCudaError(const char *msg) {
109 cudaError_t err = cudaGetLastError();
110 if (cudaSuccess != err) {
111 printf( "CUDA error: %d : %s at %s:%d \n", err, cudaGetErrorString(err), __FILE__, __LINE__);
112 throw std::runtime_error(std::string(msg) + ": " + cudaGetErrorString(err));
113 }
114 }
115
116 // use this macro to make sure no error occurs when cuda functions are called
117#ifdef NDEBUG
118#define cuSafeCall(X) X
119#else
120#define cuSafeCall(X) X; dyno::checkCudaError(#X);
121#endif
122
127#ifdef NDEBUG
128#define cuSynchronize() {}
129#else
130#define cuSynchronize() { \
131 char str[200]; \
132 cudaDeviceSynchronize(); \
133 cudaError_t err = cudaGetLastError(); \
134 if (err != cudaSuccess) \
135 { \
136 sprintf(str, "CUDA error: %d : %s at %s:%d \n", err, cudaGetErrorString(err), __FILE__, __LINE__); \
137 throw std::runtime_error(std::string(str)); \
138 } \
139 }
140#endif
141
148#define cuExecute(size, Func, ...){ \
149 uint pDims = cudaGridSize((uint)size, BLOCK_SIZE); \
150 Func << <pDims, BLOCK_SIZE >> > ( \
151 __VA_ARGS__); \
152 cuSynchronize(); \
153 }
154
155#define cuExecute2D(size, Func, ...){ \
156 uint3 pDims = cudaGridSize2D(size, 8); \
157 dim3 threadsPerBlock(8, 8, 1); \
158 Func << <pDims, threadsPerBlock >> > ( \
159 __VA_ARGS__); \
160 cuSynchronize(); \
161 }
162
163#define cuExecute3D(size, Func, ...){ \
164 dim3 pDims = cudaGridSize3D(size, 8); \
165 dim3 threadsPerBlock(8, 8, 8); \
166 Func << <pDims, threadsPerBlock >> > ( \
167 __VA_ARGS__); \
168 cuSynchronize(); \
169 }
170
171#define cuExecuteNoSync(size, Func, ...){ \
172 uint pDims = cudaGridSize((uint)size, BLOCK_SIZE); \
173 Func << <pDims, BLOCK_SIZE >> > ( \
174 __VA_ARGS__); \
175}
176
177#endif
178
179 class Bool
180 {
181 public:
182 DYN_FUNC Bool(bool v = false) { val = v ? 1 : 0; }
183
184 DYN_FUNC inline bool operator! () const {
185 return 1 - val ? true : false;
186 }
187
188 DYN_FUNC inline bool operator== (bool v) const {
189 uint tmpV = v ? 1 : 0;
190 return val == tmpV;
191 }
192
193 DYN_FUNC inline bool operator== (const Bool& v) const {
194 return val == v.val;
195 }
196
197 DYN_FUNC inline Bool& operator= (const bool v) {
198 val = v ? 1 : 0;
199 return *this;
200 }
201
202 DYN_FUNC inline Bool& operator= (const Bool& v) {
203 val = v.val;
204 return *this;
205 }
206
207 DYN_FUNC inline Bool& operator&= (const bool v) {
208 val &= (v ? 1 : 0);
209 return *this;
210 }
211
212 DYN_FUNC inline Bool& operator|= (const bool v) {
213 val |= (v ? 1 : 0);
214 return *this;
215 }
216
217 DYN_FUNC inline Bool operator& (const bool v) const {
218 Bool ret;
219 ret.val = val & (v ? 1 : 0);
220 return ret;
221 }
222
223 DYN_FUNC inline Bool operator| (const bool v) const {
224 Bool ret;
225 ret.val = val | (v ? 1 : 0);
226 return ret;
227 }
228
229 DYN_FUNC inline Bool& operator&= (const Bool& v) {
230 val &= v.val;
231 return *this;
232 }
233
234 DYN_FUNC inline Bool& operator|= (const Bool& v) {
235 val |= v.val;
236 return *this;
237 }
238
239 DYN_FUNC inline Bool operator& (const Bool& v) const {
240 Bool ret;
241 ret.val = val & v.val;
242 return ret;
243 }
244
245 DYN_FUNC inline Bool operator| (const Bool& v) const {
246 Bool ret;
247 ret.val = val | v.val;
248 return ret;
249 }
250
251 DYN_FUNC inline bool operator&& (const Bool& v) const {
252 return val & v.val;
253 }
254
255 DYN_FUNC inline bool operator|| (const Bool& v) const {
256 return val | v.val;
257 }
258
259 DYN_FUNC inline bool operator&& (const bool& v) const {
260 uint tmpV = v ? 1 : 0;
261 return val & tmpV;
262 }
263
264 DYN_FUNC inline bool operator|| (const bool& v) const {
265 uint tmpV = v ? 1 : 0;
266 return val | tmpV;
267 }
268
269 DYN_FUNC inline operator bool() const {
270 return val == 1;
271 }
272
273
274 private:
276 };
277}// end of namespace dyno
278
279
280namespace TypeInfo
281{
282 template<class T, class ... Args>
283 std::shared_ptr<T> New(Args&& ... args) { std::shared_ptr<T> p(new T(std::forward<Args>(args)...)); return p; }
284
285 template<class TA, class TB>
286 inline TA* cast(TB* b)
287 {
288 TA* ptr = dynamic_cast<TA*>(b);
289 return ptr;
290 }
291
292 template<class TA, class TB>
293 inline std::shared_ptr<TA> cast(std::shared_ptr<TB> b)
294 {
295 std::shared_ptr<TA> ptr = std::dynamic_pointer_cast<TA>(b);
296 return ptr;
297 }
298}
299
300
double Real
Definition Typedef.inl:23
unsigned int uint
Definition VkReduce.h:5
DYN_FUNC Bool & operator=(const bool v)
Definition Typedef.inl:197
DYN_FUNC bool operator||(const Bool &v) const
Definition Typedef.inl:255
DYN_FUNC bool operator&&(const Bool &v) const
Definition Typedef.inl:251
DYN_FUNC bool operator==(bool v) const
Definition Typedef.inl:188
DYN_FUNC Bool & operator&=(const bool v)
Definition Typedef.inl:207
DYN_FUNC bool operator!() const
Definition Typedef.inl:184
DYN_FUNC Bool & operator|=(const bool v)
Definition Typedef.inl:212
DYN_FUNC Bool(bool v=false)
Definition Typedef.inl:182
DYN_FUNC Bool operator|(const bool v) const
Definition Typedef.inl:223
DYN_FUNC Bool operator&(const bool v) const
Definition Typedef.inl:217
#define T(t)
TA * cast(TB *b)
Definition Typedef.inl:286
std::shared_ptr< T > New(Args &&... args)
Definition Typedef.inl:283
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
constexpr Real REAL_EPSILON_SQUARED
Definition Typedef.inl:44
constexpr Real REAL_MAX
Definition Typedef.inl:45
signed long long int64
Definition Typedef.inl:32
constexpr uint BLOCK_SIZE
Definition Typedef.inl:48
constexpr Real REAL_MIN
Definition Typedef.inl:46
constexpr Real EPSILON
Definition Typedef.inl:42
constexpr Real REAL_EPSILON
Definition Typedef.inl:43
unsigned char uchar
Definition Typedef.inl:30
unsigned int uint
Definition VkProgram.h:14
unsigned long long uint64
Definition Typedef.inl:31
constexpr Real REAL_INF
Definition Typedef.inl:47
static uint iDivUp(uint a, uint b)
Definition VkProgram.h:28