forked from davidzarruk/Parallel_Computing
-
Notifications
You must be signed in to change notification settings - Fork 9
/
MPI_main.cpp
277 lines (207 loc) · 6.75 KB
/
MPI_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// Created by David Zarruk Valencia on June, 2016.
// Copyright (c) 2016 David Zarruk Valencia. All rights reserved.
//
#include <iostream>
#include <nlopt.hpp>
#include <math.h>
#include <mpi.h>
using namespace std;
//======================================
// Grids
//======================================
void gridx(const int nx, const float xmin, const float xmax, float* xgrid){
const float size = nx;
const float xstep = (xmax - xmin) /(size - 1);
float it = 0;
for(int i = 0; i < nx; i++){
xgrid[i] = xmin + it*xstep;
it++;
}
}
void gride(const int ne, const float ssigma_eps, const float llambda_eps, const float m, float* egrid){
// This grid is made with Tauchen (1986)
const float size = ne;
const float ssigma_y = sqrt(pow(ssigma_eps, 2) / (1 - pow(llambda_eps, 2)));
const float estep = 2*ssigma_y*m / (size-1);
float it = 0;
for(int i = 0; i < ne; i++){
egrid[i] = (-m*sqrt(pow(ssigma_eps, 2) / (1 - pow(llambda_eps, 2))) + it*estep);
it++;
}
}
float normCDF(const float value){
return 0.5 * erfc(-value * M_SQRT1_2);
}
void eprob(const int ne, const float ssigma_eps, const float llambda_eps, const float m, const float* egrid, float* P){
// This grid is made with Tauchen (1986)
// P is: first ne elements are transition from e_0 to e_i,
// second ne elementrs are from e_1 to e_i, ...
const float w = egrid[1] - egrid[0];
for(int j = 0; j < ne; j++){
for(int k = 0; k < ne; k++){
if(k == 0){
P[j*ne + k] = normCDF((egrid[k] - llambda_eps*egrid[j] + (w/2))/ssigma_eps);
} else if(k == ne-1){
P[j*ne + k] = 1 - normCDF((egrid[k] - llambda_eps*egrid[j] - (w/2))/ssigma_eps);
} else{
P[j*ne + k] = normCDF((egrid[k] - llambda_eps*egrid[j] + (w/2))/ssigma_eps) - normCDF((egrid[k] - llambda_eps*egrid[j] - (w/2))/ssigma_eps);
}
}
}
}
//======================================
// MAIN MAIN MAIN
//======================================
int main()
{
//--------------------------------//
// MPI //
//--------------------------------//
// Thread id and number of threads
int tid,nthreads;
// Initialize MPI
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &tid);
MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
//--------------------------------//
// Initialization //
//--------------------------------//
// Grid for x
const int nx = 300;
const float xmin = 0.1;
const float xmax = 4.0;
// Grid for e
const int ne = 15;
const float ssigma_eps = 0.02058;
const float llambda_eps = 0.99;
const float m = 1.5;
// Utility function
const float ssigma = 2;
const float eeta = 0.36;
const float ppsi = 0.89;
const float rrho = 0.5;
const float llambda = 1;
const float bbeta = 0.97;
const int T = 10;
// Prices
const float r = 0.07;
const float w = 5;
// Initialize the grid for X
float xgrid[nx];
// Initialize the grid for E and the probability matrix
float egrid[ne];
float P[ne*ne];
//--------------------------------//
// Grid creation //
//--------------------------------//
gridx(nx, xmin, xmax, xgrid);
gride(ne, ssigma_eps, llambda_eps, m, egrid);
eprob(ne, ssigma_eps, llambda_eps, m, egrid, P);
// Exponential of the grid e
for(int i=0; i<ne; i++){
egrid[i] = exp(egrid[i]);
}
// Loop limits of parallelization
int loop_min = (int)((tid + 0) * ceil((float)(nx*ne)/nthreads));
int loop_max = (int)((tid + 1) * ceil((float)(nx*ne)/nthreads));
if(ne*nx < loop_max){
loop_max = ne*nx;
}
int leng = (loop_max - loop_min);
// Total Value function
size_t sizeVal = T*ne*nx*sizeof(float);
float *Val;
Val = (float *)malloc(sizeVal);
// Value function at every age t+1
size_t sizeVal1 = ne*nx*sizeof(float);
float *Val1;
Val1 = (float *)malloc(sizeVal1);
// Value function at every age t, computed by every processor
size_t sizeValp = leng*sizeof(float);
float *Valp;
Valp = (float *)malloc(sizeValp);
float expected;
float utility;
float cons;
float VV;
int ix;
int ie;
int iter;
// Parameters for the gathering (MPI_Gatherv) of value function after each iteration
int displs[nthreads],rcounts[nthreads];
for(int i = 0; i < nthreads; i++){
displs[i] = i*leng;
rcounts[i] = leng;
}
//--------------------------------//
// Life-cycle computation //
//--------------------------------//
if(tid == 0){
cout << " " << endl;
cout << "Life cycle computation: " << endl;
cout << " " << endl;
}
double t0 = MPI_Wtime();
double t = t0;
// Compute value function bakwards
for(int age=T-1; age>=0; age--){
// Synchronize
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(Val, (T*ne*nx), MPI_FLOAT, 0, MPI_COMM_WORLD);
iter = 0;
for(int ind = loop_min; ind < loop_max; ind++){
ix = floor(ind/ne);
ie = ind % ne;
VV = pow(-10, 5);
for(int ixp = 0; ixp < nx; ixp++){
expected = 0.0;
if(age < T-1){
for(int iep = 0; iep < ne; iep++){
expected = expected + P[ie*ne + iep]*Val[(age+1)*nx*ne + ixp*ne + iep];
}
}
cons = (1 + r)*xgrid[ix] + egrid[ie]*w - xgrid[ixp];
utility = pow(cons, 1-ssigma) / (1-ssigma) + bbeta*expected;
if(cons <= 0){
utility = pow(-10.0, 5.0);
}
if(utility >= VV){
VV = utility;
}
}
Valp[iter] = VV;
iter = iter + 1;
}
MPI_Gatherv(Valp, leng, MPI_FLOAT, Val1, rcounts, displs, MPI_FLOAT, 0, MPI_COMM_WORLD);
if(tid == 0){
for(int ind = 0; ind < (nx*ne); ind++){
ix = floor(ind/ne);
ie = ind % ne;
Val[age*nx*ne + ix*ne + ie] = Val1[ix*ne + ie];
}
t = MPI_Wtime() - t0;
cout << "Age: " << age << ". Time: " << 1000000*((float)t)/CLOCKS_PER_SEC << " seconds." << endl;
}
}
MPI_Finalize();
if(tid == 0){
std::cout << " " << std::endl;
t = MPI_Wtime() - t0;
std::cout << "TOTAL ELAPSED TIME: " << 1000000*((float)t)/CLOCKS_PER_SEC << " seconds. " << std::endl;
}
// //--------------------------------//
// // Some checks //
// //--------------------------------//
if(tid == 0){
std::cout << " " << std::endl;
std::cout << " - - - - - - - - - - - - - - - - - - - - - " << std::endl;
std::cout << " " << std::endl;
std::cout << "The first entries of the value function: " << std::endl;
std::cout << " " << std::endl;
for(int i = 0; i<3; i++){
std::cout << Val[i] << std::endl;
}
}
return 0;
}