-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Joeperdefloep
committed
Jun 3, 2022
1 parent
603cd9d
commit e96a122
Showing
14 changed files
with
5,454 additions
and
3,847 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <richdem/common/random.hpp> | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <cstdint> | ||
#include <functional> | ||
#include <iostream> | ||
#include <limits> | ||
#include <random> | ||
#include <sstream> | ||
|
||
namespace richdem { | ||
|
||
our_random_engine& rand_engine(){ | ||
static our_random_engine e[PRNG_THREAD_MAX]; | ||
return e[omp_get_thread_num()]; | ||
} | ||
|
||
|
||
//Be sure to read: http://www.pcg-random.org/posts/cpp-seeding-surprises.html | ||
//and http://www.pcg-random.org/posts/cpps-random_device.html | ||
void seed_rand(unsigned long seed){ | ||
#pragma omp parallel //All threads must come here | ||
{ | ||
#pragma omp critical //But only one at a time | ||
if(seed==0){ | ||
std::uint_least32_t seed_data[std::mt19937::state_size]; | ||
std::random_device r; | ||
std::generate_n(seed_data, std::mt19937::state_size, std::ref(r)); | ||
std::seed_seq q(std::begin(seed_data), std::end(seed_data)); | ||
rand_engine().seed(q); | ||
} else | ||
rand_engine().seed( seed*omp_get_thread_num() ); | ||
} | ||
} | ||
|
||
|
||
int uniform_rand_int(int from, int thru){ | ||
static std::uniform_int_distribution<> d[PRNG_THREAD_MAX]; | ||
using parm_t = std::uniform_int_distribution<>::param_type; | ||
return d[omp_get_thread_num()]( rand_engine(), parm_t{from, thru} ); | ||
} | ||
|
||
|
||
double uniform_rand_real(double from, double thru){ | ||
static std::uniform_real_distribution<> d[PRNG_THREAD_MAX]; | ||
using parm_t = std::uniform_real_distribution<>::param_type; | ||
return d[omp_get_thread_num()]( rand_engine(), parm_t{from, thru} ); | ||
} | ||
|
||
|
||
double normal_rand(double mean, double stddev){ | ||
static std::normal_distribution<double> d[PRNG_THREAD_MAX]; | ||
using parm_t = std::normal_distribution<double>::param_type; | ||
return d[omp_get_thread_num()]( rand_engine(), parm_t{mean, stddev} ); | ||
} | ||
|
||
RandomEngineState SaveRandomState(){ | ||
std::ostringstream oss; | ||
oss<<rand_engine(); | ||
return oss.str(); | ||
} | ||
|
||
void SetRandomState(const RandomEngineState &res){ | ||
std::istringstream iss(res); | ||
iss>>rand_engine(); | ||
} | ||
|
||
} |
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,46 @@ | ||
#include <richdem/common/logger.hpp> | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
namespace richdem { | ||
|
||
//TODO: Could use vector for this since the enum just compiles to an int. But | ||
//need to make sure things are in the right order. | ||
std::map<LogFlag, std::string> log_flag_chars_begin = { | ||
{ALG_NAME, "\nA"}, | ||
{CITATION, "C"}, | ||
{CONFIG, "c"}, | ||
{DEBUG, "\033[95md"}, | ||
{ERROR, "E"}, | ||
{MEM_USE, " "}, | ||
{MISC, "m"}, | ||
{PROGRESS, "p"}, | ||
{TIME_USE, "t"}, | ||
{WARN, "\033[91mW"} | ||
}; | ||
|
||
std::map<LogFlag, std::string> log_flag_chars_end = { | ||
{ALG_NAME, ""}, | ||
{CITATION, "\n"}, | ||
{CONFIG, ""}, | ||
{DEBUG, ""}, | ||
{ERROR, ""}, | ||
{MEM_USE, ""}, | ||
{MISC, ""}, | ||
{PROGRESS, ""}, | ||
{TIME_USE, ""}, | ||
{WARN, ""} | ||
}; | ||
|
||
void RDLOGfunc(LogFlag flag, const char* file, const char* func, unsigned line, std::string msg) { | ||
std::cerr<<log_flag_chars_begin.at(flag)<<" "<<msg | ||
#ifdef RICHDEM_DEBUG | ||
<<" ("<<file<<" : "<<func<<" : "<<line<<")" | ||
#endif | ||
<<"\033[39m" | ||
<<log_flag_chars_end.at(flag) | ||
<<std::endl; | ||
} | ||
|
||
} |
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,16 @@ | ||
ncols 10 | ||
nrows 10 | ||
xllcorner 421568 | ||
yllcorner 4872699 | ||
cellsize 3 | ||
NODATA_value 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
2 1 1 1 1 1 1 1 1 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 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,16 @@ | ||
ncols 10 | ||
nrows 10 | ||
xllcorner 421568 | ||
yllcorner 4872699 | ||
cellsize 3 | ||
NODATA_value -1 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 | ||
1 2 3 4 5 6 7 8 9 10 |
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,16 @@ | ||
ncols 10 | ||
nrows 10 | ||
xllcorner 421568 | ||
yllcorner 4872699 | ||
cellsize 3 | ||
NODATA_value 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
1 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 0 0 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,16 @@ | ||
ncols 10 | ||
nrows 10 | ||
xllcorner 421568 | ||
yllcorner 4872699 | ||
cellsize 3 | ||
NODATA_value 0 | ||
0 0 0 0 1 1 1 1 1 1 | ||
0 0 0 1 1 1 1 1 1 1 | ||
0 0 1 1 1 1 1 1 1 1 | ||
0 1 1 1 1 1 1 1 1 1 | ||
2 1 1 1 1 1 1 1 1 1 | ||
0 1 1 1 1 1 1 1 1 1 | ||
0 0 1 1 1 1 1 1 1 1 | ||
0 0 0 1 1 1 1 1 1 1 | ||
0 0 0 0 1 1 1 1 1 1 | ||
0 0 0 0 0 1 1 1 1 1 |
Oops, something went wrong.