Skip to content

Commit

Permalink
PCG random number generator (#369)
Browse files Browse the repository at this point in the history
Add support for Permuted Congruential Generator (PCG) for random number generation. This is an optional drop-in replacement for C++'s Mersenne Twister. See https://www.pcg-random.org/ for more information. The new engine must be enabled at compile time with `ENABLE_PCG=on`.
  • Loading branch information
mlund authored May 27, 2021
1 parent 3d35805 commit 4cc9e5a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
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 @@ -27,7 +27,7 @@ energy:
moves:
- moltransrot: {molecule: water, dp: 20, dprot: 1, repeat: 1}
- moltransrot: {molecule: other, dp: 20, dprot: 1, repeat: 1}
- cluster: {molecules: [water, other], dp: 20, dprot: 3, threshold: 7, satellites: [other]}
- cluster: {molecules: [water, other], dp: 19.99999, dprot: 3, threshold: 7, satellites: [other]}

analysis:
- savestate: {file: confout.pqr}
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
12 changes: 11 additions & 1 deletion src/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
#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 +32,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

0 comments on commit 4cc9e5a

Please sign in to comment.