-
Notifications
You must be signed in to change notification settings - Fork 0
/
laplace_upcpp.cpp
299 lines (215 loc) · 9.29 KB
/
laplace_upcpp.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*************************************************
* Laplace Serial C Version
*
* Temperature is initially 0.0
* Boundaries are as follows:
*
* 0 T 0
* 0 +-------------------+ 0
* | |
* | |
* | |
* T | | T
* | |
* | |
* | |
* 0 +-------------------+ 100
* 0 T 100
*
* John Urbanic, PSC 2014
*
************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <upcxx/upcxx.hpp>
// size of plate
#define COLUMNS 1000
#define ROWS 1000
// largest permitted change in temp (This value takes about 3400 steps)
#define MAX_TEMP_ERROR 0.01
/*
double Temperature[ROWS+2][COLUMNS+2]; // temperature grid
double Temperature_last[ROWS+2][COLUMNS+2]; // temperature grid from last iteration
*/
// helper routines
void initialize(double * local_temp);
void track_progress(int iter);
int main(int argc, char *argv[]) {
upcxx::init();
int i, j; // grid indexes
int max_iterations; // number of iterations
int iteration=1; // current iteration
double dt=100; // largest change in t
struct timeval start_time, stop_time, elapsed_time; // timers
gettimeofday(&start_time,NULL); // Unix timer
max_iterations = atoi(argv[1]);
int rank;
int num_of_procs = upcxx::rank_n();
rank = upcxx::rank_me();
std::cout<< "iterations: " << max_iterations << " ranks: " << num_of_procs << " my_rank: " << rank << std::endl;
int total_size = (COLUMNS+2) * (ROWS+2);
int size_per_proc = total_size / num_of_procs;
upcxx::dist_object<upcxx::global_ptr<double>> Temprature_last (upcxx::new_array<double>(total_size));
double * local_temp = Temprature_last->local();
initialize(local_temp);
upcxx::barrier();
upcxx::global_ptr<double> temp_rank_prev = nullptr, temp_rank_next = nullptr;
double Temperature_new [ROWS+2][COLUMNS+2];
while ( dt > MAX_TEMP_ERROR && iteration <= max_iterations ) {
printf("iteration %d\n", iteration);
double *temp;
int first_row_index = ((ROWS+2)/num_of_procs)*upcxx::rank_me();
int last_row_index = (( (ROWS+2)/num_of_procs)*upcxx::rank_me()+1) -1;
// main calculation: average my four neighbors
for(i = first_row_index+1; i <= last_row_index-1 ; i++) {
for(j = 1; j <= COLUMNS; j++) {
//temp = upcxx::rget(local_temp).wait();
temp = local_temp;
Temperature_new[i][j] = 0.25 * (temp[(i + 1) * COLUMNS + j] + temp[(i-1) * COLUMNS + j]+
temp[i * COLUMNS + j + 1] + temp[i * COLUMNS + j - 1]);
}
}
if( rank == 0 ){
temp_rank_next = Temprature_last.fetch(upcxx::rank_me() + 1).wait();
//temp = upcxx::rget(temp_rank_next).wait();
temp = temp_rank_next.local();
i = last_row_index;
for(j = 1; j <= COLUMNS; j++)
Temperature_new[i][j] = 0.25 * (temp[(i + 1) * COLUMNS + j] + temp[(i-1) * COLUMNS + j]+
temp[i * COLUMNS + j + 1] + temp[i * COLUMNS + j - 1]);
}else if( rank == upcxx::rank_n() - 1){
temp_rank_prev = Temprature_last.fetch(upcxx::rank_me() - 1).wait();
//temp = upcxx::rget(temp_rank_prev).wait();
temp = temp_rank_prev.local();
i = first_row_index;
for(j = 1; j <= COLUMNS; j++)
Temperature_new[i][j] = 0.25 * (temp[(i + 1) * COLUMNS + j] + temp[(i-1) * COLUMNS + j]+
temp[i * COLUMNS + j + 1] + temp[i * COLUMNS + j - 1]);
}else{
temp_rank_prev = Temprature_last.fetch(upcxx::rank_me() - 1).wait();
temp_rank_next = Temprature_last.fetch(upcxx::rank_me() + 1).wait();
//temp = upcxx::rget(temp_rank_prev).wait();
temp = temp_rank_prev.local();
i = first_row_index;
for(j = 1; j <= COLUMNS; j++)
Temperature_new[i][j] = 0.25 * (temp[(i + 1) * COLUMNS + j] + temp[(i-1) * COLUMNS + j]+
temp[i * COLUMNS + j + 1] + temp[i * COLUMNS + j - 1]);
//temp = upcxx::rget(temp_rank_next).wait();
temp = temp_rank_next.local();
i = last_row_index;
for(j = 1; j <= COLUMNS; j++)
Temperature_new[i][j] = 0.25 * (temp[(i + 1) * COLUMNS + j] + temp[(i-1) * COLUMNS + j]+
temp[i * COLUMNS + j + 1] + temp[i * COLUMNS + j - 1]);
}
//temp = upcxx::rget(local_temp).wait();
temp = local_temp;
// main calculation: average my four neighbors
for(i = first_row_index; i <= last_row_index ; i++) {
for(j = 1; j <= COLUMNS; j++) {
if(i==1 || i==ROWS) continue;
dt = fmax( fabs(Temperature_new[i][j] - temp[i*COLUMNS + j]), dt);
temp[i * COLUMNS + j] = Temperature_new[i][j];
}
}
upcxx::barrier();
double max_dt = upcxx::reduce_all(dt, upcxx::op_fast_max).wait();
dt = max_dt;
iteration++;
}
if(rank == 0){
gettimeofday(&stop_time,NULL);
timersub(&stop_time, &start_time, &elapsed_time); // Unix time subtract routine
printf("\nMax error at iteration %d was %f\n", iteration-1, dt);
printf("Total time was %f seconds.\n", elapsed_time.tv_sec+elapsed_time.tv_usec/1000000.0);
}
/*
printf("Maximum iterations [100-4000]?\n");
scanf("%d", &max_iterations);
gettimeofday(&start_time,NULL); // Unix timer
initialize(); // initialize Temp_last including boundary conditions
// do until error is minimal or until max steps
while ( dt > MAX_TEMP_ERROR && iteration <= max_iterations ) {
// main calculation: average my four neighbors
for(i = 1; i <= ROWS; i++) {
for(j = 1; j <= COLUMNS; j++) {
Temperature[i][j] = 0.25 * (Temperature_last[i+1][j] + Temperature_last[i-1][j] +
Temperature_last[i][j+1] + Temperature_last[i][j-1]);
}
}
dt = 0.0; // reset largest temperature change
// copy grid to old grid for next iteration and find latest dt
for(i = 1; i <= ROWS; i++){
for(j = 1; j <= COLUMNS; j++){
dt = fmax( fabs(Temperature[i][j]-Temperature_last[i][j]), dt);
Temperature_last[i][j] = Temperature[i][j];
}
}
// periodically print test values
if((iteration % 100) == 0) {
track_progress(iteration);
}
iteration++;
}
gettimeofday(&stop_time,NULL);
timersub(&stop_time, &start_time, &elapsed_time); // Unix time subtract routine
printf("\nMax error at iteration %d was %f\n", iteration-1, dt);
printf("Total time was %f seconds.\n", elapsed_time.tv_sec+elapsed_time.tv_usec/1000000.0);
}
*/
upcxx::finalize();
}
/* OLD CODE FOR INIT
// initialize plate and boundary conditions
// Temp_last is used to to start first iteration
void initialize(){
int i,j;
for(i = 0; i <= ROWS+1; i++){
for (j = 0; j <= COLUMNS+1; j++){
Temperature_last[i][j] = 0.0;
}
}
// these boundary conditions never change throughout run
// set left side to 0 and right to a linear increase
for(i = 0; i <= ROWS+1; i++) {
Temperature_last[i][0] = 0.0;
Temperature_last[i][COLUMNS+1] = (100.0/ROWS)*i;
}
// set top to 0 and bottom to linear increase
for(j = 0; j <= COLUMNS+1; j++) {
Temperature_last[0][j] = 0.0;
Temperature_last[ROWS+1][j] = (100.0/COLUMNS)*j;
}
}
*/
void initialize(double *local_temp){
int i,j;
for(i = 0; i <= ROWS+1; i++){
for (j = 0; j <= COLUMNS+1; j++){
local_temp[i * COLUMNS + j] = 0.0;
}
}
// these boundary conditions never change throughout run
// set left side to 0 and right to a linear increase
for(i = 0; i <= ROWS+1; i++) {
local_temp[i*COLUMNS] = 0.0;
local_temp[i*COLUMNS+ COLUMNS + 1] = (100.0/ROWS)*i;
}
// set top to 0 and bottom to linear increase
for(j = 0; j <= COLUMNS+1; j++) {
local_temp[j] = 0.0;
local_temp[(ROWS+1) * COLUMNS + j] = (100.0/COLUMNS)*j;
}
}
/*
// print diagonal in bottom right corner where most action is
void track_progress(int iteration) {
int i;
printf("---------- Iteration number: %d ------------\n", iteration);
for(i = ROWS-5; i <= ROWS; i++) {
printf("[%d,%d]: %5.2f ", i, i, Temperature[i][i]);
}
printf("\n");
}
*/