-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
127 lines (105 loc) · 3.5 KB
/
main.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <mpi.h>
#include <time.h>
#include <fstream>
using namespace std;
int main ( int argc, char *argv[] );
pair<int, int> random_walks ( int a, int b, int x, double p, int N );
//****************************************************************************80
int main ( int argc, char *argv[] )
{
int a = 0;
int b = 1000;
int x = 300;
pair<int, int> hits;
int hit_total = 0;
int avg_live = 0;
int ierr;
double p = 0.6;
const int master = 0;
double pdf_estimate;
int process_num;
int process_rank;
int N = 100000;
int trial_total;
int seed;
clock_t tStart = clock();
ierr = MPI_Init(&argc, &argv);
if (ierr != 0) {
exit(1);
}
ierr = MPI_Comm_size(MPI_COMM_WORLD, &process_num);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
seed = 123456789 + process_rank * 100;
srand(seed);
if (process_rank == 0) {
cout << "The number of processes is " << process_num << endl;
cout << endl;
cout << "A = " << a << endl;
cout << "B = " << b << endl;
cout << "Start position X = " << x << endl;
cout << "Probability to go right P = " << p << endl;
}
N = 1000;
hits = random_walks(a, b, x, p, N);
ierr = MPI_Reduce(&hits.first, &hit_total, 1, MPI_INT, MPI_SUM, master,
MPI_COMM_WORLD);
ierr = MPI_Reduce(&hits.second, &avg_live, 1, MPI_INT, MPI_SUM, master,
MPI_COMM_WORLD);
if (process_rank == 0) {
trial_total = N * process_num;
avg_live /= trial_total;
pdf_estimate = (double) (hit_total) / (double) (trial_total);
cout << " " << setw(8) << trial_total
<< " " << setw(8) << hit_total
<< " " << setw(8) << pdf_estimate
<< " " << setw(8) << avg_live;
}
if (process_rank == 0) {
cout << endl;
printf("Time taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
ofstream myfile;
myfile.open("output.txt");
myfile << (double) (hit_total) / (double) (N * process_num) << ' ' << avg_live << endl;
myfile.close();
myfile.open("stat.txt");
myfile << a << ' ' << b << endl;
myfile << x << endl;
myfile << p << endl;
myfile << N * process_num << endl;
myfile << (double) (clock() - tStart) / CLOCKS_PER_SEC << endl;
myfile << process_num << endl;
myfile.close();
myfile.open("time_from_p_n=1000.txt", std::ios_base::app);
myfile << process_num << ' ' << (double) (clock() - tStart) / CLOCKS_PER_SEC << endl;
myfile.close();
}
MPI_Finalize();
return 0;
}
//****************************************************************************80
pair<int, int> random_walks(int a, int b, int x, double p, int N)
{
pair<int,int> hits = make_pair(0, 0);
int i;
for (i = 0; i < N; ++i) {
int cur_x = x;
while ((cur_x > a) and (cur_x < b)) {
double cur_rand = ((double) std::rand() / (RAND_MAX));
if (cur_rand < p) { //to right
cur_x += 1;
} else {
cur_x -= 1;
}
hits.second += 1;
}
if (cur_x == b) {
hits.first += 1;
}
}
return hits;
}