-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
executable file
·312 lines (249 loc) · 8.46 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
// #include <cuda.h>
// #include <cuda_runtime.h>
#include <vector>
#include "engine.h"
#include "support_funcs.h"
#include "loader.h"
#include <algorithm>
#include <unistd.h>
#include <stdlib.h>
#include <utility>
#include <string>
#include <iostream>
#define DOUBLE double
using namespace std;
// load input
// initialize environment
// launch kernel
int main (int argc, char ** argv){
int M, N, total_time;
vector<DOUBLE> h, hsnham, VISCOIDX, Fw, FS, dH;
vector<DOUBLE> bc_up, bc_down, bc_left, bc_right;
vector<DOUBLE> CC_u, CC_d, CC_l, CC_r;
vector<DOUBLE> u, v, z;
vector<int> khouot, bienQ, boundary_type;
// 1. parse arguments
// a. input directory
/* -f: file
-h: hour
-m: min
-s: second
-p: plot
-d: debug
-v: visualize
-b: sediment calculation begin
-B: bed change calculaition Begin
-e: sediment end
-E: bedchange end
-i : save interval - in minute
*/
string dir;
int hour(1), minute(0), sec(0);
DOUBLE sediment_start(20 * 3600), sediment_end(40 * 3600), bed_change_start(500 * 3600), bed_change_end(1000 * 3600);
int save_interval(60);
DOUBLE t_start(0);
bool kenhhepd(0), kenhhepng(0), cohesive(1);
bool plot(true), visualize(false), debug(false), load_initial_condition(false);
int c;
while ((c = getopt(argc, argv, "f:h:m:s:pdvb:B:e:E:i:cT:kn")) != -1){
switch (c)
{
case('f'):
dir = optarg;
continue;
case ('h'):
hour = (DOUBLE) atoi(optarg);
continue;
case ('m'):
minute = (DOUBLE) atoi(optarg);
continue;
case ('s'):
sec = (DOUBLE) atoi(optarg);
continue;
case ('p'):
plot = true;
continue;
case ('d'):
debug = true;
continue;
case ('v'):
visualize = true;
continue;
case ('b'):
sediment_start = (DOUBLE) atoi(optarg);
continue;
case ('B'):
bed_change_start = (DOUBLE) atoi(optarg);
continue;
case ('e'):
sediment_end = (DOUBLE) atoi(optarg);
continue;
case ('E'):
bed_change_end = (DOUBLE) atoi(optarg);
continue;
case ('i'):
save_interval = atoi(optarg);
continue;
case ('c'):
load_initial_condition = true;
continue;
case ('T'):
t_start = (DOUBLE) atoi(optarg);
continue;
case('k'):
kenhhepd = true;
continue;
case('n'):
kenhhepng = true;
continue;
}
}
DOUBLE Tmax = hour * 3600 + minute * 60 + sec;
Options ops(Tmax, t_start, save_interval, sediment_start, bed_change_start, cohesive, kenhhepng, kenhhepd, debug, plot);
cout << "dir " << dir << endl
<< "hour " << hour << endl
<< "min " << minute << endl
<< "sec " << sec << endl
<< "plot " << plot << endl
<< "debug " << debug << endl
<< "visualize " << visualize << endl
<< "sediment start " << sediment_start << endl
<< "bed_change_start " << bed_change_start << endl
<< "sediment_end " << sediment_end << endl
<< "save_interval " << save_interval << endl
<< "load initial condition " << load_initial_condition << endl;
// // 3. Call initialize functions
// // a. load inputs
// // test this first
// pair<int, int> mesh_size = load_file<DOUBLE>("dosausongLUY.txt", h);
// cout << mesh_size.first << " " << mesh_size.second << endl;
pair <int, int> mesh_size = load_inputs(dir, h, hsnham, VISCOIDX, Fw, bc_up, bc_down, bc_left, bc_right,
CC_u, CC_d, CC_l, CC_r, bienQ, boundary_type, &total_time);
cout << mesh_size.first << " " << mesh_size.second << endl;
// ATENDTION: input files that store 2d meshes must be preprocess so that the coordinates align with python indexing
// i.e bottom left origin must be tranpose, flip and pad to make the padded array in top left origin
N = mesh_size.first - 3;
M = mesh_size.second - 3;
ops.M = M;
ops.N = N;
ops.total_time = total_time;
// b. alocate memory on GPU and transfer data to GPU
// argument pointers struct's pointer on device, this is a pointer to device's memory
Argument_Pointers* d_argument_pointers;
// a copy of argument pointer struct that is stored on the host
// from this struct we can access pointers to arrays on device
Argument_Pointers h_argument_pointers;
Array_Pointers* d_arr_pointers, h_arr_pointers;
// host pointers here
Host_arrays host_ap;
host_ap.M = M;
host_ap.N = N;
host_ap.h = h;
host_ap.hsnham = hsnham;
host_ap.VISCOIDX = VISCOIDX;
host_ap.Fw = Fw;
host_ap.FS = FS;
host_ap.dH = dH;
host_ap.bc_up = bc_up;
host_ap.bc_down = bc_down;
host_ap.bc_left = bc_left;
host_ap.bc_right = bc_right;
host_ap.CC_u = CC_u;
host_ap.CC_d = CC_d;
host_ap.CC_r = CC_r;
host_ap.CC_l = CC_l;
host_ap.u = u;
host_ap.v = v;
host_ap.z = z;
host_ap.khouot = khouot;
host_ap.bienQ = bienQ;
host_ap.boundary_type = boundary_type;
// cout << "checking vector assignment: " << endl
// << "size in GPU : " << host_ap.bienQ.size()
// << " size in CPU: " << bienQ.size() << endl;
// // << "first 2 elem: " << boundary_type[ M * 2 + 100] << " " << boundary_type[M * 2 + 100] << endl
// // << " " << host_ap.boundary_type[M * 2 + 100] << " " << host_ap.boundary_type[M * 2 + 100] << endl;
// bool fail = false;
// for (int i = 0; i < bienQ.size(); i++){
// if (bienQ[i] != 0){
// cout << bienQ[i] << endl;
// if (host_ap.bienQ[i] != bienQ[i]){
// fail = true;
// cout << "test failed"
// << bienQ[i] << " != " << host_ap.bienQ[i] << endl;
// break;
// }
// }
// }
// if (!fail)
// cout << "sucess! " << endl;
DOUBLE* gpu_h;
gpu_h = (DOUBLE*) malloc(sizeof(DOUBLE) * host_ap.h.size());
// test function : attribute_arrays_memory_alloc
h_argument_pointers = attribute_arrays_memory_alloc(0, host_ap, &d_argument_pointers);
// gpu_h: the grid depth map on gou
// cout << host_ap.h.size() << endl;
cudaError_t status = cudaMemcpy((void*) gpu_h, h_argument_pointers.h, sizeof(DOUBLE) * host_ap.h.size(), cudaMemcpyDeviceToHost);
assert(status == cudaSuccess);
h_arr_pointers = supporting_arrays_alloc(M, N, &d_arr_pointers);
// load coefficients used in Device code
Constant_Coeffs h_const_coeffs, *d_const_coeffs;
Load_coeffs (h_const_coeffs);
cudaMalloc((void**) &d_const_coeffs, sizeof(Constant_Coeffs));
status = cudaMemcpy(d_const_coeffs, &h_const_coeffs, sizeof(Constant_Coeffs), cudaMemcpyHostToDevice);
assert(status == cudaSuccess);
// check struct values on GPU:
Constant_Coeffs* coeffs;
coeffs = (Constant_Coeffs*) malloc(sizeof(Constant_Coeffs));
status = cudaMemcpy((void*) coeffs, d_const_coeffs, sizeof(Constant_Coeffs), cudaMemcpyDeviceToHost);
assert(status == cudaSuccess);
// cout << coeffs->dX << " " << coeffs->dY << " " << coeffs->Ks << endl;
// cout << "data transfer Done" << endl;
// check if values on device are the same with values on host, and if we has stored the right pointers
// done
for (int i = 0; i < host_ap.h.size(); i++){
if (gpu_h[i] != host_ap.h[i]){
cout << "failed" << endl
<< "i = " << i << endl
<< "gpu: " << gpu_h[i] << endl
<< "cpu: " << host_ap.h[i] << endl;
}
}
// grid size and block size
dim3 block_2d(min(32, M + 3), 1, 1);
dim3 grid_2d((M + 3) / min(32, M + 3) + 1,N + 3, 1);
// dim3 grid_2d(4, 30, 1);
cout << grid_2d.x << " " << grid_2d.y <<" " << grid_2d.z << " " << endl;
Onetime_init <<<grid_2d, block_2d >>>(d_argument_pointers,d_const_coeffs);
synch_and_check();
int* arr;
arr = (int*) malloc(sizeof(int) * host_ap.h.size());
status = cudaMemcpy((void*) arr, h_argument_pointers.khouot, sizeof(int) * host_ap.h.size(), cudaMemcpyDeviceToHost);
assert(status == cudaSuccess);
ofstream ofs;
ofs.open("Outputs/khouot.txt");
for (int i= 0; i < N + 3; i ++){
for (int j = 0; j < M + 3; j ++){
ofs << arr[i * (M + 3) + j] << " ";
}
ofs << endl;
}
// load initial condition
if (load_initial_condition)
printf("need to write load initial condition here\n");
dim3 grid_size(1, N, 1);
Find_Calculation_limits_Horizontal <<<grid_size, 32>>> (d_argument_pointers, d_const_coeffs);
grid_size = dim3(1, M, 1);
Find_Calculation_limits_Vertical <<<grid_size, 32>>> (d_argument_pointers, d_const_coeffs);
Htuongdoi<<<grid_2d, block_2d>>> (d_argument_pointers);
synch_and_check();
preprocess_data <<<1, 32>>> (d_argument_pointers, d_const_coeffs);
synch_and_check();
// set memory heap limit for new tridiag
size_t size = sizeof(DOUBLE) * (2 * M * N + 4 * max(M, N)) * 7;
cudaDeviceSetLimit(cudaLimitMallocHeapSize, size);
cout << "heap size = " << size << endl;
// enter main loop here
Hydraulic_Calculation(h_const_coeffs.dT, h_const_coeffs.NANGDAY, d_argument_pointers, d_arr_pointers, d_const_coeffs, h_argument_pointers, ops);
return 0;
}