-
Notifications
You must be signed in to change notification settings - Fork 5
/
triples.cu
400 lines (355 loc) · 14.6 KB
/
triples.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
/*
*@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"ccsd.h"
#include"blas.h"
#include<psi4/libmints/wavefunction.h>
#include<psi4/libqt/qt.h>
#include<psi4/libpsi4util/process.h>
#include<omp.h>
using namespace psi;
namespace psi{namespace fnocc{
PsiReturnType GPUDFCoupledCluster::triples(){
char*name = new char[10];
char*space = new char[10];
double fac;
if (ccmethod == 0) {
sprintf(name,"CCSD");
sprintf(space," ");
fac = 1.0;
}else if (ccmethod == 1) {
sprintf(name,"QCISD");
sprintf(space," ");
fac = 2.0;
}else{
sprintf(name,"MP4");
//sprintf(space,"");
fac = 0.0;
}
outfile->Printf("\n");
outfile->Printf( " *******************************************************\n");
outfile->Printf( " * *\n");
outfile->Printf( " * %8s(T) *\n",name);
outfile->Printf( " * *\n");
outfile->Printf( " *******************************************************\n");
outfile->Printf("\n");
//fflush(outfile);
int o = ndoccact;
int v = nvirt_no;
double *F = eps;
double *E2ijak,**E2abci;
E2ijak = (double*)malloc(o*o*o*v*sizeof(double));
int nthreads = 1;
#ifdef _OPENMP
nthreads = omp_get_max_threads();
#endif
long int memory = Process::environment.get_memory();
if (options_["MEMORY"].has_changed()){
memory = options_.get_int("MEMORY");
memory *= (long int)1024*1024;
}
memory -= 8L*(2L*o*o*v*v+o*o*o*v+o*v+3L*nthreads*v*v*v);
outfile->Printf(" num_threads = %9i\n",nthreads);
outfile->Printf(" available memory = %9.2lf mb\n",memory/1024./1024.);
outfile->Printf(" memory requirements = %9.2lf mb\n",
8.*(2.*o*o*v*v+1.*o*o*o*v+(3.*nthreads)*v*v*v+1.*o*v)/1024./1024.);
outfile->Printf("\n");
//fflush(outfile);
int nijk = 0;
for (int i=0; i<o; i++){
for (int j=0; j<=i; j++){
for (int k=0; k<=j; k++){
nijk++;
}
}
}
int**ijk = (int**)malloc(nijk*sizeof(int*));
nijk = 0;
for (int i=0; i<o; i++){
for (int j=0; j<=i; j++){
for (int k=0; k<=j; k++){
ijk[nijk] = (int*)malloc(3*sizeof(int));
ijk[nijk][0] = i;
ijk[nijk][1] = j;
ijk[nijk][2] = k;
nijk++;
}
}
}
outfile->Printf(" Number of ijk combinations: %i\n",nijk);
outfile->Printf("\n");
//fflush(outfile);
E2abci = (double**)malloc(nthreads*sizeof(double*));
// some v^3 intermediates
double **Z = (double**)malloc(nthreads*sizeof(double*));
double **Z2 = (double**)malloc(nthreads*sizeof(double*));
for (int i=0; i<nthreads; i++){
E2abci[i] = (double*)malloc(v*v*v*sizeof(double));
Z[i] = (double*)malloc(v*v*v*sizeof(double));
Z2[i] = (double*)malloc(v*v*v*sizeof(double));
}
std::shared_ptr<PSIO> psio(new PSIO());
psio->open(PSIF_DCC_IJAK,PSIO_OPEN_OLD);
psio->read_entry(PSIF_DCC_IJAK,"E2ijak",(char*)&E2ijak[0],o*o*o*v*sizeof(double));
psio->close(PSIF_DCC_IJAK,1);
double *tempt = (double*)malloc(o*o*v*v*sizeof(double));
// first-order amplitudes for mp4
if (ccmethod == 2) {
psio->open(PSIF_DCC_T2,PSIO_OPEN_OLD);
psio->read_entry(PSIF_DCC_T2,"first",(char*)&tb[0],o*o*v*v*sizeof(double));
psio->close(PSIF_DCC_T2,1);
}
for (int a=0; a<v*v; a++){
C_DCOPY(o*o,tb+a*o*o,1,tempt+a,v*v);
}
// might as well use t2's memory
double*E2klcd = tb;
psio->open(PSIF_DCC_IAJB,PSIO_OPEN_OLD);
psio->read_entry(PSIF_DCC_IAJB,"E2iajb", (char*)&E2klcd[0],o*o*v*v*sizeof(double));
psio->close(PSIF_DCC_IAJB,1);
double *etrip = (double*)malloc(nthreads*sizeof(double));
for (int i=0; i<nthreads; i++) etrip[i] = 0.0;
outfile->Printf(" Computing (T) correction...\n");
outfile->Printf("\n");
outfile->Printf(" %% complete total time\n");
//fflush(outfile);
time_t stop,start = time(NULL);
int pct10,pct20,pct30,pct40,pct50,pct60,pct70,pct80,pct90;
pct10=pct20=pct30=pct40=pct50=pct60=pct70=pct80=pct90=0;
int pct01 = 0;
int pct02 = 0;
int pct03 = 0;
int pct04 = 0;
int pct05 = 0;
/**
* if there is enough memory to explicitly thread, do so
*/
#pragma omp parallel for schedule (dynamic) num_threads(nthreads)
for (int ind=0; ind<nijk; ind++){
int i = ijk[ind][0];
int j = ijk[ind][1];
int k = ijk[ind][2];
int thread = 0;
#ifdef _OPENMP
thread = omp_get_thread_num();
#endif
std::shared_ptr<PSIO> mypsio(new PSIO());
mypsio->open(PSIF_DCC_ABCI,PSIO_OPEN_OLD);
psio_address addr = psio_get_address(PSIO_ZERO,(long int)k*v*v*v*sizeof(double));
mypsio->read(PSIF_DCC_ABCI,"E2abci",(char*)&E2abci[thread][0],v*v*v*sizeof(double),addr,&addr);
helper_->GPUTiledDGEMM_NoThread('t','t',v*v,v,v,1.0,E2abci[thread],v,tempt+j*v*v*o+i*v*v,v,0.0,Z[thread],v*v,thread);
helper_->GPUTiledDGEMM_NoThread('n','t',v,v*v,o,-1.0,E2ijak+j*o*o*v+k*o*v,v,tempt+i*v*v*o,v*v,1.0,Z[thread],v,thread);
//(ab)(ij)
helper_->GPUTiledDGEMM_NoThread('t','t',v*v,v,v,1.0,E2abci[thread],v,tempt+i*v*v*o+j*v*v,v,0.0,Z2[thread],v*v,thread);
helper_->GPUTiledDGEMM_NoThread('n','t',v,v*v,o,-1.0,E2ijak+i*o*o*v+k*o*v,v,tempt+j*v*v*o,v*v,1.0,Z2[thread],v,thread);
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
C_DAXPY(v,1.0,Z2[thread]+b*v*v+a*v,1,Z[thread]+a*v*v+b*v,1);
}
}
//(bc)(jk)
addr = psio_get_address(PSIO_ZERO,(long int)j*v*v*v*sizeof(double));
mypsio->read(PSIF_DCC_ABCI,"E2abci",(char*)&E2abci[thread][0],v*v*v*sizeof(double),addr,&addr);
helper_->GPUTiledDGEMM_NoThread('t','t',v*v,v,v,1.0,E2abci[thread],v,tempt+k*v*v*o+i*v*v,v,0.0,Z2[thread],v*v,thread);
helper_->GPUTiledDGEMM_NoThread('n','t',v,v*v,o,-1.0,E2ijak+k*o*o*v+j*o*v,v,tempt+i*v*v*o,v*v,1.0,Z2[thread],v,thread);
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
C_DAXPY(v,1.0,Z2[thread]+a*v*v+b,v,Z[thread]+a*v*v+b*v,1);
}
}
//(ikj)(acb)
helper_->GPUTiledDGEMM_NoThread('t','t',v*v,v,v,1.0,E2abci[thread],v,tempt+i*v*v*o+k*v*v,v,0.0,Z2[thread],v*v,thread);
helper_->GPUTiledDGEMM_NoThread('n','t',v,v*v,o,-1.0,E2ijak+i*o*o*v+j*o*v,v,tempt+k*v*v*o,v*v,1.0,Z2[thread],v,thread);
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
C_DAXPY(v,1.0,Z2[thread]+a*v+b,v*v,Z[thread]+a*v*v+b*v,1);
}
}
//(ac)(ik)
addr = psio_get_address(PSIO_ZERO,(long int)i*v*v*v*sizeof(double));
mypsio->read(PSIF_DCC_ABCI,"E2abci",(char*)&E2abci[thread][0],v*v*v*sizeof(double),addr,&addr);
helper_->GPUTiledDGEMM_NoThread('t','t',v*v,v,v,1.0,E2abci[thread],v,tempt+j*v*v*o+k*v*v,v,0.0,Z2[thread],v*v,thread);
helper_->GPUTiledDGEMM_NoThread('n','t',v,v*v,o,-1.0,E2ijak+j*o*o*v+i*o*v,v,tempt+k*v*v*o,v*v,1.0,Z2[thread],v,thread);
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
C_DAXPY(v,1.0,Z2[thread]+b*v+a,v*v,Z[thread]+a*v*v+b*v,1);
}
}
//(ijk)(abc)
helper_->GPUTiledDGEMM_NoThread('t','t',v*v,v,v,1.0,E2abci[thread],v,tempt+k*v*v*o+j*v*v,v,0.0,Z2[thread],v*v,thread);
helper_->GPUTiledDGEMM_NoThread('n','t',v,v*v,o,-1.0,E2ijak+k*o*o*v+i*o*v,v,tempt+j*v*v*o,v*v,1.0,Z2[thread],v,thread);
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
C_DAXPY(v,1.0,Z2[thread]+b*v*v+a,v,Z[thread]+a*v*v+b*v,1);
}
}
C_DCOPY(v*v*v,Z[thread],1,Z2[thread],1);
for (int a=0; a<v; a++){
double tai = t1[a*o+i];
for (int b=0; b<v; b++){
int ab = 1+(a==b);
double tbj = t1[b*o+j];
double E2iajb = E2klcd[i*v*v*o+a*v*o+j*v+b];
for (int c=0; c<v; c++){
Z2[thread][a*v*v+b*v+c] += fac*(tai *E2klcd[j*v*v*o+b*v*o+k*v+c] +
tbj *E2klcd[i*v*v*o+a*v*o+k*v+c] +
t1[c*o+k]*E2iajb);
Z2[thread][a*v*v+b*v+c] /= (ab + (b==c) + (a==c));
}
}
}
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
for (int c=0; c<v; c++){
long int abc = a*v*v+b*v+c;
long int bac = b*v*v+a*v+c;
long int acb = a*v*v+c*v+b;
long int cba = c*v*v+b*v+a;
E2abci[thread][abc] = Z2[thread][acb] + Z2[thread][bac] + Z2[thread][cba];
}
}
}
double dijk = F[i]+F[j]+F[k];
int ijkfac = ( 2-((i==j)+(j==k)+(i==k)) );
// separate out these bits to save v^3 storage
double tripval = 0.0;
for (int a=0; a<v; a++){
double dijka = dijk-F[a+o];
for (int b=0; b<=a; b++){
double dijkab = dijka-F[b+o];
for (int c=0; c<=b; c++){
long int abc = a*v*v+b*v+c;
long int bca = b*v*v+c*v+a;
long int cab = c*v*v+a*v+b;
long int acb = a*v*v+c*v+b;
long int bac = b*v*v+a*v+c;
long int cba = c*v*v+b*v+a;
double dum = Z[thread][abc]*Z2[thread][abc] + Z[thread][acb]*Z2[thread][acb]
+ Z[thread][bac]*Z2[thread][bac] + Z[thread][bca]*Z2[thread][bca]
+ Z[thread][cab]*Z2[thread][cab] + Z[thread][cba]*Z2[thread][cba];
dum = (E2abci[thread][abc])
* ((Z[thread][abc] + Z[thread][bca] + Z[thread][cab])*-2.0
+ (Z[thread][acb] + Z[thread][bac] + Z[thread][cba]))
+ 3.0*dum;
double denom = dijkab-F[c+o];
tripval += dum/denom;
}
}
}
etrip[thread] += tripval*ijkfac;
// the second bit
for (int a=0; a<v; a++){
for (int b=0; b<v; b++){
for (int c=0; c<v; c++){
long int abc = a*v*v+b*v+c;
long int bca = b*v*v+c*v+a;
long int cab = c*v*v+a*v+b;
E2abci[thread][abc] = Z2[thread][abc] + Z2[thread][bca] + Z2[thread][cab];
}
}
}
tripval = 0.0;
for (int a=0; a<v; a++){
double dijka = dijk-F[a+o];
for (int b=0; b<=a; b++){
double dijkab = dijka-F[b+o];
for (int c=0; c<=b; c++){
long int abc = a*v*v+b*v+c;
long int bca = b*v*v+c*v+a;
long int cab = c*v*v+a*v+b;
long int acb = a*v*v+c*v+b;
long int bac = b*v*v+a*v+c;
long int cba = c*v*v+b*v+a;
double dum = (E2abci[thread][abc])
* (Z[thread][abc] + Z[thread][bca] + Z[thread][cab]
+ (Z[thread][acb] + Z[thread][bac] + Z[thread][cba])*-2.0);
double denom = dijkab-F[c+o];
tripval += dum/denom;
}
}
}
etrip[thread] += tripval*ijkfac;
// print out update
if (thread==0){
int print = 0;
stop = time(NULL);
if ((double)ind/nijk >= 0.01 && !pct01){ pct01 = 1; print=1;}
else if ((double)ind/nijk >= 0.02 && !pct02){ pct02 = 1; print=1;}
else if ((double)ind/nijk >= 0.03 && !pct03){ pct03 = 1; print=1;}
else if ((double)ind/nijk >= 0.04 && !pct04){ pct04 = 1; print=1;}
else if ((double)ind/nijk >= 0.05 && !pct05){ pct05 = 1; print=1;}
else if ((double)ind/nijk >= 0.1 && !pct10){ pct10 = 1; print=1;}
else if ((double)ind/nijk >= 0.2 && !pct20){ pct20 = 1; print=1;}
else if ((double)ind/nijk >= 0.3 && !pct30){ pct30 = 1; print=1;}
else if ((double)ind/nijk >= 0.4 && !pct40){ pct40 = 1; print=1;}
else if ((double)ind/nijk >= 0.5 && !pct50){ pct50 = 1; print=1;}
else if ((double)ind/nijk >= 0.6 && !pct60){ pct60 = 1; print=1;}
else if ((double)ind/nijk >= 0.7 && !pct70){ pct70 = 1; print=1;}
else if ((double)ind/nijk >= 0.8 && !pct80){ pct80 = 1; print=1;}
else if ((double)ind/nijk >= 0.9 && !pct90){ pct90 = 1; print=1;}
if (print){
outfile->Printf(" %3.1lf %8d s\n",100.0*ind/nijk,(int)stop-(int)start);
//fflush(outfile);
}
}
mypsio->close(PSIF_DCC_ABCI,1);
mypsio.reset();
}
double myet = 0.0;
for (int i=0; i<nthreads; i++) myet += etrip[i];
// ccsd(t) or qcisd(t)
if (ccmethod <= 1) {
et = myet;
outfile->Printf("\n");
outfile->Printf(" (T) energy %s %20.12lf\n",space,et);
outfile->Printf("\n");
outfile->Printf(" %s(T) correlation energy %20.12lf\n",name,eccsd+et);
outfile->Printf(" * %s(T) total energy %20.12lf\n",name,eccsd+et+escf);
outfile->Printf("\n");
}else {
emp4_t = myet;
outfile->Printf("\n");
outfile->Printf(" MP4(T) correlation energy: %20.12lf\n",emp4_t);
outfile->Printf("\n");
outfile->Printf(" MP4(SDTQ) correlation energy: %20.12lf\n",emp2+emp3+emp4_sd+emp4_q+emp4_t);
outfile->Printf(" * MP4(SDTQ) total energy: %20.12lf\n",emp2+emp3+emp4_sd+emp4_q+emp4_t+escf);
outfile->Printf("\n");
}
//fflush(outfile);
// free memory:
free(E2ijak);
free(tempt);
for (int i=0; i<nthreads; i++){
free(E2abci[i]);
free(Z[i]);
free(Z2[i]);
}
free(Z);
free(Z2);
free(E2abci);
free(etrip);
delete name;
delete space;
return Success;
}
}} // end of namespaces