forked from edeprince3/gpu_dfcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpuhelper.cu
2127 lines (1889 loc) · 88.9 KB
/
gpuhelper.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
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
/*
*@BEGIN LICENSE
*
* GPU-accelerated density-fitted coupled-cluster, a plugin to:
*
* PSI4: an ab initio quantum chemistry software package
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*@END LICENSE
*/
// TODO: interleaved dgemm seems to be broken
bool interleaved_dgemm = true;
#include<psi4/libplugin/plugin.h>
#include<psi4/psi4-dec.h>
#include<psi4/liboptions/liboptions.h>
#include<psi4/libqt/qt.h>
#include<psi4/libtrans/integraltransform.h>
#include<psi4/libtrans/mospace.h>
#include<psi4/libmints/matrix.h>
#include<psi4/libmints/vector.h>
#include<psi4/libiwl/iwl.h>
#include<psi4/libpsio/psio.hpp>
#include<psi4/libpsi4util/process.h>
#include"blas.h"
#include"gpuhelper.h"
#include"gpuonly.h"
#include<omp.h>
using namespace psi;
using namespace std;
namespace psi{namespace fnocc{
void GPUHelper::Check_CUDA_Error(FILE*fp,const char *message){
cudaError_t error = cudaGetLastError();
if (error!=cudaSuccess) {
fprintf(fp,"\n ERROR: %s: %s\n\n", message, cudaGetErrorString(error) );
fflush(fp);
exit(-1);
}
}
/*===================================================================
initialize cublas and get device properties
===================================================================*/
void GPUHelper::CudaInitGPU(Options&options){
max_mapped_memory=0;
num_gpus=gpumemory=extraroom=0;
int n;
size_t freemem;
size_t total;
struct cudaDeviceProp cudaProp;
std::vector<unsigned long> mem;
cudaGetDeviceCount(&n);
num_gpus = n;
if ( num_gpus == 0 ) {
throw PsiException("no GPU found",__FILE__,__LINE__);
}
num_cpus=0;
if ( options["NUM_GPUS"].has_changed() ) {
num_gpus = options.get_int("NUM_GPUS");
}else {
unsigned long tmp_mem = 0;
long tmp_gpu = 0;
num_gpus = 1;
for(int i = 0; i < n; ++i){
cudaSetDevice(i);
cudaMemGetInfo(&freemem,&total);
if(freemem > tmp_mem){
tmp_mem = freemem;
tmp_gpu = i;
}
}
gpus_used.push_back(tmp_gpu);
mem.push_back(tmp_mem);
}
if (num_gpus>0 && options["NUM_GPUS"].has_changed()){
cublasInit();
int gpu_id;
//cudaGetDevice(&gpu_id);
stringstream tmp_str(options.get_str("ACTIVE_GPUS"));
string tmp_str2;
if(tmp_str.peek()!=-1){
while (getline(tmp_str, tmp_str2, '-')) {
gpus_used.push_back(stoi(tmp_str2));
}
} else {
for(int i = 0; i < n; ++i){
cudaSetDevice(i);
cudaMemGetInfo(&freemem,&total);
if (gpus_used.size()<num_gpus){
gpus_used.push_back(i);
mem.push_back(freemem);
} else {
int temp = 0;
int final = 0;
bool changed = false;
for(int j = 0; j < num_gpus; ++j){
if(mem[j] < mem[temp]){
temp = j;
}
}
if (mem[temp] < freemem){
final = temp;
changed = true;
}
if (changed){
gpus_used[final] = i;
mem[final] = freemem;
}
}
}
}
}
for(int i=0;i<num_gpus;i++){
cudaSetDevice(gpus_used[i]);
cudaGetDeviceProperties( &cudaProp,gpus_used[i] );
outfile->Printf(
"\n _________________________________________________________\n");
outfile->Printf(" CUDA device properties:\n");
outfile->Printf(" name: %20s\n",cudaProp.name);
outfile->Printf(" major version: %20d\n",cudaProp.major);
outfile->Printf(" minor version: %20d\n",cudaProp.minor);
outfile->Printf(" canMapHostMemory: %20d\n",cudaProp.canMapHostMemory);
outfile->Printf(" totalGlobalMem: %20lu mb\n",
cudaProp.totalGlobalMem/(1024*1024));
outfile->Printf(" sharedMemPerBlock: %20lu\n",cudaProp.sharedMemPerBlock);
outfile->Printf(" clockRate: %20.3f ghz\n",
cudaProp.clockRate/1.0e6);
outfile->Printf(" regsPerBlock: %20d\n",cudaProp.regsPerBlock);
outfile->Printf(" warpSize: %20d\n",cudaProp.warpSize);
outfile->Printf(" maxThreadsPerBlock: %20d\n",cudaProp.maxThreadsPerBlock);
outfile->Printf(
" _________________________________________________________\n\n");
cudaMemGetInfo(&freemem,&total);
freemem-=200L*1024L*1024L;
if(i==0) {
gpumemory = freemem;
}
if(freemem < gpumemory) {
gpumemory = freemem;
}
}
//gpumemory = cudaProp.totalGlobalMem;
cudaMemGetInfo(&freemem,&total);
//gpumemory = freemem;
extraroom = 200L*1024L*1024L;
cudaThreadExit();
// default memory for mapped cpu memory is the sum of all gpu memory
max_mapped_memory = gpumemory;
if (options["MAX_MAPPED_MEMORY"].has_changed()){
long int temp_mem = options.get_int("MAX_MAPPED_MEMORY");
temp_mem *= 1024L*1024L;
if (temp_mem<max_mapped_memory)
max_mapped_memory = temp_mem;
}
max_mapped_memory_per_thread = max_mapped_memory/(num_gpus+num_cpus);
outfile->Printf("\n");
outfile->Printf(" allocating gpu memory...");
//fflush(outfile);
tmp = (double**)malloc(num_gpus*sizeof(double*));
gpubuffer = (double**)malloc(num_gpus*sizeof(double*));
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (long int i=0; i<num_gpus; i++){
long int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
cudaSetDevice(gpus_used[thread]);
Check_CUDA_Error(stdout,"cudaSetDevice");
cudaMallocHost((void**)&tmp[thread],max_mapped_memory_per_thread);
//tmp[thread] = (double*)malloc(max_mapped_memory_per_thread*sizeof(double));
Check_CUDA_Error(stdout,"cpu tmp");
//cudaMemGetInfo(&freemem,&total);
cudaMalloc((void**)&gpubuffer[thread],gpumemory);
// cudaMalloc((void**)&gpubuffer[thread],gpumemory-extraroom);
Check_CUDA_Error(stdout,"gpu memory");
}
// thread-safe tiling info: TODO: these are never free'd at the end
myntilesM = (long int*)malloc(num_gpus*sizeof(long int));
myntilesN = (long int*)malloc(num_gpus*sizeof(long int));
myntilesK = (long int*)malloc(num_gpus*sizeof(long int));
mytilesizeM = (long int*)malloc(num_gpus*sizeof(long int));
mytilesizeN = (long int*)malloc(num_gpus*sizeof(long int));
mytilesizeK = (long int*)malloc(num_gpus*sizeof(long int));
mylasttileM = (long int*)malloc(num_gpus*sizeof(long int));
mylasttileN = (long int*)malloc(num_gpus*sizeof(long int));
mylasttileK = (long int*)malloc(num_gpus*sizeof(long int));
mytilesizesM = (long int**)malloc(num_gpus*sizeof(long int*));
mytilesizesN = (long int**)malloc(num_gpus*sizeof(long int*));
mytilesizesK = (long int**)malloc(num_gpus*sizeof(long int*));
//fflush(outfile);
// some cpu memory for cores to use when stealing gpu work
//cpuarray = (double**)malloc(num_cpus*sizeof(double*));
//for (long int i=0; i<num_cpus; i++){
// // TODO: need to be more intelligent about this...
// cpuarray[i] = (double*)malloc(3*max_mapped_memory_per_thread+20*max_mapped_memory_per_thread/30);
//}
}
/*===================================================================
free gpu and mapped cpu memory
===================================================================*/
void GPUHelper::CudaFinalizeGPU(Options&options){
if (num_gpus>0){
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (long int i=0; i<num_gpus; i++){
long int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
cudaSetDevice(gpus_used[thread]);
Check_CUDA_Error(stdout,"cudaSetDevice (free)");
cudaFreeHost(tmp[thread]);
Check_CUDA_Error(stdout,"cpu tmp (free)");
cudaFree(gpubuffer[thread]);
Check_CUDA_Error(stdout,"gpu memory (free)");
}
free(tmp);
free(gpubuffer);
//for (long int i=0; i<num_cpus; i++){
// free(cpuarray[i]);
//}
//free(cpuarray);
}
}
/**
* dgemm assuming no tiling is necessary
*/
void GPUHelper::GPU_DGEMM(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc){
double*gpuA,*gpuB,*gpuC;
cudaMalloc((void**)&gpuA,m*k*sizeof(double));
cudaMalloc((void**)&gpuB,n*k*sizeof(double));
cudaMalloc((void**)&gpuC,m*n*sizeof(double));
cudaMemcpy(gpuA,A,m*k*sizeof(double),cudaMemcpyHostToDevice);
cudaMemcpy(gpuB,B,n*k*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,m,n,k,alpha,gpuA,lda,gpuB,ldb,beta,gpuC,ldc);
cudaMemcpy(C,gpuC,m*n*sizeof(double),cudaMemcpyDeviceToHost);
cudaFree(gpuA);
cudaFree(gpuB);
cudaFree(gpuC);
}
void GPUHelper::freecudamem(){
cudaFree(myntilesM);
cudaFree(myntilesN);
cudaFree(myntilesK);
cudaFree(mytilesizeM);
cudaFree(mytilesizeN);
cudaFree(mytilesizeK);
cudaFree(mylasttileM);
cudaFree(mylasttileN);
cudaFree(mylasttileK);
cudaFree(mytilesizesM);
cudaFree(mytilesizesN);
cudaFree(mytilesizesK);
}
/**
* dgemm using a 2-dimensional tile - threaded versions for multiple gpus
*/
void GPUHelper::GPU_DGEMM_2DTile_nn_threaded_WithCpuStealing(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc){
throw PsiException("GPU_DGEMM_2DTile_nn_threaded_WithCpuStealing: not implemented",__FILE__,__LINE__);
//DPG commented out to remove statement unreachable warning
/*
TilingWithCpuStealing((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
//Tiling((gpumemory-extraroom)/8L,max_mapped_memory/num_gpus/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else
for (long int i=0; i<n*ldc; i++) C[i] *= beta;
#pragma omp parallel num_threads(num_gpus+num_cpus)
{
long int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
double*gpuA,*gpuB,*gpuC;
// pointers to gpu memory
if (thread<num_gpus){
cudaSetDevice(thread);
gpuA = gpubuffer[thread];
gpuB = gpubuffer[thread]+tilesizeM*tilesizeK;
gpuC = gpubuffer[thread]+tilesizeM*tilesizeK+tilesizeN*tilesizeK;
}
// pointers to cpu memory
else {
gpuA = cpuarray[thread-num_gpus];
gpuB = cpuarray[thread-num_gpus]+tilesizeMprime*tilesizeK;
gpuC = cpuarray[thread-num_gpus]+tilesizeMprime*tilesizeK+tilesizeNprime*tilesizeK;
}
// cpu takes some of the 'N' tile
if (StolenDimension=='N'){
for (long int tm=0; tm<ntilesM; tm++){
for (long int tk=0; tk<ntilesK; tk++){
// this is for the gpus:
if (thread<num_gpus){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int tn=0; tn<ntilesN; tn++){
if ((tm*ntilesN+tn)%num_gpus!=thread) continue;
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[tk],B+(i+tn*tilesizeN)*ldb+tk*tilesizeK,1,tmp[thread]+i*tilesizesK[tk],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesK[tk],0.0,gpuC,tilesizesM[tm]);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
}
// this if for any cpu cores that might be helping:
else{
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,gpuA+i*tilesizesM[tm],1);
}
for (long int tn=0; tn<ntilesNprime; tn++){
if ((tm*ntilesNprime+tn)%num_cpus + num_gpus!=thread) continue;
for (long int i=0; i<tilesizesNprime[tn]; i++){
C_DCOPY(tilesizesK[tk],B+(NprimeOffSet+i+tn*tilesizeNprime)*ldb+tk*tilesizeK,1,gpuB+i*tilesizesK[tk],1);
}
F_DGEMM(transa,transb,tilesizesM[tm],tilesizesNprime[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesK[tk],0.0,gpuC,tilesizesM[tm]);
for (long int j=0; j<tilesizesNprime[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,gpuC+j*tilesizesM[tm],1,C+(NprimeOffSet+j+tn*tilesizeNprime)*ldc+tm*tilesizeM,1);
}
}
}
}
}
}
// cpu takes some of the 'M' tile
else if (StolenDimension=='M'){
for (long int tn=0; tn<ntilesN; tn++){
for (long int tk=0; tk<ntilesK; tk++){
// this is for the gpus:
if (thread<num_gpus){
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[tk],B+(i+tn*tilesizeN)*ldb+tk*tilesizeK,1,tmp[thread]+i*tilesizesK[tk],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int tm=0; tm<ntilesM; tm++){
if ((tm*ntilesN+tn)%num_gpus!=thread) continue;
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesK[tk],0.0,gpuC,tilesizesM[tm]);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
}
// this if for any cpu cores that might be helping:
else{
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[tk],B+(i+tn*tilesizeN)*ldb+tk*tilesizeK,1,gpuB+i*tilesizesK[tk],1);
}
for (long int tm=0; tm<ntilesMprime; tm++){
if ((tm*ntilesN+tn)%num_cpus+num_gpus!=thread) continue;
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesMprime[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeMprime+MprimeOffSet,1,gpuA+i*tilesizesMprime[tm],1);
}
F_DGEMM(transa,transb,tilesizesMprime[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesMprime[tm],gpuB,tilesizesK[tk],0.0,gpuC,tilesizesMprime[tm]);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesMprime[tm],1.0,gpuC+j*tilesizesMprime[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeMprime+MprimeOffSet,1);
}
}
}
}
}
}
else{
if (thread<num_gpus){
for (long int tm=0; tm<ntilesM; tm++){
for (long int tk=0; tk<ntilesK; tk++){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int tn=0; tn<ntilesN; tn++){
if ((tm*ntilesN+tn)%num_gpus!=thread) continue;
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[tk],B+(i+tn*tilesizeN)*ldb+tk*tilesizeK,1,tmp[thread]+i*tilesizesK[tk],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesK[tk],0.0,gpuC,tilesizesM[tm]);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
}
}
}
}
}
free(tilesizesMprime);
free(tilesizesNprime);
free(tilesizesM);
free(tilesizesN);
free(tilesizesK);
*/
}
/**
* dgemm using a 2-dimensional tile - threaded versions for multiple gpus
*/
void report_num_threads(int level)
{
#pragma omp single
{
printf("Level %d: number of threads in the team - %d\n",
level, omp_get_num_threads());
}
}
void GPUHelper::GPU_DGEMM_2DTile_nn_threaded(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc){
Tiling((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else
for (long int i=0; i<n*ldc; i++) C[i] *= beta;
omp_set_nested(1);
omp_set_dynamic(0);
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (long int mn=0; mn<ntilesM*ntilesN; mn++){
long int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
cudaSetDevice(gpus_used[thread]);
// pointers to gpu memory
double*gpuA = gpubuffer[thread];
double*gpuB = gpubuffer[thread]+tilesizeM*tilesizeK*2;
double*gpuC = gpubuffer[thread]+tilesizeM*tilesizeK*2+tilesizeN*tilesizeK*2;
long int offsetA = tilesizeM * tilesizeK;
long int offsetB = tilesizeN * tilesizeK;
long int tn = mn%ntilesN;
long int tm = (mn-tn)/ntilesN;
cudaMemset((void*)gpuC,'\0',tilesizesM[tm]*tilesizesN[tn]*sizeof(double));
omp_set_nested(1);
omp_set_dynamic(0);
if (interleaved_dgemm) {
// create streams:
cudaStream_t stream1;
cudaStreamCreate(&stream1);
cudaEvent_t estart1,estop1;
cudaEventCreate(&estart1);
cudaEventCreate(&estop1);
cublasSetKernelStream(stream1);
cudaStream_t stream2;
cudaStreamCreate(&stream2);
cudaEvent_t estart2,estop2;
cudaEventCreate(&estart2);
cudaEventCreate(&estop2);
double start = omp_get_wtime();
// need to transfer data for first tile
for (long int i=0; i<tilesizesK[0]; i++){
C_DCOPY(tilesizesM[tm],A+(i+0*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpyAsync(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[0]*sizeof(double),cudaMemcpyHostToDevice,stream1);
cudaStreamSynchronize(stream1);
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[0],B+(i+tn*tilesizeN)*ldb+0*tilesizeK,1,tmp[thread]+i*tilesizesK[0],1);
}
cudaMemcpyAsync(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[0]*sizeof(double),cudaMemcpyHostToDevice,stream1);
cudaStreamSynchronize(stream1);
for (long int tk=0; tk<ntilesK; tk++){
#pragma omp parallel num_threads(2)
{
long int thread2 = omp_get_thread_num();
if (thread2 == 0) {
double * A_curr = ( tk % 2 == 0 ) ? gpuA : gpuA + offsetA;
double * B_curr = ( tk % 2 == 0 ) ? gpuB : gpuB + offsetB;
cudaEventRecord(estart1,stream1);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,A_curr,tilesizesM[tm],B_curr,tilesizesK[tk],1.0,gpuC,tilesizesM[tm]);
cudaStreamSynchronize(stream1);
cudaEventRecord(estop1,stream1);
} else {
// only copy next tiles if we need them:
if ( tk < ntilesK - 1 ) {
double * A_next = ( tk % 2 == 0 ) ? gpuA + offsetA : gpuA;
double * B_next = ( tk % 2 == 0 ) ? gpuB + offsetB : gpuB;
cudaEventRecord(estart2,stream2);
for (long int i=0; i<tilesizesK[tk+1]; i++){
C_DCOPY(tilesizesM[tm],A+(i+(tk+1)*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpyAsync(A_next,tmp[thread],tilesizesM[tm]*tilesizesK[tk+1]*sizeof(double),cudaMemcpyHostToDevice,stream2);
cudaStreamSynchronize(stream2);
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[tk+1],B+(i+tn*tilesizeN)*ldb+(tk+1)*tilesizeK,1,tmp[thread]+i*tilesizesK[tk+1],1);
}
cudaMemcpyAsync(B_next,tmp[thread],tilesizesN[tn]*tilesizesK[tk+1]*sizeof(double),cudaMemcpyHostToDevice,stream2);
cudaStreamSynchronize(stream2);
cudaEventRecord(estop2,stream2);
}
}
}
cudaThreadSynchronize();
}
cublasSetKernelStream(NULL);
cudaEventDestroy(estart2);
cudaEventDestroy(estart1);
cudaEventDestroy(estop1);
cudaEventDestroy(estop2);
cudaStreamDestroy(stream1);
cudaStreamDestroy(stream2);
}else {
// original version:
for (long int tk=0; tk<ntilesK; tk++){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int i=0; i<tilesizesN[tn]; i++){
C_DCOPY(tilesizesK[tk],B+(i+tn*tilesizeN)*ldb+tk*tilesizeK,1,tmp[thread]+i*tilesizesK[tk],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesK[tk],1.0,gpuC,tilesizesM[tm]);
}
}
omp_set_nested(0);
omp_set_dynamic(1);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
free(tilesizesM);
free(tilesizesN);
free(tilesizesK);
}
void GPUHelper::GPU_DGEMM_2DTile_nn(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc,int thread){
TilingNoThread((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else
for (long int i=0; i<n*ldc; i++) C[i] *= beta;
cudaSetDevice(gpus_used[thread]);
for (long int mn=0; mn<myntilesM[thread]*myntilesN[thread]; mn++){
// pointers to gpu memory
double*gpuA = gpubuffer[thread];
double*gpuB = gpubuffer[thread]+mytilesizeM[thread]*mytilesizeK[thread];
double*gpuC = gpubuffer[thread]+mytilesizeM[thread]*mytilesizeK[thread]+mytilesizeN[thread]*mytilesizeK[thread];
long int tn = mn%myntilesN[thread];
long int tm = (mn-tn)/myntilesN[thread];
cudaMemset((void*)gpuC,'\0',mytilesizesM[thread][tm]*mytilesizesN[thread][tn]*sizeof(double));
for (long int tk=0; tk<myntilesK[thread]; tk++){
for (long int i=0; i<mytilesizesK[thread][tk]; i++){
C_DCOPY(mytilesizesM[thread][tm],A+(i+tk*mytilesizeK[thread])*lda+tm*mytilesizeM[thread],1,tmp[thread]+i*mytilesizesM[thread][tm],1);
}
cudaMemcpy(gpuA,tmp[thread],mytilesizesM[thread][tm]*mytilesizesK[thread][tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int i=0; i<mytilesizesN[thread][tn]; i++){
C_DCOPY(mytilesizesK[thread][tk],B+(i+tn*mytilesizeN[thread])*ldb+tk*mytilesizeK[thread],1,tmp[thread]+i*mytilesizesK[thread][tk],1);
}
cudaMemcpy(gpuB,tmp[thread],mytilesizesN[thread][tn]*mytilesizesK[thread][tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,mytilesizesM[thread][tm],mytilesizesN[thread][tn],mytilesizesK[thread][tk],alpha,gpuA,mytilesizesM[thread][tm],gpuB,mytilesizesK[thread][tk],1.0,gpuC,mytilesizesM[thread][tm]);
}
cudaMemcpy(tmp[thread],gpuC,mytilesizesN[thread][tn]*mytilesizesM[thread][tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<mytilesizesN[thread][tn]; j++){
C_DAXPY(mytilesizesM[thread][tm],1.0,tmp[thread]+j*mytilesizesM[thread][tm],1,C+(j+tn*mytilesizeN[thread])*ldc+tm*mytilesizeM[thread],1);
}
}
free(mytilesizesM[thread]);
free(mytilesizesN[thread]);
free(mytilesizesK[thread]);
}
void GPUHelper::GPU_DGEMM_2DTile_nt_threaded_WithCpuStealing(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc){
throw PsiException("GPU_DGEMM_2DTile_nt_threaded_WithCpuStealing: not implemented",__FILE__,__LINE__);
//DPG commented out to remove statement unreachable warning
/*
//Tiling((gpumemory-extraroom)/8L,max_mapped_memory/num_gpus/8L,m,n,k);
TilingWithCpuStealing((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else
for (long int i=0; i<n*ldc; i++) C[i] *= beta;
#pragma omp parallel num_threads(num_gpus+num_cpus)
{
long int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
double*gpuA,*gpuB,*gpuC;
// pointers to gpu memory
if (thread<num_gpus){
cudaSetDevice(thread);
gpuA = gpubuffer[thread];
gpuB = gpubuffer[thread]+tilesizeM*tilesizeK;
gpuC = gpubuffer[thread]+tilesizeM*tilesizeK+tilesizeN*tilesizeK;
}
// pointers to cpu memory
else {
gpuA = cpuarray[thread-num_gpus];
gpuB = cpuarray[thread-num_gpus]+tilesizeMprime*tilesizeK;
gpuC = cpuarray[thread-num_gpus]+tilesizeMprime*tilesizeK+tilesizeNprime*tilesizeK;
}
// cpu takes some of the 'N' tile
if (StolenDimension=='N'){
for (long int tm=0; tm<ntilesM; tm++){
for (long int tk=0; tk<ntilesK; tk++){
if (thread<num_gpus){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int tn=0; tn<ntilesN; tn++){
if ((tm*ntilesN+tn)%num_gpus!=thread) continue;
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesN[tn],B+(i+tk*tilesizeK)*ldb+tn*tilesizeN,1,tmp[thread]+i*tilesizesN[tn],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesN[tn],0.0,gpuC,tilesizesM[tm]);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
}
else{
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,gpuA+i*tilesizesM[tm],1);
}
for (long int tn=0; tn<ntilesNprime; tn++){
if ((tm*ntilesNprime+tn)%num_cpus+num_gpus!=thread) continue;
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesNprime[tn],B+(i+tk*tilesizeK)*ldb+tn*tilesizeNprime+NprimeOffSet,1,gpuB+i*tilesizesNprime[tn],1);
}
F_DGEMM(transa,transb,tilesizesM[tm],tilesizesNprime[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesNprime[tn],0.0,gpuC,tilesizesM[tm]);
for (long int j=0; j<tilesizesNprime[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,gpuC+j*tilesizesM[tm],1,C+(j+tn*tilesizeNprime+NprimeOffSet)*ldc+tm*tilesizeM,1);
}
}
}
}
}
}
else if (StolenDimension=='M'){
for (long int tn=0; tn<ntilesN; tn++){
for (long int tk=0; tk<ntilesK; tk++){
if (thread<num_gpus){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesN[tn],B+(i+tk*tilesizeK)*ldb+tn*tilesizeN,1,tmp[thread]+i*tilesizesN[tn],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int tm=0; tm<ntilesM; tm++){
if ((tm*ntilesN+tn)%num_gpus!=thread) continue;
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesN[tn],0.0,gpuC,tilesizesM[tm]);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
}
else{
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesN[tn],B+(i+tk*tilesizeK)*ldb+tn*tilesizeN,1,gpuB+i*tilesizesN[tn],1);
}
for (long int tm=0; tm<ntilesMprime; tm++){
if ((tm*ntilesN+tn)%num_cpus+num_gpus!=thread) continue;
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesMprime[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeMprime+MprimeOffSet,1,gpuA+i*tilesizesMprime[tm],1);
}
F_DGEMM(transa,transb,tilesizesMprime[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesMprime[tm],gpuB,tilesizesN[tn],0.0,gpuC,tilesizesMprime[tm]);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesMprime[tm],1.0,gpuC+j*tilesizesMprime[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeMprime+MprimeOffSet,1);
}
}
}
}
}
}
else{
for (long int tm=0; tm<ntilesM; tm++){
for (long int tn=0; tn<ntilesN; tn++){
if (thread<num_gpus){
if ((tm*ntilesN+tn)%num_gpus!=thread) continue;
cudaMemset((void*)gpuC,'\0',tilesizesM[tm]*tilesizesN[tn]*sizeof(double));
for (long int tk=0; tk<ntilesK; tk++){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesN[tn],B+(i+tk*tilesizeK)*ldb+tn*tilesizeN,1,tmp[thread]+i*tilesizesN[tn],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesN[tn],1.0,gpuC,tilesizesM[tm]);
}
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
}
}
}
}
free(tilesizesNprime);
free(tilesizesMprime);
free(tilesizesM);
free(tilesizesN);
free(tilesizesK);
*/
}
void GPUHelper::GPU_DGEMM_2DTile_nt_threaded(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc){
Tiling((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else
for (long int i=0; i<n*ldc; i++) C[i] *= beta;
omp_set_nested(1);
omp_set_dynamic(0);
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (long int mn=0; mn<ntilesM*ntilesN; mn++){
long int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
cudaSetDevice(gpus_used[thread]);
// pointers to gpu memory ... keep in mind that tilesizeK has been reduced by at least a factor of 2.
double*gpuA = gpubuffer[thread];
double*gpuB = gpubuffer[thread]+tilesizeM*tilesizeK*2;
double*gpuC = gpubuffer[thread]+tilesizeM*tilesizeK*2+tilesizeN*tilesizeK*2;
long int offsetA = tilesizeM * tilesizeK;
long int offsetB = tilesizeN * tilesizeK;
long int tn = mn%ntilesN;
long int tm = (mn-tn)/ntilesN;
cudaMemset((void*)gpuC,'\0',tilesizesM[tm]*tilesizesN[tn]*sizeof(double));
omp_set_nested(1);
omp_set_dynamic(0);
if (interleaved_dgemm) {
// create streams:
cudaStream_t stream1;
cudaStreamCreate(&stream1);
cudaEvent_t estart1,estop1;
cudaEventCreate(&estart1);
cudaEventCreate(&estop1);
cublasSetKernelStream(stream1);
cudaStream_t stream2;
cudaStreamCreate(&stream2);
cudaEvent_t estart2,estop2;
cudaEventCreate(&estart2);
cudaEventCreate(&estop2);
double start = omp_get_wtime();
// need to transfer data for first tile
for (long int i=0; i<tilesizesK[0]; i++){
C_DCOPY(tilesizesM[tm],A+(i+0*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpyAsync(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[0]*sizeof(double),cudaMemcpyHostToDevice,stream1);
cudaStreamSynchronize(stream1);
for (long int i=0; i<tilesizesK[0]; i++){
C_DCOPY(tilesizesN[tn],B+(i+0*tilesizeK)*ldb+tn*tilesizeN,1,tmp[thread]+i*tilesizesN[tn],1);
}
cudaMemcpyAsync(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[0]*sizeof(double),cudaMemcpyHostToDevice,stream1);
cudaStreamSynchronize(stream1);
for (long int tk=0; tk<ntilesK; tk++){
#pragma omp parallel num_threads(2)
{
int thread2 = omp_get_thread_num();
if (thread2 == 0) {
double * A_curr = ( tk % 2 == 0 ) ? gpuA : gpuA + offsetA;
double * B_curr = ( tk % 2 == 0 ) ? gpuB : gpuB + offsetB;
cudaEventRecord(estart1,stream1);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,A_curr,tilesizesM[tm],B_curr,tilesizesN[tn],1.0,gpuC,tilesizesM[tm]);
cudaStreamSynchronize(stream1);
cudaEventRecord(estop1,stream1);
} else {
// only copy next tiles if we need them:
if ( tk < ntilesK - 1) {
double * A_next = ( tk % 2 == 0 ) ? gpuA + offsetA : gpuA;
double * B_next = ( tk % 2 == 0 ) ? gpuB + offsetB : gpuB;
cudaEventRecord(estart2,stream2);
for (long int i=0; i<tilesizesK[tk+1]; i++){
C_DCOPY(tilesizesM[tm],A+(i+(tk+1)*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpyAsync(A_next,tmp[thread],tilesizesM[tm]*tilesizesK[tk+1]*sizeof(double),cudaMemcpyHostToDevice,stream2);
cudaStreamSynchronize(stream2);
for (long int i=0; i<tilesizesK[(tk+1)]; i++){
C_DCOPY(tilesizesN[tn],B+(i+(tk+1)*tilesizeK)*ldb+tn*tilesizeN,1,tmp[thread]+i*tilesizesN[tn],1);
}
cudaMemcpyAsync(B_next,tmp[thread],tilesizesN[tn]*tilesizesK[tk+1]*sizeof(double),cudaMemcpyHostToDevice,stream2);
cudaStreamSynchronize(stream2);
cudaEventRecord(estop2,stream2);
}
}
}
cudaThreadSynchronize();
}
cublasSetKernelStream(NULL);
cudaEventDestroy(estart2);
cudaEventDestroy(estart1);
cudaEventDestroy(estop1);
cudaEventDestroy(estop2);
cudaStreamDestroy(stream1);
cudaStreamDestroy(stream2);
}else {
// original version:
for (long int tk=0; tk<ntilesK; tk++){
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesM[tm],A+(i+tk*tilesizeK)*lda+tm*tilesizeM,1,tmp[thread]+i*tilesizesM[tm],1);
}
cudaMemcpy(gpuA,tmp[thread],tilesizesM[tm]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int i=0; i<tilesizesK[tk]; i++){
C_DCOPY(tilesizesN[tn],B+(i+tk*tilesizeK)*ldb+tn*tilesizeN,1,tmp[thread]+i*tilesizesN[tn],1);
}
cudaMemcpy(gpuB,tmp[thread],tilesizesN[tn]*tilesizesK[tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,tilesizesM[tm],tilesizesN[tn],tilesizesK[tk],alpha,gpuA,tilesizesM[tm],gpuB,tilesizesN[tn],1.0,gpuC,tilesizesM[tm]);
}
}
omp_set_nested(0);
omp_set_dynamic(1);
cudaMemcpy(tmp[thread],gpuC,tilesizesN[tn]*tilesizesM[tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<tilesizesN[tn]; j++){
C_DAXPY(tilesizesM[tm],1.0,tmp[thread]+j*tilesizesM[tm],1,C+(j+tn*tilesizeN)*ldc+tm*tilesizeM,1);
}
}
free(tilesizesM);
free(tilesizesN);
free(tilesizesK);
}
void GPUHelper::GPU_DGEMM_2DTile_nt(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc,int thread){
TilingNoThread((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else
for (long int i=0; i<n*ldc; i++) C[i] *= beta;
cudaSetDevice(gpus_used[thread]);
for (long int mn=0; mn<myntilesM[thread]*myntilesN[thread]; mn++){
// pointers to gpu memory
double*gpuA = gpubuffer[thread];
double*gpuB = gpubuffer[thread]+mytilesizeM[thread]*mytilesizeK[thread];
double*gpuC = gpubuffer[thread]+mytilesizeM[thread]*mytilesizeK[thread]+mytilesizeN[thread]*mytilesizeK[thread];
long int tn = mn%myntilesN[thread];
long int tm = (mn-tn)/myntilesN[thread];
cudaMemset((void*)gpuC,'\0',mytilesizesM[thread][tm]*mytilesizesN[thread][tn]*sizeof(double));
for (long int tk=0; tk<myntilesK[thread]; tk++){
for (long int i=0; i<mytilesizesK[thread][tk]; i++){
C_DCOPY(mytilesizesM[thread][tm],A+(i+tk*mytilesizeK[thread])*lda+tm*mytilesizeM[thread],1,tmp[thread]+i*mytilesizesM[thread][tm],1);
}
cudaMemcpy(gpuA,tmp[thread],mytilesizesM[thread][tm]*mytilesizesK[thread][tk]*sizeof(double),cudaMemcpyHostToDevice);
for (long int i=0; i<mytilesizesK[thread][tk]; i++){
C_DCOPY(mytilesizesN[thread][tn],B+(i+tk*mytilesizeK[thread])*ldb+tn*mytilesizeN[thread],1,tmp[thread]+i*mytilesizesN[thread][tn],1);
}
cudaMemcpy(gpuB,tmp[thread],mytilesizesN[thread][tn]*mytilesizesK[thread][tk]*sizeof(double),cudaMemcpyHostToDevice);
cublasDgemm(transa,transb,mytilesizesM[thread][tm],mytilesizesN[thread][tn],mytilesizesK[thread][tk],alpha,gpuA,mytilesizesM[thread][tm],gpuB,mytilesizesN[thread][tn],1.0,gpuC,mytilesizesM[thread][tm]);
}
cudaMemcpy(tmp[thread],gpuC,mytilesizesN[thread][tn]*mytilesizesM[thread][tm]*sizeof(double),cudaMemcpyDeviceToHost);
for (long int j=0; j<mytilesizesN[thread][tn]; j++){
C_DAXPY(mytilesizesM[thread][tm],1.0,tmp[thread]+j*mytilesizesM[thread][tm],1,C+(j+tn*mytilesizeN[thread])*ldc+tm*mytilesizeM[thread],1);
}
}
free(mytilesizesM[thread]);
free(mytilesizesN[thread]);
free(mytilesizesK[thread]);
}
void GPUHelper::GPU_DGEMM_2DTile_tn_threaded_WithCpuStealing(char transa,char transb,long int m,long int n,long int k,double alpha,double*A,long int lda,double*B,long int ldb,double beta,double*C,long int ldc){
throw PsiException("GPU_DGEMM_2DTile_tn_threaded_WithCpuStealing: not implemented",__FILE__,__LINE__);
//DPG commented out to remove statement unreachable warning
/*
//Tiling((gpumemory-extraroom)/8L,max_mapped_memory/num_gpus/8L,m,n,k);
TilingWithCpuStealing((gpumemory-extraroom)/8L,max_mapped_memory_per_thread/8L,m,n,k);
// initialize result
if (beta==0.0)
memset((void*)C,'\0',n*ldc*sizeof(double));
else