Expression Templates Library (ETL)
curand.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include "curand.h"
16 
17 namespace etl::impl::curand {
18 
19 #define curand_call(call) \
20  { \
21  auto status = call; \
22  if (status != CURAND_STATUS_SUCCESS) { \
23  std::cerr << "CURAND error: " << status << " from " << #call << std::endl << "from " << __FILE__ << ":" << __LINE__ << std::endl; \
24  } \
25  }
26 
37 inline void generate_normal(curandGenerator_t generator, float* gpu_memory, size_t n, float mean, float stddev) {
38  // Note: CURAND is dumb, cannot generate odd sequences...
39 
40  if (n % 2 == 0) {
41  curand_call(curandGenerateNormal(generator, gpu_memory, n, mean, stddev));
42  } else {
43  // Generate the first n - 1 numbers
44  curand_call(curandGenerateNormal(generator, gpu_memory, n - 1, mean, stddev));
45 
46  // Generate the last two numbers
47  curand_call(curandGenerateNormal(generator, gpu_memory + (n - 3), 2, mean, stddev));
48  }
49 }
50 
61 inline void generate_normal(curandGenerator_t generator, double* gpu_memory, size_t n, double mean, double stddev) {
62  // Note: CURAND is dumb, cannot generate odd sequences...
63 
64  if (n % 2 == 0) {
65  curand_call(curandGenerateNormalDouble(generator, gpu_memory, n, mean, stddev));
66  } else {
67  // Generate the first n - 1 numbers
68  curand_call(curandGenerateNormalDouble(generator, gpu_memory, n - 1, mean, stddev));
69 
70  // Generate the last two numbers
71  curand_call(curandGenerateNormalDouble(generator, gpu_memory + (n - 3), 2, mean, stddev));
72  }
73 }
74 
83 inline void generate_uniform(curandGenerator_t generator, float* gpu_memory, size_t n) {
84  curand_call(curandGenerateUniform(generator, gpu_memory, n));
85 }
86 
95 inline void generate_uniform(curandGenerator_t generator, double* gpu_memory, size_t n) {
96  curand_call(curandGenerateUniformDouble(generator, gpu_memory, n));
97 }
98 
99 } //end of namespace etl::impl::curand
value_t< E > mean(E &&values)
Returns the mean of all the values contained in the given expression.
Definition: expression_builder.hpp:650
Definition: curand.hpp:17
value_t< E > stddev(E &&values)
Returns the standard deviation of all the values contained in the given expression.
Definition: expression_builder.hpp:670
void generate_uniform(curandGenerator_t generator, float *gpu_memory, size_t n)
Generate a uniform distribution between 0 and 1, in single precision.
Definition: curand.hpp:83
value_type * gpu_memory() const noexcept
Return GPU memory of this expression, if any.
Definition: sub_view.hpp:674
void generate_normal(curandGenerator_t generator, float *gpu_memory, size_t n, float mean, float stddev)
Generate a normal distribution with the given mean and standard deviation with CURAND, in single-precision.
Definition: curand.hpp:37