-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for seeder function. Refactor seeder class for thre…
…ad safety and better functionality
- Loading branch information
1 parent
70d6bdd
commit 4d5c5ed
Showing
21 changed files
with
148 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ | |
\.so$ | ||
\.git/ | ||
^\.dll$ | ||
^\.github$ | ||
^\.github$ | ||
^\.log$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
.Rhistory | ||
.vscode/* | ||
.RData | ||
*.tar.gz | ||
*.tar.gz | ||
*.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#define STRICT_R_HEADERS | ||
#pragma once | ||
#ifndef SEEDER | ||
#define SEEDER | ||
|
||
#include <Rcpp.h> | ||
#include <gsl/gsl_rng.h> | ||
#include <gsl/gsl_randist.h> | ||
|
||
class Seeder { | ||
private: | ||
gsl_rng* r = nullptr; | ||
static Seeder* instance; | ||
int currentSeed = -1; | ||
Seeder() {} | ||
|
||
public: | ||
Seeder(Seeder const&) = delete; | ||
Seeder& operator=(Seeder const&) = delete; | ||
static Seeder* getInstance() { | ||
if(!instance) { | ||
instance = new Seeder(); | ||
} | ||
return instance; | ||
} | ||
|
||
~Seeder() { | ||
if(r) { | ||
Rcpp::Rcout << "Singleton destroyed" << std::endl; | ||
gsl_rng_free(r); | ||
} | ||
} | ||
|
||
void setSeed(int seed) { | ||
if (seed < 0) { | ||
Rcpp::stop("Error: Seed must be a positive integer."); | ||
} | ||
|
||
// if(currentSeed > 0) { | ||
// Rcpp::Rcout << "Previous GSL seed: " << currentSeed << std::endl; | ||
// } | ||
|
||
if(r) { | ||
// Rcpp::Rcout << "Freeing r before setting it to a new value" << std::endl; | ||
gsl_rng_free(r); | ||
} | ||
gsl_rng_env_setup(); | ||
r = gsl_rng_alloc(gsl_rng_mt19937); | ||
gsl_rng_set(r, seed); | ||
if (currentSeed != seed) { | ||
Rcpp::Rcout << "Updated GSL seed: " << currentSeed << std::endl; | ||
} | ||
currentSeed = seed; | ||
} | ||
|
||
double get_uniform() { | ||
return gsl_rng_uniform(r); | ||
} | ||
|
||
double get_gaussian_ziggurat() { | ||
return gsl_ran_gaussian_ziggurat(r, 1.0); | ||
} | ||
|
||
double get_ran_flat() { | ||
return gsl_ran_flat(r, -1, 1); | ||
} | ||
|
||
gsl_rng* get_gsl_rng() { | ||
return r; | ||
} | ||
}; | ||
#endif |
Oops, something went wrong.