Skip to content

Commit

Permalink
Merge pull request #273 from fnalacceleratormodeling/fix_include_errors
Browse files Browse the repository at this point in the history
add missing header in foundation/distribution.h
  • Loading branch information
Sajid Ali authored May 8, 2024
2 parents 8e309ad + b4df4f9 commit b7e0273
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 89 deletions.
69 changes: 33 additions & 36 deletions src/synergia/foundation/distribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,68 @@
unsigned long int
Random_distribution::get_default_seed(const char* device)
{
unsigned long int seed;
std::ifstream devrandom(device, std::ios::binary);
if (devrandom) {
devrandom.read((char*)&seed, sizeof(unsigned long int));
} else {
seed = std::time(0);
}
return seed;
unsigned long int seed;
std::ifstream devrandom(device, std::ios::binary);
if (devrandom) {
devrandom.read((char*)&seed, sizeof(unsigned long int));
} else {
seed = std::time(0);
}
return seed;
}

Random_distribution::Random_distribution(unsigned long int seed,
int rank,
Generator generator) :
rng(nullptr),
rng_type(nullptr),
rank(rank),
original_seed(0)
Generator generator)
: rng(nullptr), rng_type(nullptr), rank(rank), original_seed(0)
{
gsl_rng_env_setup();
if (generator == ranlxd2) {
rng_type = gsl_rng_ranlxd2;
} else if (generator == mt19937) {
rng_type = gsl_rng_mt19937;
}
rng = gsl_rng_alloc(rng_type);
set_seed(seed); // NOTE: this resets original_seed
gsl_rng_env_setup();
if (generator == ranlxd2) {
rng_type = gsl_rng_ranlxd2;
} else if (generator == mt19937) {
rng_type = gsl_rng_mt19937;
}
rng = gsl_rng_alloc(rng_type);
set_seed(seed); // NOTE: this resets original_seed
}

void
Random_distribution::set_seed(unsigned long int seed)
{
if (seed == 0) {
original_seed = get_default_seed();
} else {
original_seed = seed;
}
unsigned long int distributed_seed;
distributed_seed =
(1000 + 5 * (rank + original_seed)) * ((rank + original_seed) + 7) - 1;
if (seed == 0) {
original_seed = get_default_seed();
} else {
original_seed = seed;
}
unsigned long int distributed_seed;
distributed_seed =
(1000 + 5 * (rank + original_seed)) * ((rank + original_seed) + 7) - 1;

gsl_rng_set(rng, distributed_seed);
gsl_rng_set(rng, distributed_seed);
}

unsigned long int
Random_distribution::get_original_seed() const
{
return original_seed;
return original_seed;
}

double
Random_distribution::get()
{
return gsl_rng_uniform(rng);
return gsl_rng_uniform(rng);
}

double
Random_distribution::get_uniform(double min, double max)
{
return gsl_ran_flat(rng, min, max);
return gsl_ran_flat(rng, min, max);
}

double
Random_distribution::get_unit_gaussian()
{
return gsl_ran_ugaussian_ratio_method(rng);
return gsl_ran_ugaussian_ratio_method(rng);
}

#if 0
Expand Down Expand Up @@ -102,11 +99,11 @@ Random_distribution::fill_unit_disk(double* x_array, double* y_array)

Random_distribution::~Random_distribution()
{
gsl_rng_free(rng);
gsl_rng_free(rng);
}

void
Random_distribution::advance(uint64_t)
{
throw std::runtime_error("Random_distribution can not be advanced.");
throw std::runtime_error("Random_distribution can not be advanced.");
}
107 changes: 54 additions & 53 deletions src/synergia/foundation/distribution.h
Original file line number Diff line number Diff line change
@@ -1,88 +1,89 @@
#ifndef DISTRIBUTION_H_
#define DISTRIBUTION_H_

#include <cstdint>
#include <gsl/gsl_rng.h>

