-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsea_main.cpp
More file actions
411 lines (363 loc) · 12.7 KB
/
Copy pathdsea_main.cpp
File metadata and controls
411 lines (363 loc) · 12.7 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Data Streaming for Explicit Algorithms - DSEA
#include <dsea.h>
using namespace std;
// this routine waits for output slots to become free and claims the slots for a block
// todo: move into class
void DS::part_out_ready_wait (int32_t i_part, int32_t i_center) {
for (int32_t j=-order_out;j<order_out+1;j++) {
int32_t k=i_part+j;
if ((k>=0) && (k<=n_part-1)) {
int32_t islot=i_center+j; // memory slot
if (islot<0) islot+=n_store_out;
if (islot>=n_store_out) islot-=n_store_out;
if (stat_mem_out[islot].state==mem_state_free) {
// no waiting required
stat_mem_out[islot].i_part=k;
}
else if (stat_mem_out[islot].state==mem_state_bussy) {
if (i_part==0) {
// at the start of the super cycle, the first part needs all memory slots in state free
// int nwait=0;
while (stat_mem_out[islot].state!=mem_state_free) {
} // wait till slot is free
stat_mem_out[islot].i_part=k;
}
if (j==order_out) {//&&(i_part<n_part-order_out+1)) {
int nwait=0;
while (stat_mem_out[islot].state!=mem_state_free) {
} // wait till slot is free
stat_mem_out[islot].i_part=k;
}
}
}
}
}
// this routine waits till defined part and required neighbours are present in the input buffers
// returns location of part
// todo: move into class
int32_t DS::part_in_present_wait (int32_t i_part, int32_t i_cycle) {
//bool data_present=true;
int32_t ifirst=-1;
int32_t last_test=-1;
for (int32_t j=-order_in;j<order_in+1;j++) {
int32_t k=i_part+j;
// cout << "part_present " << k << endl;
bool k_found=false;
while (k_found==false) {
if ((k>=0) && (k<=n_part-1)) {
for (int32_t i=0;i<n_store_in;i++) {
last_test=i;
if ((stat_mem_in[i].i_part==k)&&(stat_mem_in[i].i_cycle==i_cycle)&&((stat_mem_in[i].state==mem_state_ready)||(stat_mem_in[i].state==mem_state_ready_b)||(stat_mem_in[i].state==mem_state_bussy))) {
k_found=true;
break;
}
}
}
else {
k_found=true;
}
}
if ((k>=0) && (k<=n_part-1) && (k_found==true) && (ifirst==-1) && (k==i_part)) {
ifirst=last_test;
}
}
// cout << endl;
return ifirst;
}
void DS::update_mem_info (int32_t * islot, volatile mem_info * mem, int32_t * i_event) {
if (mem[0].type==mem_type_input) {
for (int i=0;i<16;i++) {
int32_t j=islot[i];
if (j!=-1) {
mem[j].state=mem_state_bussy; // todo: not sure if this is necessary!
mem[j].n_use++;
// check if this is the last usage of this block, if yes add event to wait for
int32_t max_use=1+2*order_in;
// parts at the start and end are used less often
if (mem[j].i_part<order_in) {
// first parts
max_use-=order_in-mem[j].i_part;
// cout << "ia_" << i << " " << j << " " << mem[j].i_part << " " << order_in << endl;
}
else if (n_part-mem[j].i_part-1<order_in) {
// last parts
max_use-=n_part-mem[j].i_part;
// cout << "ib_" << i << " " << j << " " << mem[j].i_part << " " << order_in << endl;
}
if (mem[j].n_use==max_use) {
// cout << "input_last_use_" << j << "_" << max_use << "_" << *i_event << endl;
cudaError_t cer=cudaEventRecord(worker_event[*i_event],stream_worker); cudaCheckError(__LINE__,__FILE__);
if (cer==cudaSuccess) {
mem[j].i_event=*i_event;
mem[j].i_cycle=-1; // this might make the check for i_cycle in part_in_present_wait obsolete
mem[j].i_part=-1;
(*i_event)++;
}
}
}
}
}
else if (mem[0].type==mem_type_output) {
for (int i=0;i<16;i++) {
int32_t j=islot[i];
if (j!=-1) {
mem[j].state=mem_state_bussy; // todo: not sure if this is necessary!
mem[j].n_use++;
// check if this is the last usage of this block, if yes add event to wait for
int32_t max_use=1+2*order_out;
// parts at the start and end are used less often
if (mem[j].i_part<order_out) {
// first parts
max_use-=order_out-mem[j].i_part;
// cout << "oa_" << i << " " << j << " " << mem[j].i_part << " " << order_out << endl;
}
else if (n_part-mem[j].i_part-1<order_out) {
// last parts
max_use-=n_part-mem[j].i_part;
// cout << "ob_" << i << " " << j << " " << mem[j].i_part << " " << order_out << endl;
}
if (mem[j].n_use==max_use) {
// cout << "output_last_use_" << j << "_" << max_use << "_" << *i_event << endl;
cudaError_t cer=cudaEventRecord(worker_event[*i_event],stream_worker); cudaCheckError(__LINE__,__FILE__);
if (cer==cudaSuccess) {
mem[j].i_event=*i_event;
mem[j].i_cycle=-1; // this might make the check for i_cycle in part_in_present_wait obsolete
mem[j].i_part=-1;
(*i_event)++;
}
}
}
}
}
else {
cout << "not_impl_update_mem_info" << endl;
}
}
int32_t DS::thread_main (
int32_t n_super_cycle,
int32_t order_in,
int32_t order_out,
int32_t myID) {
// current part of worker
int32_t * w_part = new int32_t [n_worker];
int32_t * w_i_in = new int32_t [n_worker];
int32_t * w_i_out = new int32_t [n_worker];
int32_t last_worker_mem_out_center=0;
int32_t i_worker_event=0;
for (int32_t i_super_cycle=0;i_super_cycle<n_super_cycle;i_super_cycle++) {
if (myID==0) cout << "super_cycle: " << i_super_cycle << endl;
for (int32_t i=0;i<n_worker;i++) {
w_part[i]=0;
w_i_in[i]=0;
w_i_out[i]=0;
}
int32_t n_stage=n_part;//*n_worker;
bool any_workers_active=true;
int32_t i_stage=0;
while (any_workers_active==true) {
any_workers_active=false;
// cout << " i_stage " << i_stage << endl;
i_stage++;
for (int32_t i_worker=0;i_worker<n_worker;i_worker++) {
int32_t part_to_process=w_part[i_worker];
// current worker wants to process part part_tp_process
bool worker_first = (i_worker==0);
bool worker_last = (i_worker==(n_worker-1));
// determine input arrays
double * p_in [16];
double * p_out [16];
int32_t islot_in [16];
int32_t islot_out [16];
for (int32_t i=0;i<16;i++) {
p_in[i]={(double*)-1};
p_out[i]={(double*)-1};
islot_in[i]=-1;
islot_out[i]=-1;
}
bool worker_active=true;
if (w_part[i_worker]==my_n_part) {
worker_active=false; // worker done
}
else {
if (i_worker>0) {
if ((w_part[i_worker-1]==my_n_part)||(w_part[i_worker-1]-part_to_process>order_out+1)) {
// worker active
}
else {
worker_active=false;
}
}
}
// cout << "i_worker: " << i_worker << " part: " << part_to_process << " active: " << worker_active << endl;
if (worker_active==true) {
any_workers_active=true;
// determine input arrays
if (worker_first) {
// first worker uses input from d_in
int res=part_in_present_wait(part_to_process,i_super_cycle);
int32_t ibuf=0;
int32_t slot=res;
// cout << "slot_to_in_buf:"<< slot << "_" << ibuf << "_" << k <<endl;
islot_in[ibuf]=slot;
p_in[ibuf]=(double*)d_in[slot];
ibuf++;
for (int32_t j=1;j<order_in+1;j++) {
int32_t k=part_to_process-j; // index of input block
if ((k>=0)&&(k<=n_part-1)) {
slot=res-j;
if (slot<0) slot+=n_store_in;
// cout << "slot_to_in_buf:"<< slot << "_" << ibuf << endl;
islot_in[ibuf]=slot;
p_in[ibuf]=(double*)d_in[slot];
}
ibuf++;
k=part_to_process+j; // index of input block
if ((k>=0)&&(k<=n_part-1)) {
slot=res+j;
if (slot>=n_store_in) slot-=n_store_in;
// cout << "slot_to_in_buf:"<< slot << "_" << ibuf << "_" << k <<endl;
islot_in[ibuf]=slot;
p_in[ibuf]=(double*)d_in[slot];
}
ibuf++;
}
}
else {
int32_t ibuf=0;
int32_t worker_mem_in_center=w_i_in[i_worker];
// center part
int32_t slot=worker_mem_in_center;
islot_in[ibuf]=slot;
p_in[ibuf]=(double*)d_worker[(i_worker-1)*n_store_worker+slot];
ibuf++;
for (int32_t j=1;j<order_in+1;j++) {
int32_t k=part_to_process-j;
if ((k>=0)&&(k<=n_part-1)) {
slot=worker_mem_in_center-j;
if (slot<0) slot+=n_store_worker;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << endl;
islot_in[ibuf]=slot;
p_in[ibuf]=(double*)d_worker[(i_worker-1)*n_store_worker+slot];
}
ibuf++;
k=part_to_process+j;
if ((k>=0)&&(k<=n_part-1)) {
slot=worker_mem_in_center+j;
// if (slot<0) slot+=n_store_out;
if (slot>=n_store_worker) slot-=n_store_worker;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << endl;
islot_in[ibuf]=slot;
p_in[ibuf]=(double*)d_worker[(i_worker-1)*n_store_worker+slot];
}
ibuf++;
}
worker_mem_in_center++;
if (worker_mem_in_center==n_store_worker) worker_mem_in_center=0;
w_i_in[i_worker]=worker_mem_in_center;
}
// determine output arrays
if (worker_last) {
part_out_ready_wait(part_to_process,last_worker_mem_out_center);
int32_t ibuf=0;
int32_t slot=last_worker_mem_out_center;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << "_" << k <<endl;
islot_out[ibuf]=slot;
p_out[ibuf]=(double*)d_out[slot];
ibuf++;
for (int32_t j=1;j<order_out+1;j++) {
int32_t k=part_to_process-j;
if ((k>=0)&&(k<=n_part-1)) {
slot=last_worker_mem_out_center-j;
if (slot<0) slot+=n_store_out;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << endl;
islot_out[ibuf]=slot;
p_out[ibuf]=(double*)d_out[slot];
}
ibuf++;
k=part_to_process+j;
if ((k>=0)&&(k<=n_part-1)) {
slot=last_worker_mem_out_center+j;
if (slot>=n_store_out) slot-=n_store_out;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << endl;
islot_out[ibuf]=slot;
p_out[ibuf]=(double*)d_out[slot];
}
ibuf++;
}
last_worker_mem_out_center++;
if (last_worker_mem_out_center==n_store_out) last_worker_mem_out_center=0;
}
else {
int32_t ibuf=0;
int32_t worker_mem_out_center=w_i_out[i_worker];
// center part
int32_t slot=worker_mem_out_center;
islot_out[ibuf]=slot;
p_out[ibuf]=(double*)d_worker[i_worker*n_store_worker+slot];
ibuf++;
for (int32_t j=1;j<order_out+1;j++) {
int32_t k=part_to_process-j;
if ((k>=0)&&(k<=n_part-1)) {
slot=worker_mem_out_center-j;
if (slot<0) slot+=n_store_worker;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << endl;
islot_out[ibuf]=slot;
p_out[ibuf]=(double*)d_worker[i_worker*n_store_worker+slot];
}
ibuf++;
k=part_to_process+j;
if ((k>=0)&&(k<=n_part-1)) {
slot=worker_mem_out_center+j;
// if (slot<0) slot+=n_store_out;
if (slot>=n_store_worker) slot-=n_store_worker;
// cout << "slot_to_out_buf:"<< slot << "_" << ibuf << endl;
islot_out[ibuf]=slot;
p_out[ibuf]=(double*)d_worker[i_worker*n_store_worker+slot];
}
ibuf++;
}
worker_mem_out_center++;
if (worker_mem_out_center==n_store_worker) worker_mem_out_center=0;
w_i_out[i_worker]=worker_mem_out_center;
}
// enqueue kernel
// for (int32_t i=0;i<8;i++) {
// cout << p_in[i] << " " << p_out[i] << endl;
// cout << i << "_" << islot_in[i] << "_" << islot_out[i] << endl;
// }
// reference version - no optimizations
ref_caller_worker (p_in,p_out,part_to_process,i_super_cycle,order_in,order_out,i_worker,n_worker,&stream_worker,worker_threads_per_block,worker_n_block,myID);
// optimized version - DSEA paper
// b_caller_worker (p_in,p_out,part_to_process,i_super_cycle,order_in,order_out,i_worker,n_worker,&stream_worker,worker_threads_per_block,worker_n_block,myID);
// visualization
if (false)
if (my_id==0) {
if (worker_first) {
// if (i_super_cycle > 0)
if (i_super_cycle % 10 == 0) {
// cout << "output_pre_" << part_to_process << endl;
// caller_output_vtk(p_in[0],(double*)d_visual,&stream_worker,worker_threads_per_block,worker_n_block,myID,i_super_cycle);
caller_output_vtk_rectilinear(p_in[0],(double*)d_visual,&stream_worker,worker_threads_per_block,worker_n_block,myID,i_super_cycle,part_to_process);
// cout << "output_post" << endl;
}
}
}
// record event
if (worker_first) {
update_mem_info(islot_in,stat_mem_in,&i_worker_event);
if (i_worker_event==n_worker_event) i_worker_event=0;
}
if (worker_last) {
update_mem_info(islot_out,stat_mem_out,&i_worker_event);
if (i_worker_event==n_worker_event) i_worker_event=0;
}
for (int i=0;i<n_store_in;i++) {
// cout << i << "_use_in_out_"<< "_" << stat_mem_in[i].n_use << "_" << stat_mem_out[i].n_use << " part_in_out_" << stat_mem_in[i].i_part << "_" << stat_mem_out[i].i_part <<endl;
}
w_part[i_worker]++;
}
}
}
}
return 0;
}