-
Notifications
You must be signed in to change notification settings - Fork 172
/
Mewefd2d_gpu.cu
707 lines (555 loc) · 26.8 KB
/
Mewefd2d_gpu.cu
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*2D elastic time-domain FD modeling with GPU*/
/*
Authors: Robin M. Weiss and Jeffrey Shragge
Use of this code is freely available. In publications, please reference the paper:
Weiss and Shragge, "Solving 3D Anisotropic Elastic Wave Equations on Parallel
GPU Devices", GEOPHYSICS. http://software.seg.org/2012/0063
This code is a GPU-enabled version of the ewefd2d module from the Madagascar
software package (see: http://www.reproducibility.org). It implements a 2D
Finite-Difference Time Domain solver for the elastice wave equation with
2nd- and 8th- order temporal and spatial accuracy, respectively. For more
information, see (Weiss and Shragge, "Solving 3D Anisotropic Elastic Wave
Equations on Parallel GPU Devices", GEOPHYSICS. http://software.seg.org/2012/0063)
*/
/*
Copyright (C) 2012 University of Western Australia
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <cuda.h>
#include <cuda_runtime_api.h>
extern "C" {
#include <rsf.h>
}
#include "fdutil.c"
#include "ewefd2d_kernels.cu"
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define NOP 4 /* derivative operator half-size */
// checks the current GPU device for an error flag and prints to stderr
static void sf_check_gpu_error (const char *msg) {
cudaError_t err = cudaGetLastError ();
if (cudaSuccess != err)
sf_error ("Cuda error: %s: %s", msg, cudaGetErrorString (err));
}
// entry point
int main(int argc, char* argv[]) {
bool verb,fsrf,snap,ssou,dabc;
int jsnap,ntsnap,jdata;
/* I/O files */
sf_file Fwav=NULL; /* wavelet */
sf_file Fsou=NULL; /* sources */
sf_file Frec=NULL; /* receivers */
sf_file Fccc=NULL; /* velocity */
sf_file Fden=NULL; /* density */
sf_file Fdat=NULL; /* data */
sf_file Fwfl=NULL; /* wavefield */
/* cube axes */
sf_axis at,ax,az;
sf_axis as,ar,ac;
int nt,nz,nx,ns,nr,nc,nb;
int it,iz,ix;
float dt,dz,dx,idz,idx;
/* FDM structure */
fdm2d fdm=NULL;
abcone2d abcs=NULL;
sponge spo=NULL;
/* I/O arrays */
float***ww=NULL; /* wavelet */
pt2d *ss=NULL; /* sources */
pt2d *rr=NULL; /* receivers */
float **dd=NULL; /* data */
/*------------------------------------------------------------*/
/* orthorombic footprint - 4 coefficients */
/* c11 c13
. c33
c55 */
float *h_c11, *h_c33, *h_c55, *h_c13;
float *d_c11, *d_c33, *d_c55, *d_c13;
float **vs=NULL;
// density
float *h_ro, *d_ro;
/*------------------------------------------------------------*/
/* displacement: um = U @ t-1; uo = U @ t; up = U @ t+1 */
float *d_umz, *d_uoz, *d_upz, *d_uaz, *d_utz;
float *d_umx, *d_uox, *d_upx, *d_uax, *d_utx;
// used for writing wavefield to output file
float *h_uoz, *h_uox;
float **uoz, **uox;
/* stress/strain tensor */
float *d_tzz, *d_tzx, *d_txx;
/*------------------------------------------------------------*/
/* linear interpolation weights/indices */
lint2d cs,cr;
/* Gaussian bell */
int nbell;
/* wavefield cut params */
sf_axis acz=NULL,acx=NULL;
int nqz,nqx;
float oqz,oqx;
float dqz,dqx;
float **uc=NULL;
/*------------------------------------------------------------*/
/* init RSF */
sf_init(argc,argv);
/*------------------------------------------------------------*/
/* execution flags */
if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
if(! sf_getbool("ssou",&ssou)) ssou=false; /* stress source */
if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* I/O files */
Fwav = sf_input ("in" ); /* wavelet */
Fccc = sf_input ("ccc"); /* stiffness */
Fden = sf_input ("den"); /* density */
Fsou = sf_input ("sou"); /* sources */
Frec = sf_input ("rec"); /* receivers */
Fdat = sf_output("out"); /* data */
if(snap)
Fwfl = sf_output("wfl"); /* wavefield */
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* init GPU */
int gpu;
if (! sf_getint("gpu", &gpu)) gpu = 0; /* ID of the GPU to be used */
sf_warning("using GPU #%d", gpu);
cudaSetDevice(gpu);
cudaDeviceSetCacheConfig(cudaFuncCachePreferL1);
cudaStream_t stream[6];
for (int i = 0; i < 6; ++i) {
cudaStreamCreate(&stream[i]);
}
/*------------------------------------------------------------*/
/* axes */
at = sf_iaxa(Fwav,3); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */
as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */
nt = sf_n(at); dt = sf_d(at);
nz = sf_n(az); dz = sf_d(az);
nx = sf_n(ax); dx = sf_d(ax);
ns = sf_n(as);
nr = sf_n(ar);
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* other execution parameters */
if(! sf_getint("nbell",&nbell)) nbell=5; /* bell size */
if(verb) sf_warning("nbell=%d",nbell);
if(! sf_getint("jdata",&jdata)) jdata=1; /* extract receiver data every jdata time steps */
if(snap) {
if(! sf_getint("jsnap",&jsnap)) jsnap=nt; /* save wavefield every jsnap time steps */
}
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* expand domain for FD operators and ABC */
if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;
fdm=fdutil_init(verb,fsrf,az,ax,nb,1);
if (nbell * 2 + 1 > 32){
sf_error("nbell must be <= 15\n");
}
float *h_bell;
float *d_bell;
h_bell = (float*)malloc((2*nbell+1)*(2*nbell+1)*sizeof(float));
float s = 0.5*nbell;
for (ix=-nbell;ix<=nbell;ix++) {
for (iz=-nbell;iz<=nbell;iz++) {
h_bell[(iz + nbell) * (2*nbell+1) + (ix + nbell)] = exp(-(iz*iz+ix*ix)/s);
}
}
cudaMalloc((void**)&d_bell, (2*nbell+1)*(2*nbell+1)*sizeof(float));
cudaMemcpy(d_bell, h_bell, (2*nbell+1)*(2*nbell+1)*sizeof(float), cudaMemcpyHostToDevice);
sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* 2D vector components */
nc=2;
ac=sf_maxa(nc,0,1);
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* setup output data file and arrays*/
sf_oaxa(Fdat,ar,1);
sf_oaxa(Fdat,ac,2);
sf_setn(at,nt/jdata);
sf_setd(at,dt*jdata);
sf_oaxa(Fdat,at,3);
/* setup output wavefield header and arrays*/
if(snap) {
uoz=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
uox=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
h_uoz = (float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
h_uox = (float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
nqz=sf_n(az);
nqx=sf_n(ax);
oqz=sf_o(az);
oqx=sf_o(ax);
dqz=sf_d(az);
dqx=sf_d(ax);
acz = sf_maxa(nqz,oqz,dqz); if(verb)sf_raxa(acz);
acx = sf_maxa(nqx,oqx,dqx); if(verb)sf_raxa(acx);
uc=sf_floatalloc2(sf_n(acz),sf_n(acx));
ntsnap=0;
for(it=0; it<nt; it++) {
if(it%jsnap==0) ntsnap++;
}
sf_setn(at, ntsnap);
sf_setd(at,dt*jsnap);
if(verb) sf_raxa(at);
sf_oaxa(Fwfl,acz,1);
sf_oaxa(Fwfl,acx,2);
sf_oaxa(Fwfl,ac, 3);
sf_oaxa(Fwfl,at, 4);
}
/*------------------------------------------------------------*/
/* read source wavelet(s) and copy to GPU (into d_ww) */
ww=sf_floatalloc3(ns,nc,nt);
sf_floatread(ww[0][0],nt*nc*ns,Fwav);
float *h_ww;
h_ww = (float*)malloc(ns*nc*nt*sizeof(float));
for (int t = 0; t < nt; t++){
for (int c = 0; c < nc; c++){
for (int s = 0; s < ns; s++){
h_ww[t * nc * ns + c * ns + s]=ww[t][c][s];
}
}
}
float *d_ww;
cudaMalloc((void**)&d_ww, ns*nc*nt*sizeof(float));
cudaMemcpy(d_ww, h_ww, ns*nc*nt*sizeof(float), cudaMemcpyHostToDevice);
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* data array */
dd=sf_floatalloc2(nr,nc);
float *d_dd;
float *h_dd;
h_dd = (float*)malloc(nr * nc * sizeof(float));
cudaMalloc((void**)&d_dd, nr*nc*sizeof(float));
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* setup source/receiver coordinates */
ss = (pt2d*) sf_alloc(ns,sizeof(*ss));
rr = (pt2d*) sf_alloc(nr,sizeof(*rr));
pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */
pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */
/* calculate 2d linear interpolation coefficients for source locations */
cs = lint2d_make(ns,ss,fdm);
float *d_Sw00, *d_Sw01, *d_Sw10, *d_Sw11;
cudaMalloc((void**)&d_Sw00, ns * sizeof(float));
cudaMalloc((void**)&d_Sw01, ns * sizeof(float));
cudaMalloc((void**)&d_Sw10, ns * sizeof(float));
cudaMalloc((void**)&d_Sw11, ns * sizeof(float));
cudaMemcpy(d_Sw00, cs->w00, ns * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Sw01, cs->w01, ns * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Sw10, cs->w10, ns * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Sw11, cs->w11, ns * sizeof(float), cudaMemcpyHostToDevice);
// z and x coordinates of each source
int *d_Sjz, *d_Sjx;
cudaMalloc((void**)&d_Sjz, ns * sizeof(int));
cudaMalloc((void**)&d_Sjx, ns * sizeof(int));
cudaMemcpy(d_Sjz, cs->jz, ns * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_Sjx, cs->jx, ns * sizeof(int), cudaMemcpyHostToDevice);
/* calculate 2d linear interpolation coefficients for receiver locations */
cr = lint2d_make(nr,rr,fdm);
float *d_Rw00, *d_Rw01, *d_Rw10, *d_Rw11;
cudaMalloc((void**)&d_Rw00, nr * sizeof(float));
cudaMalloc((void**)&d_Rw01, nr * sizeof(float));
cudaMalloc((void**)&d_Rw10, nr * sizeof(float));
cudaMalloc((void**)&d_Rw11, nr * sizeof(float));
cudaMemcpy(d_Rw00, cr->w00, nr * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Rw01, cr->w01, nr * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Rw10, cr->w10, nr * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Rw11, cr->w11, nr * sizeof(float), cudaMemcpyHostToDevice);
// z and x coordinates of each receiver
int *d_Rjz, *d_Rjx;
cudaMalloc((void**)&d_Rjz, nr * sizeof(int));
cudaMalloc((void**)&d_Rjx, nr * sizeof(int));
cudaMemcpy(d_Rjz, cr->jz, nr * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_Rjx, cr->jx, nr * sizeof(int), cudaMemcpyHostToDevice);
/*------------------------------------------------------------*/
/* setup FD coefficients */
idz = 1/dz;
idx = 1/dx;
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
/* Read density and stiffness model data and transfer to GPU */
float *tt1 = (float*)malloc(nz * nx * sizeof(float));
h_ro=(float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
h_c11=(float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
h_c33=(float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
h_c55=(float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
h_c13=(float*)malloc(fdm->nzpad * fdm->nxpad * sizeof(float));
/* input density */
sf_floatread(tt1,nz*nx,Fden); expand_cpu(tt1,h_ro , fdm->nb, nx, fdm->nxpad, nz, fdm->nzpad);
/* input stiffness */
sf_floatread(tt1,nz*nx,Fccc ); expand_cpu(tt1,h_c11, fdm->nb, nx, fdm->nxpad, nz, fdm->nzpad);
sf_floatread(tt1,nz*nx,Fccc ); expand_cpu(tt1,h_c33, fdm->nb, nx, fdm->nxpad, nz, fdm->nzpad);
sf_floatread(tt1,nz*nx,Fccc ); expand_cpu(tt1,h_c55, fdm->nb, nx, fdm->nxpad, nz, fdm->nzpad);
sf_floatread(tt1,nz*nx,Fccc ); expand_cpu(tt1,h_c13, fdm->nb, nx, fdm->nxpad, nz, fdm->nzpad);
cudaMalloc((void **)&d_ro, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_c11, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_c33, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_c55, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_c13, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemcpy(d_ro, h_ro, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_c11, h_c11, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_c33, h_c33, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_c55, h_c55, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_c13, h_c13, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
/*------------------------------------------------------------*/
/* boundary condition setup */
float *d_bzl_s, *d_bzh_s;
float *d_bxl_s, *d_bxh_s;
float *d_spo;
if(dabc) {
/* one-way abc setup */
vs = sf_floatalloc2(fdm->nzpad,fdm->nxpad);
for (ix=0; ix<fdm->nxpad; ix++) {
for(iz=0; iz<fdm->nzpad; iz++) {
vs[ix][iz] = sqrt(h_c55[iz * fdm->nxpad + ix]/h_ro[iz * fdm->nxpad + ix] );
}
}
abcs = abcone2d_make(NOP,dt,vs,fsrf,fdm);
free(*vs); free(vs);
cudaMalloc((void**)&d_bzl_s, fdm->nxpad * sizeof(float));
cudaMalloc((void**)&d_bzh_s, fdm->nxpad * sizeof(float));
cudaMalloc((void**)&d_bxl_s, fdm->nzpad * sizeof(float));
cudaMalloc((void**)&d_bxh_s, fdm->nzpad * sizeof(float));
cudaMemcpy(d_bzl_s, abcs->bzl, fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_bzh_s, abcs->bzh, fdm->nxpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_bxl_s, abcs->bxl, fdm->nzpad * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_bxh_s, abcs->bxh, fdm->nzpad * sizeof(float), cudaMemcpyHostToDevice);
/* sponge abc setup */
spo = sponge_make(fdm->nb);
// d_spo contains all of the sponge coefficients
cudaMalloc((void**)&d_spo, fdm->nb * sizeof(float));
cudaMemcpy(d_spo, spo->w, fdm->nb * sizeof(float), cudaMemcpyHostToDevice);
}
/*------------------------------------------------------------*/
/* allocate wavefield arrays */
cudaMalloc((void **)&d_umz, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_uoz, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_upz, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_uaz, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_umx, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_uox, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_upx, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_uax, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_tzz, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_tzx, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMalloc((void **)&d_txx, fdm->nzpad * fdm->nxpad * sizeof(float));
sf_check_gpu_error("allocate grid arrays");
cudaMemset(d_umz, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_uoz, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_upz, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_uaz, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_umx, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_uox, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_upx, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_uax, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_tzz, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_tzx, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
cudaMemset(d_txx, 0, fdm->nzpad * fdm->nxpad * sizeof(float));
sf_check_gpu_error("initialize grid arrays");
/*------------------------------------------------------------*/
/* precompute 1/ro * dt^2 */
/*------------------------------------------------------------*/
dim3 dimGrid5(ceil(fdm->nxpad/16.0f),ceil(fdm->nzpad/16.0f));
dim3 dimBlock5(16,16);
computeRo<<<dimGrid5, dimBlock5>>>(d_ro, dt, fdm->nxpad, fdm->nzpad, NOP);
sf_check_gpu_error("computeRo Kernel");
/*------------------------------------------------------------*/
/*
* MAIN LOOP
*/
/*------------------------------------------------------------*/
if(verb) fprintf(stderr,"\n");
for (it=0; it<nt; it++) {
if(verb) fprintf(stderr,"\b\b\b\b\b\b%d",it);
/*------------------------------------------------------------*/
/* from displacement to strain AND strain to stress */
/* - Compute strains from displacements as in equation 1 */
/* - Step #1 (Steps denoted are as in Figure 2) */
/* - Compute stress from strain as in equation 2 */
/* - Step #2 */
/*------------------------------------------------------------*/
dim3 dimGrid9(ceil(fdm->nxpad/16.0f), ceil(fdm->nzpad/16.0f));
dim3 dimBlock9(16,16);
dispToStrain_strainToStress<<<dimGrid9, dimBlock9>>>(d_txx, d_tzz, d_tzx, d_uox, d_uoz, d_c11, d_c33, d_c55, d_c13, idx, idz, fdm->nxpad, fdm->nzpad, NOP);
sf_check_gpu_error("dispToStrainToStress Kernel");
/*------------------------------------------------------------*/
/* free surface boundary condition */
/* - sets the z-component of stress tensor along the */
/* free surface boundary to 0 */
/* - Step #3 */
/*------------------------------------------------------------*/
if(fsrf) {
dim3 dimGrid3(ceil(fdm->nxpad/16.0f),ceil(fdm->nb/16.0f));
dim3 dimBlock3(16,16);
freeSurf<<<dimGrid3,dimBlock3>>>(d_tzz, d_tzx, fdm->nxpad, fdm->nb);
sf_check_gpu_error("freeSurf Kernel");
}
/*------------------------------------------------------------*/
/* inject stress source */
/* - Step #4 */
/*------------------------------------------------------------*/
if(ssou) {
dim3 dimGrid7(ns, 1, 1);
dim3 dimBlock7(2 * nbell + 1, 2 * nbell + 1, 1);
lint2d_bell_gpu<<<dimGrid7, dimBlock7>>>(d_tzz, d_ww, d_Sw00, d_Sw01, d_Sw10, d_Sw11, d_bell, d_Sjz, d_Sjx, it, nc, ns, 0, nbell, fdm->nxpad);
lint2d_bell_gpu<<<dimGrid7, dimBlock7>>>(d_txx, d_ww, d_Sw00, d_Sw01, d_Sw10, d_Sw11, d_bell, d_Sjz, d_Sjx, it, nc, ns, 1, nbell, fdm->nxpad);
sf_check_gpu_error("lint2d_bell_gpu Kernel");
}
/*------------------------------------------------------------*/
/* from stress to acceleration (first term in RHS of eq. 3) */
/* - Step #5 */
/*------------------------------------------------------------*/
dim3 dimGrid4(ceil((fdm->nxpad-(2*NOP))/16.0f),ceil((fdm->nzpad-(2*NOP))/16.0f));
dim3 dimBlock4(16,16);
stressToAcceleration<<<dimGrid4, dimBlock4>>>(d_uax, d_uaz, d_txx, d_tzz, d_tzx, idx, idz, fdm->nxpad, fdm->nzpad);
sf_check_gpu_error("stressToAcceleration Kernel");
/*------------------------------------------------------------*/
/* inject acceleration source (second term in RHS of eq. 3) */
/* - Step #6 */
/*------------------------------------------------------------*/
if(!ssou) {
dim3 dimGrid8(ns, 1, 1);
dim3 dimBlock8(2 * nbell + 1, 2 * nbell + 1, 1);
lint2d_bell_gpu<<<dimGrid8, dimBlock8>>>(d_uaz, d_ww, d_Sw00, d_Sw01, d_Sw10, d_Sw11, d_bell, d_Sjz, d_Sjx, it, nc, ns, 0, nbell, fdm->nxpad);
lint2d_bell_gpu<<<dimGrid8, dimBlock8>>>(d_uax, d_ww, d_Sw00, d_Sw01, d_Sw10, d_Sw11, d_bell, d_Sjz, d_Sjx, it, nc, ns, 1, nbell, fdm->nxpad);
sf_check_gpu_error("lint2d_bell_gpu Kernel");
}
/*------------------------------------------------------------*/
/* step forward in time */
/* - Compute forward time step based on acceleration */
/* - Step #7
/*------------------------------------------------------------*/
dim3 dimGrid6(ceil(fdm->nxpad/16.0f),ceil(fdm->nzpad/12.0f));
dim3 dimBlock6(16,12);
stepTime<<<dimGrid6, dimBlock6>>>(d_upz, d_uoz, d_umz, d_uaz, d_upx, d_uox, d_umx, d_uax, d_ro, fdm->nxpad, fdm->nzpad);
sf_check_gpu_error("stepTime Kernel");
/* circulate wavefield arrays */
d_utz=d_umz; d_utx=d_umx;
d_umz=d_uoz; d_umx=d_uox;
d_uoz=d_upz; d_uox=d_upx;
d_upz=d_utz; d_upx=d_utx;
/*------------------------------------------------------------*/
/* apply boundary conditions */
/* - Step #8
/*------------------------------------------------------------*/
if(dabc) {
/*---------------------------------------------------------------*/
/* apply One-way Absorbing BC as in (Clayton and Enquist, 1977) */
/*---------------------------------------------------------------*/
/* One-way Absorbing BC */
dim3 dimGrid_TB(ceil(fdm->nxpad/192.0f), 2, 1);
dim3 dimBlock_TB(MIN(192, fdm->nxpad), 1, 1);
dim3 dimGrid_LR(2, ceil(fdm->nzpad/192.0f), 1);
dim3 dimBlock_LR(1, MIN(192, fdm->nzpad), 1);
abcone2d_apply_TB_gpu<<<dimGrid_TB, dimBlock_TB, 0, stream[0]>>>(d_uoz, d_umz, d_bzl_s, d_bzh_s, fdm->nxpad, fdm->nzpad, fsrf);
abcone2d_apply_LR_gpu<<<dimGrid_LR, dimBlock_LR, 0, stream[0]>>>(d_uoz, d_umz, d_bxl_s, d_bxh_s, fdm->nxpad, fdm->nzpad);
abcone2d_apply_TB_gpu<<<dimGrid_TB, dimBlock_TB, 0, stream[1]>>>(d_uox, d_umx, d_bzl_s, d_bzh_s, fdm->nxpad, fdm->nzpad, fsrf);
abcone2d_apply_LR_gpu<<<dimGrid_LR, dimBlock_LR, 0, stream[1]>>>(d_uox, d_umx, d_bxl_s, d_bxh_s, fdm->nxpad, fdm->nzpad);
/*---------------------------------------------------------------*/
/* apply Sponge BC as in (Cerjan, et al., 1985) */
/*---------------------------------------------------------------*/
dim3 dimGrid_TB2(ceil(fdm->nxpad/256.0f), (fdm->nb * 1), 1);
dim3 dimBlock_TB2(256,1,1);
dim3 dimGrid_LR2(ceil(fdm->nb/256.0f), fdm->nzpad, 1);
dim3 dimBlock_LR2(MIN(256, fdm->nb),1,1);
sponge2d_apply_LR_gpu<<<dimGrid_LR2, dimBlock_LR2, 0, stream[2]>>>(d_upz, d_spo, fdm->nxpad, fdm->nb, nx);
sponge2d_apply_TB_gpu<<<dimGrid_TB2, dimBlock_TB2, 0, stream[2]>>>(d_upz, d_spo, fdm->nxpad, fdm->nb, nz);
sponge2d_apply_LR_gpu<<<dimGrid_LR2, dimBlock_LR2, 0, stream[3]>>>(d_upx, d_spo, fdm->nxpad, fdm->nb, nx);
sponge2d_apply_TB_gpu<<<dimGrid_TB2, dimBlock_TB2, 0, stream[3]>>>(d_upx, d_spo, fdm->nxpad, fdm->nb, nz);
cudaStreamSynchronize(stream[0]);
sponge2d_apply_LR_gpu<<<dimGrid_LR2, dimBlock_LR2, 0, stream[0]>>>(d_umz, d_spo, fdm->nxpad, fdm->nb, nx);
sponge2d_apply_TB_gpu<<<dimGrid_TB2, dimBlock_TB2, 0, stream[0]>>>(d_umz, d_spo, fdm->nxpad, fdm->nb, nz);
sponge2d_apply_LR_gpu<<<dimGrid_LR2, dimBlock_LR2, 0, stream[4]>>>(d_uoz, d_spo, fdm->nxpad, fdm->nb, nx);
sponge2d_apply_TB_gpu<<<dimGrid_TB2, dimBlock_TB2, 0, stream[4]>>>(d_uoz, d_spo, fdm->nxpad, fdm->nb, nz);
cudaStreamSynchronize(stream[1]);
sponge2d_apply_LR_gpu<<<dimGrid_LR2, dimBlock_LR2, 0, stream[1]>>>(d_umx, d_spo, fdm->nxpad, fdm->nb, nx);
sponge2d_apply_TB_gpu<<<dimGrid_TB2, dimBlock_TB2, 0, stream[1]>>>(d_umx, d_spo, fdm->nxpad, fdm->nb, nz);
sponge2d_apply_LR_gpu<<<dimGrid_LR2, dimBlock_LR2, 0, stream[5]>>>(d_uox, d_spo, fdm->nxpad, fdm->nb, nx);
sponge2d_apply_TB_gpu<<<dimGrid_TB2, dimBlock_TB2, 0, stream[5]>>>(d_uox, d_spo, fdm->nxpad, fdm->nb, nz);
cudaDeviceSynchronize();
sf_check_gpu_error("boundary condition kernels");
}
/*------------------------------------------------------------*/
/* cut wavefield and save */
/* - Step #9
/*------------------------------------------------------------*/
if(snap && it%jsnap==0) {
cudaMemcpy(h_uox, d_uox, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(h_uoz, d_uoz, fdm->nzpad * fdm->nxpad * sizeof(float), cudaMemcpyDeviceToHost);
for (int x = 0; x < fdm->nxpad; x++){
for (int z = 0; z < fdm->nzpad; z++){
uox[x][z] = h_uox[z * fdm->nxpad + x];
uoz[x][z] = h_uoz[z * fdm->nxpad + x];
}
}
cut2d(uoz,uc,fdm,acz,acx);
sf_floatwrite(uc[0],sf_n(acz)*sf_n(acx),Fwfl);
cut2d(uox,uc,fdm,acz,acx);
sf_floatwrite(uc[0],sf_n(acz)*sf_n(acx),Fwfl);
}
/*------------------------------------------------------------*/
/* extract receiver data */
/*------------------------------------------------------------*/
if(it%jdata==0) {
dim3 dimGrid_extract(MIN(nr,ceil(nr/1024.0f)), 1, 1);
dim3 dimBlock_extract(MIN(nr, 1024), 1, 1);
lint2d_extract_gpu<<<dimGrid_extract, dimBlock_extract>>>(d_dd, nr, fdm->nxpad, d_uoz, d_uox, d_Rjz, d_Rjx, d_Rw00, d_Rw01, d_Rw10, d_Rw11);
sf_check_gpu_error("lint2d_extract kernel");
cudaMemcpy(h_dd, d_dd, nr * nc * sizeof(float), cudaMemcpyDeviceToHost);
sf_floatwrite(h_dd, nr*nc, Fdat);
}
}
if(verb) fprintf(stderr,"\n");
/*------------------------------------------------------------*/
/* deallocate host arrays */
free(**ww); free(*ww); free(ww);
free(ss);
free(rr);
free(*dd); free(dd);
if (snap){
free(*uoz); free(uoz);
free(*uox); free(uox);
free(h_uoz);
free(h_uox);
free(*uc); free(uc);
}
free(h_c11); free(h_c33); free(h_c55); free(h_c13); free(h_ro);
free(h_bell);
free(h_dd);
/*------------------------------------------------------------*/
/* deallocate GPU arrays */
cudaFree(d_ro);
cudaFree(d_c11); cudaFree(d_c33); cudaFree(d_c55); cudaFree(d_c13);
cudaFree(d_umz); cudaFree(d_uoz); cudaFree(d_upz); cudaFree(d_uaz); cudaFree(d_utz);
cudaFree(d_umx); cudaFree(d_uox); cudaFree(d_upx); cudaFree(d_uax); cudaFree(d_utx);
cudaFree(d_tzz); cudaFree(d_tzx); cudaFree(d_txx);
cudaFree(d_Sw00); cudaFree(d_Sw01); cudaFree(d_Sw10); cudaFree(d_Sw11);
cudaFree(d_Sjz); cudaFree(d_Sjx);
cudaFree(d_Rjz); cudaFree(d_Rjx);
cudaFree(d_bell);
cudaFree(d_ww);
cudaFree(d_dd);
if (dabc){
cudaFree(d_bzl_s); cudaFree(d_bzh_s);
cudaFree(d_bxl_s); cudaFree(d_bxh_s);
cudaFree(d_spo);
}
sf_close();
exit(0);
}