/// Distribution is a virtual base class for obtaining the next number or set
/// of numbers from a sequence according to a limited set of shapes.
class Distribution {
public:
virtual ~Distribution() = default;
public:
virtual ~Distribution() = default;

/// Get the next number in the sequence (between 0 and 1).
virtual double get() = 0;
/// Get the next number in the sequence (between 0 and 1).
virtual double get() = 0;

/// Fill a one-dimensional array uniformly between min and max.
virtual double get_uniform(double min, double max) = 0;
/// Fill a one-dimensional array uniformly between min and max.
virtual double get_uniform(double min, double max) = 0;

/// Fill a one-dimensional array with Gaussian distribution of
/// zero mean and unit standard deviation.
virtual double get_unit_gaussian() = 0;
/// Fill a one-dimensional array with Gaussian distribution of
/// zero mean and unit standard deviation.
virtual double get_unit_gaussian() = 0;

/// Fill two one-dimensional arrays such that (x,y) are distributed
/// uniformly in the unit disk.
/// virtual void fill_unit_disk(double* x_array, double* y_array) = 0;
/// Fill two one-dimensional arrays such that (x,y) are distributed
/// uniformly in the unit disk.
/// virtual void fill_unit_disk(double* x_array, double* y_array) = 0;

/// Skip ahead the random number generator by delta.
/// NOTE: subclasses may implement this as a no-op.
virtual void advance(uint64_t delta) = 0;
/// Skip ahead the random number generator by delta.
/// NOTE: subclasses may implement this as a no-op.
virtual void advance(uint64_t delta) = 0;
};

/// Random_distribution provides a Distribution of random numbers. The random
/// seed is maintained across multiple processors. The implementation uses
/// random numbers from the GNU Scientific Library.
class Random_distribution : public Distribution {

private:
gsl_rng* rng;
const gsl_rng_type* rng_type;
int rank;
unsigned long int original_seed;
private:
gsl_rng* rng;
const gsl_rng_type* rng_type;
int rank;
unsigned long int original_seed;

public:
enum Generator { ranlxd2, mt19937 };
public:
enum Generator { ranlxd2, mt19937 };

/// Construct a Random_distribution.
/// @param seed The random number seed. If seed == 0, the seed is
/// obtained from Random_distribution::get_default_seed().
/// @param rank Set the seed for this rank.
/// @param generator The underlying random number generator to be used.
Random_distribution(unsigned long int seed,
int rank,
Generator generator = ranlxd2);
/// Construct a Random_distribution.
/// @param seed The random number seed. If seed == 0, the seed is
/// obtained from Random_distribution::get_default_seed().
/// @param rank Set the seed for this rank.
/// @param generator The underlying random number generator to be used.
Random_distribution(unsigned long int seed,
int rank,
Generator generator = ranlxd2);

virtual ~Random_distribution();
virtual ~Random_distribution();

/// Generate a random seed. Attempt to read from device if present.
/// Otherwise, use the system clock.
/// @param device Read from pathname device.
static unsigned long int get_default_seed(
const char* device = "/dev/urandom");
/// Generate a random seed. Attempt to read from device if present.
/// Otherwise, use the system clock.
/// @param device Read from pathname device.
static unsigned long int get_default_seed(
const char* device = "/dev/urandom");

/// Set the random number generator seed.
/// @param seed The seed.
void set_seed(unsigned long int seed);
/// Set the random number generator seed.
/// @param seed The seed.
void set_seed(unsigned long int seed);

/// Get the seed used to start the random number generator.
unsigned long int get_original_seed() const;
/// Get the seed used to start the random number generator.
unsigned long int get_original_seed() const;

/// Get the next random number between 0 and 1.
double get() override;
/// Get the next random number between 0 and 1.
double get() override;

/// Fill a one-dimensional array uniformly between min and max.
double get_uniform(double min, double max) override;
/// Fill a one-dimensional array uniformly between min and max.
double get_uniform(double min, double max) override;

/// Fill a one-dimensional array with Gaussian distribution of
/// zero mean and unit standard deviation.
double get_unit_gaussian() override;
/// Fill a one-dimensional array with Gaussian distribution of
/// zero mean and unit standard deviation.
double get_unit_gaussian() override;

/// Fill two one-dimensional arrays such that (x,y) are distributed
/// uniformly in the unit disk.
/// void fill_unit_disk(double* x_array, double* y_array) override;
/// Fill two one-dimensional arrays such that (x,y) are distributed
/// uniformly in the unit disk.
/// void fill_unit_disk(double* x_array, double* y_array) override;

void advance(uint64_t delta) override;
void advance(uint64_t delta) override;

void test();
void test();
};

#endif /* DISTRIBUTION_H_ */

0 comments on commit b7e0273

Please sign in to comment.