-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move random number related code to src/gnu (#2689)
* This patch moves most of the code that is not HOC to the directory gnu. * `Isaac`, `MCellRan4`, `Random123`, `Rand` * In the future this directory will be cleaned to remove gnu stuff and renamed something like 'src/random'. * This way nrn will have an external random library, and a clean hoc_interface.
- Loading branch information
Nicolas Cornu
authored
Jan 27, 2024
1 parent
f64b609
commit 08920e7
Showing
29 changed files
with
286 additions
and
267 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
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,20 @@ | ||
#include "Isaac64RNG.hpp" | ||
|
||
uint32_t Isaac64::cnt_ = 0; | ||
|
||
Isaac64::Isaac64(std::uint32_t seed) { | ||
if (cnt_ == 0) { | ||
cnt_ = 0xffffffff; | ||
} | ||
--cnt_; | ||
seed_ = seed; | ||
if (seed_ == 0) { | ||
seed_ = cnt_; | ||
} | ||
rng_ = nrnisaac_new(); | ||
reset(); | ||
} | ||
|
||
Isaac64::~Isaac64() { | ||
nrnisaac_delete(rng_); | ||
} |
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,33 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "RNG.h" | ||
#include "nrnisaac.h" | ||
|
||
class Isaac64: public RNG { | ||
public: | ||
Isaac64(std::uint32_t seed = 0); | ||
~Isaac64(); | ||
std::uint32_t asLong() { | ||
return nrnisaac_uint32_pick(rng_); | ||
} | ||
void reset() { | ||
nrnisaac_init(rng_, seed_); | ||
} | ||
double asDouble() { | ||
return nrnisaac_dbl_pick(rng_); | ||
} | ||
std::uint32_t seed() { | ||
return seed_; | ||
} | ||
void seed(std::uint32_t s) { | ||
seed_ = s; | ||
reset(); | ||
} | ||
|
||
private: | ||
std::uint32_t seed_; | ||
void* rng_; | ||
static std::uint32_t cnt_; | ||
}; |
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,15 @@ | ||
#include "MCellRan4RNG.hpp" | ||
|
||
MCellRan4::MCellRan4(std::uint32_t ihigh, std::uint32_t ilow) { | ||
++cnt_; | ||
ilow_ = ilow; | ||
ihigh_ = ihigh; | ||
if (ihigh_ == 0) { | ||
ihigh_ = cnt_; | ||
ihigh_ = (std::uint32_t) asLong(); | ||
} | ||
orig_ = ihigh_; | ||
} | ||
MCellRan4::~MCellRan4() {} | ||
|
||
std::uint32_t MCellRan4::cnt_ = 0; |
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,34 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "RNG.h" | ||
#include "mcran4.h" | ||
|
||
// The decision that has to be made is whether each generator instance | ||
// should have its own seed or only one seed for all. We choose separate | ||
// seed for each but if arg not present or 0 then seed chosen by system. | ||
|
||
// the addition of ilow > 0 means that value is used for the lowindex | ||
// instead of the mcell_ran4_init global 32 bit lowindex. | ||
|
||
class MCellRan4: public RNG { | ||
public: | ||
MCellRan4(std::uint32_t ihigh = 0, std::uint32_t ilow = 0); | ||
virtual ~MCellRan4(); | ||
virtual std::uint32_t asLong() { | ||
return (std::uint32_t) (ilow_ == 0 ? mcell_iran4(&ihigh_) : nrnRan4int(&ihigh_, ilow_)); | ||
} | ||
virtual void reset() { | ||
ihigh_ = orig_; | ||
} | ||
virtual double asDouble() { | ||
return (ilow_ == 0 ? mcell_ran4a(&ihigh_) : nrnRan4dbl(&ihigh_, ilow_)); | ||
} | ||
std::uint32_t ihigh_; | ||
std::uint32_t orig_; | ||
std::uint32_t ilow_; | ||
|
||
private: | ||
static std::uint32_t cnt_; | ||
}; |
Empty file.
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,28 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "nrnran123.h" | ||
#include "RNG.h" | ||
|
||
class NrnRandom123: public RNG { | ||
public: | ||
NrnRandom123(std::uint32_t id1, std::uint32_t id2, std::uint32_t id3 = 0); | ||
~NrnRandom123(); | ||
std::uint32_t asLong() { | ||
return nrnran123_ipick(s_); | ||
} | ||
double asDouble() { | ||
return nrnran123_dblpick(s_); | ||
} | ||
void reset() { | ||
nrnran123_setseq(s_, 0, 0); | ||
} | ||
nrnran123_State* s_; | ||
}; | ||
NrnRandom123::NrnRandom123(std::uint32_t id1, std::uint32_t id2, std::uint32_t id3) { | ||
s_ = nrnran123_newstream3(id1, id2, id3); | ||
} | ||
NrnRandom123::~NrnRandom123() { | ||
nrnran123_deletestream(s_); | ||
} |
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,19 @@ | ||
#include "Rand.hpp" | ||
|
||
#include "ACG.h" | ||
#include "Normal.h" | ||
|
||
Rand::Rand(unsigned long seed, int size, Object* obj) { | ||
// printf("Rand\n"); | ||
gen = new ACG(seed, size); | ||
rand = new Normal(0., 1., gen); | ||
type_ = 0; | ||
obj_ = obj; | ||
} | ||
|
||
Rand::~Rand() { | ||
// printf("~Rand\n"); | ||
delete gen; | ||
delete rand; | ||
} | ||
|
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 |
---|---|---|
@@ -1,20 +1,24 @@ | ||
#ifndef random1_h | ||
#define random1_h | ||
#pragma once | ||
|
||
#include "RNG.h" | ||
#include "Random.h" | ||
|
||
struct Object; | ||
|
||
/* type_: | ||
* 0: ACG | ||
* 1: MLCG | ||
* 2: MCellRan4 | ||
* 3: Isaac64 | ||
* 4: Random123 | ||
*/ | ||
class Rand { | ||
public: | ||
Rand(unsigned long seed = 0, int size = 55, Object* obj = NULL); | ||
Rand(unsigned long seed = 0, int size = 55, Object* obj = nullptr); | ||
~Rand(); | ||
RNG* gen; | ||
Random* rand; | ||
int type_; // can do special things with some kinds of RNG | ||
// double* looks like random variable that gets changed on every fadvance | ||
Object* obj_; | ||
}; | ||
|
||
#endif |
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,25 @@ | ||
#include <cstdint> | ||
#include "nrnisaac.h" | ||
#include "isaac64.h" | ||
|
||
using RNG = struct isaac64_state; | ||
|
||
void* nrnisaac_new() { | ||
return new RNG; | ||
} | ||
|
||
void nrnisaac_delete(void* v) { | ||
delete static_cast<RNG*>(v); | ||
} | ||
|
||
void nrnisaac_init(void* v, unsigned long int seed) { | ||
isaac64_init(static_cast<RNG*>(v), seed); | ||
} | ||
|
||
double nrnisaac_dbl_pick(void* v) { | ||
return isaac64_dbl32(static_cast<RNG*>(v)); | ||
} | ||
|
||
std::uint32_t nrnisaac_uint32_pick(void* v) { | ||
return isaac64_uint32(static_cast<RNG*>(v)); | ||
} |
Oops, something went wrong.