XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rng_seed.hpp
Go to the documentation of this file.
1 #ifndef RNG_SEED_HPP
2 #define RNG_SEED_HPP
3 
4 #include "globals.hpp"
5 
6 // RNG seed must be different for every MPI rank, and to be safe should be different for every call. The seeds should also be deterministic. So each call gets a different seed, by setting the seed equal to MPI rank and the stride equal to total number of ranks.
7 class RNGSeed{
8  int seed;
9  int stride;
10 
11  public:
12 
14  : seed(0),
15  stride(1){}
16 
17  void init(int new_seed, int new_stride){
18  seed = new_seed;
19  stride = new_stride;
20  }
21 
23  int out = seed;
24 
25  // Overflow might happen if there are many ranks and many time steps
26  if(causes_addition_overflow(seed, stride)) exit_XGC("\nError: RNG Seed causes int overflow. Implement a reset.\n");
27 
28  seed += stride;
29 
30  return out;
31  }
32 };
33 
34 extern RNGSeed XGC_RNG_SEED;
35 
36 #endif
int get_and_increment()
Definition: rng_seed.hpp:22
RNGSeed()
Definition: rng_seed.hpp:13
int seed
Definition: rng_seed.hpp:8
Definition: rng_seed.hpp:7
int stride
Definition: rng_seed.hpp:9
void exit_XGC(std::string msg)
Definition: globals.hpp:37
void init(int new_seed, int new_stride)
Definition: rng_seed.hpp:17
RNGSeed XGC_RNG_SEED
Definition: rng_seed.cpp:4
bool causes_addition_overflow(int a, int b)
Definition: globals.hpp:68