diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2016-03-17 15:23:51 -0700 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2016-03-17 15:23:51 -0700 |
commit | 95b8961a9b2ac3e063ba9ddb7ac8515e24cae6c2 (patch) | |
tree | 8eeda141083d783fe891deca7e74879a9fc01be4 /unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h | |
parent | f7329619da8d493fecc30e2a5d44bc3a672741a3 (diff) |
Allocate the mersenne twister used by the random number generators on the heap instead of on the stack since they tend to keep a lot of state (i.e. about 5k) around.
Diffstat (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h')
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h index 8e45ae9e5..26e5dafce 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h @@ -364,19 +364,23 @@ template <> class UniformRandomGenerator<float> { public: static const bool PacketAccess = true; - UniformRandomGenerator(bool deterministic = true) : m_deterministic(deterministic) { + UniformRandomGenerator(bool deterministic = true) : m_deterministic(deterministic), m_generator(new std::mt19937()) { if (!deterministic) { - m_generator.seed(get_random_seed()); + m_generator->seed(get_random_seed()); } } UniformRandomGenerator(const UniformRandomGenerator<float>& other) { - m_generator.seed(other(0) * UINT_MAX); + m_generator = new std::mt19937(); + m_generator->seed(other(0) * UINT_MAX); m_deterministic = other.m_deterministic; } + ~UniformRandomGenerator() { + delete m_generator; + } template<typename Index> float operator()(Index) const { - return m_distribution(m_generator); + return m_distribution(*m_generator); } template<typename Index, typename PacketType> PacketType packetOp(Index i) const { @@ -393,7 +397,7 @@ template <> class UniformRandomGenerator<float> { // Make sure m_deterministic comes first to match the layout of the cpu // version of the code. bool m_deterministic; - mutable std::mt19937 m_generator; + mutable std::mt19937* m_generator; mutable std::uniform_real_distribution<float> m_distribution; }; @@ -401,19 +405,23 @@ template <> class UniformRandomGenerator<double> { public: static const bool PacketAccess = true; - UniformRandomGenerator(bool deterministic = true) : m_deterministic(deterministic) { + UniformRandomGenerator(bool deterministic = true) : m_deterministic(deterministic), m_generator(new std::mt19937()) { if (!deterministic) { - m_generator.seed(get_random_seed()); + m_generator->seed(get_random_seed()); } } UniformRandomGenerator(const UniformRandomGenerator<double>& other) { - m_generator.seed(other(0) * UINT_MAX); + m_generator = new std::mt19937(); + m_generator->seed(other(0) * UINT_MAX); m_deterministic = other.m_deterministic; } + ~UniformRandomGenerator() { + delete m_generator; + } template<typename Index> double operator()(Index) const { - return m_distribution(m_generator); + return m_distribution(*m_generator); } template<typename Index, typename PacketType> PacketType packetOp(Index i) const { @@ -430,7 +438,7 @@ template <> class UniformRandomGenerator<double> { // Make sure m_deterministic comes first to match the layout of the cpu // version of the code. bool m_deterministic; - mutable std::mt19937 m_generator; + mutable std::mt19937* m_generator; mutable std::uniform_real_distribution<double> m_distribution; }; #endif @@ -571,34 +579,39 @@ template <typename T> class NormalRandomGenerator { public: static const bool PacketAccess = true; - NormalRandomGenerator(bool deterministic = true) : m_deterministic(deterministic), m_distribution(0, 1) { + NormalRandomGenerator(bool deterministic = true) : m_deterministic(deterministic), m_distribution(0, 1), m_generator(new std::mt19937()) { if (!deterministic) { - m_generator.seed(get_random_seed()); + m_generator->seed(get_random_seed()); } } NormalRandomGenerator(const NormalRandomGenerator& other) - : m_deterministic(other.m_deterministic), m_distribution(other.m_distribution) { - m_generator.seed(other(0) * UINT_MAX); + : m_deterministic(other.m_deterministic), m_distribution(other.m_distribution), m_generator(new std::mt19937()) { + m_generator->seed(other(0) * UINT_MAX); + } + ~NormalRandomGenerator() { + delete m_generator; } - template<typename Index> T operator()(Index) const { - return m_distribution(m_generator); + return m_distribution(*m_generator); } template<typename Index, typename PacketType> PacketType packetOp(Index) const { const int packetSize = internal::unpacket_traits<PacketType>::size; EIGEN_ALIGN_MAX T values[packetSize]; for (int i = 0; i < packetSize; ++i) { - values[i] = m_distribution(m_generator); + values[i] = m_distribution(*m_generator); } return internal::pload<PacketType>(values); } private: + // No assignment + NormalRandomGenerator& operator = (const NormalRandomGenerator&); + bool m_deterministic; mutable std::normal_distribution<T> m_distribution; - mutable std::mt19937 m_generator; + mutable std::mt19937* m_generator; }; #elif defined (EIGEN_USE_GPU) && defined(__CUDACC__) && defined(__CUDA_ARCH__) |