-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise3_mpi_samerng.cpp
More file actions
43 lines (37 loc) · 1.32 KB
/
exercise3_mpi_samerng.cpp
File metadata and controls
43 lines (37 loc) · 1.32 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// mpicxx -std=c++11 exercise3_mpi_samerng.cpp -o ex3_mpi_samerng
#include <iostream>
#include <stdlib.h>
#include <mpi.h>
#include <random>
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
std::random_device rd{};
std::vector<unsigned int> seed_data = {114381446, 2427727386, 806223567, 832414962,
4121923627, 1581162203, 2468991901, 3237492387};
std::seed_seq seed_s{seed_data.begin(), seed_data.end()};
std::mt19937 generator{seed_s}; // initialize our Mersenne Twister with the std::seed_seq
std::uniform_real_distribution<double> distribution(0, 1);
// draw a few numbers (identical sequence on every rank)
const int N = 5;
std::vector<double> numbers(N);
for (int i = 0; i < N; ++i) {
numbers[i] = distribution(generator);
}
// print in rank order so output isn't interleaved
for (int r = 0; r < size; ++r) {
if (rank == r) {
std::cout << "Rank " << rank << ": ";
for (int i = 0; i < N; ++i) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl << std::flush;
}
MPI_Barrier(MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}