File size: 1,049 Bytes
28958dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once

#ifdef __CUDACC__
    #include <cuda.h>
    #include <cuda_runtime.h>
#endif
#include <cstdio>
#include <cassert>
#include <limits>

#ifdef __CUDACC__
#define checkCuda(x) do { if((x)!=cudaSuccess) { \
    printf("CUDA Runtime Error: %s at %s:%d\n",\
    cudaGetErrorString(x),__FILE__,__LINE__);\
    exit(1);}} while(0)
#endif

template <typename T>
DEVICE
inline T infinity() {
#ifdef __CUDA_ARCH__
    const unsigned long long ieee754inf = 0x7ff0000000000000;
    return __longlong_as_double(ieee754inf);
#else
    return std::numeric_limits<T>::infinity();
#endif
}

template <>
DEVICE
inline double infinity() {
#ifdef __CUDA_ARCH__
    return __longlong_as_double(0x7ff0000000000000ULL);
#else
    return std::numeric_limits<double>::infinity();
#endif
}

template <>
DEVICE
inline float infinity() {
#ifdef __CUDA_ARCH__
    return __int_as_float(0x7f800000);
#else
    return std::numeric_limits<float>::infinity();
#endif
}

inline void cuda_synchronize() {
#ifdef __CUDACC__
    checkCuda(cudaDeviceSynchronize());
#endif
}