Skip to content

Commit c718c2a

Browse files
authored
Merge pull request #465 from pdziekan/curand_dbg
urand: use gpuErrchk for debugging
2 parents 2f645a8 + cecd585 commit c718c2a

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

Readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,3 @@ Some CMake hints:
115115

116116
- the output of commands executed by "make test" can be viewed with:
117117
$ less Testing/Temporary/LastTest.log
118-
119-

src/detail/gpu_assert.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <mutex>
77
#include <condition_variable>
88

9+
#include <curand.h>
10+
911
// macro to check for cuda errors, taken from
1012
// http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api
1113
#define gpuErrchk(ans) { detail::gpuAssert((ans), __FILE__, __LINE__); }
@@ -16,6 +18,57 @@ namespace libcloudphxx
1618
{
1719
namespace detail
1820
{
21+
// https://stackoverflow.com/questions/40704147/built-in-function-to-get-curands-curandstatus-t-as-string
22+
#ifdef CURAND_H_
23+
// cuRAND API errors
24+
static const char *curandGetErrorString(curandStatus_t error)
25+
{
26+
switch (error)
27+
{
28+
case CURAND_STATUS_SUCCESS:
29+
return "CURAND_STATUS_SUCCESS";
30+
31+
case CURAND_STATUS_VERSION_MISMATCH:
32+
return "CURAND_STATUS_VERSION_MISMATCH";
33+
34+
case CURAND_STATUS_NOT_INITIALIZED:
35+
return "CURAND_STATUS_NOT_INITIALIZED";
36+
37+
case CURAND_STATUS_ALLOCATION_FAILED:
38+
return "CURAND_STATUS_ALLOCATION_FAILED";
39+
40+
case CURAND_STATUS_TYPE_ERROR:
41+
return "CURAND_STATUS_TYPE_ERROR";
42+
43+
case CURAND_STATUS_OUT_OF_RANGE:
44+
return "CURAND_STATUS_OUT_OF_RANGE";
45+
46+
case CURAND_STATUS_LENGTH_NOT_MULTIPLE:
47+
return "CURAND_STATUS_LENGTH_NOT_MULTIPLE";
48+
49+
case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED:
50+
return "CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";
51+
52+
case CURAND_STATUS_LAUNCH_FAILURE:
53+
return "CURAND_STATUS_LAUNCH_FAILURE";
54+
55+
case CURAND_STATUS_PREEXISTING_FAILURE:
56+
return "CURAND_STATUS_PREEXISTING_FAILURE";
57+
58+
case CURAND_STATUS_INITIALIZATION_FAILED:
59+
return "CURAND_STATUS_INITIALIZATION_FAILED";
60+
61+
case CURAND_STATUS_ARCH_MISMATCH:
62+
return "CURAND_STATUS_ARCH_MISMATCH";
63+
64+
case CURAND_STATUS_INTERNAL_ERROR:
65+
return "CURAND_STATUS_INTERNAL_ERROR";
66+
}
67+
68+
return "<unknown>";
69+
}
70+
#endif
71+
1972
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
2073
{
2174
if (code != cudaSuccess)
@@ -25,6 +78,15 @@ namespace libcloudphxx
2578
}
2679
}
2780

81+
inline void gpuAssert(curandStatus_t code, const char *file, int line, bool abort=true)
82+
{
83+
if (code != CURAND_STATUS_SUCCESS)
84+
{
85+
fprintf(stderr,"GPUassert (curand): %s %s %d\n", curandGetErrorString(code), file, line);
86+
if (abort) exit(code);
87+
}
88+
}
89+
2890
// max(1, n)
2991
inline int m1(int n) { return n == 0 ? 1 : n; }
3092

src/detail/urand.hpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -100,40 +100,26 @@ namespace libcloudphxx
100100

101101
rng(int seed)
102102
{
103-
{
104-
int status = curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_MTGP32);
105-
assert(status == CURAND_STATUS_SUCCESS /* && "curandCreateGenerator failed"*/);
106-
_unused(status);
107-
}
108-
{
109-
int status = curandSetPseudoRandomGeneratorSeed(gen, seed);
110-
assert(status == CURAND_STATUS_SUCCESS /* && "curandSetPseudoRandomGeneratorSeed failed"*/);
111-
_unused(status);
112-
}
103+
gpuErrchk(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_MTGP32));
104+
gpuErrchk(curandSetPseudoRandomGeneratorSeed(gen, seed));
113105
}
114106

