-
Notifications
You must be signed in to change notification settings - Fork 19
/
tridiag.F90
1527 lines (1324 loc) · 58.3 KB
/
tridiag.F90
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
#ifdef DEBUG
module ep_debug
use precision
use decomp_2d
use mpih
#ifdef USE_CUDA
use cudafor
#endif
implicit none
integer :: counter=0
interface compare
module procedure compare_cpu_1d
module procedure compare_cpu_3d
module procedure compare_cpu_complex_3d
#ifdef USE_CUDA
module procedure compare_gpu_1d
module procedure compare_cpugpu_1d
module procedure compare_gpu_3d
module procedure compare_gpu_complex_3d
module procedure compare_cpugpu_3d
#endif
end interface compare
interface write_plane_jk
module procedure write_cpu_plane_jk
#ifdef USE_CUDA
module procedure write_gpu_plane_jk
#endif
end interface write_plane_jk
contains
subroutine compare_cpu_1d(A,filename)
implicit none
real(fp_kind), dimension(:), intent(IN) :: A
character (len=*), intent(IN) :: filename
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
!print *,"open file..."
open(unit=10, status='replace',file=filename//TRIM(ADJUSTL(itcount))//".dbg."//TRIM(ADJUSTL(proc)), form='unformatted')
!print *,"write file..."
write(10) A
!print *,"close file..."
close(10)
end subroutine compare_cpu_1d
subroutine compare_cpu_3d(A,filename)
implicit none
real(fp_kind), dimension(:,:,:), intent(IN) :: A
character (len=*), intent(IN) :: filename
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
!print *,"open file..."
open(unit=10, status='replace', file=filename//TRIM(ADJUSTL(itcount))//".dbg."//TRIM(ADJUSTL(proc)), form='unformatted')
!print *,"write file..."
write(10) A
!print *,"close file..."
close(10)
end subroutine compare_cpu_3d
subroutine compare_cpu_complex_3d(A,filename)
implicit none
complex(fp_kind), dimension(:,:,:), intent(IN) :: A
character (len=*), intent(IN) :: filename
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
!print *,"open file..."
open(unit=10, status='replace',file=filename//TRIM(ADJUSTL(itcount))//".dbg."//TRIM(ADJUSTL(proc)), form='unformatted')
!print *,"write file..."
write(10) A
!print *,"close file..."
close(10)
end subroutine compare_cpu_complex_3d
#ifdef USE_CUDA
subroutine compare_gpu_1d(A,filename)
implicit none
real(fp_kind), dimension(:), device :: A
character (len=*), intent(IN) :: filename
real(fp_kind), dimension(:), allocatable :: A_d
real(fp_kind), dimension(:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf
integer :: i,imax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
allocate(A_h, source=A)
allocate(A_d, source=A)
A_h=0.0d0
open(unit=10, status='old', file=filename//TRIM(ADJUSTL(itcount))//".dbg."//TRIM(ADJUSTL(proc)),form='unformatted')
read(10) A_h
close(10)
l2normerr = 0.d0
norm = 0.d0
maxerr = 0.d0
imax=1
do i=lbound(A,1),ubound(A,1)
!print *,nrank,"compare1d i: ",i," cpu: ",A_h(i)
!print *,nrank,"compare1d i: ",i," gpu: ",A_d(i)
if(abs(A_h(i)) > 1e-10) then
perr = abs(A_h(i) - A_d(i))/abs(A_h(i))*100.d0
norm = norm + A_h(i)*A_h(i);
l2normerr = l2normerr + (A_h(i) - A_d(i))*(A_h(i) - A_d(i))
else
perr = 0.0d0
endif
if(perr>maxerr .and. A_h(i) /= 0.d0) then
maxerr = perr
imax = i
endif
enddo
call MPI_ALLREDUCE(norm,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
norm = buf
call MPI_ALLREDUCE(l2normerr,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
l2normerr = buf
call MPI_ALLREDUCE(maxerr,buf,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
if(maxerr == buf) then
norm = sqrt(norm)
l2normerr = sqrt(l2normerr)
if(l2normerr /= 0.d0) then
l2normerr = l2normerr/norm
write(*,"(I4,A8,A16,2X,ES10.3,A12,ES10.3,A6,I5,A6,2X,E20.14,2X,A6,2X,E20.14)")nrank,filename,"l2norm error",l2normerr,"max error",maxerr,"% at",imax,"cpu=",A_h(imax),"gpu=",A_d(imax)
else
if(nrank==0) write(*,"(A8,A16)") filename, "EXACT MATCH"
endif
endif
!if(counter<=900) A = A_h
deallocate(A_h,A_d)
end subroutine compare_gpu_1d
subroutine compare_gpu_complex_3d(A,filename)
implicit none
complex(fp_kind), dimension(:,:,:), device :: A
character (len=*), intent(IN) :: filename
complex(fp_kind), dimension(:,:,:), allocatable :: A_d
complex(fp_kind), dimension(:,:,:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
allocate(A_h, source=A)
allocate(A_d, source=A)
A_h=0.0d0
open(unit=10, status='old', file=filename//TRIM(ADJUSTL(itcount))//".dbg."//TRIM(ADJUSTL(proc)),form='unformatted')
read(10) A_h
close(10)
l2normerr = 0.d0
norm = 0.d0
maxerr = 0.d0
imax=1
jmax=1
kmax=1
do k=lbound(A,3),ubound(A,3)
do j=lbound(A,2),ubound(A,2)
do i=lbound(A,1),ubound(A,1)
if(abs(A_h(i,j,k)) >= 1e-10) then
perr = abs(A_h(i,j,k) - A_d(i,j,k))/abs(A_h(i,j,k))*100.d0
norm = norm + abs(A_h(i,j,k)*A_h(i,j,k));
l2normerr = l2normerr + abs((A_h(i,j,k) - A_d(i,j,k))*(A_h(i,j,k) - A_d(i,j,k)))
else
perr = 0.d0
endif
if(perr>maxerr .and. a_h(i,j,k)/=0.0d0 .and. a_d(i,j,k)/=0.0d0) then
maxerr = perr
imax = i
jmax = j
kmax = k
endif
!if(counter==1504 .and. perr /= 0.d0) then
#if 0
if(k<=2.and.j<=2.and.i<3) then
write(*,"(I4,1X,I3,I3,I3,1X,A4,1X,E20.14,1X,A4,E20.14)") nrank,i,j,k,"cpu=",REAL(A_h(i,j,k))," ",AIMAG(A_h(i,j,k))
write(*,"(I4,1X,I3,I3,I3,1X,A4,1X,E20.14,1X,A4,E20.14)") nrank,i,j,k,"gpu=",REAL(A_d(i,j,k))," ",AIMAG(A_d(i,j,k))
!pause
endif
#endif
enddo
enddo
enddo
call MPI_ALLREDUCE(norm,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
norm = buf
call MPI_ALLREDUCE(l2normerr,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
l2normerr = buf
!print *,"maxerr=",maxerr
call MPI_ALLREDUCE(maxerr,buf,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
!print *,"merr,buf=",maxerr,buf
if(maxerr == buf) then
norm = sqrt(norm)
l2normerr = sqrt(l2normerr)
if(l2normerr /= 0.d0) then
l2normerr = l2normerr/norm
write(*,"(I4,A8,A16,2X,ES10.3,A12,ES10.3,A6,I5,I5,I5,A6,2X,E20.14,1X,E20.14,2X,A6,2X,E20.14,1X,E20.14)") &
nrank,filename,"l2norm error",l2normerr,"max error",maxerr,"% at",kmax,jmax,imax,"cpu=",REAL(A_h(imax,jmax,kmax)),AIMAG(A_h(imax,jmax,kmax)),"gpu=",REAL(A_d(imax,jmax,kmax)),AIMAG(A_d(imax,jmax,kmax))
else
if(nrank==0) write(*,"(A8,A16)") filename, "EXACT MATCH"
endif
endif
!if(counter<=900) A = A_h
deallocate(A_h,A_d)
end subroutine compare_gpu_complex_3d
subroutine compare_cpugpu_1d(A1,A2)
implicit none
real(fp_kind), dimension(:), device :: A2
real(fp_kind), dimension(:) :: A1
real(fp_kind), dimension(:), allocatable :: A_d
real(fp_kind), dimension(:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf,tl2n,yl2n,cl2n
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
allocate(A_h, source=A1)
allocate(A_d, source=A2)
l2normerr = 0.d0
cl2n = 0.d0
norm = 0.d0
maxerr = 0.d0
imax=1
do i=lbound(A1,1),ubound(A1,1)
if(abs(A_h(i)) >= 1e-10) then
perr = abs(A_h(i) - A_d(i))/abs(A_h(i))*100.d0
norm = norm + A_h(i)*A_h(i);
yl2n = (A_h(i) - A_d(i))*(A_h(i) - A_d(i)) - cl2n
tl2n = l2normerr + yl2n
cl2n = (tl2n-l2normerr) - yl2n
l2normerr = tl2n
else
perr = 0.d0
endif
if(perr>maxerr .and. a_h(i)/=0.0d0 .and. a_d(i)/=0.0d0) then
maxerr = perr
imax = i
endif
enddo
call MPI_ALLREDUCE(norm,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
norm = buf
call MPI_ALLREDUCE(l2normerr,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
l2normerr = buf
!print *,"maxerr=",maxerr
call MPI_ALLREDUCE(maxerr,buf,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
!print *,"merr,buf=",maxerr,buf
if(maxerr == buf) then
norm = sqrt(norm)
l2normerr = sqrt(l2normerr)
if(l2normerr /= 0.d0) then
l2normerr = l2normerr/norm
write(*,"(I4,A16,2X,ES10.3,A12,ES10.3,A6,I5,I5,I5,A6,2X,E20.14,2X,A6,2X,E20.14)") nrank,"l2norm error",l2normerr,"max error",maxerr,"% at",imax,"cpu=",A_h(imax),"gpu=",A_d(imax)
else
if(nrank==0) write(*,"(A8,A16)") ,"EXACT MATCH"
endif
endif
!if(counter<=900) A = A_h
deallocate(A_h,A_d)
!A1 = A2
end subroutine compare_cpugpu_1d
subroutine compare_gpu_3d(A,filename)
implicit none
real(fp_kind), dimension(:,:,:), device :: A
character (len=*), intent(IN) :: filename
real(fp_kind), dimension(:,:,:), allocatable :: A_d
real(fp_kind), dimension(:,:,:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf,tl2n,yl2n,cl2n
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
allocate(A_h, source=A)
allocate(A_d, source=A)
A_h=0.0d0
open(unit=10, status='old', file=filename//TRIM(ADJUSTL(itcount))//".dbg."//TRIM(ADJUSTL(proc)), form='unformatted')
read(10) A_h
close(10)
l2normerr = 0.d0
cl2n = 0.d0
norm = 0.d0
maxerr = 0.d0
imax=1
jmax=1
kmax=1
do k=lbound(A,3),ubound(A,3)
do j=lbound(A,2),ubound(A,2)
do i=lbound(A,1),ubound(A,1)
if(abs(A_h(i,j,k)) >= 1e-10) then
perr = abs(A_h(i,j,k) - A_d(i,j,k))/abs(A_h(i,j,k))*100.d0
norm = norm + A_h(i,j,k)*A_h(i,j,k);
yl2n = (A_h(i,j,k) - A_d(i,j,k))*(A_h(i,j,k) - A_d(i,j,k)) - cl2n
tl2n = l2normerr + yl2n
cl2n = (tl2n-l2normerr) - yl2n
l2normerr = tl2n
else
perr = 0.d0
endif
if(perr>maxerr .and. a_h(i,j,k)/=0.0d0 .and. a_d(i,j,k)/=0.0d0) then
maxerr = perr
imax = i
jmax = j
kmax = k
endif
enddo
enddo
enddo
call MPI_ALLREDUCE(norm,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
norm = buf
call MPI_ALLREDUCE(l2normerr,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
l2normerr = buf
!print *,"maxerr=",maxerr
call MPI_ALLREDUCE(maxerr,buf,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
!print *,"merr,buf=",maxerr,buf
if(maxerr == buf) then
norm = sqrt(norm)
l2normerr = sqrt(l2normerr)
if(l2normerr /= 0.d0) then
l2normerr = l2normerr/norm
write(*,"(I4,A8,A16,2X,ES10.3,A12,ES10.3,A6,I5,I5,I5,A6,2X,E20.14,2X,A6,2X,E20.14)") nrank,filename,"l2norm error",l2normerr,"max error",maxerr,"% at",kmax,jmax,imax,"cpu=",A_h(imax,jmax,kmax),"gpu=",A_d(imax,jmax,kmax)
else
if(nrank==0) write(*,"(A8,A16)") filename, "EXACT MATCH"
endif
endif
!if(counter<=900) A = A_h
deallocate(A_h,A_d)
end subroutine compare_gpu_3d
subroutine compare_cpugpu_3d(A1,A2)
implicit none
real(fp_kind), dimension(:,:,:), device :: A2
real(fp_kind), dimension(:,:,:) :: A1
real(fp_kind), dimension(:,:,:), allocatable :: A_d
real(fp_kind), dimension(:,:,:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf,tl2n,yl2n,cl2n
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
write(proc,'(i1)') nrank
allocate(A_h, source=A1)
allocate(A_d, source=A2)
l2normerr = 0.d0
cl2n = 0.d0
norm = 0.d0
maxerr = 0.d0
imax=1
jmax=1
kmax=1
do k=lbound(A1,3),ubound(A1,3)
do j=lbound(A1,2),ubound(A1,2)
do i=lbound(A1,1),ubound(A1,1)
if(abs(A_h(i,j,k)) >= 1e-10) then
perr = abs(A_h(i,j,k) - A_d(i,j,k))/abs(A_h(i,j,k))*100.d0
norm = norm + A_h(i,j,k)*A_h(i,j,k);
yl2n = (A_h(i,j,k) - A_d(i,j,k))*(A_h(i,j,k) - A_d(i,j,k)) - cl2n
tl2n = l2normerr + yl2n
cl2n = (tl2n-l2normerr) - yl2n
l2normerr = tl2n
else
perr = 0.d0
endif
if(perr>maxerr .and. a_h(i,j,k)/=0.0d0 .and. a_d(i,j,k)/=0.0d0) then
maxerr = perr
imax = i
jmax = j
kmax = k
endif
enddo
enddo
enddo
call MPI_ALLREDUCE(norm,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
norm = buf
call MPI_ALLREDUCE(l2normerr,buf,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
l2normerr = buf
!print *,"maxerr=",maxerr
call MPI_ALLREDUCE(maxerr,buf,1,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
!print *,"merr,buf=",maxerr,buf
if(maxerr == buf) then
norm = sqrt(norm)
l2normerr = sqrt(l2normerr)
if(l2normerr /= 0.d0) then
l2normerr = l2normerr/norm
write(*,"(I4,A16,2X,ES10.3,A12,ES10.3,A6,I5,I5,I5,A6,2X,E20.14,2X,A6,2X,E20.14)") nrank,"l2norm error",l2normerr,"max error",maxerr,"% at",imax,jmax,kmax,"cpu=",A_h(imax,jmax,kmax),"gpu=",A_d(imax,jmax,kmax)
else
if(nrank==0) write(*,"(A8,A16)") ,"EXACT MATCH"
endif
endif
!if(counter<=900) A = A_h
deallocate(A_h,A_d)
!A1 = A2
end subroutine compare_cpugpu_3d
subroutine write_cpu_plane_jk(A1,iw,filename)
implicit none
real(fp_kind), dimension(:,:,:) :: A1
integer, intent(IN) :: iw
character (len=*), intent(IN) :: filename
real(fp_kind), dimension(:,:,:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf,tl2n,yl2n,cl2n
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
allocate(A_h, source=A1)
open(unit=10, status='replace',file=filename//".planejk.cpu.dat")
do k=lbound(A1,3),ubound(A1,3)
do j=lbound(A1,2),ubound(A1,2)
write(10,"(ES30.16,1x)", advance='no') A_h(iw,j,k)
end do
write(10,*)
end do
close(10)
deallocate(A_h)
end subroutine write_cpu_plane_jk
subroutine write_gpu_plane_jk(A1,iw,filename)
implicit none
real(fp_kind), dimension(:,:,:), device :: A1
integer, intent(IN) :: iw
character (len=*), intent(IN) :: filename
real(fp_kind), dimension(:,:,:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf,tl2n,yl2n,cl2n
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
allocate(A_h, source=A1)
open(unit=10, status='replace',file=filename//".planejk.gpu.dat")
do k=lbound(A1,3),ubound(A1,3)
do j=lbound(A1,2),ubound(A1,2)
write(10,"(ES30.16,1x)", advance='no') A_h(iw,j,k)
end do
write(10,*)
end do
close(10)
deallocate(A_h)
end subroutine write_gpu_plane_jk
subroutine write_cpugpu_2d_ik(A1,A2,jw,filename)
implicit none
real(fp_kind), dimension(:,:,:), device :: A2
real(fp_kind), dimension(:,:,:) :: A1
integer, intent(IN) :: jw
character (len=*), intent(IN) :: filename
real(fp_kind), dimension(:,:,:), allocatable :: A_d
real(fp_kind), dimension(:,:,:), allocatable :: A_h
real(fp_kind) :: maxerr,perr,l2normerr,norm,buf,tl2n,yl2n,cl2n
integer :: i,j,k,imax,jmax,kmax
character (len=4) :: itcount
character (len=1) :: proc
counter = counter + 1
write(itcount,'(i4)') counter
allocate(A_h, source=A1)
allocate(A_d, source=A2)
open(unit=10, status='replace',file=filename//".planeik.cpu.dat")
do k=lbound(A1,3),ubound(A1,3)
do i=lbound(A1,1),ubound(A1,1)
write(10,"(ES12.5,1x)", advance='no') A_h(i,jw,k)
end do
write(10,*)
end do
close(10)
open(unit=10, status='replace',file=filename//".planeik.gpu.dat")
do k=lbound(A1,3),ubound(A1,3)
do i=lbound(A1,1),ubound(A1,1)
write(10,"(ES12.5, 1x)", advance='no') A_d(i,jw,k)
end do
write(10,*)
end do
close(10)
!A1 = A2
deallocate(A_h,A_d)
end subroutine write_cpugpu_2d_ik
#endif
end module ep_debug
#endif
#ifdef USE_CUDA
!#define USE_DBLDBL
module ep_solve
use precision
use cudafor
use decomp_2d, only: a2a_comp
!#ifdef USE_DBLDBL
use libm
type dbldbl
sequence
real(fp_kind) hi,lo
end type dbldbl
!#endif
contains
attributes(global) subroutine tepDgtsv_nopivot_kernel(n,nrhs,a,b,c,x,ldx,tmp)
implicit none
integer, value, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), device, intent(in) :: a,b,c
real(fp_kind), dimension(nrhs,ldx), device :: x,tmp
real(fp_kind) :: m
integer irhs,i
irhs = threadIdx%x + (blockIdx%x-1)*blockDim%x
if ( irhs <= nrhs) then
tmp(irhs,1) = c(1) /b(1)
x(irhs,1) = x(irhs,1)/b(1)
do i=2,N
m = b(i) - tmp(irhs,i-1)*a(i)
tmp(irhs,i) = c(i)/m
x(irhs,i) = (x(irhs,i) - x(irhs,i-1)*a(i))/m
end do
do i=n-1,1,-1
x(irhs,i) = x(irhs,i) - tmp(irhs,i)*x(irhs,i+1)
end do
end if
end subroutine tepDgtsv_nopivot_kernel
#ifdef USE_HYBRID
!JR TODO: Using a slow carbon-copy of cuda kernel. Can remove transponse and use openmp to improve.
subroutine tepDgtsv_nopivot_kernel_cpu(n,nrhs,a,b,c,x,ldx,tmp)
implicit none
integer, value, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), intent(in) :: a,b,c
real(fp_kind), dimension(nrhs,ldx) :: x,tmp
real(fp_kind) :: m
integer irhs,i
!$OMP PARALLEL DO DEFAULT(none) PRIVATE(irhs, i, m) SHARED(n, nrhs, tmp, a, b, c, x)
do irhs = 1, nrhs
tmp(irhs,1) = c(1) /b(1)
x(irhs,1) = x(irhs,1)/b(1)
do i=2,N
m = b(i) - tmp(irhs,i-1)*a(i)
tmp(irhs,i) = c(i)/m
x(irhs,i) = (x(irhs,i) - x(irhs,i-1)*a(i))/m
end do
do i=n-1,1,-1
x(irhs,i) = x(irhs,i) - tmp(irhs,i)*x(irhs,i+1)
end do
end do
!$OMP END PARALLEL DO
end subroutine tepDgtsv_nopivot_kernel_cpu
subroutine epDgtsv_nopivot_kernel_cpu(n,nrhs,a,b,c,x,ldx,tmp)
implicit none
integer, value, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), intent(in) :: a,b,c
real(fp_kind), dimension(ldx, nrhs) :: x,tmp
real(fp_kind) :: m
integer irhs,i
!$OMP PARALLEL DO DEFAULT(none) PRIVATE(irhs, i, m) SHARED(n, nrhs, tmp, a, b, c, x)
do irhs = 1, nrhs
tmp(1,irhs) = c(1) /b(1)
x(1,irhs) = x(1,irhs)/b(1)
do i=2,N
m = b(i) - tmp(i-1,irhs)*a(i)
tmp(i,irhs) = c(i)/m
x(i,irhs) = (x(i,irhs) - x(i-1,irhs)*a(i))/m
end do
do i=n-1,1,-1
x(i,irhs) = x(i,irhs) - tmp(i,irhs)*x(i+1,irhs)
end do
end do
!$OMP END PARALLEL DO
end subroutine epDgtsv_nopivot_kernel_cpu
#endif
attributes(global) subroutine tran_rhs_kernel(idata,odata,n1,n2)
implicit none
integer, value, intent(IN) :: n1,n2
real(fp_kind), dimension(n2,n1), device, intent(IN) :: idata
real(fp_kind), dimension(n1,n2), device, intent(OUT) :: odata
integer :: i,j
i = threadIdx%x + (blockIdx%x-1)*blockDim%x
j = threadIdx%y + (blockIdx%y-1)*blockDim%y
if(i<=n1 .and. j<=n2) then
odata(i,j) = idata(j,i)
end if
end subroutine tran_rhs_kernel
subroutine tepDgtsv_nopivot(n,nrhs,a,b,c,x,ldx)
use decomp_2d, only: rhs_t_d=>work2_r_d
implicit none
integer, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), device, intent(IN) :: a,b,c
real(fp_kind), dimension(LDX,NRHS), device :: x
type(dim3) :: tBlock, grid, tBlock_tran, grid_tran
tBlock_tran = dim3(16,16,1)
grid_tran = dim3(ceiling(real(nrhs)/tBlock_tran%x), ceiling(real(n)/tBlock_tran%y), 1)
#ifdef USE_HYBRID
call tran_rhs_kernel<<<grid_tran,tBlock_tran,0,a2a_comp>>>(x,rhs_t_d,nrhs,ldx)
#else
call tran_rhs_kernel<<<grid_tran,tBlock_tran>>>(x,rhs_t_d,nrhs,ldx)
#endif
tBlock = dim3(128,1,1)
grid = dim3(ceiling(real(nrhs)/tBlock%x), 1, 1)
#ifdef USE_HYBRID
call tepDgtsv_nopivot_kernel<<<grid,tBlock,0,a2a_comp>>>(n,nrhs,a,b,c,rhs_t_d,ldx,x)
#else
call tepDgtsv_nopivot_kernel<<<grid,tBlock>>>(n,nrhs,a,b,c,rhs_t_d,ldx,x)
#endif
tBlock_tran = dim3(16,16,1)
grid_tran = dim3(ceiling(real(n)/tBlock_tran%x),ceiling(real(nrhs)/tBlock_tran%y), 1)
#ifdef USE_HYBRID
call tran_rhs_kernel<<<grid_tran,tBlock_tran,0,a2a_comp>>>(rhs_t_d,x,ldx,nrhs)
#else
call tran_rhs_kernel<<<grid_tran,tBlock_tran>>>(rhs_t_d,x,ldx,nrhs)
#endif
end subroutine tepDgtsv_nopivot
#ifdef USE_HYBRID
subroutine tepDgtsv_nopivot_cpu(n,nrhs,a,b,c,x,ldx)
implicit none
integer, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), intent(IN) :: a,b,c
real(fp_kind), dimension(LDX,NRHS) :: x
real(fp_kind), dimension(NRHS,LDX) :: rhs_t
integer :: irhs, i
do irhs = 1,nrhs
do i = 1, LDX
rhs_t(irhs, i) = x(i, irhs)
end do
end do
call tepDgtsv_nopivot_kernel_cpu(n,nrhs,a,b,c,rhs_t,ldx,x)
do irhs = 1,nrhs
do i = 1, LDX
x(i, irhs) = rhs_t(irhs, i)
end do
end do
end subroutine tepDgtsv_nopivot_cpu
subroutine epDgtsv_nopivot_cpu(n,nrhs,a,b,c,x,ldx)
implicit none
integer, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), intent(IN) :: a,b,c
real(fp_kind), dimension(NRHS,LDX) :: x
real(fp_kind), dimension(NRHS,LDX) :: tmp
integer :: irhs, i
call epDgtsv_nopivot_kernel_cpu(n,nrhs,a,b,c,x,ldx,tmp)
end subroutine epDgtsv_nopivot_cpu
#endif
attributes(global) subroutine epDgtsv_nopivot_kernel(n,nrhs,a,b,c,x,ldx,tmp)
implicit none
integer, value, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), device, intent(in) :: a,b,c
real(fp_kind), dimension(ldx,nrhs), device :: x,tmp
real(fp_kind) :: m
integer irhs,i
irhs = threadIdx%x + (blockIdx%x-1)*blockDim%x
if ( irhs <= nrhs) then
tmp(1,irhs) = c(1) /b(1)
x(1,irhs) = x(1,irhs)/b(1)
do i=2,N
m = b(i) - tmp(i-1,irhs)*a(i)
tmp(i,irhs) = c(i)/m
x(i,irhs) = (x(i,irhs) - x(i-1,irhs)*a(i))/m
end do
do i=n-1,1,-1
x(i,irhs) = x(i,irhs) - tmp(i,irhs)*x(i+1,irhs)
end do
end if
end subroutine epDgtsv_nopivot_kernel
subroutine epDgtsv_nopivot(n,nrhs,a,b,c,x,ldx,tmp)
implicit none
integer, intent(in) :: n,nrhs,ldx
real(fp_kind), dimension(*), device, intent(IN) :: a,b,c
real(fp_kind), dimension(LDX,NRHS), device :: x,tmp
type(dim3) :: tBlock, grid
tBlock = dim3(128,1,1)
grid = dim3(ceiling(real(nrhs)/tBlock%x), 1, 1)
call epDgtsv_nopivot_kernel<<<grid,tBlock>>>(n,nrhs,a,b,c,x,ldx,tmp)
end subroutine epDgtsv_nopivot
attributes(global) subroutine tepZgtsv_pressure_kernel(n,nrhs,ny,nz,amphk,acphk,apphk,ak1,ak2,x,ldx,tmp,joff,koff)
implicit none
integer, value, intent(in) :: n,nrhs,ny,nz,ldx,joff,koff
real(fp_kind), dimension(:), device, intent(in) :: amphk,acphk,apphk,ak1,ak2
complex(fp_kind), dimension(nrhs,ldx), device :: x
!pgi$ ignore_tkr (t) tmp
real(fp_kind), dimension(nrhs,ldx), device :: tmp
real(fp_kind) :: m,acphT_b,a,c
integer :: irhs,i,j,k
irhs = threadIdx%x + (blockIdx%x-1)*blockDim%x
j = mod(irhs-1,ny) + 1 + joff - 1
k = (irhs+ny-1)/ny + koff - 1
if ( irhs <= nrhs) then
acphT_b = 1.0d0/(acphk(1)-ak2(j)-ak1(k))
tmp(irhs,1) = apphk(1)*acphT_b
x(irhs,1) = x(irhs,1)*acphT_b
do i=2,N
acphT_b = 1.0d0/(acphk(i)-ak2(j)-ak1(k))
a = amphk(i)*acphT_b
c = apphk(i)*acphT_b
! When using the FMA instruction, m could be zero
!m = 1.0d0 - tmp(irhs,i-1)*a
! explicit use of rounded multiply
m = 1.0d0 - __dmul_rn( tmp(irhs,i-1),a)
tmp(irhs,i) = c/m
x(irhs,i) = (x(irhs,i)*acphT_b - x(irhs,i-1)*a)/m
end do
do i=n-1,1,-1
x(irhs,i) = x(irhs,i) - tmp(irhs,i)*x(irhs,i+1)
end do
end if
end subroutine tepZgtsv_pressure_kernel
attributes(global) subroutine tran_complex_rhs_kernel(idata,odata,n1,n2)
implicit none
integer, value, intent(IN) :: n1,n2
complex(fp_kind), dimension(n2,n1), device, intent(IN) :: idata
complex(fp_kind), dimension(n1,n2), device, intent(OUT) :: odata
integer :: i,j
i = threadIdx%x + (blockIdx%x-1)*blockDim%x
j = threadIdx%y + (blockIdx%y-1)*blockDim%y
if(i<=n1 .and. j<=n2) then
odata(i,j) = idata(j,i)
end if
end subroutine tran_complex_rhs_kernel
subroutine tepZgtsv_pressure(n, ny, nz, amphk, acphk, apphk, ak1, ak2, x, ldx, joff, koff, work)
!use fftw_params, only: dphc_t_d
implicit none
integer, intent(in) :: n,ny,nz,ldx,joff,koff
real(fp_kind), dimension(:), device, intent(IN) :: amphk,acphk,apphk,ak1,ak2
complex(fp_kind), dimension(ny*nz,n), device :: work
complex(fp_kind), dimension(n,ny*nz), device :: x
type(dim3) :: tBlock, grid, tBlock_tran, grid_tran
integer :: nrhs
nrhs = ny*nz
tBlock_tran = dim3(16,16,1)
grid_tran = dim3(ceiling(real(nrhs)/tBlock_tran%x),ceiling(real(n)/tBlock_tran%y), 1)
call tran_complex_rhs_kernel<<<grid_tran,tBlock_tran>>>(x,work,nrhs,ldx)
tBlock = dim3(128,1,1)
grid = dim3(ceiling(real(nrhs)/tBlock%x), 1, 1)
call tepZgtsv_pressure_kernel<<<grid,tBlock>>>(n,nrhs,ny,nz,amphk,acphk,apphk,ak1,ak2,work,ldx,x,joff,koff)
tBlock_tran = dim3(16,16,1)
grid_tran = dim3(ceiling(real(n)/tBlock_tran%x),ceiling(real(nrhs)/tBlock_tran%y), 1)
call tran_complex_rhs_kernel<<<grid_tran,tBlock_tran>>>(work,x,ldx,nrhs)
end subroutine tepZgtsv_pressure
attributes(device) type(dbldbl) function add_dbldbl(a,b) !add=a+b
implicit none
type(dbldbl) a, b
real(fp_kind) t1, t2, t3, t4, t5, e
t1 = __dadd_rn (a%hi, b%hi)
t2 = __dadd_rn (t1, -a%hi)
t3 = __dadd_rn (__dadd_rn (a%hi, t2 - t1), __dadd_rn (b%hi, -t2))
t4 = __dadd_rn (a%lo, b%lo)
t2 = __dadd_rn (t4, -a%lo)
t5 = __dadd_rn (__dadd_rn (a%lo, t2 - t4), __dadd_rn (b%lo, -t2))
t3 = __dadd_rn (t3, t4)
t4 = __dadd_rn (t1, t3)
t3 = __dadd_rn (t1 - t4, t3)
t3 = __dadd_rn (t3, t5)
e = __dadd_rn (t4, t3)
add_dbldbl%hi = e
add_dbldbl%lo = __dadd_rn (t4 - e, t3)
end function add_dbldbl
attributes(device) type(dbldbl) function sub_dbldbl(a,b) !sub=a-b
implicit none
type(dbldbl) a, b
real(fp_kind) t1, t2, t3, t4, t5, e
t1 = __dadd_rn (a%hi, -b%hi)
t2 = __dadd_rn (t1, -a%hi);
t3 = __dadd_rn (__dadd_rn (a%hi, t2 - t1), - __dadd_rn (b%hi, t2))
t4 = __dadd_rn (a%lo, -b%lo)
t2 = __dadd_rn (t4, -a%lo)
t5 = __dadd_rn (__dadd_rn (a%lo, t2 - t4), - __dadd_rn (b%lo, t2))
t3 = __dadd_rn (t3, t4)
t4 = __dadd_rn (t1, t3)
t3 = __dadd_rn (t1 - t4, t3)
t3 = __dadd_rn (t3, t5)
e = __dadd_rn (t4, t3)
sub_dbldbl%hi = e
sub_dbldbl%lo = __dadd_rn (t4 - e, t3)
end function sub_dbldbl
attributes(device) type(dbldbl) function mul_dbldbl(a,b) !mul=a*b
implicit none
type(dbldbl) a, b
type(dbldbl) t
real(fp_kind) e
t%hi = __dmul_rn (a%hi, b%hi)
t%lo = fma (a%hi, b%hi,-t%hi)
t%lo = fma (a%lo, b%lo, t%lo)
t%lo = fma (a%hi, b%lo, t%lo)
t%lo = fma (a%lo, b%hi, t%lo)
e = __dadd_rn (t%hi, t%lo)
mul_dbldbl%hi = e
mul_dbldbl%lo = __dadd_rn (t%hi - e, t%lo)
end function mul_dbldbl
attributes(device) type(dbldbl) function div_dbldbl(a,b) !div=a/b
implicit none
type(dbldbl) a, b
type(dbldbl) t
real(fp_kind) e, r
r = 1.0d0 / b%hi;
t%hi = __dmul_rn (a%hi, r)
e = fma (b%hi, -t%hi, a%hi)
t%hi = fma (r, e, t%hi)
t%lo = fma (b%hi, -t%hi, a%hi)
t%lo = __dadd_rn (a%lo, t%lo)
t%lo = fma (b%lo, -t%hi, t%lo)
e = __dmul_rn (r, t%lo)
t%lo = fma (b%hi, -e, t%lo)
t%lo = fma (r, t%lo, e)
e = __dadd_rn (t%hi, t%lo)
div_dbldbl%hi = e
div_dbldbl%lo = __dadd_rn (t%hi - e, t%lo)
end function div_dbldbl
attributes(device) type(dbldbl) function neg_dbldbl(a) !neg=-a
implicit none
type(dbldbl) a
neg_dbldbl%hi = -a%hi
neg_dbldbl%lo = -a%lo
end function neg_dbldbl
attributes(device) type(dbldbl) function mul_double_to_dbldbl(a,b) !mul=a*b
implicit none
real(fp_kind) a,b
real(fp_kind) c
c = __dmul_rn(a,b)
mul_double_to_dbldbl%hi = c
mul_double_to_dbldbl%lo = fma(a, b, -c)
end function mul_double_to_dbldbl
attributes(device) type(dbldbl) function add_double_to_dbldbl(a,b) !add=a+b
implicit none
real(fp_kind) a,b
real(fp_kind) t1, t2, c
c = __dadd_rn (a, b )
t1 = __dadd_rn (c, -a )
t2 = __dadd_rn (c, -t1)
t1 = __dadd_rn (b, -t1)
t2 = __dadd_rn (a, -t2)
add_double_to_dbldbl%hi = c
add_double_to_dbldbl%lo = __dadd_rn (t1, t2)
end function add_double_to_dbldbl
attributes(device) real(fp_kind) function get_dbldbl_hi(a) !get=a%hi
implicit none
type(dbldbl) a
get_dbldbl_hi = a%hi
end function get_dbldbl_hi
attributes(device) real(fp_kind) function get_dbldbl_lo(a) !get=a%lo
implicit none
type(dbldbl) a
get_dbldbl_lo = a%lo
end function get_dbldbl_lo
attributes(device) type(dbldbl) function make_dbldbl(a,b) !make%hi=a make%lo=b
implicit none
real(fp_kind) a, b
make_dbldbl%hi = a
make_dbldbl%lo = b
end function make_dbldbl
attributes(global) subroutine epZgtsv_pressure_piv_kernel_dd(n,nrhs,ny,nz,amphk,acphk,apphk,ak1,ak2,x,ldx,DLHI,DLLO,DHI,DLO,DUHI,DULO,XRHI,XRLO,XIHI,XILO,joff,koff)
implicit none
integer, value, intent(in) :: n,nrhs,ny,nz,ldx,joff,koff
real(fp_kind), dimension(:), device, intent(in) :: amphk,acphk,apphk,ak1,ak2
complex(fp_kind), dimension(ldx,nrhs), device :: x
! real(fp_kind), dimension(ldx,nrhs), device :: DLHI,DLLO,DHI,DLO,DUHI,DULO,XRHI,XRLO,XIHI,XILO
real(fp_kind), dimension(nrhs,ldx), device :: DLHI,DLLO,DHI,DLO,DUHI,DULO,XRHI,XRLO,XIHI,XILO
type(dbldbl) :: acphT_b,fact,duip,dlip
real(fp_kind) :: tempxrhi,tempxrlo,tempxihi,tempxilo
integer :: irhs,i,j,k
! j = threadIdx%x + (blockIdx%x-1)*blockDim%x
! k = threadIdx%y + (blockIdx%y-1)*blockDim%y
! irhs = (k-1)*ny + j
irhs = threadIdx%x + (blockIdx%x-1)*blockDim%x
j = mod(irhs-1,ny) + 1
k = (irhs+ny-1)/ny
if ( irhs <= nrhs .and. j<=ny .and. k<=nz) then
j = j + joff - 1