imate
C++/CUDA Reference
|
Generates 64-bit integers on multiple parallel threads. More...
#include <random_number_generator.h>
Public Member Functions | |
RandomNumberGenerator () | |
Initializes with one parallel thread and default seed. More... | |
RandomNumberGenerator (const int num_threads_) | |
Initializes with a given number of parallel thread, but with the default seed. More... | |
RandomNumberGenerator (const int num_threads_, const int64_t seed) | |
Initializes with given number of parallel thread and seed. More... | |
~RandomNumberGenerator () | |
Deallocates the array of xoshiro_256_star_star . More... | |
uint64_t | next (const int thread_id) |
Generates the next random number in the sequence, depending on the thread id. More... | |
Protected Member Functions | |
void | initialize (int64_t seed) |
Initializes an array of xoshiro_256_star_star objects. More... | |
Protected Attributes | |
const int | num_threads |
Xoshiro256StarStar ** | xoshiro_256_star_star |
Generates 64-bit integers on multiple parallel threads.
This class creates multiple instances of Xoshiro256StarStar
class, with the number of parallel threads. On each thread, it generates random integers independently.
The necessity of using this class is when all threads start at the same time, hence the initial seeds cannot be distinguished using the time()
function (since time the same for all threads). Thus the random generator in all threads generate the same senquence of numbers. To avoid this issue, this class jumps the sequence of each instance of Xoshiro256StarStar
. Thus, even if their initial state seed is the same, the jump in the state variable makes it different by 2^128 on the sequence.
All member variables of this class aer static and they can be used without declaring an instance of this class. Also, in the same thread, if we call this class from multiple functions, the same sequence if used since the internal state variable is static.
This class can be used by either
num_threads = omp_get_num_threads(); // Declare an instance for num_thread parallel threads RandomNumberGenerator random_number_generator(num_threads); // Generate random numbers #pragma omp parallel { int tid = omp_get_thread_num(); #pragma omp for for (int i = 0; i < n; ++i) { uint64_t a = RandomNumberGenerator::next(tid); } } // The random_number_generator will go out of scope and it // automatically deallocate internal static arrays.
num_threads = omp_get_num_threads(); // Create an internal array of random generator per thread RandomNumberGenerator::initialize(num_threads); // Generate random numbers #pragma omp parallel { int tid = omp_get_thread_num(); #pragma omp for for (int i = 0; i < n; ++i) { uint64_t a = RandomNumberGenerator::next(tid); } } // Deallcoate internal static arrays of this class RandomNumberGenerator::deallocate();
Definition at line 103 of file random_number_generator.h.
RandomNumberGenerator::RandomNumberGenerator | ( | ) |
Initializes with one parallel thread and default seed.
Definition at line 29 of file random_number_generator.cpp.
References initialize().
|
explicit |
Initializes with a given number of parallel thread, but with the default seed.
[in] | num_threads_ | Number of independent xoshiro_256_star_star objects to be created for each parallel thread. |
Definition at line 48 of file random_number_generator.cpp.
References initialize().
|
explicit |
Initializes with given number of parallel thread and seed.
[in] | num_threads_ | Number of independent xoshiro_256_star_star objects to be created for each parallel thread. |
[in] | seed | Seed for pseudo-random number generation. The same seed value is used for all threads. If seed is negative integer, the given seed value is ignored, and the processor time is used istead. |
Definition at line 70 of file random_number_generator.cpp.
References initialize().
RandomNumberGenerator::~RandomNumberGenerator | ( | ) |
Deallocates the array of xoshiro_256_star_star
.
Definition at line 86 of file random_number_generator.cpp.
References num_threads, and xoshiro_256_star_star.
|
protected |
Initializes an array of xoshiro_256_star_star
objects.
The size of the array is num_threads
, corresponding to each parallel thread. Also, the state of the i-th object is jumped (i+1) times so that all random generators have diferent start states. This is the main reason of using this class since it aggregates multiple random generator objects, one for each parallel thread, and all have different initial random state.
[in] | seed | Seed for pseudo-random number generation. The same seed value is used for all threads. If seed is negative integer, the given seed value is ignored, and the processor time is used istead. |
Definition at line 120 of file random_number_generator.cpp.
References Xoshiro256StarStar::jump(), num_threads, and xoshiro_256_star_star.
Referenced by RandomNumberGenerator().
uint64_t RandomNumberGenerator::next | ( | const int | thread_id | ) |
Generates the next random number in the sequence, depending on the thread id.
[in] | thread_id | The thread id of the parallel process. |
Definition at line 150 of file random_number_generator.cpp.
References Xoshiro256StarStar::next(), and xoshiro_256_star_star.
Referenced by RandomArrayGenerator< DataType >::generate_random_array().
|
protected |
Definition at line 120 of file random_number_generator.h.
Referenced by initialize(), and ~RandomNumberGenerator().
|
protected |
Definition at line 121 of file random_number_generator.h.
Referenced by initialize(), next(), and ~RandomNumberGenerator().