-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmpi_random.cc
More file actions
29 lines (28 loc) · 1003 Bytes
/
Copy pathmpi_random.cc
File metadata and controls
29 lines (28 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "mpi_random.hh"
/** @file Implementation of mpi_random.hh */
/* Seed random number generator */
void parallel_mt19937_64::seed(result_type value) {
unsigned int n_rank = mpi_comm_size(); // Number of processors
unsigned int *master_seed_list = nullptr;
unsigned int local_seed; // Local seed
typedef std::linear_congruential_engine<unsigned int, 48271, 0, 2147483647>
SeedEngine;
// Generate seeds on master processor
if (mpi_master()) {
SeedEngine seed_engine;
seed_engine.seed(value);
std::set<SeedEngine::result_type> seed_set;
seed_set.insert(value);
while (seed_set.size() < n_rank) {
seed_set.insert(seed_engine());
}
master_seed_list = (unsigned int *)malloc(n_rank * sizeof(unsigned int));
std::copy(seed_set.begin(), seed_set.end(), master_seed_list);
}
// Distribute seeds to all processes
mpi_scatter(master_seed_list, local_seed);
if (mpi_master()) {
free(master_seed_list);
}
base_engine.seed(local_seed);
}