-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCUDAQRTM.cu
More file actions
1641 lines (1414 loc) · 46.7 KB
/
CUDAQRTM.cu
File metadata and controls
1641 lines (1414 loc) · 46.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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is CUDA file for kernel functions and extern "C" functions definition
// There two major extern "C" functions in this file:
// void cuda_visco_PSM_2d_forward(...) for viscoacoustic forward modeling;
// void cuda_visco_PSM_2d_backward(...) for wavefield reconstruction, backpropagation and imaging.
// These two extern "C" function calls a set of CUDA kernel functions.
// Since we use PSM for numerical simulation, the discretization of forward and
// backward wave equation can be split into two parts via CUFFT calls:
//
// cufftExecC2C(...,CUFFT_FORWARD);
// cuda_kernel_visco_PSM_2d_forward_k_space<<<...>>>;
// cufftExecC2C(...,CUFFT_BACKWARD);
// cuda_kernel_visco_PSM_2d_forward_x_space<<<...>>>;
//
// For more details please refer to Eq(1) for forward and Eq(2) for backward propagation in our paper.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <cuda.h>
#define BLOCK_SIZE 16
#define PI 3.1415926
#include "Myfunctions.h"
using namespace std;
//=========================================================
// This subroutine is used for Error Checking
// during Memory Malloc on device
//=========================================================
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
//=========================================================
// define multistream to prepare for streaming execution
//=========================================================
struct Multistream
{
cudaStream_t stream,stream_back;
};
//=========================================================
// This subroutine is used for initializating wavefield
// variables in space domain and wavenumber domain
//=========================================================
__global__ void cuda_kernel_initialization
(
int ntx, int ntz, cufftComplex *u0, cufftComplex *u1, cufftComplex *u2,
cufftComplex *uk0, cufftComplex *uk, cufftComplex *Lap, cufftComplex *amp_Lap,
cufftComplex *pha_Lap, cufftComplex *sta_Lap
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
u0[ip].x=0.0; u0[ip].y=0.0;
u1[ip].x=0.0; u1[ip].y=0.0;
u2[ip].x=0.0; u2[ip].y=0.0;
uk0[ip].x=0.0; uk0[ip].y=0.0;
uk[ip].x=0.0; uk[ip].y=0.0;
Lap[ip].x=0.0; Lap[ip].y=0.0;
amp_Lap[ip].x=0.0; amp_Lap[ip].y=0.0;
pha_Lap[ip].x=0.0; pha_Lap[ip].y=0.0;
sta_Lap[ip].x=0.0; sta_Lap[ip].y=0.0;
}
}
//=========================================================
// This subroutine is used for updating wavefield variables
//=========================================================
__global__ void cuda_kernel_update
(
int ntx, int ntz, cufftComplex *u0, cufftComplex *u1, cufftComplex *u2
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
u0[ip].x=u1[ip].x;
u0[ip].y=u1[ip].y;
u1[ip].x=u2[ip].x;
u1[ip].y=u2[ip].y;
}
}
//=============================================================
// This subroutine is used for initializating image variables
//=============================================================
__global__ void cuda_kernel_initialization_images
(
int ntx, int ntz, float *image_cor, float *image_nor, float *image_sources, float *image_receivers
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
image_cor[ip]=0;
image_nor[ip]=0;
image_sources[ip]=0;
image_receivers[ip]=0;
}
}
//=========================================================
// This subroutine is used for defining wavenumber k
// (kx=2*PI*ix/(ntx*dx))
//=========================================================
__global__ void cuda_kernel_k_define
(
int ntx, int ntz, float dx, float dz, float *kx, float *kz
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int nxh=ntx/2;
int nzh=ntz/2;
float dkx=1.0/(ntx*dx);
float dkz=1.0/(ntz*dz);
if(ix>=0&&ix<=nxh)
{
kx[ix]=2*PI*ix*dkx;
}
if(ix>nxh&&ix<ntx)
{
kx[ix]=kx[ntx-ix];
}
if(iz>=0&&iz<=nzh)
{
kz[iz]=2*PI*iz*dkz;
}
if(iz>nzh&&iz<ntz)
{
kz[iz]=kz[ntz-iz];
}
}
//=========================================================
// This subroutine is used for calculating forward
// wavefileds in k-space when using PSM
// ========================================================
__global__ void cuda_kernel_visco_PSM_2d_forward_k_space
(
float beta1, float beta2,
int it, int nt, int ntx, int ntz, float dx, float dz, float dt,
float *vp, float *Gamma, float averGamma, float f0, float Omega0, float alphaorder,
float *kx, float *kz,
cufftComplex *uk, cufftComplex *uk0,
cufftComplex *Lap_uk, cufftComplex *amp_uk, cufftComplex *pha_uk, cufftComplex *sta_uk
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
Lap_uk[ip].x=-(powf(kx[ix],2)+powf(kz[iz],2))*uk[ip].x;
Lap_uk[ip].y=-(powf(kx[ix],2)+powf(kz[iz],2))*uk[ip].y;
if(beta1!=0)
{
pha_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+1)*uk[ip].x;
pha_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+1)*uk[ip].y;
}
if(beta2!=0)
{
amp_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+0.5)*(uk[ip].x-uk0[ip].x)/dt;
amp_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+0.5)*(uk[ip].y-uk0[ip].y)/dt;
}
if(beta2<0)
{
sta_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), 0.5*alphaorder)*(uk[ip].x-uk0[ip].x)/dt;
sta_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), 0.5*alphaorder)*(uk[ip].y-uk0[ip].y)/dt;
}
uk0[ip].x=uk[ip].x;
uk0[ip].y=uk[ip].y;
}
}
//==========================================================
// This subroutine is used for calculating forward
// wavefileds in x-space when using PSM
//========================================================
__global__ void cuda_kernel_visco_PSM_2d_forward_x_space
(
float beta1, float beta2,
int it, int nt, int ntx, int ntz, int nx, int nz, int L, float dx, float dz, float dt,
float *vp, float *Gamma, float averGamma, float f0, float Omega0, float sigmafactor,
float *seismogram, int *r_ix, int *r_iz, int rnmax, float *ricker, int s_ix, int s_iz,
cufftComplex *u0, cufftComplex *u1, cufftComplex *u2,
cufftComplex *Lap, cufftComplex *amp_Lap, cufftComplex *pha_Lap, cufftComplex *sta_Lap,
float *borders_up, float *borders_bottom, float *borders_left, float *borders_right,
float *u2_final0, float *u2_final1,
int Sto_Rec, int vp_type
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
int icp;
float eta, tau;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
eta= -powf(vp[ip],2*Gamma[ip])*powf(Omega0,-2*Gamma[ip])*cos(Gamma[ip]*PI);
tau= -powf(vp[ip],2*Gamma[ip]-1)*powf(Omega0,-2*Gamma[ip])*sin(Gamma[ip]*PI);
// scale fft by dividing (ntx*ntz)
Lap[ip].x=Lap[ip].x/(ntx*ntz);
pha_Lap[ip].x=pha_Lap[ip].x/(ntx*ntz);
amp_Lap[ip].x=amp_Lap[ip].x/(ntx*ntz);
sta_Lap[ip].x=sta_Lap[ip].x/(ntx*ntz);
u2[ip].x=powf(vp[ip]*cos(Gamma[ip]*PI/2),2)*powf(dt,2)
*(
Lap[ip].x
+beta1*(eta*pha_Lap[ip].x-Lap[ip].x)
+beta2*tau*amp_Lap[ip].x
+tau*sigmafactor*sta_Lap[ip].x
)
+2*u1[ip].x-u0[ip].x;
}
// add Ricker source
if(iz==s_iz&&ix==s_ix)
{
u2[ip].x+=ricker[it];
}
if(ix>=0&&ix<=rnmax-1)
{
seismogram[it*rnmax+ix] = u2[r_iz[ix]*ntx + r_ix[ix]].x;
}
// store borders and final two-step wavefileds for wavefield reconstruction
if(Sto_Rec==0&&vp_type==2)
{
if(ix>=L&&ix<=ntx-L-1&&iz==L)
{
borders_up[it*nx+ix-L]=u2[ip].x;
}
if(ix>=L&&ix<=ntx-L-1&&iz==ntz-L-1)
{
borders_bottom[it*nx+ix-L]=u2[ip].x;
}
if(iz>=L&&iz<=ntz-L-1&&ix==L)
{
borders_left[it*nz+iz-L]=u2[ip].x;
}
if(iz>=L&&iz<=ntz-L-1&&ix==ntx-L-1)
{
borders_right[it*nz+iz-L]=u2[ip].x;
}
if(it==nt-1)
{
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
u2_final0[ip]=u2[ip].x;
u2_final1[ip]=u1[ip].x;
}
}
}
}
//========================================================
// This subroutine is used for writing checkpoints
//========================================================
__global__ void cuda_kernel_checkpoints_Out
(
int it, int nt, int ntx, int ntz, int nx, int nz, int L, float dx, float dz, float dt,
cufftComplex *u1, cufftComplex *u2,
float *u_cp, int N_cp, int *t_cp
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
int icp;
for(icp=0;icp<N_cp;icp++)
{
if(icp%2==1&&it==t_cp[icp])
{
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
u_cp[icp*ntx*ntz+ip]=u2[ip].x;
u_cp[(icp-1)*ntx*ntz+ip]=u1[ip].x;
}
}
}
}
//=========================================================
// This two subroutines are used for initializing
// Final two wavefileds for reconstruction
//=========================================================
__global__ void cuda_kernel_initialization_Finals
(
int ntx, int ntz, cufftComplex *u0, cufftComplex *u1, float *u2_final0, float *u2_final1
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
u0[ip].x=u2_final0[ip];
u1[ip].x=u2_final1[ip];
}
}
//==========================================================
// This subroutine is used for calculating reconstructed
// wavefileds in k-space when using PSM
//==========================================================
__global__ void cuda_kernel_visco_PSM_2d_reconstruction_k_space
(
float beta1, float beta2,
int it, int nt, int ntx, int ntz, int nx, int nz, int L, float dx, float dz, float dt,
float *vp, float *Gamma, float averGamma, float f0, float Omega0, float alphaorder,
float *kx, float *kz,
cufftComplex *uk, cufftComplex *uk0,
cufftComplex *Lap_uk, cufftComplex *amp_uk, cufftComplex *pha_uk, cufftComplex *sta_uk
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
Lap_uk[ip].x=-(powf(kx[ix],2)+powf(kz[iz],2))*uk[ip].x;
Lap_uk[ip].y=-(powf(kx[ix],2)+powf(kz[iz],2))*uk[ip].y;
if(beta1!=0)
{
pha_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+1)*uk[ip].x;
pha_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+1)*uk[ip].y;
}
if(beta2!=0)
{
amp_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+0.5)*(uk[ip].x-uk0[ip].x)/dt;
amp_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+0.5)*(uk[ip].y-uk0[ip].y)/dt;
}
if(beta2<0)
{
sta_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), 0.5*alphaorder)*(uk[ip].x-uk0[ip].x)/dt;
sta_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), 0.5*alphaorder)*(uk[ip].y-uk0[ip].y)/dt;
}
uk0[ip].x=uk[ip].x;
uk0[ip].y=uk[ip].y;
}
}
//=========================================================
// This subroutine is used for calculating reconstructed
// wavefileds in x-space when using PSM
//=========================================================
__global__ void cuda_kernel_visco_PSM_2d_reconstruction_x_space
(
float beta1, float beta2,
int it, int nt, int ntx, int ntz, int nx, int nz, int L, float dx, float dz, float dt,
float *vp, float *Gamma, float averGamma, float f0, float Omega0, float sigmafactor,
float *ricker, int s_ix, int s_iz,
cufftComplex *u0, cufftComplex *u1, cufftComplex *u2,
cufftComplex *Lap, cufftComplex *amp_Lap, cufftComplex *pha_Lap, cufftComplex *sta_Lap,
float *borders_up, float *borders_bottom, float *borders_left, float *borders_right
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
int icp;
float eta, tau;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
eta= -powf(vp[ip],2*Gamma[ip])*powf(Omega0,-2*Gamma[ip])*cos(Gamma[ip]*PI);
tau= -powf(vp[ip],2*Gamma[ip]-1)*powf(Omega0,-2*Gamma[ip])*sin(Gamma[ip]*PI);
// scale fft by dividing (ntx*ntz)
Lap[ip].x=Lap[ip].x/(ntx*ntz);
pha_Lap[ip].x=pha_Lap[ip].x/(ntx*ntz);
amp_Lap[ip].x=amp_Lap[ip].x/(ntx*ntz);
sta_Lap[ip].x=sta_Lap[ip].x/(ntx*ntz);
u2[ip].x=powf(vp[ip]*cos(Gamma[ip]*PI/2),2)*powf(dt,2)
*(
Lap[ip].x
+beta1*(eta*pha_Lap[ip].x-Lap[ip].x)
+beta2*tau*amp_Lap[ip].x
+tau*sigmafactor*sta_Lap[ip].x
)
+2*u1[ip].x-u0[ip].x;
}
// add borders
if(ix>=L&&ix<=ntx-L-1&&iz==L)
{
u2[ip].x=borders_up[it*nx+ix-L];
}
if(ix>=L&&ix<=ntx-L-1&&iz==ntz-L-1)
{
u2[ip].x=borders_bottom[it*nx+ix-L];
}
if(iz>=L&&iz<=ntz-L-1&&ix==L)
{
u2[ip].x=borders_left[it*nz+iz-L];
}
if(iz>=L&&iz<=ntz-L-1&&ix==ntx-L-1)
{
u2[ip].x=borders_right[it*nz+iz-L];
}
}
//=========================================================
// This subroutine is used for reading checkpoints
//=========================================================
__global__ void cuda_kernel_checkpoints_In
(
int it, int nt, int ntx, int ntz, int nx, int nz, int L, float dx, float dz, float dt,
cufftComplex *u1, cufftComplex *u2,
float *u_cp, int N_cp, int *t_cp
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
int icp;
for(icp=0;icp<N_cp;icp++)
{
if(icp%2==0&&it==t_cp[icp])
{
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
u2[ip].x=u_cp[icp*ntx*ntz+ip];
u1[ip].x=u_cp[(icp+1)*ntx*ntz+ip];
}
}
}
}
//=========================================================
// This subroutine is used for calculating backward
// wavefileds in k-space when using PSM
//=========================================================
__global__ void cuda_kernel_visco_PSM_2d_backward_k_space
(
float beta1, float beta2,
int it, int nt, int ntx, int ntz, float dx, float dz, float dt,
float *vp, float *Gamma, float averGamma, float f0, float Omega0, float alphaorder,
float *kx, float *kz,
cufftComplex *uk, cufftComplex *uk0,
cufftComplex *Lap_uk, cufftComplex *amp_uk, cufftComplex *pha_uk, cufftComplex *sta_uk
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
Lap_uk[ip].x=-(powf(kx[ix],2)+powf(kz[iz],2))*uk[ip].x;
Lap_uk[ip].y=-(powf(kx[ix],2)+powf(kz[iz],2))*uk[ip].y;
if(beta1!=0)
{
pha_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+1)*uk[ip].x;
pha_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+1)*uk[ip].y;
}
if(beta2!=0)
{
amp_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+0.5)*(uk[ip].x-uk0[ip].x)/dt;
amp_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), averGamma+0.5)*(uk[ip].y-uk0[ip].y)/dt;
}
if(beta2<0)
{
sta_uk[ip].x=powf((powf(kx[ix],2)+powf(kz[iz],2)), 0.5*alphaorder)*(uk[ip].x-uk0[ip].x)/dt;
sta_uk[ip].y=powf((powf(kx[ix],2)+powf(kz[iz],2)), 0.5*alphaorder)*(uk[ip].y-uk0[ip].y)/dt;
}
uk0[ip].x=uk[ip].x;
uk0[ip].y=uk[ip].y;
}
}
//========================================================
// This subroutine is used for calculating backward
// wavefileds in x-space when using PSM
//========================================================
__global__ void cuda_kernel_visco_PSM_2d_backward_x_space
(
int Geometry, float beta1, float beta2,
int it, int nt, int ntx, int ntz, float dx, float dz, float dt,
float *vp, float *Gamma, float averGamma, float f0, float Omega0, float sigmafactor,
float *seismogram_rms, int *r_ix, int *r_iz, int s_ix, int s_iz, int rnmax, int nrx_obs,
cufftComplex *u0, cufftComplex *u1, cufftComplex *u2,
cufftComplex *Lap, cufftComplex *amp_Lap, cufftComplex *pha_Lap, cufftComplex *sta_Lap
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
float eta, tau;
if(iz>=0&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
eta= -powf(vp[ip],2*Gamma[ip])*powf(Omega0,-2*Gamma[ip])*cos(Gamma[ip]*PI);
tau= -powf(vp[ip],2*Gamma[ip]-1)*powf(Omega0,-2*Gamma[ip])*sin(Gamma[ip]*PI);
// scaling fft
Lap[ip].x=Lap[ip].x/(ntx*ntz);
pha_Lap[ip].x=pha_Lap[ip].x/(ntx*ntz);
amp_Lap[ip].x=amp_Lap[ip].x/(ntx*ntz);
sta_Lap[ip].x=sta_Lap[ip].x/(ntx*ntz);
u2[ip].x=powf(vp[ip]*cos(Gamma[ip]*PI/2),2)*powf(dt,2)
*(
Lap[ip].x
+beta1*(eta*pha_Lap[ip].x-Lap[ip].x)
+beta2*tau*amp_Lap[ip].x
+tau*sigmafactor*sta_Lap[ip].x
)
+2*u1[ip].x-u0[ip].x;
}
// add seismogram as source for surface geometry
if(Geometry==0)
{
int irx_min = s_ix-nrx_obs;
int irx_max = s_ix+nrx_obs;
if(irx_min<r_ix[0])
irx_min = r_ix[0];
if(irx_max>r_ix[rnmax-1])
irx_max = r_ix[rnmax-1];
if(ix>=irx_min&&ix<=irx_max)
u2[r_iz[ix]*ntx + r_ix[ix]].x = seismogram_rms[it*rnmax+ix];
}
// add seismogram as source for crosswell geometry
if(Geometry==1)
{
if(ix>=0&&ix<=rnmax-1)
{
if(abs(s_iz-r_iz[ix])>=20)
u2[r_iz[ix]*ntx + r_ix[ix]].x = seismogram_rms[it*rnmax+ix];
}
}
}
//========================================================
// This subroutine is used for cross-correlation imaging
//========================================================
__global__ void cuda_kernel_image
(
int ntx, int ntz, int L,
cufftComplex *u2_inv, cufftComplex *u2,
float *image_cor, float *image_sources, float *image_receivers
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
if(iz>=L&&iz<=ntz-L-1&&ix>=L&&ix<=ntx-L-1)
{
image_cor[ip]+=u2_inv[ip].x*u2[ip].x;
image_sources[ip]+=u2_inv[ip].x*u2_inv[ip].x;
image_receivers[ip]+=u2[ip].x*u2[ip].x;
}
//__syncthreads();
}
//========================================================
// This subroutine is used for MTF
// absorbing boundary condition
//========================================================
__global__ void cuda_kernel_MTF_2nd
(
int L, int ntx, int ntz, float dx, float dz, float dt,
float *vp, cufftComplex *u0, cufftComplex *u1, cufftComplex *u2
)
{
int bx=blockIdx.x;
int by=blockIdx.y;
int tx=threadIdx.x;
int ty=threadIdx.y;
int iz=by*BLOCK_SIZE+ty;
int ix=bx*BLOCK_SIZE+tx;
int ip=iz*ntx+ix;
int ipp=iz*ntx+(ntx-1-ix);
int ippp=(ntz-1-iz)*ntx+ix;
float alpha=1.0;
float w, s, t1, t2, t3;
// left ABC ...
if(ix>=0&&ix<=L-1&&iz>=0&&iz<=ntz-1)
{
w=1-1.0*ix/L;
s=alpha*vp[ip]*dt/dx;
t1=(2-s)*(1-s)/2;
t2=s*(2-s);
t3=s*(s-1)/2;
u2[ip].x=w*
(
(1*2)*
(
t1*u1[ip].x+t2*u1[ip+1].x+t3*u1[ip+2].x
)
+(-1*1)*
(
t1*t1*u0[ip].x
+2*t1*t2*u0[ip+1].x
+(2*t1*t3+t2*t2)*u0[ip+2].x
+2*t2*t3*u0[ip+3].x
+t3*t3*u0[ip+4].x
)
)
+(1-w)*u2[ip].x;
}
// right ABC ...
if(ix>=ntx-L&&ix<=ntx-1&&iz>=0&&iz<=ntz-1)
{
w=1-1.0*(ntx-1-ix)/L;
s=alpha*vp[ip]*dt/dx;
t1=(2-s)*(1-s)/2;
t2=s*(2-s);
t3=s*(s-1)/2;
u2[ip].x=w*
(
(1*2)*
(
t1*u1[ip].x
+t2*u1[ip-1].x
+t3*u1[ip-2].x
)
+(-1*1)*
(
t1*t1*u0[ip].x
+2*t1*t2*u0[ip-1].x
+(2*t1*t3+t2*t2)*u0[ip-2].x
+2*t2*t3*u0[ip-3].x
+t3*t3*u0[ip-4].x
)
)
+(1-w)*u2[ip].x;
}
// up ABC ...
if(iz>=0&&iz<=L-1&&ix>=0&&ix<=ntx-1)
{
w=1-1.0*iz/L;
s=alpha*vp[ip]*dt/dz;
t1=(2-s)*(1-s)/2;
t2=s*(2-s);
t3=s*(s-1)/2;
u2[ip].x=w*
(
(1*2)*
(
t1*u1[ip].x
+t2*u1[ip+ntx].x
+t3*u1[ip+2*ntx].x
)
+(-1*1)*
(
t1*t1*u0[ip].x
+2*t1*t2*u0[ip+ntx].x
+(2*t1*t3+t2*t2)*u0[ip+2*ntx].x
+2*t2*t3*u0[ip+3*ntx].x
+t3*t3*u0[ip+4*ntx].x
)
)
+(1-w)*u2[ip].x;
}
// bottom ABC ...
if(iz>=ntz-L&&iz<=ntz-1&&ix>=0&&ix<=ntx-1)
{
w=1-1.0*(ntz-1-iz)/L;
s=alpha*vp[ip]*dt/dz;
t1=(2-s)*(1-s)/2;
t2=s*(2-s);
t3=s*(s-1)/2;
u2[ip].x=w*
(
(1*2)*
(
t1*u1[ip].x
+t2*u1[ip-ntx].x
+t3*u1[ip-2*ntx].x
)
+(-1*1)*
(
t1*t1*u0[ip].x
+2*t1*t2*u0[ip-ntx].x
+(2*t1*t3+t2*t2)*u0[ip-2*ntx].x
+2*t2*t3*u0[ip-3*ntx].x
+t3*t3*u0[ip-4*ntx].x
)
)
+(1-w)*u2[ip].x;
}
//__syncthreads();
}
//================================================================
// This subroutine are used for viscoacoustic forward modeling
// with decoupled fractional laplacians wave equation which is
// calculated by PSM. The CUFFT calls split the discretation
// into two parts in k-space and x-space calls:
//
// cufftExecC2C(...,CUFFT_FORWARD);
// cuda_kernel_visco_PSM_2d_forward_k_space<<<...>>>;
// cufftExecC2C(...,CUFFT_BACKWARD);
// cuda_kernel_visco_PSM_2d_forward_x_space<<<...>>>;
//
// For more details please refer to Eq(1) in our paper
//===============================================================
extern "C"
void cuda_visco_PSM_2d_forward
(
int beta1, int beta2,
int nt, int ntx, int ntz, int ntp, int nx, int nz, int L, float dx, float dz, float dt,
float *vp, float *Gamma, float avervp, float averGamma, float f0, float Omega0, float *ricker,
int myid, int is, struct Source ss[], struct MultiGPU plan[], int GPU_N, int rnmax, int nrx_obs, int N_cp, int *t_cp,
float alphaorder, float sigmafactor,
int Sto_Rec, int vp_type, int Save_Not
)
{
int i, it, ix, iz, ip, icp;
size_t size_model=sizeof(float)*ntp;
FILE *fp;
char filename[40];
float *u2_real;
u2_real = (float*)malloc(sizeof(float)*ntp);
// define multistream variable
Multistream plans[GPU_N];
// define streaming cufft handle (very important!!!)
for(i=0;i<GPU_N;i++)
{
cudaSetDevice(i);
cudaStreamCreate(&plans[i].stream);
cufftSetStream(plan[i].PLAN_FORWARD,plans[i].stream);
cufftSetStream(plan[i].PLAN_BACKWARD,plans[i].stream);
}
// block size 16*16;
dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE);
// grid size ntx/16*ntz/16
dim3 dimGrid((ntx+dimBlock.x-1)/dimBlock.x,(ntz+dimBlock.y-1)/dimBlock.y);
// copy the vectors from the host to the device
for(i=0;i<GPU_N;i++)
{
cudaSetDevice(i);
cudaMemcpyAsync(plan[i].d_r_ix,ss[is+i].r_ix,sizeof(int)*rnmax,cudaMemcpyHostToDevice,plans[i].stream);
cudaMemcpyAsync(plan[i].d_r_iz,ss[is+i].r_iz,sizeof(int)*rnmax,cudaMemcpyHostToDevice,plans[i].stream);
cudaMemcpyAsync(plan[i].d_ricker,ricker,sizeof(float)*nt,cudaMemcpyHostToDevice,plans[i].stream);
cudaMemcpyAsync(plan[i].d_vp,vp,size_model,cudaMemcpyHostToDevice,plans[i].stream);
cudaMemcpyAsync(plan[i].d_Gamma,Gamma,size_model,cudaMemcpyHostToDevice,plans[i].stream);
cudaMemcpyAsync(plan[i].d_t_cp,t_cp,N_cp*sizeof(int),cudaMemcpyHostToDevice,plans[i].stream);
}
// initializing wavefield variables and define k variables
for(i=0;i<GPU_N;i++)
{
cudaSetDevice(i);
cuda_kernel_initialization<<<dimGrid,dimBlock,0,plans[i].stream>>>
(ntx, ntz, plan[i].d_u0, plan[i].d_u1, plan[i].d_u2, plan[i].d_uk0, plan[i].d_uk,
plan[i].d_Lap, plan[i].d_amp_Lap, plan[i].d_pha_Lap, plan[i].d_sta_Lap);
cuda_kernel_k_define<<<dimGrid,dimBlock,0,plans[i].stream>>>
(ntx, ntz, dx, dz, plan[i].d_kx, plan[i].d_kz);
if (cudaSuccess != cudaGetLastError())
printf( "Error!\n" );
}
// forward time iteration
for(it=0;it<nt;it++)
{
for(i=0;i<GPU_N;i++)
{
cudaSetDevice(i);
cufftExecC2C(plan[i].PLAN_FORWARD,plan[i].d_u1,plan[i].d_uk,CUFFT_FORWARD); //CUFFT_FORWARD
cuda_kernel_visco_PSM_2d_forward_k_space<<<dimGrid,dimBlock,0,plans[i].stream>>>
(
beta1, beta2,
it, nt, ntx, ntz, dx, dz, dt,
plan[i].d_vp, plan[i].d_Gamma, averGamma, f0, Omega0, alphaorder,
plan[i].d_kx, plan[i].d_kz,
plan[i].d_uk, plan[i].d_uk0,
plan[i].d_Lap_uk, plan[i].d_amp_uk, plan[i].d_pha_uk, plan[i].d_sta_uk
);
cufftExecC2C(plan[i].PLAN_BACKWARD,plan[i].d_Lap_uk,plan[i].d_Lap,CUFFT_INVERSE); //CUFFT_INVERSE
if(beta1!=0)
{
cufftExecC2C(plan[i].PLAN_BACKWARD,plan[i].d_pha_uk,plan[i].d_pha_Lap,CUFFT_INVERSE); //CUFFT_INVERSE
}
if(beta2!=0)
{
cufftExecC2C(plan[i].PLAN_BACKWARD,plan[i].d_amp_uk,plan[i].d_amp_Lap,CUFFT_INVERSE); //CUFFT_INVERSE
cufftExecC2C(plan[i].PLAN_BACKWARD,plan[i].d_sta_uk,plan[i].d_sta_Lap,CUFFT_INVERSE); //CUFFT_INVERSE
}
cuda_kernel_visco_PSM_2d_forward_x_space<<<dimGrid,dimBlock,0,plans[i].stream>>>
(
beta1, beta2,
it, nt, ntx, ntz, nx, nz, L, dx, dz, dt,
plan[i].d_vp, plan[i].d_Gamma, averGamma, f0, Omega0, sigmafactor,
plan[i].d_seismogram, plan[i].d_r_ix, plan[i].d_r_iz, rnmax, plan[i].d_ricker, ss[is+i].s_ix, ss[is+i].s_iz,
plan[i].d_u0, plan[i].d_u1, plan[i].d_u2,
plan[i].d_Lap, plan[i].d_amp_Lap, plan[i].d_pha_Lap, plan[i].d_sta_Lap,
plan[i].d_borders_up, plan[i].d_borders_bottom, plan[i].d_borders_left, plan[i].d_borders_right,
plan[i].d_u2_final0, plan[i].d_u2_final1,
Sto_Rec, vp_type
);
// MTF absorbing boundary condition
cuda_kernel_MTF_2nd<<<dimGrid,dimBlock,0,plans[i].stream>>>
(L, ntx, ntz, dx, dz, dt, plan[i].d_vp, plan[i].d_u0, plan[i].d_u1, plan[i].d_u2);
// record wavefields at checkpoints
if(Sto_Rec==0&&vp_type==2)
{
cuda_kernel_checkpoints_Out<<<dimGrid,dimBlock,0,plans[i].stream>>>
(
it, nt, ntx, ntz, nx, nz, L, dx, dz, dt,
plan[i].d_u1, plan[i].d_u2,
plan[i].d_u_cp, N_cp, plan[i].d_t_cp
);
}
// write wavefields at checkpoints and last two time steps
if(Sto_Rec==1&&vp_type==2||Save_Not==1)
{
cudaMemcpyAsync(plan[i].u2,plan[i].d_u2,sizeof(cufftComplex)*ntp,cudaMemcpyDeviceToHost,plans[i].stream);
sprintf(filename,"./output/GPU_%d_u2_%d.dat",i,it);
fp=fopen(filename,"wb");
for(ix=0;ix<ntx-0;ix++)
{
for(iz=0;iz<ntz-0;iz++)
{
u2_real[iz*ntx+ix]=plan[i].u2[iz*ntx+ix].x;
fwrite(&u2_real[iz*ntx+ix],sizeof(float),1,fp);
}
}
fclose(fp);
}
// updating wavefields
cuda_kernel_update<<<dimGrid,dimBlock,0,plans[i].stream>>>
(ntx, ntz, plan[i].d_u0, plan[i].d_u1, plan[i].d_u2);
if (cudaSuccess != cudaGetLastError())
printf( "Error!\n" );
if(myid==0&&it%100==0)
{
printf("shot %d forward %d has finished!\n", is+i+1, it);
}
}// GPU_N end
}// nt end
for(i=0;i<GPU_N;i++)
{
cudaSetDevice(i);
// copy seismograms to host memory
if(vp_type==0) // homogeneous model
{
cudaMemcpyAsync(plan[i].seismogram_dir,plan[i].d_seismogram,
sizeof(float)*ss[is+i].r_n*nt,cudaMemcpyDeviceToHost,plans[i].stream);
}
else if(vp_type==1) // ture model
{
cudaMemcpyAsync(plan[i].seismogram_obs,plan[i].d_seismogram,
sizeof(float)*ss[is+i].r_n*nt,cudaMemcpyDeviceToHost,plans[i].stream);
}
else if(vp_type==2) // initial model
{
cudaMemcpyAsync(plan[i].seismogram_syn,plan[i].d_seismogram,