tvm
random_engine.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef TVM_SUPPORT_RANDOM_ENGINE_H_
27 #define TVM_SUPPORT_RANDOM_ENGINE_H_
28 
29 #include <tvm/runtime/logging.h>
30 
31 #include <cstdint> // for uint64_t
32 
33 namespace tvm {
34 namespace support {
35 
47  public:
52  using result_type = uint64_t;
53  using TRandState = int64_t;
54 
56  static constexpr TRandState multiplier = 48271;
57 
59  static constexpr TRandState increment = 0;
60 
62  static constexpr TRandState modulus = 2147483647;
63 
68  static constexpr result_type min() { return 0; }
69 
74  static constexpr result_type max() { return modulus - 1; }
75 
87  (*rand_state_ptr_) = ((*rand_state_ptr_) * multiplier + increment) % modulus;
88  return *rand_state_ptr_;
89  }
90 
95  void Seed(TRandState rand_state = 1) {
96  rand_state %= modulus; // Make sure the seed is within the range of modulus.
97  if (rand_state == 0)
98  rand_state = 1; // Avoid getting all 0 given the current parameter set.
99  else if (rand_state < 0)
100  rand_state += modulus; // Make sure the rand state is non-negative.
101  ICHECK(rand_state_ptr_ != nullptr); // Make sure the pointer is not null.
102  *rand_state_ptr_ = rand_state; // Change pointed random state to given random state value.
103  }
104 
110  // In order for reproducibility, we computer the new seed using RNG's random state and a
111  // different set of parameters. Note that both 32767 and 1999999973 are prime numbers.
112  return ((*this)() * 32767) % 1999999973;
113  }
114 
122  explicit LinearCongruentialEngine(TRandState* rand_state_ptr) {
123  rand_state_ptr_ = rand_state_ptr;
124  }
125 
126  private:
127  TRandState* rand_state_ptr_;
128 };
129 
130 } // namespace support
131 } // namespace tvm
132 
133 #endif // TVM_SUPPORT_RANDOM_ENGINE_H_
static constexpr TRandState modulus
The modulus.
Definition: random_engine.h:62
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
static constexpr TRandState multiplier
The multiplier.
Definition: random_engine.h:56
int64_t TRandState
Definition: random_engine.h:53
LinearCongruentialEngine(TRandState *rand_state_ptr)
Construct a random number generator with a random state pointer.
Definition: random_engine.h:122
This linear congruential engine is a drop-in replacement for std::minstd_rand. It strictly correspond...
Definition: random_engine.h:46
uint64_t result_type
The result type is defined as uint64_t here to avoid overflow.
Definition: random_engine.h:52
TRandState ForkSeed()
Fork a new seed for another RNG from current random state.
Definition: random_engine.h:109
void Seed(TRandState rand_state=1)
Change the start random state of RNG with the seed of a new random state value.
Definition: random_engine.h:95
static constexpr TRandState increment
The increment.
Definition: random_engine.h:59
result_type operator()()
Operator to move the random state to the next and return the new random state. According to definitio...
Definition: random_engine.h:86
static constexpr result_type max()
The maximum possible value of random state here.
Definition: random_engine.h:74
static constexpr result_type min()
The minimum possible value of random state here.
Definition: random_engine.h:68