19 curandStatus_t status;
20 status = curandCreateGenerator(&gen_, CURAND_RNG_PSEUDO_DEFAULT);
39 gettimeofday(&time, NULL);
40 seed_ = time.tv_sec + 1123*time.tv_usec;
44 curandStatus_t status;
45 status = curandSetPseudoRandomGeneratorSeed(gen_, seed_);
48 isInitialized_ =
true;
57 if (!isInitialized_) {
61 curandStatus_t status = curandGenerateUniform(gen_, data.
cArray(),
72 if (!isInitialized_) {
76 curandStatus_t status = curandGenerateUniformDouble(gen_, data.
cArray(),
87 if (!isInitialized_) {
93 UTIL_THROW(
"normal() requires array size to be an even number.");
96 curandStatus_t status = curandGenerateNormal(gen_, data.
cArray(),
105 double stddev,
double mean)
108 if (!isInitialized_) {
114 UTIL_THROW(
"normal() requires array size to be an even number.");
117 curandStatus_t status = curandGenerateNormalDouble(gen_, data.
cArray(),
125 void CudaRandom::errorCheck(curandStatus_t
const & error)
127 if (error == CURAND_STATUS_SUCCESS) {
130 std::string errString;
134 errString =
"UNKNOWN";
136 case CURAND_STATUS_VERSION_MISMATCH:
137 errString =
"CURAND_STATUS_VERSION_MISMATCH";
139 case CURAND_STATUS_NOT_INITIALIZED:
140 errString =
"CURAND_STATUS_NOT_INITIALIZED";
142 case CURAND_STATUS_ALLOCATION_FAILED:
143 errString =
"CURAND_STATUS_ALLOCATION_FAILED";
145 case CURAND_STATUS_TYPE_ERROR:
146 errString =
"CURAND_STATUS_TYPE_ERROR";
148 case CURAND_STATUS_OUT_OF_RANGE:
149 errString =
"CURAND_STATUS_OUT_OF_RANGE";
151 case CURAND_STATUS_LENGTH_NOT_MULTIPLE:
152 errString =
"CURAND_STATUS_LENGTH_NOT_MULTIPLE";
154 case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED:
155 errString =
"CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";
157 case CURAND_STATUS_LAUNCH_FAILURE:
158 errString =
"CURAND_STATUS_LAUNCH_FAILURE";
160 case CURAND_STATUS_PREEXISTING_FAILURE:
161 errString =
"CURAND_STATUS_PREEXISTING_FAILURE";
163 case CURAND_STATUS_INITIALIZATION_FAILED:
164 errString =
"CURAND_STATUS_INITIALIZATION_FAILED";
166 case CURAND_STATUS_INTERNAL_ERROR:
167 errString =
"CURAND_STATUS_INTERNAL_ERROR";
169 case CURAND_STATUS_ARCH_MISMATCH:
170 errString =
"CURAND_STATUS_ARCH_MISMATCH";
174 Log::file() <<
"CudaRandom error: " << errString << std::endl;
175 UTIL_THROW(
"CudaRandom number generation failed.");
void normal(DeviceArray< float > &data, float stddev, float mean=0.0)
Populate array on device with normal-distributed random floats.
long seed()
Returns value of random seed (private member variable seed_).
void uniform(DeviceArray< float > &data)
Populate array on device with random floats in (0, 1], uniform dist.
virtual ~CudaRandom()
Destructor.
void setSeed(unsigned long long seed)
Set value of random seed (private member variable seed_).
Dynamic array on the GPU device with aligned data.
int capacity() const
Return allocated capacity.
Data * cArray()
Return pointer to underlying C array.
static std::ostream & file()
Get log ostream by reference.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
PSCF package top-level namespace.
Utility classes for scientific computation.