Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCG random number generator #369

Merged
merged 4 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmake/ExternalTools.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
include(ExternalProject)
include(FetchContent)

#############
# PCG RANDOM
#############

option(ENABLE_PCG "Enable PCG random number generator" off)
if (ENABLE_PCG)
FetchContent_Declare(
pcg-cpp
URL https://github.com/imneme/pcg-cpp/archive/ffd522e7188bef30a00c74dc7eb9de5faff90092.tar.gz
URL_HASH MD5=051b969bbaf924f35f2159813f93e341)
FetchContent_GetProperties(pcg-cpp)
if(NOT pcg-cpp_POPULATED)
FetchContent_Populate(pcg-cpp)
endif()
include_directories(SYSTEM ${pcg-cpp_SOURCE_DIR}/include)
add_definitions("-DENABLE_PCG")
endif()

#########
# EXPRTK
#########
Expand Down
1 change: 1 addition & 0 deletions docs/_docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ CMake Option | Description
`-DENABLE_PYTHON=OFF` | Build python bindings (experimental)
`-DENABLE_FREESASA=ON` | Enable SASA routines (external download)
`-DENABLE_TBB=OFF` | Build with Intel Threading Building Blocks (experimental)
`-DENABLE_PCG=OFF` | Use PCG random number generator instead of C++'s Mersenne Twister
`-DBUILD_STATIC=OFF` | Build statically linked binaries
`-DCMAKE_BUILD_TYPE=Release` | Alternatives: `Debug` or `RelWithDebInfo`
`-DCMAKE_CXX_FLAGS_RELEASE="..."` | Compiler options for Release mode
Expand Down
2 changes: 1 addition & 1 deletion examples/cluster-ideal/cluster-ideal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# N_c = 1 + 4 * pi * Rc**3 * N / (3 * L**3) = 1.22 in this case
#
temperature: 300
random: {seed: fixed}
random: {seed: hardware}
geometry: {type: cuboid, length: 40}
mcloop: {macro: 10, micro: 20000}

Expand Down
2 changes: 1 addition & 1 deletion src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Point ranunit(Random &rand, const Point &dir) {

TEST_CASE("[Faunus] ranunit") {
Random r;
int n = 2e5;
int n = 4e5;
Point rtp(0, 0, 0);
for (int i = 0; i < n; i++)
rtp += xyz2rtp(ranunit(r));
Expand Down
7 changes: 6 additions & 1 deletion src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ void to_json(nlohmann::json &j, const Random &random) {
std::ostringstream stream;
stream << random.engine; // dump engine state to stream
j["seed"] = stream.str();
if constexpr (std::is_same<RandomNumberEngine, std::mt19937>::value) {
j["engine"] = "Mersenne Twister (std::mt19937)";
} else {
j["engine"] = "Permuted Congruential Generator (pcg32)";
}
}

void Random::seed() { engine = std::mt19937(std::random_device()()); }
void Random::seed() { engine = RandomNumberEngine(std::random_device()()); }

Random::Random() : dist01(0, 1) {}

Expand Down
13 changes: 12 additions & 1 deletion src/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
#include <stdexcept>
#include <nlohmann/json_fwd.hpp>

#ifdef ENABLE_PCG
#include "pcg_random.hpp"
#endif

namespace Faunus {

#ifdef ENABLE_PCG
using RandomNumberEngine = pcg32;
#else
using RandomNumberEngine = std::mt19937;
#endif


/**
* @brief Random number generator
*
Expand All @@ -22,7 +33,7 @@ class Random {
private:
std::uniform_real_distribution<double> dist01; //!< Uniform real distribution [0,1)
public:
std::mt19937 engine; //!< Random number engine used for all operations
RandomNumberEngine engine; //!< Random number engine used for all operations
Random(); //!< Constructor with deterministic seed
void seed(); //!< Set a non-deterministic ("hardware") seed
double operator()(); //!< Random double in uniform range [0,1)
Expand Down