-
Notifications
You must be signed in to change notification settings - Fork 14
/
gpu_transform_3index_teint.cc
346 lines (179 loc) · 7.69 KB
/
gpu_transform_3index_teint.cc
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
#include "gpu_transform_3index_teint.h"
void transform_3index_teints_driver_(double *int2_, double *U_, int *nmopi_, int* nirrep_, int* nQ_){
int* U_offset;
int* nmo_offset;
double *int2_tmp1;
double *int2_tmp2;
int nirrep = *nirrep_;
int nQ = *nQ_;
int nmo_tot = 0;
int max_nmopi = 0;
for (int sym_R = 0; sym_R < nirrep; sym_R++){
nmo_tot += nmopi_[sym_R];
if ( max_nmopi > nmopi_[sym_R] ) continue;
max_nmopi = nmopi_[sym_R];
}
double max_gpu_mem_mb = 1.0e3;
int ngem_tot_lt = nmo_tot * ( nmo_tot + 1 ) / 2;
int max_tile_size=max_numQ_per_pass(nQ, nmo_tot, max_nmopi, nirrep, max_gpu_mem_mb, nmopi_);
int num_tiles= nQ / max_tile_size;
// allocate and figure out offset matrix for indexing elements of U
U_offset = (int*) malloc(nirrep*sizeof(int));
nmo_offset = (int*) malloc(nirrep*sizeof(int));
U_offset[0] = 0;
nmo_offset[0] = 0;
for ( int sym_L = 1; sym_L < nirrep; sym_L++ ){
U_offset[sym_L] = U_offset[sym_L - 1] + nmopi_[sym_L - 1] * nmopi_[sym_L - 1];
nmo_offset[sym_L] = nmo_offset[sym_L - 1] + nmopi_[sym_L - 1];
}
int2_tmp1 = (double*)malloc(max_tile_size * max_nmopi * max_nmopi * sizeof(double));
int2_tmp2 = (double*)malloc(max_tile_size * max_nmopi * max_nmopi * sizeof(double));
for ( int tile = 0 ; tile < num_tiles; tile++){
// first Q index is tile*max_tile_size, and max_tile_size Qs are copied
transform_3index_teints_block( &int2_[tile*max_tile_size*ngem_tot_lt], int2_tmp1, int2_tmp2, U_, nmopi_, U_offset, nmo_offset, nirrep, max_tile_size, max_nmopi, nmo_tot, ngem_tot_lt);
}
if ( nQ > num_tiles * max_tile_size){
int tile_size = nQ - num_tiles*max_tile_size;
// first Q index is num_tiles*max_tile_size and tile_size Qs are copied
transform_3index_teints_block( &int2_[num_tiles*max_tile_size*ngem_tot_lt], int2_tmp1, int2_tmp2, U_, nmopi_, U_offset, nmo_offset, nirrep, tile_size, max_nmopi, nmo_tot, ngem_tot_lt);
}
free(int2_tmp1);
free(int2_tmp2);
free(U_offset);
free(nmo_offset);
}
void transform_3index_teints_block(double *int2_, double *int2_tmp1, double *int2_tmp2, double *U_, int *nmopi_, int* U_offset, int* nmo_offset, int nirrep, int nQ, int max_nmopi, int nmo_tot, int ngem_tot_lt){
long int int_ind_off, int_ind, int_tmp_off;
// transform integrals
for ( int Q = 0; Q < nQ; Q++){
int_tmp_off = (long int) Q * (long int) max_nmopi * (long int) max_nmopi;
int_ind_off = (long int) ngem_tot_lt * (long int) Q;
// unpack integrals for this Q (L > R)
for (int sym_L = 0; sym_L < nirrep; sym_L++){
// *************
// SYM_R < SYM_L
// *************
for (int sym_R = 0; sym_R < sym_L; sym_R++){
// UNPACK
for (int L = 0; L < nmopi_[sym_L]; L++){
int_ind = int_ind_off + (long int) gamma_mn(L+nmo_offset[sym_L],nmo_offset[sym_R]);
for (int R = 0; R < nmopi_[sym_R]; R++){
int LR = (long int) ind_ij( L, R, nmopi_[sym_L] ) + int_tmp_off;
int2_tmp1[LR] = int2_[int_ind];
int_ind++;
}
}
// TRANSFORM R
for ( int L = 0; L < nmopi_[sym_L]; L++){
for ( int R = 0; R < nmopi_[sym_R]; R++){
double tmp = 0.0;
for ( int K = 0; K < nmopi_[sym_R]; K++){
long int LK = (long int) ind_ij( L, K, nmopi_[sym_L] ) + int_tmp_off;
int KR = ind_ij( K, R, nmopi_[sym_R] ) + U_offset[sym_R];
tmp += int2_tmp1[LK] * U_[KR];
}
long int LR = (long int) ind_ij( L, R, nmopi_[sym_L] ) + int_tmp_off;
int2_tmp2[LR] = tmp;
}
}
// TRANSFORM L
for ( int R = 0; R < nmopi_[sym_R]; R++){
for ( int L = 0; L < nmopi_[sym_L]; L++){
double tmp = 0.0;
for ( int K = 0; K < nmopi_[sym_L]; K++){
int KL = ind_ij(K, L, nmopi_[sym_L]) + U_offset[sym_L];
long int KR = (long int) ind_ij(K, R, nmopi_[sym_L]) + int_tmp_off;
tmp += U_[KL] * int2_tmp2[KR];
}
long int LR = (long int) ind_ij(L, R, nmopi_[sym_L]) + int_tmp_off;
int2_tmp1[LR] = tmp;
}
}
// REPACK
for (int L = 0; L < nmopi_[sym_L]; L++){
int_ind = int_ind_off + (long int) gamma_mn(L+nmo_offset[sym_L],nmo_offset[sym_R]);
for (int R = 0; R < nmopi_[sym_R]; R++){
long int LR = (long int) ind_ij( L, R, nmopi_[sym_L] ) + int_tmp_off;
int2_[int_ind] = int2_tmp1[LR];
int_ind++;
}
}
}
// **************
// SYM_R == SYM_L
// **************
// UNPACK
for (int L = 0; L < nmopi_[sym_L]; L++){
int_ind = int_ind_off + (long int) gamma_mn(L+nmo_offset[sym_L],nmo_offset[sym_L]);
for (int R = 0; R < L; R++){
long int LR = (long int) ind_ij( L, R, nmopi_[sym_L] ) + int_tmp_off;
int2_tmp1[LR] = int2_[int_ind];
LR = (long int) ind_ij( R, L, nmopi_[sym_L] ) + int_tmp_off;
int2_tmp1[LR] = int2_[int_ind];
int_ind++;
}
long int LL = (long int) ind_ij( L, L, nmopi_[sym_L] ) + int_tmp_off;
int2_tmp1[LL] = int2_[int_ind];
}
// TRANSFORM R
for ( int L = 0; L < nmopi_[sym_L]; L++){
for ( int R = 0; R < nmopi_[sym_L]; R++){
double tmp = 0.0;
for ( int K = 0; K < nmopi_[sym_L]; K++){
long int LK = (long int) ind_ij( L, K, nmopi_[sym_L] ) + int_tmp_off;
int KR = ind_ij( K, R, nmopi_[sym_L] ) + U_offset[sym_L];
tmp += int2_tmp1[LK] * U_[KR];
}
long int LR = (long int) ind_ij( L, R, nmopi_[sym_L] ) + int_tmp_off;
int2_tmp2[LR] = tmp;
}
}
// TRANSFORM L
for ( int R = 0; R < nmopi_[sym_L]; R++){
for ( int L = 0; L < nmopi_[sym_L]; L++){
double tmp = 0.0;
for ( int K = 0; K < nmopi_[sym_L]; K++){
int KL = ind_ij(K, L, nmopi_[sym_L]) + U_offset[sym_L];
long int KR = (long int) ind_ij(K, R, nmopi_[sym_L]) + int_tmp_off;
tmp += U_[KL] * int2_tmp2[KR];
}
long int LR = (long int) ind_ij(L, R, nmopi_[sym_L]) + int_tmp_off;
int2_tmp1[LR] = tmp;
}
}
// UNPACK
for (int L = 0; L < nmopi_[sym_L]; L++){
int_ind = int_ind_off + (long int) gamma_mn(L+nmo_offset[sym_L],nmo_offset[sym_L]);
for (int R = 0; R < L; R++){
long int LR = (long int) ind_ij( L, R, nmopi_[sym_L] ) + int_tmp_off;
int2_[int_ind] = int2_tmp1[LR];
int_ind++;
}
long int LL = (long int) ind_ij( L, L, nmopi_[sym_L] ) + int_tmp_off;
int2_[int_ind] = int2_tmp1[LL];
}
}
}
}
int gamma_mn(int gamma_m, int gamma_n){
if ( gamma_m >= gamma_n ){
return gamma_m * ( gamma_m + 1 ) / 2 + gamma_n;
}
return gamma_n * ( gamma_n + 1 ) / 2 + gamma_m;
}
int ind_ij(int i, int j, int dim_i){
return j * dim_i + i;
}
int max_numQ_per_pass(int nQ, int nmo_tot, int nirrep, int max_nmopi, double max_gpu_mem_mb, int *nmopi){
//number of doubles for a single instance of:
// intermediate matrices transformation (int2_tmp1 and int2_tmp2)
// packed integrals
double mem_total = 2.0 * (double) max_nmopi * (double) max_nmopi;
mem_total += (double) nmo_tot * ( (double) nmo_tot + 1.0 ) / 2.0;
// this is the total memory (in mb) for auxiliary arrays for a single GPU block
mem_total *= (double) sizeof(double) / ( 1024.0 * 1024.0 );
int num_Q = max_gpu_mem_mb / mem_total;
if ( num_Q < 1 ) num_Q = 1;
if ( num_Q > nQ ) num_Q = nQ;
return num_Q;
}