OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
1//
2// Random number generator to replace GSL RNG
3//
4// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#ifndef OPAL_RANDOM_HH
19#define OPAL_RANDOM_HH
20
21#include <cstddef>
22#include <memory>
23#include <random>
24
25// GSL-compatible random number generator wrapper
26class gsl_rng {
27public:
28 gsl_rng() : engine_(std::mt19937_64(std::random_device{}())) {}
29
30 double uniform() { return uniform_dist_(engine_); }
31
32 double gaussian(double sigma) {
33 std::normal_distribution<double> dist(0.0, sigma);
34 return dist(engine_);
35 }
36
37 void seed(unsigned long seed) { engine_.seed(seed); }
38
39 std::mt19937_64& engine() { return engine_; }
40 const std::mt19937_64& engine() const { return engine_; }
41
42private:
43 std::mt19937_64 engine_;
44 std::uniform_real_distribution<double> uniform_dist_{0.0, 1.0};
45};
46
47// GSL RNG type (not used, but kept for compatibility)
49 const char* name;
50};
51
52// Default RNG type
53extern const gsl_rng_type* gsl_rng_default;
54
55// GSL-compatible functions
56inline void gsl_rng_env_setup() {
57 // No-op: environment setup not needed with std::random
58}
59
60inline gsl_rng* gsl_rng_alloc(const gsl_rng_type* /* type */) { return new gsl_rng(); }
61
62inline void gsl_rng_free(gsl_rng* rng) { delete rng; }
63
64inline double gsl_rng_uniform(gsl_rng* rng) { return rng->uniform(); }
65
66inline double gsl_ran_gaussian(gsl_rng* rng, double sigma) { return rng->gaussian(sigma); }
67
68// Dummy implementation (not used in practice)
70 static const gsl_rng_type dummy = {"mt19937"};
71 return &dummy;
72}
73#define gsl_rng_default (get_gsl_rng_default())
74
75#endif // OPAL_RANDOM_HH
double gsl_rng_uniform(gsl_rng *rng)
Definition Random.h:64
#define gsl_rng_default
Definition Random.h:73
void gsl_rng_free(gsl_rng *rng)
Definition Random.h:62
const char * name
Definition Random.h:49
void gsl_rng_env_setup()
Definition Random.h:56
const gsl_rng_type * get_gsl_rng_default()
Definition Random.h:69
double gsl_ran_gaussian(gsl_rng *rng, double sigma)
Definition Random.h:66
gsl_rng * gsl_rng_alloc(const gsl_rng_type *)
Definition Random.h:60
std::mt19937_64 engine_
Definition Random.h:43
const std::mt19937_64 & engine() const
Definition Random.h:40
double uniform()
Definition Random.h:30
double gaussian(double sigma)
Definition Random.h:32
std::mt19937_64 & engine()
Definition Random.h:39
gsl_rng()
Definition Random.h:28
std::uniform_real_distribution< double > uniform_dist_
Definition Random.h:44
void seed(unsigned long seed)
Definition Random.h:37
STL namespace.