imate
C++/CUDA Reference
RandomNumberGenerator Class Reference

Generates 64-bit integers on multiple parallel threads. More...

#include <random_number_generator.h>

Collaboration diagram for RandomNumberGenerator:

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
 

Detailed Description

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

  1. Declare a new instance of this class. In this case, the destructor will remove the variables at the end of its lifetime once the last instance of this class goes out of scope.
    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.
    
  2. Call all functions sttaically. In this case, the internal static arrays should be deallocated manually. Such as
    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();
    
    See also
    RandomArrayGenerator, Xoshiro256StarStar

Definition at line 103 of file random_number_generator.h.

Constructor & Destructor Documentation

◆ RandomNumberGenerator() [1/3]

RandomNumberGenerator::RandomNumberGenerator ( )

Initializes with one parallel thread and default seed.

Definition at line 29 of file random_number_generator.cpp.

29  :
30  num_threads(1)
31 {
32  int64_t seed = -1; // Negative indicates using processor time as seed
33  this->initialize(seed);
34 }
void initialize(int64_t seed)
Initializes an array of xoshiro_256_star_star objects.

References initialize().

Here is the call graph for this function:

◆ RandomNumberGenerator() [2/3]

RandomNumberGenerator::RandomNumberGenerator ( const int  num_threads_)
explicit

Initializes with a given number of parallel thread, but with the default seed.

Parameters
[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.

48  :
49  num_threads(num_threads_)
50 {
51  int64_t seed = -1; // Negative indicates using processor time as seed
52  this->initialize(seed);
53 }

References initialize().

Here is the call graph for this function:

◆ RandomNumberGenerator() [3/3]

RandomNumberGenerator::RandomNumberGenerator ( const int  num_threads_,
const int64_t  seed 
)
explicit

Initializes with given number of parallel thread and seed.

Parameters
[in]num_threads_Number of independent xoshiro_256_star_star objects to be created for each parallel thread.
[in]seedSeed 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.

72  :
73  num_threads(num_threads_)
74 {
75  this->initialize(seed);
76 }

References initialize().

Here is the call graph for this function:

◆ ~RandomNumberGenerator()

RandomNumberGenerator::~RandomNumberGenerator ( )

Deallocates the array of xoshiro_256_star_star.

Definition at line 86 of file random_number_generator.cpp.

87 {
88  if (this->xoshiro_256_star_star != NULL)
89  {
90  for (int thread_id=0; thread_id < this->num_threads; ++thread_id)
91  {
92  delete this->xoshiro_256_star_star[thread_id];
93  this->xoshiro_256_star_star[thread_id] = NULL;
94  }
95 
96  delete[] this->xoshiro_256_star_star;
97  this->xoshiro_256_star_star = NULL;
98  }
99 }
Xoshiro256StarStar ** xoshiro_256_star_star

References num_threads, and xoshiro_256_star_star.

Member Function Documentation

◆ initialize()

void RandomNumberGenerator::initialize ( int64_t  seed)
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.

Parameters
[in]seedSeed 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.

121 {
122  assert(this->num_threads > 0);
123 
125 
126  for (int thread_id=0; thread_id < this->num_threads; ++thread_id)
127  {
128  this->xoshiro_256_star_star[thread_id] = new Xoshiro256StarStar(seed);
129 
130  // Repeate jump j times to have different initial state for each thread
131  // This is the main purpose of this class.
132  for (int j=0; j < thread_id+1; ++j)
133  {
134  this->xoshiro_256_star_star[thread_id]->jump();
135  }
136  }
137 }
Pseudo-random integer generator. This class generates 64-bit integer using Xoshiro256** algorithm.
void jump()
Jump function for the generator. It is equivalent to 2^128 calls to next(); it can be used to generat...

References Xoshiro256StarStar::jump(), num_threads, and xoshiro_256_star_star.

Referenced by RandomNumberGenerator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ next()

uint64_t RandomNumberGenerator::next ( const int  thread_id)

Generates the next random number in the sequence, depending on the thread id.

Parameters
[in]thread_idThe thread id of the parallel process.

Definition at line 150 of file random_number_generator.cpp.

151 {
152  return this->xoshiro_256_star_star[thread_id]->next();
153 }
uint64_t next()
Generates the next presudo-random number.

References Xoshiro256StarStar::next(), and xoshiro_256_star_star.

Referenced by RandomArrayGenerator< DataType >::generate_random_array().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ num_threads

const int RandomNumberGenerator::num_threads
protected

Definition at line 120 of file random_number_generator.h.

Referenced by initialize(), and ~RandomNumberGenerator().

◆ xoshiro_256_star_star

Xoshiro256StarStar** RandomNumberGenerator::xoshiro_256_star_star
protected

Definition at line 121 of file random_number_generator.h.

Referenced by initialize(), next(), and ~RandomNumberGenerator().


The documentation for this class was generated from the following files: