SUNphi  1.0
TrueRandomGenerator.hpp
Go to the documentation of this file.
1 #ifndef _TRUERANDOMGENERATOR_HPP
2 #define _TRUERANDOMGENERATOR_HPP
3 
4 /// \file TrueRandomGenerator.hpp
5 ///
6 /// \brief Generate random numbers according to the true random device
7 ///
8 /// This random generator should be used only once, to init the master
9 /// random generator, when a "true" (non reproducible) random sequence
10 /// is needed.
11 
12 #include <random>
13 
14 #include <system/Mpi.hpp>
15 #include <utility/SingleInstance.hpp>
16 
17 namespace SUNphi
18 {
19  /// Class which enables true random numbers
20  ///
21  /// The random numbers are always generated on all nodes, but only
22  /// the value on the given node is returned
24  : public std::random_device,
26  {
27  /// Rank of which the result is returning
29 
30  public:
31 
32  /// Type returned
33  using ResultType=
35 
36  /// Creates specifying the rank to be used for returning
37  TrueRandomGenerator(int returningRank=Mpi::MASTER_RANK)
39  {
40  }
41 
42  /// Returns the inner generated value
43  auto operator()()
44  {
45  /// Draw the value on every node
46  ResultType val=
47  std::random_device::operator()();
48 
49  // Broadcast from the preferred node
50  if(returningRank!=Mpi::ALL_RANKS)
51  mpi.broadcast(val,returningRank);
52 
53  return
54  val;
55  }
56  };
57 
59 }
60 
61 #endif
auto operator()()
Returns the inner generated value.
TrueRandomGenerator trueRandomGenerator
Global true random generator.
Definition: SUNphi.cpp:218
int returningRank
Rank of which the result is returning.
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
Definition: Add.hpp:87
TrueRandomGenerator(int returningRank=Mpi::MASTER_RANK)
Creates specifying the rank to be used for returning.