115107
void reseed(int seed)
116108
{
117-
int status = curandSetPseudoRandomGeneratorSeed(gen, seed);
118-
assert(status == CURAND_STATUS_SUCCESS /* && "curandSetPseudoRandomGeneratorSeed failed"*/);
119-
_unused(status);
109+
gpuErrchk(curandSetPseudoRandomGeneratorSeed(gen, seed));
120110
}
121111

122112
~rng()
123113
{
124-
int status = curandDestroyGenerator(gen);
125-
assert(status == CURAND_STATUS_SUCCESS /* && "curandDestroyGenerator failed"*/);
126-
_unused(status);
114+
gpuErrchk(curandDestroyGenerator(gen));
127115
}
128116

129117
void generate_n(
130118
thrust_device::vector<float> &v,
131119
const thrust_size_t n
132120
)
133121
{
134-
int status = curandGenerateUniform(gen, thrust::raw_pointer_cast(v.data()), n); // (0,1] range
135-
assert(status == CURAND_STATUS_SUCCESS /* && "curandGenerateUniform failed"*/);
136-
_unused(status);
122+
gpuErrchk(curandGenerateUniform(gen, thrust::raw_pointer_cast(v.data()), n)); // (0,1] range
137123
// shift into the expected [0,1) range
138124
namespace arg = thrust::placeholders;
139125
thrust::transform(v.begin(), v.begin() + n, v.begin(), float(1) - arg::_1);
@@ -144,9 +130,7 @@ namespace libcloudphxx
144130
const thrust_size_t n
145131
)
146132
{
147-
int status = curandGenerateUniformDouble(gen, thrust::raw_pointer_cast(v.data()), n); // (0,1] range
148-
assert(status == CURAND_STATUS_SUCCESS /* && "curandGenerateUniform failed"*/);
149-
_unused(status);
133+
gpuErrchk(curandGenerateUniformDouble(gen, thrust::raw_pointer_cast(v.data()), n)); // (0,1] range
150134
// shift into the expected [0,1) range
151135
namespace arg = thrust::placeholders;
152136
thrust::transform(v.begin(), v.begin() + n, v.begin(), double(1) - arg::_1);
@@ -157,29 +141,23 @@ namespace libcloudphxx
157141
const thrust_size_t n
158142
)
159143
{
160-
int status = curandGenerateNormal(gen, thrust::raw_pointer_cast(v.data()), n, float(0), float(1));
161-
assert(status == CURAND_STATUS_SUCCESS /* && "curandGenerateUniform failed"*/);
162-
_unused(status);
144+
gpuErrchk(curandGenerateNormal(gen, thrust::raw_pointer_cast(v.data()), n, float(0), float(1)));
163145
}
164146

165147
void generate_normal_n(
166148
thrust_device::vector<double> &v,
167149
const thrust_size_t n
168150
)
169151
{
170-
int status = curandGenerateNormalDouble(gen, thrust::raw_pointer_cast(v.data()), n, double(0), double(1));
171-
assert(status == CURAND_STATUS_SUCCESS /* && "curandGenerateUniform failed"*/);
172-
_unused(status);
152+
gpuErrchk(curandGenerateNormalDouble(gen, thrust::raw_pointer_cast(v.data()), n, double(0), double(1)));
173153
}
174154

175155
void generate_n(
176156
thrust_device::vector<unsigned int> &v,
177157
const thrust_size_t n
178158
)
179159
{
180-
int status = curandGenerate(gen, thrust::raw_pointer_cast(v.data()), n);
181-
assert(status == CURAND_STATUS_SUCCESS /* && "curandGenerateUniform failed"*/);
182-
_unused(status);
160+
gpuErrchk(curandGenerate(gen, thrust::raw_pointer_cast(v.data()), n));
183161
}
184162
#endif
185163
};

0 commit comments

Comments
 (0)