-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.cpp
executable file
·300 lines (217 loc) · 9.52 KB
/
engine.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
#include "engine.h"
#include "UVZSolver_multithread.h"
#include "support_funcs.h"
#include "sediment_transport.h"
#include "loader.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void synch_and_check(){
cudaDeviceSynchronize();
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
cout << "error: " << cudaGetErrorString(err) << endl;
assert(err == cudaSuccess);
}
void update_boundary_at_t(int M, int N, float t, bool channel, int total_time, Argument_Pointers* d_arg_ptr, Constant_Coeffs* coeffs)
{
if (channel){
// int offset = M + 3;
// boundary_up(t, d_arg_ptr, coeffs);
// boundary_down(t, d_arg_ptr, coeffs);
// boundary_left(t, d_arg_ptr, coeffs);
// boundary_right(t, d_arg_ptr, coeffs);
} else{
dim3 grid(1, max(M, N) / 1024 + 1);
dim3 block(1, 1024);
Update_Boundary_Value<<<grid, block>>>(t, total_time, d_arg_ptr);
}
}
void Hydraulic_Calculation(DOUBLE dT, DOUBLE NANGDAY, Argument_Pointers* d_arg_ptr, Array_Pointers* d_arr_ptr,
Constant_Coeffs* coeffs, Argument_Pointers h_arg_pointer, Options ops){
// note: blocksize in this case is fixed to be 1024 threads, can change later
int blocksize = 1024;
int M = ops.M;
int N = ops.N;
int M1 = M + 3;
int N1 = N + 3;
dim3 block_2d(min(blocksize, M1), 1, 1);
dim3 grid_2d((int) ceil((DOUBLE)(M1) / min(blocksize, M1)), N1, 1) ;
dim3 block_shape;
dim3 grid_shape;
int start_idx, end_idx, jump_step;
bool isU;
bool channel = ops.channel;
DOUBLE t = ops.t_start;
int Tmax = ops.Tmax;
// DOUBLE Tmax = 0.5;
cout << "t = " << t << " sediment start = " << ops.sediment_start << endl;
while (t < Tmax){
t += 0.5 * dT;
cout << t << endl;
// update boundary conditionmake
update_boundary_at_t(M, N, t, channel, ops.total_time, d_arg_ptr, coeffs);
synch_and_check();
// set start/ end index for kernels
start_idx = 2;
end_idx = M;
jump_step = 2;
if ((channel) && (ops.kenhhepd)){
start_idx = 3;
end_idx = M - 1;
}
isU = true;
jump_step = 2;
// set block size
block_shape = dim3(1, 512, 1);
grid_shape = dim3(M1, 1, 1);
UZSolver_calculate_preindex <<<grid_shape, block_shape>>> (start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
UZSolver_calculate_abcd<<<grid_shape, block_shape>>>(start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
UZSolver_calculate_matrix_coeff<<<grid_shape, block_shape>>>(start_idx, end_idx, NANGDAY, d_arg_ptr, d_arr_ptr);
synch_and_check();
dim3 grid(1, M - 1, 1);
//tridiagSolver_v2<<<1, M - 1>>> (false,isU, start_idx, end_idx, jump_step, 2 * N + 1, d_arg_ptr, d_arr_ptr);
tridiagSolver<<<grid, 32>>> (false,isU, start_idx, end_idx, jump_step, 2 * N + 1, d_arg_ptr, d_arr_ptr);
synch_and_check();
UZSolver_extract_solution <<<grid_shape, block_shape>>>(start_idx, end_idx, NANGDAY, d_arg_ptr, d_arr_ptr);
synch_and_check();
Normalize<<<grid_2d, block_2d>>> (isU,d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
update_buffer <<<grid_2d, block_2d>>>(isU, d_arg_ptr, d_arr_ptr);
synch_and_check();
solveV <<<grid_shape, block_shape>>>(t, 2, N, d_arg_ptr, coeffs);
synch_and_check();
grid = dim3(1, N, 1);
update_margin_elem_V<<<grid, 32>>> (2, N, NANGDAY, d_arg_ptr);
synch_and_check();
// note that isU here is false since it normalize value of v after solving for v
Normalize<<<grid_2d, block_2d>>> (false,d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
update_buffer <<<grid_2d, block_2d>>>(false, d_arg_ptr, d_arr_ptr);
synch_and_check();
update_h_moi <<<grid_2d, block_2d>>> (d_arg_ptr);
synch_and_check();
update_uvz <<<grid_2d, block_2d>>> (d_arg_ptr, coeffs);
synch_and_check();
grid = dim3(1, N, 1);
Find_Calculation_limits_Horizontal <<<grid, 32>>> (d_arg_ptr, coeffs);
grid = dim3(1, M, 1);
Find_Calculation_limits_Vertical <<<grid, 32>>>(d_arg_ptr, coeffs);
Htuongdoi <<<grid_2d, block_2d>>> (d_arg_ptr);
synch_and_check();
// get result from device here and check
if ( ((int) t % ops.interval == 0) && (t - (int) t == 0))
save_result(h_arg_pointer, (int) t);
// sediment transport simulation condition start here
if (t >= ops.sediment_start){
Find_VTH<<<grid_2d, block_2d>>>(coeffs, d_arg_ptr);
hesoK<<<grid_2d, block_2d>>> (coeffs, d_arg_ptr);
synch_and_check();
start_idx = 3;
end_idx = M - 1;
Scan_FSj<<<grid_shape, block_shape>>>(t, ops.bed_change_start, ops.ketdinh, start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
// Tridiag
jump_step = 1;
grid= dim3(1, M - 1 , 1);
tridiagSolver<<<grid, 32>>>(false, isU, start_idx,
end_idx, jump_step, N + 3,
d_arg_ptr, d_arr_ptr);
synch_and_check();
// # Extract Solution
FSj_extract_solution<<<grid_shape, block_shape>>>(ops.ketdinh, start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
Update_FS<<<grid_2d, block_2d>>>(d_arg_ptr);
}
// second half of the simulation
t += dT * 0.5;
update_boundary_at_t(M, N, t, channel, ops.total_time, d_arg_ptr, coeffs);
synch_and_check();
block_shape = dim3(512, 1, 1) ;
grid_shape = dim3((int) (ceil(M / 1024.0)), N, 1);
start_idx = 2;
end_idx = N;
jump_step = 2;
isU = false;
if ((ops.channel) && (ops.kenhhepng)){
start_idx = 3;
end_idx = N;
}
VZSolver_calculate_preindex <<<grid_shape,block_shape>>> (start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
VZSolver_calculate_abcd<<<grid_shape,block_shape>>> (start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
VZSolver_calculate_matrix_coeff<<<grid_shape,block_shape>>> (start_idx, end_idx, NANGDAY, d_arg_ptr, d_arr_ptr);
synch_and_check();
grid = dim3(1, N-1, 1);
tridiagSolver<<<grid, 32>>> (false, isU, start_idx, end_idx, jump_step, 2 * M + 1, d_arg_ptr, d_arr_ptr);
synch_and_check();
VZSolver_extract_solution<<<grid_shape,block_shape>>> (start_idx, end_idx, NANGDAY, d_arg_ptr, d_arr_ptr);
synch_and_check();
Normalize<<<grid_2d, block_2d>>> (isU, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
update_buffer<<<grid_2d, block_2d>>> (isU, d_arg_ptr, d_arr_ptr);
synch_and_check();
solveU<<<grid_2d, block_2d>>> (t, 2, M, d_arg_ptr, coeffs);
synch_and_check();
grid = dim3(1, M, 1);
update_margin_elem_U<<<grid,32>>> (2, M, NANGDAY, d_arg_ptr);
synch_and_check();
// similar to first haft, isU here is true, since it normalize u value after solving for u
Normalize<<<grid_2d, block_2d>>> (true, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
update_buffer <<<grid_2d, block_2d>>>(true, d_arg_ptr, d_arr_ptr );
synch_and_check();
update_h_moi <<<grid_2d, block_2d>>> (d_arg_ptr);
synch_and_check();
grid = dim3(M, 1, 1);
dim3 block(1, 32, 1);
Reset_states_vertical <<<grid, block>>> (d_arg_ptr, coeffs);
synch_and_check();
if ( ((int) t % ops.interval == 0) && (t - (int) t == 0))
save_result(h_arg_pointer, (int) t);
update_uvz <<<grid_2d, block_2d>>> (d_arg_ptr, coeffs);
synch_and_check();
grid = dim3(1, N, 1);
Find_Calculation_limits_Horizontal <<<grid, 32>>> (d_arg_ptr, coeffs);
grid = dim3(1, M, 1);
Find_Calculation_limits_Vertical <<<grid, 32>>>(d_arg_ptr, coeffs);
Htuongdoi <<<grid_2d, block_2d>>> (d_arg_ptr);
synch_and_check();
if (t >= ops.sediment_start){
Find_VTH<<<grid_shape, block_shape>>>(coeffs, d_arg_ptr);
hesoK<<<grid_shape, block_shape>>>(coeffs, d_arg_ptr);
synch_and_check();
// # sediment kernels come here
// # Scan FSj
start_idx = 3;
end_idx = N - 1;
Scan_FSi<<<grid_shape, block_shape>>>(t, ops.bed_change_start, ops.ketdinh, start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
jump_step = 1;
// # Tridiag
grid = dim3(1, N-3, 1);
tridiagSolver<<<grid, 32>>>(false, isU, start_idx,
end_idx, jump_step, M + 3,
d_arg_ptr, d_arr_ptr);
synch_and_check();
// # Extract Solution
FSi_extract_solution<<<grid_shape, block_shape>>>(ops.ketdinh, start_idx, end_idx, d_arg_ptr, d_arr_ptr, coeffs);
synch_and_check();
Update_FS<<<grid_2d, block_2d>>>(d_arg_ptr);
synch_and_check();
if ( ((int) t % 360 == 0) && (t - (int) t == 0)){
// note: ask prof Bay about ros coefficient, ros here should be the same with the one in constant coeffs
DOUBLE ros = 2000;
save_FS(h_arg_pointer, (int) t, ros);
}
}
// only here is new. Verify if this work
// if int(t) % bed_change_start == 0 and t - int(t) == 0:
// Calculate_Qb(ketdinh, arg_struct_ptr, arr_struct_ptr, block=block_2d, grid=grid_2d)
// BedLoad(floattype(t), ketdinh, start_idx, end_idx, arg_struct_ptr, arr_struct_ptr, block=block_2d, grid=grid_2d)
}
}