-
Notifications
You must be signed in to change notification settings - Fork 5
/
ccsd.cu
2245 lines (1912 loc) · 85.3 KB
/
ccsd.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
*/
//include<iostream>
#include"ccsd.h"
#include"blas.h"
#include<psi4/libmints/matrix.h>
#include<psi4/libmints/vector.h>
#include<psi4/libmints/molecule.h>
#include"gpuhelper.h"
#include<psi4/libmints/mintshelper.h>
#include<psi4/libciomr/libciomr.h>
#include<psi4/libqt/qt.h>
#include<psi4/libpsi4util/process.h>
#include<omp.h>
#include<stdio.h>
#ifdef HAVE_MKL
#include<mkl.h>
#else
#define mkl_set_dynamic(a)
#define mkl_set_num_threads(a)
#define mkl_domain_set_num_threads(a,b)
#endif
#define NUMTHREADS 32
#define MAXBLOCKS 65535
__device__ int GPUKernel_Position(int i,int j) {
if (i<j){
return j*(j+1)/2+i;
}
return i*(i+1)/2+j;
}
__global__ void GPUKernel_VpVm_tiled(int a, int bstart, int bsize,int v,double * in,double * outp,double * outm) {
int blockid = blockIdx.x*gridDim.y + blockIdx.y;
int id = blockid*blockDim.x + threadIdx.x;
int v2 = v*v;
if ( id >= v2*bsize ) return;
// id : b*v2+c*v+d
int d = id%v;
int c = (id-d)%(v*v)/v;
if ( d > c ) return;
//int b = (id-d)%(v*bsize)/v;
//int c = (id-d-b*v)/(bsize*v);
int b = (id-d-c*v)/(v*v);
if ( b + bstart < a ) return;
int cd = c*(c+1)/2 + d;
int vtri = v*(v+1)/2;
int bv2 = b*v2;
//outp[b*vtri+cd] = in[bv2+d*v+c] + in[bv2+c*v+d];
//outm[b*vtri+cd] = in[bv2+d*v+c] - in[bv2+c*v+d];
outp[b*vtri+cd] = in[bv2+d*v+c] + in[id];
outm[b*vtri+cd] = in[bv2+d*v+c] - in[id];
}
__global__ void GPUKernel_VpVm_v2(int a, int b,int v,double * in,double * outp,double * outm) {
int blockid = blockIdx.x*gridDim.y + blockIdx.y;
int id = blockid*blockDim.x + threadIdx.x;
int v2 = v*v;
if ( id >= v2 ) return;
int d = id%v;
int c = (id-d)/v;
if ( d > c ) return;
int cd = GPUKernel_Position(c,d);
outp[cd] = in[d*v+c] + in[c*v+d];
outm[cd] = in[d*v+c] - in[c*v+d];
}
__global__ void GPUKernel_VpVm(int a, int v,double * in,double * outp,double * outm) {
int blockid = blockIdx.x*gridDim.y + blockIdx.y;
int id = blockid*blockDim.x + threadIdx.x;
int v2 = v*v;
if ( id >= v2*v ) return;
int d = id%v;
int b = (id-d)%(v2)/v;
if ( b < a ) return;
int bma = b - a;
int c = (id-d-b*v)/(v2);
if ( d > c ) return;
int cd = GPUKernel_Position(c,d);
int vtri = v*(v+1)/2;
outp[bma*vtri+cd] = in[bma*v2+d*v+c] + in[bma*v2+c*v+d];
outm[bma*vtri+cd] = in[bma*v2+d*v+c] - in[bma*v2+c*v+d];
}
__global__ void GPUKernel_Vm(int a, int v,double * in,double * out) {
int blockid = blockIdx.x*gridDim.y + blockIdx.y;
int id = blockid*blockDim.x + threadIdx.x;
if ( id >= v*v*v ) return;
int d = id%v;
int b = (id-d)%(v*v)/v;
int c = (id-d-b*v)/(v*v);
if ( b < a ) return;
if ( d > c ) return;
int cd = GPUKernel_Position(c,d);
int vtri = v*(v+1)/2;
out[(b-a)*vtri+cd] = in[(b-a)*v*v+d*v+c] - in[(b-a)*v*v+c*v+d];
}
__global__ void GPUKernel_Vp(int a, int v,double * in,double * out) {
int blockid = blockIdx.x*gridDim.y + blockIdx.y;
int id = blockid*blockDim.x + threadIdx.x;
if ( id >= v*v*v ) return;
int d = id%v;
int b = (id-d)%(v*v)/v;
int c = (id-d-b*v)/(v*v);
if ( b < a ) return;
if ( d > c ) return;
int cd = GPUKernel_Position(c,d);
int vtri = v*(v+1)/2;
out[(b-a)*vtri+cd] = in[(b-a)*v*v+d*v+c] + in[(b-a)*v*v+c*v+d];
}
using namespace psi;
namespace psi{namespace fnocc{
GPUDFCoupledCluster::GPUDFCoupledCluster(std::shared_ptr<Wavefunction> reference_wavefunction, Options &options):
DFCoupledCluster(reference_wavefunction,options)
{
common_init();
}
GPUDFCoupledCluster::~GPUDFCoupledCluster()
{
}
// this is where we'll set up cuda/gpu stuff i suppose
void GPUDFCoupledCluster::common_init() {
helper_ = std::shared_ptr<GPUHelper>(new GPUHelper);
/**
* GPU helper class knows if we have gpus or not and how to use them.
* all gpu memory is allocated by the helper.
*/
// get device parameters, allocate gpu memory and pinned cpu memory
helper_->ndoccact = ndoccact;
helper_->nvirt = nvirt;
helper_->nmo = nmo;
helper_->CudaInit(options_);
long int nthreads = omp_get_max_threads();
if ( nthreads <= helper_->num_gpus ) {
throw PsiException("GPU DFCC must be run with at least 1 more thread than gpu",__FILE__,__LINE__);
}
//helper_->CudaInit(options_);
gpubuffer = helper_->gpubuffer;
left = helper_->gpumemory / 8.0;
wasted = helper_->extraroom / 8.0;
num_gpus = helper_->num_gpus;
gpus_used = helper_->gpus_used;
long int v = nvirt;
ngputhreads=NUMTHREADS;
num=1;
if ((v*v*v)%ngputhreads==0)
nblocks = (v*v*v)/ngputhreads;
else
nblocks = (v*v*v+ngputhreads-(v*v*v)%ngputhreads)/ngputhreads;
if (nblocks>MAXBLOCKS){
num = nblocks/MAXBLOCKS+1;
nblocks = nblocks/num + 1;
}
ncputhreads = omp_get_max_threads();
if ( options_.get_bool("DGEMM_TIMINGS") ) {
helper_->DGEMM_Timings();
}
}
// accumulate results of contraction of (ac|bd) and t2
void GPUDFCoupledCluster::useVabcd1(){
long int o = ndoccact;
long int v = nvirt;
long int oov = o*o*v;
long int oo = o*o;
long int otri = o*(o+1)/2;
long int vtri = v*(v+1)/2;
std::shared_ptr<PSIO> psio(new PSIO());
psio->open(PSIF_DCC_R2,PSIO_OPEN_OLD);
psio->read_entry(PSIF_DCC_R2,"residual",(char*)&tempv[0],o*o*v*v*sizeof(double));
// available gpu memory (in doubles)
long int ndoubles = (left - wasted) - 2*otri*vtri;
for (long int a = 0; a < v; a++) {
// do we need to tile loop over b >= a?
long int ntiles = 1;
while ( ntiles < v-a ) {
long int size = (v - a) / ntiles;
if (size * ntiles < v - a) size++;
long int max = (size*nQ*v+nQ*v > 2*size*vtri ? size*nQ*v + nQ*v : 2*size*vtri);;
//if ( ndoubles >= max + 2*size*otri ) break;
if ( ndoubles >= max + size*nQ*v ) break;
ntiles++;
}
// tile dimensions
long int * tilesize = (long int *)malloc(ntiles*sizeof(long int));
for (long int tile = 0; tile < ntiles - 1; tile++) {
tilesize[tile] = (v-a) / ntiles;
if ( tilesize[tile] * ntiles < v - a) tilesize[tile]++;
}
tilesize[ntiles-1] = (v - a) - tilesize[0] * (ntiles - 1);
//if (ntiles > 1) printf("%5i/%5i ntiles %5i\n",a,v,ntiles);fflush(stdout);
for (long int tileb = 0; tileb < ntiles; tileb++) {
long int bsize = tilesize[tileb];
long int bstart = a + tileb*tilesize[0];
// contribute to residual
#pragma omp parallel for schedule (static)
for (long int ij = 0; ij < o*o; ij++) {
long int j = ij % o;
long int i = ( ij - j ) / o;
int sg = ( i > j ) ? 1 : -1;
for (long int b = bstart; b < bstart + bsize; b++) {
tempv[a*oo*v+b*oo+i*o+j] += tempr[Position(i,j) * vtri + Position(a,b)]
+ sg*tempr[Position(i,j) * vtri + Position(a,b) + otri*vtri];
if (a!=b) {
tempv[b*oov+a*oo+i*o+j] += tempr[Position(i,j) * vtri + Position(a,b)]
- sg*tempr[Position(i,j) * vtri + Position(a,b) + otri*vtri];
}
}
}
//gohere
}
free(tilesize);
}
// contribute to residual
psio->write_entry(PSIF_DCC_R2,"residual",(char*)&tempv[0],o*o*v*v*sizeof(double));
psio->close(PSIF_DCC_R2,1);
}
void GPUDFCoupledCluster::Vabcd1(){
long int o = ndoccact;
long int v = nvirt;
long int oov = o*o*v;
long int oo = o*o;
long int otri = o*(o+1)/2;
long int vtri = v*(v+1)/2;
std::shared_ptr<PSIO> psio(new PSIO());
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (long int i=0; i<o; i++){
for (long int j=i; j<o; j++){
long int ij = Position(i,j);
for (long int a=0; a<v; a++){
for (long int b=a; b<v; b++){
tempr[ij*vtri+Position(a,b)] =
(tb[a*oov+b*oo+i*o+j]+tb[b*oov+a*oo+i*o+j]);
tempr[ij*vtri+Position(a,b)+vtri*otri] =
(tb[a*oov+b*oo+i*o+j]-tb[b*oov+a*oo+i*o+j]);
}
tempr[ij*vtri+Position(a,a)] = tb[a*oov+a*oo+i*o+j];
}
}
}
if ( v > nQ ) {
throw PsiException("GPU DFCC will break if Nv > Naux",__FILE__,__LINE__);
}
// available gpu memory (in doubles)
long int ndoubles = (left - wasted) - 2*otri*vtri;
long int ntiles_ij = 1;
// do we need to tile ij?
if ( ndoubles < 0 ) {
while ( ntiles_ij < otri ) {
ntiles_ij++;
long int size = otri / ntiles_ij;
if ( size * ntiles_ij < otri ) size++;
if ( left - wasted - size * 2*vtri ) {
ndoubles = (left - wasted) - size * 2*vtri;
break;
}
}
outfile->Printf(" <<< warning >>> tiling composite ij index (%5li tiles)\n",ntiles_ij);
}
// sizes of ij tiles:
long int * tilesize_ij = (long int *)malloc(ntiles_ij*sizeof(long int));
for (long int tile = 0; tile < ntiles_ij - 1; tile++) {
tilesize_ij[tile] = otri / ntiles_ij;
if ( tilesize_ij[tile] * ntiles_ij < otri ) tilesize_ij[tile]++;
}
tilesize_ij[ntiles_ij-1] = otri - tilesize_ij[0] * (ntiles_ij - 1);
for (long int tile_ij = 0; tile_ij < ntiles_ij; tile_ij++) {
// copy this tile of t2 to the gpus
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (int i = 0; i < num_gpus; i++) {
int thread = omp_get_thread_num();
cudaSetDevice(gpus_used[thread]);
double * gput2 = gpubuffer[thread];
cudaMemcpy(gput2, tempr + tile_ij * tilesize_ij[0] * vtri, sizeof(double) * tilesize_ij[tile_ij] * vtri,cudaMemcpyHostToDevice);
cudaMemcpy(gput2+tilesize_ij[0]*vtri,tempr + tile_ij * tilesize_ij[0] * vtri + otri * vtri,sizeof(double) * tilesize_ij[tile_ij] * vtri,cudaMemcpyHostToDevice);
}
last_a = v;
// parallelize over multiple gpus
#pragma omp parallel for schedule (dynamic) num_threads(num_gpus)
for (long int a = 0; a < v; a++) {
if (cpudone && last_a == v) { last_a = a; }
if (last_a == v) {
cudaStream_t stream;
cudaEvent_t estart,estop;
cudaEventCreate(&estart);
cudaEventCreate(&estop);
int thread = omp_get_thread_num();
cudaSetDevice(gpus_used[thread]);
double * gput2 = gpubuffer[thread];
// do we need to tile loop over b >= a?
long int ntiles = 1;
while ( ntiles < v-a ) {
long int size = (v - a) / ntiles;
if (size * ntiles < v - a) size++;
long int max = (size*nQ*v+nQ*v > 2*size*vtri ? size*nQ*v + nQ*v : 2*size*vtri);
//if ( ndoubles >= max + 2*size*otri ) break;
if ( ndoubles >= max + size*nQ*v ) break;
ntiles++;
}
// tile dimensions
long int * tilesize = (long int *)malloc(ntiles*sizeof(long int));
for (long int tile = 0; tile < ntiles - 1; tile++) {
tilesize[tile] = (v-a) / ntiles;
if ( tilesize[tile] * ntiles < v - a) tilesize[tile]++;
}
tilesize[ntiles-1] = (v - a) - tilesize[0] * (ntiles - 1);
if (ntiles > 1) outfile->Printf("%5i/%5i ntiles %5i tilesize %5i\n",a,v,ntiles,tilesize[0]);fflush(stdout);
for (long int tileb = 0; tileb < ntiles; tileb++) {
long int bsize = tilesize[tileb];
long int bstart = a + tileb*tilesize[0];
// shift other buffers by 2 * tilesize_ij * vtri
long int shift = 2L * tilesize_ij[0] * vtri;
double * gpuVcdb = gpubuffer[thread] + shift + (bsize*nQ*v + nQ*v > 2*bsize*vtri ? bsize*nQ*v + nQ*v : 2*bsize*vtri);
double * gpuVm = gpubuffer[thread] + shift;
double * gpuVp = gpubuffer[thread] + shift + bsize*vtri;
double * gpuA = gpubuffer[thread] + shift + 2*bsize*vtri;
double * gpuIqd = gpubuffer[thread] + shift;
double * gpuIqc = gpubuffer[thread] + shift + bsize*nQ*v;
long int num = 1;
long int nblocks = ( bsize*v*v )/ NUMTHREADS;
if ( (bsize*v*v) % NUMTHREADS != 0 ) {
nblocks = (bsize*v*v+NUMTHREADS-(bsize*v*v)%NUMTHREADS)/NUMTHREADS;
}
if (nblocks > MAXBLOCKS){
num = nblocks / MAXBLOCKS + 1;
nblocks = nblocks / num + 1;
}
dim3 dimgrid (nblocks,num);
stream = NULL;
double start2 = omp_get_wtime();
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"before anything. ");
cudaEventRecord(estart,stream);
cudaMemcpyAsync(gpuIqc,Qvv+a*nQ*v,sizeof(double)*nQ*v,cudaMemcpyHostToDevice,stream);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"memcpy 1");
cudaMemcpyAsync(gpuIqd,Qvv+bstart*nQ*v,sizeof(double)*bsize*nQ*v,cudaMemcpyHostToDevice,stream);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"memcpy 2");
cublasDgemm('t','n',v,bsize*v,nQ,1.0,gpuIqc,nQ,gpuIqd,nQ,0.0,gpuVcdb,v);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"building v");
GPUKernel_VpVm_tiled<<<dimgrid,NUMTHREADS>>>(a,bstart,bsize,v,gpuVcdb,gpuVp,gpuVm);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"building v+/v-");
cublasDgemm('t','n',tilesize_ij[tile_ij],bsize,vtri,0.5,gput2, vtri,gpuVp,vtri,0.0,gpuA, tilesize_ij[tile_ij]);
cublasDgemm('t','n',tilesize_ij[tile_ij],bsize,vtri,0.5,gput2+tilesize_ij[0]*vtri,vtri,gpuVm,vtri,0.0,gpuA+bsize*tilesize_ij[tile_ij],tilesize_ij[tile_ij]);
cudaMemcpyAsync(tempr2[thread],gpuA,sizeof(double)*2*bsize*tilesize_ij[tile_ij],cudaMemcpyDeviceToHost,stream);
cudaEventRecord(estop,stream);
while( cudaEventQuery(estop) == cudaErrorNotReady );
double end2 = omp_get_wtime();
for (int ij = 0; ij < tilesize_ij[tile_ij]; ij++) {
for (int b = bstart; b < bstart + bsize; b++) {
tempr[(ij+tile_ij*tilesize_ij[0])*vtri + Position(a,b)] = tempr2[thread][(b-bstart)*tilesize_ij[tile_ij]+ij];
tempr[(ij+tile_ij*tilesize_ij[0])*vtri + Position(a,b)+otri*vtri] = tempr2[thread][(b-bstart)*tilesize_ij[tile_ij]+ij+bsize*tilesize_ij[tile_ij]];
}
}
//gohere
}
free(tilesize);
}
}
}
free(tilesize_ij);
}
void GPUDFCoupledCluster::FinishVabcd1(){
long int o = ndoccact;
long int v = nvirt;
long int oov = o*o*v;
long int oo = o*o;
long int otri = o*(o+1)/2;
long int vtri = v*(v+1)/2;
std::shared_ptr<PSIO> psio(new PSIO());
// need to build t2+/- for CPU to use
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (long int i=0; i<o; i++){
for (long int j=i; j<o; j++){
long int ij = Position(i,j);
for (long int a=0; a<v; a++){
for (long int b=a; b<v; b++){
tempt[ij*vtri+Position(a,b)] =
(tb[a*oov+b*oo+i*o+j]+tb[b*oov+a*oo+i*o+j]);
tempt[ij*vtri+Position(a,b)+vtri*otri] =
(tb[a*oov+b*oo+i*o+j]-tb[b*oov+a*oo+i*o+j]);
}
tempt[ij*vtri+Position(a,a)] = tb[a*oov+a*oo+i*o+j];
}
}
}
// available gpu memory (in doubles)
long int ndoubles = (left - wasted) - 2*otri*vtri;
long int ntiles_ij = 1;
// available cpu memory (in doubles)
long int nQmax = nQ > nQ_scf ? nQ : nQ_scf;
long int dim = 2L*v*v*v;
if (2*nQmax*o*v>dim) dim = 2*nQmax*o*v;
if (o*o*v*v>dim) dim = o*o*v*v;
if (nQmax*v*v>dim) dim = nQmax*v*v;
if (nQmax*nso*nso>dim) dim = nQmax*nso*nso;
// do we need to tile ij?
if ( ndoubles < 0 ) {
while ( ntiles_ij < otri ) {
ntiles_ij++;
long int size = otri / ntiles_ij;
if ( size * ntiles_ij < otri ) size++;
if ( left - wasted - size * 2*vtri ) {
ndoubles = (left - wasted) - size * 2*vtri;
break;
}
}
//outfile->Printf(" <<< warning >>> tiling composite ij index (%5li tiles)\n",ntiles_ij);
//outfile->Printf(" <<< warning >>> tiling composite ij index (%5li tiles)\n",ntiles_ij);
throw PsiException(" <<< warning >>> tiling composite ij index ... feature temporarily disabled",__FILE__,__LINE__);
}
// sizes of ij tiles:
long int * tilesize_ij = (long int *)malloc(ntiles_ij*sizeof(long int));
for (long int tile = 0; tile < ntiles_ij - 1; tile++) {
tilesize_ij[tile] = otri / ntiles_ij;
if ( tilesize_ij[tile] * ntiles_ij < otri ) tilesize_ij[tile]++;
}
tilesize_ij[ntiles_ij-1] = otri - tilesize_ij[0] * (ntiles_ij - 1);
omp_set_nested(1);
omp_set_dynamic(0);
mkl_set_dynamic(0);
int nthreads = omp_get_max_threads();
for (long int tile_ij = 0; tile_ij < ntiles_ij; tile_ij++) {
// copy this tile of t2 to the gpus (already there)
// parallelize over multiple gpus
#pragma omp parallel for schedule (dynamic) num_threads(num_gpus + 1)
for (long int a = last_a; a < v; a++) {
int thread = omp_get_thread_num();
if ( thread < num_gpus ) {
cudaStream_t stream;
cudaEvent_t estart,estop;
cudaEventCreate(&estart);
cudaEventCreate(&estop);
cudaSetDevice(gpus_used[thread]);
double * gput2 = gpubuffer[thread];
// do we need to tile loop over b >= a?
long int ntiles = 1;
while ( ntiles < v-a ) {
long int size = (v - a) / ntiles;
if (size * ntiles < v - a) size++;
long int max = (size*nQ*v+nQ*v > 2*size*vtri ? size*nQ*v + nQ*v : 2*size*vtri);
//if ( ndoubles >= max + 2*size*otri ) break;
if ( ndoubles >= max + size*nQ*v ) break;
ntiles++;
}
// tile dimensions
long int * tilesize = (long int *)malloc(ntiles*sizeof(long int));
for (long int tile = 0; tile < ntiles - 1; tile++) {
tilesize[tile] = (v-a) / ntiles;
if ( tilesize[tile] * ntiles < v - a) tilesize[tile]++;
}
tilesize[ntiles-1] = (v - a) - tilesize[0] * (ntiles - 1);
for (long int tileb = 0; tileb < ntiles; tileb++) {
long int bsize = tilesize[tileb];
long int bstart = a + tileb*tilesize[0];
// shift other buffers by 2 * tilesize_ij * vtri
long int shift = 2L * tilesize_ij[0] * vtri;
double * gpuVcdb = gpubuffer[thread] + shift + (bsize*nQ*v + nQ*v > 2*bsize*vtri ? bsize*nQ*v + nQ*v : 2*bsize*vtri);
double * gpuVm = gpubuffer[thread] + shift;
double * gpuVp = gpubuffer[thread] + shift + bsize*vtri;
double * gpuA = gpubuffer[thread] + shift + 2*bsize*vtri;
double * gpuIqd = gpubuffer[thread] + shift;
double * gpuIqc = gpubuffer[thread] + shift + bsize*nQ*v;
long int num = 1;
long int nblocks = ( bsize*v*v )/ NUMTHREADS;
if ( (bsize*v*v) % NUMTHREADS != 0 ) {
nblocks = (bsize*v*v+NUMTHREADS-(bsize*v*v)%NUMTHREADS)/NUMTHREADS;
}
if (nblocks > MAXBLOCKS){
num = nblocks / MAXBLOCKS + 1;
nblocks = nblocks / num + 1;
}
dim3 dimgrid (nblocks,num);
stream = NULL;
double start2 = omp_get_wtime();
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"before anything. ");
cudaEventRecord(estart,stream);
cudaMemcpyAsync(gpuIqc,Qvv+a*nQ*v,sizeof(double)*nQ*v,cudaMemcpyHostToDevice,stream);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"memcpy 1");
cudaMemcpyAsync(gpuIqd,Qvv+bstart*nQ*v,sizeof(double)*bsize*nQ*v,cudaMemcpyHostToDevice,stream);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"memcpy 2");
cublasDgemm('t','n',v,bsize*v,nQ,1.0,gpuIqc,nQ,gpuIqd,nQ,0.0,gpuVcdb,v);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"building v");
GPUKernel_VpVm_tiled<<<dimgrid,NUMTHREADS>>>(a,bstart,bsize,v,gpuVcdb,gpuVp,gpuVm);
//cudaThreadSynchronize();
//helper_->Check_CUDA_Error(outfile,"building v+/v-");
cublasDgemm('t','n',tilesize_ij[tile_ij],bsize,vtri,0.5,gput2, vtri,gpuVp,vtri,0.0,gpuA, tilesize_ij[tile_ij]);
cublasDgemm('t','n',tilesize_ij[tile_ij],bsize,vtri,0.5,gput2+tilesize_ij[0]*vtri,vtri,gpuVm,vtri,0.0,gpuA+bsize*tilesize_ij[tile_ij],tilesize_ij[tile_ij]);
cudaMemcpyAsync(tempr2[thread],gpuA,sizeof(double)*2*bsize*tilesize_ij[tile_ij],cudaMemcpyDeviceToHost,stream);
cudaEventRecord(estop,stream);
while( cudaEventQuery(estop) == cudaErrorNotReady );
double end2 = omp_get_wtime();
for (int ij = 0; ij < tilesize_ij[tile_ij]; ij++) {
for (int b = bstart; b < bstart + bsize; b++) {
tempr[(ij+tile_ij*tilesize_ij[0])*vtri + Position(a,b)] = tempr2[thread][(b-bstart)*tilesize_ij[tile_ij]+ij];
tempr[(ij+tile_ij*tilesize_ij[0])*vtri + Position(a,b)+otri*vtri] = tempr2[thread][(b-bstart)*tilesize_ij[tile_ij]+ij+bsize*tilesize_ij[tile_ij]];
}
}
}
free(tilesize);
}else {
// cpu work
mkl_set_num_threads(nthreads - num_gpus);
// do we need to tile loop over b >= a?
long int ntiles = 1;
/*
while ( ntiles < v-a ) {
long int size = (v - a) / ntiles;
if (size * ntiles < v - a) size++;
long int max = (size*nQ*v+nQ*v > 2*size*vtri ? size*nQ*v + nQ*v : 2*size*vtri);
//if ( ndoubles >= max + 2*size*otri ) break;
if ( ndoubles_cpu >= max + size*nQ*v ) break;
ntiles++;
}
*/
// tile dimensions
long int * tilesize = (long int *)malloc(ntiles*sizeof(long int));
for (long int tile = 0; tile < ntiles - 1; tile++) {
tilesize[tile] = (v-a) / ntiles;
if ( tilesize[tile] * ntiles < v - a) tilesize[tile]++;
}
tilesize[ntiles-1] = (v - a) - tilesize[0] * (ntiles - 1);
if (ntiles > 1) outfile->Printf("%5i/%5i ntiles %5i tilesize %5i (cpu) \n",a,v,ntiles,tilesize[0]);fflush(stdout);
for (long int tileb = 0; tileb < ntiles; tileb++) {
long int bsize = tilesize[tileb];
long int bstart = a + tileb*tilesize[0];
// shift other buffers by 2 * tilesize_ij * vtri
long int shift = 0;//2L * tilesize_ij[0] * vtri;
double * gpuVm = integrals + shift;
double * gpuVp = integrals + shift + bsize*vtri;
double * gpuA = integrals + shift + 2*bsize*vtri;
double * gpuVcdb = integrals + shift + 3*bsize*vtri;//(bsize*nQ*v + nQ*v > 2*bsize*vtri ? bsize*nQ*v + nQ*v : 2*bsize*vtri);
//double * gpuIqd = integrals + shift;
//double * gpuIqc = integrals + shift + bsize*nQ*v;
double start2 = omp_get_wtime();
//cudaMemcpyAsync(gpuIqc,Qvv+a*nQ*v,sizeof(double)*nQ*v,cudaMemcpyHostToDevice,stream);
//cudaMemcpyAsync(gpuIqd,Qvv+bstart*nQ*v,sizeof(double)*bsize*nQ*v,cudaMemcpyHostToDevice,stream);
F_DGEMM('t','n',v,bsize*v,nQ,1.0,Qvv+a*nQ*v,nQ,Qvv+bstart*nQ*v,nQ,0.0,gpuVcdb,v);
#pragma omp parallel for schedule (dynamic) num_threads(nthreads - num_gpus)
for (int d = 0; d < v; d++) {
for (int c = d; c < v; c++) {
int cd = c*(c+1)/2 + d;
for (int b = bstart; b < v; b++) {
int id = d + c*v + (b-bstart)*v*v;
int bv2 = (b-bstart)*v*v;
gpuVp[(b-bstart)*vtri+cd] = gpuVcdb[bv2+d*v+c] + gpuVcdb[id];
gpuVm[(b-bstart)*vtri+cd] = gpuVcdb[bv2+d*v+c] - gpuVcdb[id];
}
}
}
F_DGEMM('t','n',tilesize_ij[tile_ij],bsize,vtri,0.5,tempt, vtri,gpuVp,vtri,0.0,gpuA, tilesize_ij[tile_ij]);
F_DGEMM('t','n',tilesize_ij[tile_ij],bsize,vtri,0.5,tempt+tilesize_ij[0]*vtri,vtri,gpuVm,vtri,0.0,gpuA+bsize*tilesize_ij[tile_ij],tilesize_ij[tile_ij]);
//cudaMemcpyAsync(tempr2[thread],gpuA,sizeof(double)*2*bsize*tilesize_ij[tile_ij],cudaMemcpyDeviceToHost,stream);
#pragma omp parallel for schedule (dynamic) num_threads(nthreads - num_gpus)
for (int ij = 0; ij < tilesize_ij[tile_ij]; ij++) {
for (int b = bstart; b < bstart + bsize; b++) {
tempr[(ij+tile_ij*tilesize_ij[0])*vtri + Position(a,b)] = gpuA[(b-bstart)*tilesize_ij[tile_ij]+ij];
tempr[(ij+tile_ij*tilesize_ij[0])*vtri + Position(a,b)+otri*vtri] = gpuA[(b-bstart)*tilesize_ij[tile_ij]+ij+bsize*tilesize_ij[tile_ij]];
}
}
}
free(tilesize);
}
}
}
free(tilesize_ij);
omp_set_nested(0);
omp_set_dynamic(1);
mkl_set_dynamic(1);
mkl_set_num_threads(nthreads);
}
void GPUDFCoupledCluster::CudaInit(){
num_gpus = 0;
cublasInit();
helper_->Check_CUDA_Error(stdout,"cudaInit");
struct cudaDeviceProp cudaProp;
int gpu_id;
// how many GPUs do we have?
cudaGetDeviceCount(&num_gpus);
helper_->Check_CUDA_Error(stdout,"cudaGetDeviceCount");
if ( num_gpus == 0 ) {
throw PsiException(" Error: no cuda capable device detected.",__FILE__,__LINE__);
}
if (options_["NUM_GPUS"].has_changed()) {
num_gpus = options_.get_int("NUM_GPUS");
}
cudaGetDevice(&gpu_id);
helper_->Check_CUDA_Error(stdout,"cudaGetDevice");
cudaGetDeviceProperties( &cudaProp,gpu_id );
helper_->Check_CUDA_Error(stdout,"cudaGetDeviceProperties");
outfile->Printf("\n");
outfile->Printf(" _________________________________________________________\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");
outfile->Printf("\n");
//fflush(outfile);
// device memory left after some arrays (no, now total memory)
int v = nvirt;
left = cudaProp.totalGlobalMem/8.;// - 3*o*o*v*v - o*v-nmo*nmo;
wasted = 200*1024*1024/8.; // leave an extra 200 mb on there.
ngputhreads=NUMTHREADS;
num=1;
if ((v*v*v)%ngputhreads==0)
nblocks = (v*v*v)/ngputhreads;
else
nblocks = (v*v*v+ngputhreads-(v*v*v)%ngputhreads)/ngputhreads;
if (nblocks>MAXBLOCKS){
num = nblocks/MAXBLOCKS+1;
nblocks = nblocks/num + 1;
}
cudaDeviceReset();
helper_->Check_CUDA_Error(stdout,"cudaDeviceReset");
}
void GPUDFCoupledCluster::CudaFinalize(){
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (int i=0; i<num_gpus; i++){
int thread = omp_get_thread_num();
cudaSetDevice(gpus_used[thread]);
cudaFree(gpubuffer[thread]);
}
cudaDeviceReset();
}
void GPUDFCoupledCluster::AllocateGPUMemory(){
gpubuffer = (double**)malloc(num_gpus*sizeof(double*));
#pragma omp parallel for schedule (static) num_threads(num_gpus)
for (int i=0; i<num_gpus; i++){
int thread = omp_get_thread_num();
cudaSetDevice(gpus_used[thread]);
helper_->Check_CUDA_Error(stdout,"cudaSetDevice");
cudaMalloc((void**)&gpubuffer[thread],sizeof(double)*(left-wasted));
helper_->Check_CUDA_Error(stdout,"gpu memory");
}
}
void GPUDFCoupledCluster::AllocateMemory() {
if (nirrep_>1){
throw PsiException("df_ccsd requires symmetry c1",__FILE__,__LINE__);
}
ischolesky_ = ( options_.get_str("DF_BASIS_CC") == "CHOLESKY" );
nQ = (int)Process::environment.globals["NAUX (CC)"];
nQ_scf = (int)Process::environment.globals["NAUX (SCF)"];
int count=0;
eps = (double*)malloc((ndoccact+nvirt)*sizeof(double));
std::shared_ptr<Vector> eps_test = reference_wavefunction_->epsilon_a();
for (int h=0; h<nirrep_; h++){
for (int norb = frzcpi_[h]; norb<doccpi_[h]; norb++){
eps[count++] = eps_test->get(h,norb);
}
}
for (int h=0; h<nirrep_; h++){
for (int norb = doccpi_[h]; norb<nmopi_[h]-frzvpi_[h]; norb++){
eps[count++] = eps_test->get(h,norb);
}
}
long int o = ndoccact;
long int v = nvirt;
/*========================================================
ccsd memory requirements:
tb: o^2v^2
tempt: o^2v^2+ov ( actually o(o+1)v(v+1) + ov )
tempv: max (o^2v^2+ov , o*v*nQ)
integrals: max(2v^3,nQ*nso^2, o^2v^2, 2v^3, 2nQ*o*v) (this is a minimum)
Abij (SJS v^4 result): o(o+1)v/2
Sbij (SJS v^4 result): o(o+1)v/2
other stuff: 2ov+2v^2+(o+v)
total: 3o^2v^2 + 2v^3 + o(o+1)v + 4ov + 2v^2 + (o+v) or
4o^2v^2 + o(o+1)v + 4ov + 2v^2 + (o+v) or
3o^2v^2 + 2ovnQ + o(o+1)v + 4ov + 2v^2 + (o+v)
compare to the requirements for the (T) part:
2o^2v^2 + 3v^3*nthreads + o^3v + ov
========================================================*/
// reduce available memory by the amount required by the helper class
memory -= helper_->max_mapped_memory;
long int nQmax = nQ > nQ_scf ? nQ : nQ_scf;
// for the df version, the dimension of the large buffer:
long int dim = 2L*v*v*v;
if (2*nQmax*o*v>dim) dim = 2*nQmax*o*v;
if (o*o*v*v>dim) dim = o*o*v*v;
if (nQmax*v*v>dim) dim = nQmax*v*v;
if (nQmax*nso*nso>dim) dim = nQmax*nso*nso;
double total_memory = dim+(o*o*v*v+o*v)+(o*(o+1)*v*(v+1)+o*v)+o*o*v*v+2.*o*v+2.*v*v;
long int max = nvirt*nvirt*nQmax > (nfzv+ndocc+nvirt)*ndocc*nQmax ? nvirt*nvirt*nQmax : (nfzv+ndocc+nvirt)*ndocc*nQmax;
double df_memory = nQmax*(o*o+o*v)+max;
total_memory *= 8./1024./1024.;
df_memory *= 8./1024./1024.;
outfile->Printf(" Total memory requirements: %9.2lf mb\n",df_memory+total_memory);
outfile->Printf(" 3-index integrals: %9.2lf mb\n",df_memory);
outfile->Printf(" CCSD intermediates: %9.2lf mb\n",total_memory);
outfile->Printf("\n");
if (1.0 * memory / 1024. / 1024. < total_memory + df_memory) {
outfile->Printf("\n");
outfile->Printf(" error: not enough memory for ccsd. increase available memory by %7.2lf mb\n",total_memory+df_memory-1.0*memory/1024./1024.);
outfile->Printf("\n");
//fflush(outfile);
throw PsiException("not enough memory (ccsd).",__FILE__,__LINE__);
}
if (options_.get_bool("COMPUTE_TRIPLES")) {
long int nthreads = omp_get_max_threads();
double tempmem = 8.*(2L*o*o*v*v+o*o*o*v+o*v+3L*v*v*v*nthreads);
if (tempmem > memory) {
outfile->Printf("\n <<< warning! >>> switched to low-memory (t) algorithm\n\n");
}
if (tempmem > memory || options_.get_bool("TRIPLES_LOW_MEMORY")){
throw PsiException("low-memory triples option not yet implemented",__FILE__,__LINE__);
//DPG commented out to remove unreachable warning
//isLowMemory = true;
//tempmem = 8.*(2L*o*o*v*v+o*o*o*v+o*v+5L*o*o*o*nthreads);
}
outfile->Printf(" memory requirements for CCSD(T): %9.2lf mb\n\n",tempmem/1024./1024.);
}
cudaMallocHost((void**)&Qvv,nvirt*nvirt*nQ*sizeof(double));
cudaThreadSynchronize();
helper_->Check_CUDA_Error(stdout,"allocate host Qvv");
tempr = (double*)malloc(o*(o+1)*v*(v+1)/2*sizeof(double));
cudaThreadSynchronize();
helper_->Check_CUDA_Error(stdout,"allocate host tempr");
// o*(o+1)*v mapped memory for each gpu:
// for now, give the choice of using helper's or allocating more. TODO:
// need to figure out a cleaner way to choose the memory we want to pin
// and Qvv needs to be considerred as well.
if ( o*(o+1)/v*sizeof(double) < helper_->max_mapped_memory_per_thread ) {
tempr2 = helper_->tmp;
}else {
tempr2 = (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]);
helper_->Check_CUDA_Error(stdout,"cudaSetDevice");
cudaMallocHost((void**)&tempr2[thread],o*(o+1)*v*sizeof(double));
helper_->Check_CUDA_Error(stdout,"cpu tempr2");
}
}
// allocate some memory for 3-index tensors
Qoo = (double*)malloc(ndoccact*ndoccact*nQmax*sizeof(double));
Qov = (double*)malloc(ndoccact*nvirt*nQmax*sizeof(double));
long int tempvdim = o*o*v*v+o*v;
if ( nQmax * o * v > tempvdim) tempvdim = nQmax * o * v;
integrals = (double*)malloc(dim*sizeof(double));
tempt = (double*)malloc((o*(o+1)*v*(v+1)+o*v)*sizeof(double));
tempv = (double*)malloc(tempvdim*sizeof(double));
Abij = (double*)malloc(o*(o+1)/2*v*sizeof(double));
Sbij = (double*)malloc(o*(o+1)/2*v*sizeof(double));
tb = (double*)malloc(o*o*v*v*sizeof(double));
w1 = (double*)malloc(o*v*sizeof(double));
t1 = (double*)malloc(o*v*sizeof(double));
I1 = (double*)malloc(v*v*sizeof(double));
I1p = (double*)malloc(v*v*sizeof(double));
memset((void*)integrals,'\0',dim*sizeof(double));
memset((void*)tempv,'\0',tempvdim*sizeof(double));
memset((void*)tempt,'\0',(o*(o+1)*v*(v+1)+o*v)*sizeof(double));
memset((void*)tempr,'\0',(o*(o+1)*v*(v+1)/2)*sizeof(double));
memset((void*)tb,'\0',o*o*v*v*sizeof(double));
memset((void*)w1,'\0',o*v*sizeof(double));
memset((void*)t1,'\0',o*v*sizeof(double));
memset((void*)I1,'\0',v*v*sizeof(double));
memset((void*)I1p,'\0',v*v*sizeof(double));
memset((void*)Abij,'\0',o*(o+1)/2*v*sizeof(double));
memset((void*)Sbij,'\0',o*(o+1)/2*v*sizeof(double));
// DIIS:
diisvec = (double*)malloc(sizeof(double)*(maxdiis+1));
memset((void*)diisvec,'\0',(maxdiis+1)*sizeof(double));
// new 3-index stuff for t1-transformed integrals:
Fij = (double*)malloc(o*o*sizeof(double));
Fia = (double*)malloc(o*v*sizeof(double));
Fai = (double*)malloc(o*v*sizeof(double));
Fab = (double*)malloc(v*v*sizeof(double));
Ca_R = (double*)malloc(nso*(nmo+nfzc+nfzv)*sizeof(double));