|
9 | 9 | #include <thrust/iterator/transform_iterator.h> |
10 | 10 | #include <thrust/tabulate.h> |
11 | 11 |
|
| 12 | +#include <cuda/std/optional> |
| 13 | + |
12 | 14 | #include <c2h/bfloat16.cuh> |
13 | 15 | #include <c2h/custom_type.h> |
14 | 16 | #include <c2h/detail/generators.cuh> |
@@ -44,29 +46,77 @@ struct i_to_rnd_t |
44 | 46 | }; |
45 | 47 | #endif // !C2H_HAS_CURAND |
46 | 48 |
|
47 | | -void generator_t::generate() |
| 49 | +class generator_t |
48 | 50 | { |
| 51 | +public: |
| 52 | + generator_t() |
| 53 | + { |
| 54 | +#if C2H_HAS_CURAND |
| 55 | + curandCreateGenerator(&m_gen, CURAND_RNG_PSEUDO_DEFAULT); |
| 56 | +#endif |
| 57 | + } |
| 58 | + |
| 59 | + ~generator_t() |
| 60 | + { |
| 61 | +#if C2H_HAS_CURAND |
| 62 | + curandDestroyGenerator(m_gen); |
| 63 | +#endif |
| 64 | + } |
| 65 | + |
| 66 | + float* prepare_random_generator(seed_t seed, std::size_t num_items) |
| 67 | + { |
| 68 | + m_distribution.resize(num_items); |
| 69 | + |
49 | 70 | #if C2H_HAS_CURAND |
50 | | - curandGenerateUniform(m_gen, thrust::raw_pointer_cast(m_distribution.data()), m_distribution.size()); |
| 71 | + curandSetPseudoRandomGeneratorSeed(m_gen, seed.get()); |
51 | 72 | #else |
52 | | - thrust::tabulate(device_policy, m_distribution.begin(), m_distribution.end(), i_to_rnd_t{m_re}); |
53 | | - m_re.discard(m_distribution.size()); |
| 73 | + m_gen.seed(seed.get()); |
54 | 74 | #endif |
55 | | -} |
56 | 75 |
|
57 | | -float* generator_t::prepare_random_generator(seed_t seed, std::size_t num_items) |
58 | | -{ |
59 | | - m_distribution.resize(num_items); |
| 76 | + generate(); |
60 | 77 |
|
| 78 | + return thrust::raw_pointer_cast(m_distribution.data()); |
| 79 | + } |
| 80 | + |
| 81 | + // re-fills the currently held distribution vector with new random values |
| 82 | + void generate() |
| 83 | + { |
| 84 | +#if C2H_HAS_CURAND |
| 85 | + curandGenerateUniform(m_gen, thrust::raw_pointer_cast(m_distribution.data()), m_distribution.size()); |
| 86 | +#else |
| 87 | + thrust::tabulate(device_policy, m_distribution.begin(), m_distribution.end(), i_to_rnd_t{m_gen}); |
| 88 | + m_gen.discard(m_distribution.size()); |
| 89 | +#endif |
| 90 | + } |
| 91 | + |
| 92 | +private: |
61 | 93 | #if C2H_HAS_CURAND |
62 | | - curandSetPseudoRandomGeneratorSeed(m_gen, seed.get()); |
| 94 | + curandGenerator_t |
63 | 95 | #else |
64 | | - m_re.seed(seed.get()); |
| 96 | + thrust::default_random_engine |
65 | 97 | #endif |
| 98 | + m_gen; |
| 99 | + c2h::device_vector<float> m_distribution; |
| 100 | +}; |
66 | 101 |
|
67 | | - generate(); |
| 102 | +// global generator state |
| 103 | +cuda::std::optional<generator_t> generator; |
68 | 104 |
|
69 | | - return thrust::raw_pointer_cast(m_distribution.data()); |
| 105 | +void init_generator() |
| 106 | +{ |
| 107 | + _CCCL_VERIFY(!generator.has_value(), ""); |
| 108 | + generator.emplace(); |
| 109 | +} |
| 110 | + |
| 111 | +float* prepare_random_data(seed_t seed, std::size_t num_items) |
| 112 | +{ |
| 113 | + return generator.value().prepare_random_generator(seed, num_items); |
| 114 | +} |
| 115 | + |
| 116 | +void cleanup_generator() |
| 117 | +{ |
| 118 | + _CCCL_VERIFY(generator.has_value(), ""); |
| 119 | + generator.reset(); |
70 | 120 | } |
71 | 121 |
|
72 | 122 | struct random_to_custom_t |
@@ -94,7 +144,7 @@ void gen_custom_type_state( |
94 | 144 | std::size_t element_size) |
95 | 145 | { |
96 | 146 | // FIXME(bgruber): implement min/max handling for custom_type_state_t |
97 | | - float* d_in = generator.prepare_random_generator(seed, elements * 2); |
| 147 | + float* d_in = prepare_random_data(seed, elements * 2); |
98 | 148 | thrust::for_each(device_policy, |
99 | 149 | thrust::counting_iterator<std::size_t>{0}, |
100 | 150 | thrust::counting_iterator<std::size_t>{elements}, |
|
0 commit comments