3#include "ThreadArray.h"
20 void _linearScale(
float* a,
float c,
float s,
int n)
22 int nThreads = blockDim.x * gridDim.x;
23 int startID = blockIdx.x * blockDim.x + threadIdx.x;
24 for (
int i = startID; i < n; i +=
nThreads) {
33 void _linearScale(
double* a,
double c,
double s,
int n)
35 int nThreads = blockDim.x * gridDim.x;
36 int startID = blockIdx.x * blockDim.x + threadIdx.x;
37 for (
int i = startID; i < n; i +=
nThreads) {
47 const int n = a.capacity();
54 _linearScale<<<nBlocks, nThreads>>>(a.cArray(), c, s, n);
62 const int n = a.capacity();
69 _linearScale<<<nBlocks, nThreads>>>(a.cArray(), c, s, n);
85 curandStatus_t status;
86 status = curandCreateGenerator(&gen_, CURAND_RNG_PSEUDO_DEFAULT);
105 gettimeofday(&time, NULL);
106 seed_ = time.tv_sec + 1123*time.tv_usec;
110 curandStatus_t status;
111 status = curandSetPseudoRandomGeneratorSeed(gen_, seed_);
114 isInitialized_ =
true;
122 const int n = data.capacity();
124 if (!isInitialized_) {
128 curandStatus_t status;
129 status = curandGenerateUniform(gen_, data.cArray(), n);
138 const int n = data.capacity();
140 if (!isInitialized_) {
144 curandStatus_t status;
145 status = curandGenerateUniformDouble(gen_, data.cArray(), n);
153 float min,
float max)
157 linearScale(data, max - min, min);
164 double min,
double max)
168 linearScale(data, max - min, min);
175 float stddev,
float mean)
178 if (!isInitialized_) {
186 int n = data.capacity();
187 int nGen = n + (n % 2);
188 float* ptr = data.cArray();
189 std::uintptr_t address =
reinterpret_cast<std::uintptr_t
>(ptr);
190 if (nGen != n || address % (2*
sizeof(
float)) != 0) {
191 if (floatWork_.capacity() < nGen) {
192 if (floatWork_.isAllocated()) {
193 floatWork_.deallocate();
195 floatWork_.allocate(nGen);
197 ptr = floatWork_.cArray();
200 curandStatus_t status;
201 status = curandGenerateNormal(gen_, ptr, nGen, mean, stddev);
203 if (ptr != data.cArray()) {
204 cudaErrorCheck( cudaMemcpy(data.cArray(), ptr, n*
sizeof(
float),
205 cudaMemcpyDeviceToDevice) );
213 double stddev,
double mean)
216 if (!isInitialized_) {
224 int n = data.capacity();
225 int nGen = n + (n % 2);
226 double* ptr = data.cArray();
227 std::uintptr_t address =
reinterpret_cast<std::uintptr_t
>(ptr);
228 if (nGen != n || address % (2*
sizeof(
double)) != 0) {
229 if (doubleWork_.capacity() < nGen) {
230 if (doubleWork_.isAllocated()) {
231 doubleWork_.deallocate();
233 doubleWork_.allocate(nGen);
235 ptr = doubleWork_.cArray();
238 curandStatus_t status;
239 status = curandGenerateNormalDouble(gen_, ptr, nGen, mean, stddev);
241 if (ptr != data.cArray()) {
242 cudaErrorCheck( cudaMemcpy(data.cArray(), ptr, n*
sizeof(
double),
243 cudaMemcpyDeviceToDevice) );
254 if (error == CURAND_STATUS_SUCCESS) {
257 std::string errString;
261 errString =
"UNKNOWN";
263 case CURAND_STATUS_VERSION_MISMATCH:
264 errString =
"CURAND_STATUS_VERSION_MISMATCH";
266 case CURAND_STATUS_NOT_INITIALIZED:
267 errString =
"CURAND_STATUS_NOT_INITIALIZED";
269 case CURAND_STATUS_ALLOCATION_FAILED:
270 errString =
"CURAND_STATUS_ALLOCATION_FAILED";
272 case CURAND_STATUS_TYPE_ERROR:
273 errString =
"CURAND_STATUS_TYPE_ERROR";
275 case CURAND_STATUS_OUT_OF_RANGE:
276 errString =
"CURAND_STATUS_OUT_OF_RANGE";
278 case CURAND_STATUS_LENGTH_NOT_MULTIPLE:
279 errString =
"CURAND_STATUS_LENGTH_NOT_MULTIPLE";
281 case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED:
282 errString =
"CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";
284 case CURAND_STATUS_LAUNCH_FAILURE:
285 errString =
"CURAND_STATUS_LAUNCH_FAILURE";
287 case CURAND_STATUS_PREEXISTING_FAILURE:
288 errString =
"CURAND_STATUS_PREEXISTING_FAILURE";
290 case CURAND_STATUS_INITIALIZATION_FAILED:
291 errString =
"CURAND_STATUS_INITIALIZATION_FAILED";
293 case CURAND_STATUS_INTERNAL_ERROR:
294 errString =
"CURAND_STATUS_INTERNAL_ERROR";
296 case CURAND_STATUS_ARCH_MISMATCH:
297 errString =
"CURAND_STATUS_ARCH_MISMATCH";
301 Log::file() <<
"VecRandom error: " << errString << std::endl;
302 UTIL_THROW(
"VecRandom number generation failed.");
328 { vr.setSeed(seed); }
void uniform(BackendArray< float, CUT > &data)
Populate array with uniform random floats in (0, 1].
long seed()
Returns value of random seed (private member variable seed_).
void setSeed(unsigned long long seed)
Set value of random seed (private member variable seed_).
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.
void setThreadsLogical(int nThreadsLogical)
Given total number of threads, set 1D execution configuration.
int nThreads()
Get the number of threads per block for execution.
int nBlocks()
Get the current number of blocks for execution.
PSCF package top-level namespace.