(git:e70887f)
Loading...
Searching...
No Matches
grid_gpu_collocate.cu
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7
8/*
9 * Authors :
10 - Mathieu Taillefumier (ETH Zurich / CSCS)
11 - Advanced Micro Devices, Inc.
12 - Ole Schuett
13*/
14
15#include <algorithm>
16#include <climits>
17#include <cmath>
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21
24#include "grid_gpu_separable.h"
25
26#if defined(_OMP_H)
27#error "OpenMP should not be used in .cu files to accommodate HIP."
28#endif
29
31/*******************************************************************************
32 * \brief Decontracts the subblock, going from spherical to cartesian harmonics.
33 ******************************************************************************/
34template <typename T, bool IS_FUNC_AB>
35__device__ __inline__ void block_to_cab(const kernel_params &params,
36 const smem_task<T> &task, T *cab) {
37
38 // The spherical index runs over angular momentum and then over contractions.
39 // The cartesian index runs over exponents and then over angular momentum.
40
41 // This is a T matrix product. Since the pab block can be quite large the
42 // two products are fused to conserve shared memory.
43 const int tid = thread_global_index();
44
45 for (int jco = task.first_cosetb + tid / 16; jco < task.ncosetb; jco += 4) {
46 for (int ico = task.first_coseta + (tid % 16); ico < task.ncoseta;
47 ico += 16) {
48 T pab_val = 0.0;
49 for (int i = 0; i < task.nsgf_setb; i++) {
50 const T sphib = task.sphib[i * task.maxcob + jco];
51 T tmp_val = 0.0;
52 for (int j = 0; j < task.nsgf_seta; j++) {
53 T block_val;
54 if (task.block_transposed) {
55 block_val =
56 task.pab_block[j * task.nsgfb + i] * task.off_diag_twice;
57 } else {
58 block_val =
59 task.pab_block[i * task.nsgfa + j] * task.off_diag_twice;
60 }
61 tmp_val += block_val * task.sphia[j * task.maxcoa + ico];
62 }
63 pab_val += tmp_val * sphib;
64 }
65
66 if (IS_FUNC_AB) {
67 cab[jco * task.ncoseta + ico] += pab_val;
68 } else {
69 const auto a = coset_inv[ico];
70 const auto b = coset_inv[jco];
71 // prepare_pab adds to the neighbours up/down(i, a) x up/down(i, b),
72 // which other (ico, jco) pairs of the block also update: the
73 // atomicAdd in prep_term is required.
74 prepare_pab(params.func, a, b, task.zeta, task.zetb, pab_val, task.n1,
75 cab);
76 }
77 }
78 }
79}
80
81template <typename T, bool IS_FUNC_AB>
82__global__
83__launch_bounds__(64) void calculate_coefficients(const kernel_params dev_) {
84 const int number_of_tasks = dev_.num_tasks_per_block_dev[block_index()];
85
86 if (number_of_tasks == 0)
87 return;
88
89 const int tid = thread_global_index();
90 const int offset = dev_.sorted_blocks_offset_dev[block_index()];
91 T *smem_cab = reinterpret_cast<double *>(
92 __builtin_assume_aligned(allocate_workspace<T>(dev_), 32));
93
94 for (int tk = 0; tk < number_of_tasks; tk++) {
95 __shared__ smem_task<T> task;
96 const int task_id = dev_.task_sorted_by_blocks_dev[offset + tk];
97 if (dev_.tasks[task_id].skip_task)
98 continue;
99 fill_smem_task_coef(dev_, task_id, task);
100
101 T *__restrict__ coef_ =
102 &dev_.buffers_dev.coef[dev_.tasks[task_id].coef_offset];
103
104 for (int z = tid; z < task.n1 * task.n2;
105 z += blockDim.x * blockDim.y * blockDim.z)
106 smem_cab[z] = 0.0;
108
111
113 }
114}
115
116/*******************************************************************************
117 * \brief Same as calculate_coefficients, with the spherical to cartesian
118 * transformation of block_to_cab done in two steps,
119 *
120 * T[i][ico] = sum_j pab[i][j] sphia[j][ico] (nsgfb nsgfa ncoa)
121 * P[jco][ico] = sum_i sphib[i][jco] T[i][ico] (ncob nsgfb ncoa)
122 *
123 * instead of the fused loop of cost ncoa ncob nsgfb (nsgfa + 1). The
124 * work arrays T [max_nsgf_set][ld] and P [ld][ld] live in the dynamic
125 * shared memory, see sphi_work_size.
126 ******************************************************************************/
127template <typename T, bool IS_FUNC_AB>
128__global__ __launch_bounds__(64) void calculate_coefficients_two_steps(
129 const kernel_params dev_, const int ld, const int max_nsgf_set) {
130 extern __shared__ T shared_memory[];
132
133 if (number_of_tasks == 0)
134 return;
135
136 T *smem_t = &shared_memory[0]; // [max_nsgf_set][ld]
137 T *smem_p = smem_t + max_nsgf_set * ld; // [ld][ld], P[jco][ico]
139 const int nthreads = blockDim.x * blockDim.y * blockDim.z;
141 T *smem_cab = reinterpret_cast<double *>(
142 __builtin_assume_aligned(allocate_workspace<T>(dev_), 32));
143
144 for (int tk = 0; tk < number_of_tasks; tk++) {
145 __shared__ smem_task<T> task;
146 const int task_id = dev_.task_sorted_by_blocks_dev[offset + tk];
147 if (dev_.tasks[task_id].skip_task)
148 continue;
149 fill_smem_task_coef(dev_, task_id, task);
150
151 T *__restrict__ coef_ =
152 &dev_.buffers_dev.coef[dev_.tasks[task_id].coef_offset];
153
154 for (int z = tid; z < task.n1 * task.n2; z += nthreads)
155 smem_cab[z] = 0.0;
156
157 // step 1: T[i][ico] = sum_j pab[i][j] sphia[j][ico]
158 const int nco_a = task.ncoseta - task.first_coseta;
159 const int nco_b = task.ncosetb - task.first_cosetb;
160 for (int k = tid; k < task.nsgf_setb * nco_a; k += nthreads) {
161 const int i = k / nco_a;
162 const int ico = task.first_coseta + (k - i * nco_a);
163 T val = 0.0;
164 for (int j = 0; j < task.nsgf_seta; j++) {
165 const T block_val = task.block_transposed
166 ? task.pab_block[j * task.nsgfb + i]
167 : task.pab_block[i * task.nsgfa + j];
168 val += block_val * task.sphia[j * task.maxcoa + ico];
169 }
170 smem_t[i * ld + ico] = val * task.off_diag_twice;
171 }
173
174 // step 2: P[jco][ico] = sum_i sphib[i][jco] T[i][ico]
175 for (int k = tid; k < nco_b * nco_a; k += nthreads) {
176 const int jj = k / nco_a;
177 const int jco = task.first_cosetb + jj;
178 const int ico = task.first_coseta + (k - jj * nco_a);
179 T val = 0.0;
180 for (int i = 0; i < task.nsgf_setb; i++)
181 val += task.sphib[i * task.maxcob + jco] * smem_t[i * ld + ico];
182 if (IS_FUNC_AB) {
183 smem_cab[jco * task.ncoseta + ico] += val;
184 } else {
185 smem_p[jco * ld + ico] = val;
186 }
187 }
189
190 if (!IS_FUNC_AB) {
191 // prepare_pab adds (atomically) to several elements of cab
192 for (int k = tid; k < nco_b * nco_a; k += nthreads) {
193 const int jj = k / nco_a;
194 const int jco = task.first_cosetb + jj;
195 const int ico = task.first_coseta + (k - jj * nco_a);
196 prepare_pab(dev_.func, coset_inv[ico], coset_inv[jco], task.zeta,
197 task.zetb, smem_p[jco * ld + ico], task.n1, smem_cab);
198 }
200 }
201
204 }
205}
206
207/*******************************************************************************
208 * Separable (plane streaming) collocate kernel. This is
209 * integrate_separable_kernel (grid_gpu_integrate.cu) run backwards.
210 *
211 * Axis aligned orthorhombic cell (axis_aligned_ = true):
212 *
213 * sum_{abc} C[abc] x^a y^b z^c exp(-zetp r^2)
214 * = sum_a [x^a e^{-zetp x^2}] sum_b [y^b e^{-zetp y^2}]
215 * sum_c [z^c e^{-zetp z^2}] C[abc]
216 *
217 * Other cells (axis_aligned_ = false): the
218 * cartesian coefficients are first expressed in lattice monomials u^i v^j w^k
219 * (u, v, w lattice coordinates of the point relative to the gaussian center),
220 *
221 * C_ijk[(i,j,k)] = sum_{a+b+c=i+j+k} M[(a,b,c)][(i,j,k)] C[(a,b,c)]
222 *
223 * with the matrix M of build_lattice_to_cartesian_matrix (the transpose of the
224 * integrate transformation). Only the polynomial is separated, the gaussian
225 * exp(-zetp r^2) is evaluated per grid point from the r^2 of the sphere test,
226 * as in integrate_separable_kernel. Factoring it as
227 *
228 * exp(-zetp g00 u^2) exp(-zetp g11 v^2) exp(-zetp g22 w^2)
229 * exp(-2 zetp g12 v w) exp(-2 zetp u (g01 v + g02 w))
230 *
231 * with the metric g_ij = a_i . a_j is not numerically stable: on strongly
232 * sheared cells the diagonal factors underflow while the cross terms
233 * overflow, even though their product is finite.
234 *
235 * For each z plane of the cube:
236 *
237 * D[a][b] = sum_c C[coset(a, b, c)] pz[c] a + b <= lp (step 1)
238 * E[y][a] = sum_b D[a][b] py[b][y] (step 2)
239 * grid[z][y][x] += sum_a E[y][a] px[a][x] (step 3)
240 *
241 * so that a grid point costs lp + 1 FMAs (Horner in x) instead of ncoset(lp).
242 * Steps 1 and 2 are O(lp^3) and O(ny lp^2) per plane. The set of grid points
243 * visited is the same as for the original collocate kernel: same window masks,
244 * same y / x trimming in the orthogonal case, same sphere test.
245 *
246 * The powers of y and the x coordinates are computed on the fly. The tasks are
247 * split between two instances (see sep_skip_task):
248 *
249 * - chunked_ = false: tasks with a cube edge of at most mc points, the whole
250 * plane in one pass (erows = mc) and the gaussian along x and y tabulated.
251 * - chunked_ = true: larger cubes, the rows of a plane by chunks of erows rows
252 * (see collocate_separable_erows) and the gaussian along x and y evaluated
253 * on the fly, so that the shared memory does not depend on the cube size.
254 *
255 * Dynamic shared memory layout (doubles), ls = lp_max + 1:
256 * ex[mc], ey[mc], D[ls][ls], C[ncoset(lp_max)], E[erows][ls]
257 * ex and ey are only allocated for an axis aligned cell and chunked_ = false.
258 ******************************************************************************/
259
260template <bool distributed__, bool axis_aligned_, bool chunked_>
261__global__ __launch_bounds__(64) void collocate_separable_kernel(
262 const kernel_params dev_, const int mc, const int lp_max, const int erows,
263 const double *__restrict__ M) {
264 // the orthogonal code paths are only used for an axis aligned cell (see
265 // grid_info::check_orthogonality)
266 constexpr bool orthogonal_ = axis_aligned_;
267 // the gaussian along x and y is tabulated
268 constexpr bool tables_ = axis_aligned_ && !chunked_;
269
272 return;
273
274 const int tid = threadIdx.x;
275 const int ls = lp_max + 1;
276
277 extern __shared__ double sep_smem[];
278 double *ex = sep_smem;
279 double *ey = ex + (tables_ ? mc : 0);
280 double *D = ey + (tables_ ? mc : 0);
281 double *C = D + ls * ls;
282 double *E = C + ncoset(lp_max); // [erows][ls]
283
290
291 const int lp = task.lp;
292 const int ncoef = ncoset(lp);
293 const double zetp = task.zetp;
294 const double r2max = task.radius * task.radius;
295 const bool trim = orthogonal_ && !task.apply_border_mask;
296
297 const double *__restrict__ coef =
298 &dev_.buffers_dev.coef[glb_task.coef_offset];
299
300 // coefficients, cartesian or in lattice monomials
301 for (int i = tid; i < ncoef; i += 64) {
302 if (axis_aligned_) {
303 C[i] = coef[i];
304 } else {
305 const orbital &co = coset_inv[i];
306 const int l = co.l[0] + co.l[1] + co.l[2];
307 const int base = (l > 0) ? ncoset(l - 1) : 0;
308 const int nl = ncoset(l) - base;
309 // column (i - base) of block l
310 const double *__restrict__ Ml = M + sep_m_offset[l] + (i - base);
311 double val = 0.0;
312 for (int k = 0; k < nl; k++)
313 val += Ml[k * nl] * coef[base + k];
314 C[i] = val;
315 }
316 }
317
318 // 1D tables of the gaussian for an axis aligned cell
319 if (tables_) {
320 for (int x = tid; x < task.cube_size.x; x += 64) {
321 const double r = (x + task.lb_cube.x + task.roffset.x) * dev_.dh_[0];
322 ex[x] = exp(-zetp * r * r);
323 }
324 for (int y = tid; y < task.cube_size.y; y += 64) {
325 const double r = (y + task.lb_cube.y + task.roffset.y) * dev_.dh_[4];
326 ey[y] = exp(-zetp * r * r);
327 }
328 }
330
331 const int npairs = (lp + 1) * (lp + 2) / 2;
335
336 for (int z = 0; z < task.cube_size.z; z++) {
337 // the plane decisions below are uniform over the block
338 const int z2 =
339 wrap_grid_index(z + task.cube_center.z, dev_.grid_full_size_.z);
340 if (distributed__ && task.apply_border_mask &&
341 ((z2 < task.window_shift.z) || (z2 > task.window_size.z)))
342 continue;
343
344 int ymin = 0, ymax = task.cube_size.y - 1;
345 double kremain = 0.0;
346 if (trim)
347 kremain = calculate_ymix_ymax_boundaries(task, z, ymin, ymax);
348 if (ymin > ymax)
349 continue;
350
351 const double w = z + task.lb_cube.z + task.roffset.z;
352 const double rz = axis_aligned_ ? w * dev_.dh_[8] : w;
353 const double ez = axis_aligned_ ? exp(-zetp * rz * rz) : 1.0;
354
355 // step 1: contract c (Horner in rz). D is only read by step 2 so the
356 // barrier ending step 2 of the previous plane protects it.
357 for (int ip = tid; ip < npairs; ip += 64) {
358 const int a = sep_pair_ab[ip].x;
359 const int b = sep_pair_ab[ip].y;
360 double d = 0.0;
361 for (int c = lp - a - b; c >= 0; c--)
362 d = d * rz + C[coset(a, b, c)];
363 D[a * ls + b] = d * ez;
364 }
366
367 // steps 2 and 3 by chunks of erows rows, so that E fits in the shared
368 // memory for any cube size. The trip count is uniform over the block, a
369 // single pass over the whole plane unless chunked_.
370 for (int y0 = ymin; y0 <= ymax; y0 = chunked_ ? y0 + erows : ymax + 1) {
371 const int y1 = chunked_ ? min(ymax, y0 + erows - 1) : ymax;
372 const int nyc = y1 - y0 + 1;
373
374 // step 2: contract b, with the powers of y computed on the fly
375 for (int k = tid; k < nyc * (lp + 1); k += 64) {
376 const int yy = k / (lp + 1);
377 const int a = k - yy * (lp + 1);
378 const int y = y0 + yy;
379 const double v = y + task.lb_cube.y + task.roffset.y;
380 const double r = axis_aligned_ ? v * dev_.dh_[4] : v;
381 double p = tables_ ? ey[y] : (axis_aligned_ ? exp(-zetp * r * r) : 1.0);
382 double e = 0.0;
383 for (int b = 0; b <= lp - a; b++, p *= r)
384 e += D[a * ls + b] * p;
385 E[yy * ls + a] = e;
386 }
388
389 // step 3: one group of GRID_SEP_COLLOCATE_ROW_LANES lanes per row y,
390 // Horner in x.
391 for (int y = y0 + row_group; y <= y1; y += nrow_groups) {
392 const int y2 =
393 wrap_grid_index(y + task.cube_center.y, dev_.grid_full_size_.y);
394 if (distributed__ && task.apply_border_mask &&
395 ((y2 < task.window_shift.y) || (y2 > task.window_size.y)))
396 continue;
397
398 int xmin = 0, xmax = task.cube_size.x - 1;
399 if (trim)
401 xmin, xmax);
402 const double v = y + task.lb_cube.y + task.roffset.y;
403 const double *__restrict__ Ey = &E[(y - y0) * ls];
404 double *__restrict__ row =
405 &dev_.buffers_dev.grid[(z2 * dev_.grid_local_size_.y + y2) *
406 dev_.grid_local_size_.x];
407
408 // periodic index of x, updated incrementally
409 const int nx_full = dev_.grid_full_size_.x;
410 int x2 = wrap_grid_index(xmin + lane + task.cube_center.x, nx_full);
411 for (int x = xmin + lane; x <= xmax; x += GRID_SEP_COLLOCATE_ROW_LANES,
413 x2, nx_full)) {
414 if (distributed__ && task.apply_border_mask &&
415 ((x2 < task.window_shift.x) || (x2 > task.window_size.x)))
416 continue;
417
418 const double u = x + task.lb_cube.x + task.roffset.x;
419 const double r = axis_aligned_ ? u * dev_.dh_[0] : u;
420 double gauss;
421 if (axis_aligned_) {
422 // the sphere test is never applied in this case (orthogonal_)
423 gauss = tables_ ? ex[x] : exp(-zetp * r * r);
424 } else {
425 const double3 r3 = compute_coordinates(dev_.dh_, r, v, w);
426 const double r2 = r3.x * r3.x + r3.y * r3.y + r3.z * r3.z;
427 // same sphere test as the original collocate kernel
428 const bool outside = (r2max <= r2);
429 if (distributed__) {
430 if (outside && (!orthogonal_ || task.apply_border_mask))
431 continue;
432 } else if (!orthogonal_ && outside) {
433 continue;
434 }
435 gauss = exp(-zetp * r2);
436 }
437 if (axis_aligned_ && distributed__ && task.apply_border_mask) {
438 const double ry = v * dev_.dh_[4];
439 const double r2 = r * r + ry * ry + rz * rz;
440 if (r2max <= r2)
441 continue;
442 }
443
444 double val = Ey[lp];
445 for (int a = lp - 1; a >= 0; a--)
446 val = val * r + Ey[a];
447 atomicAdd(&row[x2], val * gauss);
448 }
449 }
450 // E is overwritten by the next chunk
451 if (chunked_ && (y1 < ymax))
453 }
454 }
455}
456
457// shared memory of collocate_separable_kernel, erows rows of E per chunk and
458// ntab entries in the 1D tables of the gaussian
459static size_t collocate_separable_smem_size(const int lp_max, const int erows,
460 const int ntab) {
461 const int ls = lp_max + 1;
462 return sizeof(double) * (ntab + ls * ls + ncoset(lp_max) + erows * ls);
463}
464
465// largest cube edge processed by the instances with chunked_ = false: all the
466// rows of the cube (erows = mc) and for an axis aligned cell the tables ex and
467// ey (2 mc entries) in smem_max.
469 const bool axis_aligned,
470 const size_t smem_max) {
471 const size_t fixed = collocate_separable_smem_size(lp_max, 0, 0);
472 const size_t per_edge =
473 sizeof(double) * (lp_max + 1 + (axis_aligned ? 2 : 0));
474 return (smem_max > fixed) ? (int)((smem_max - fixed) / per_edge) : 0;
475}
476
477// rows of E per chunk of the chunked_ instances, at most mc
478static int collocate_separable_erows(const int mc, const int lp_max,
479 const size_t smem_max) {
480 const size_t fixed = collocate_separable_smem_size(lp_max, 0, 0);
481 const size_t per_row = sizeof(double) * (lp_max + 1);
482 if (fixed + per_row > smem_max) {
483 fprintf(stderr, "ERROR: lp %d too large for collocate_separable_kernel.\n",
484 lp_max);
485 abort();
486 }
487 return (int)std::min<size_t>(mc, (smem_max - fixed) / per_row);
488}
489
491 int *lp_diff) {
492 // Compute max angular momentum.
493 const ldiffs_value ldiffs = prepare_get_ldiffs(func);
494 smem_parameters smem_params(ldiffs, lmax());
495
496 *lp_diff = smem_params.lp_diff();
499
500 // kernel parameters
501 kernel_params params = set_kernel_parameters(-1, smem_params);
502 params.func = func;
503
504 // Launch !
505 const dim3 threads_per_block(4, 4, 4);
506
507 // two step sphi transformation when its work arrays fit in the shared memory
508 // next to the static shared memory of the kernel
509 const int ld = ncoset(lmax());
510 const size_t smem_two_steps =
511 sizeof(double) * sphi_work_size(lmax(), max_nsgf_set());
512 const auto two_steps_kernel =
513 (func == GRID_FUNC_AB) ? calculate_coefficients_two_steps<double, true>
514 : calculate_coefficients_two_steps<double, false>;
515 if (smem_two_steps <=
516 max_dynamic_smem(two_steps_kernel, grid_gpu_smem_budget)) {
517 GRID_GPU_LAUNCH(two_steps_kernel, nblocks, threads_per_block,
518 smem_two_steps, main_stream, params, ld, max_nsgf_set());
519 return;
520 }
521
522 const auto fused_kernel = (func == GRID_FUNC_AB)
523 ? calculate_coefficients<double, true>
524 : calculate_coefficients<double, false>;
525 GRID_GPU_LAUNCH(fused_kernel, nblocks, threads_per_block, 0, main_stream,
526 params);
527}
528using collocate_separable_kernel_t = void (*)(const kernel_params, const int,
529 const int, const int,
530 const double *);
531
532template <bool chunked_>
534select_collocate_separable(const bool distributed, const bool axis_aligned) {
535 if (distributed)
536 return axis_aligned ? collocate_separable_kernel<true, true, chunked_>
537 : collocate_separable_kernel<true, false, chunked_>;
538 return axis_aligned ? collocate_separable_kernel<false, true, chunked_>
539 : collocate_separable_kernel<false, false, chunked_>;
540}
541
542/*******************************************************************************
543 * \brief Launches the Cuda kernel that collocates all tasks of one grid level.
544 ******************************************************************************/
546 const enum grid_func func,
547 int *lp_diff) {
548
549 if (number_of_tasks_per_level_[level] == 0)
550 return;
551
552 // Compute max angular momentum.
553 const ldiffs_value ldiffs = prepare_get_ldiffs(func);
554 smem_parameters smem_params(ldiffs, lmax());
555
556 *lp_diff = smem_params.lp_diff();
558
559 // kernel parameters
560 kernel_params params = set_kernel_parameters(level, smem_params);
561 params.func = func;
562
563 // cartesian factorization for an axis aligned cell, lattice coordinates +
564 // transformation otherwise.
565 auto &grid = grid_[level];
566 const int lp_max = smem_params.lp_max();
567 const int max_cube_size = std::max(grid.max_cube_size(), 1);
568 const bool axis_aligned = grid.is_axis_aligned();
569 // lp_max <= grid_gpu_max_l == GRID_SEP_MAX_L is checked by smem_parameters
570 static_assert(grid_gpu_max_l <= GRID_SEP_MAX_L, "separable tables too small");
571
573 const double *lattice_to_cartesian =
574 axis_aligned ? nullptr : get_lattice_to_cartesian(grid, lp_max);
575 const bool distributed = grid.is_distributed();
576 const auto kernel =
577 select_collocate_separable<false>(distributed, axis_aligned);
578 const int nblocks_level = number_of_tasks_per_level_[level];
579
580 // the tasks with a cube edge of at most mc in one pass over the planes (see
581 // sep_skip_task)
582 const int mc = std::min(max_cube_size,
584 lp_max, axis_aligned,
586 if (mc > 0) {
587 const size_t smem =
588 collocate_separable_smem_size(lp_max, mc, axis_aligned ? 2 * mc : 0);
589 GRID_GPU_LAUNCH(kernel, nblocks_level, 64, smem, level_streams[level],
590 params, mc, lp_max, mc, lattice_to_cartesian);
591 }
592
593 // the larger cubes by chunks of rows, on the same stream
594 if (max_cube_size > mc) {
595 const auto chunked =
596 select_collocate_separable<true>(distributed, axis_aligned);
598 max_cube_size, lp_max, max_dynamic_smem(chunked, grid_gpu_smem_budget));
599 const size_t smem = collocate_separable_smem_size(lp_max, erows, 0);
600 GRID_GPU_LAUNCH(chunked, nblocks_level, 64, smem, level_streams[level],
602 }
603}
604} // namespace grid_gpu_backend
std::vector< offloadStream_t > level_streams
std::vector< int > number_of_tasks_per_level_
void calculate_all_coefficients(const enum grid_func func, int *lp_diff)
void collocate_one_grid_level(const int level, const enum grid_func func, int *lp_diff)
Launches the Cuda kernel that collocates all tasks of one grid level.
void prepare_cab_workspace(const int la_max_diff, const int lb_max_diff)
Lays out the cab workspace of the coefficient kernels for the angular momentum offsets of the current...
std::vector< grid_info< double > > grid_
grid_func
@ GRID_FUNC_AB
static void const int const int const int const int const int const double const int const int const int int GRID_CONST_WHEN_COLLOCATE double GRID_CONST_WHEN_INTEGRATE double * grid
static void const int const int i
#define GRID_GPU_LAUNCH(kernel, blocks, threads, smem, stream,...)
#define GRID_SEP_MAX_L
#define GRID_SEP_COLLOCATE_ROW_LANES
fill_smem_task_reduced(dev_, dev_.first_task+block_index(), task)
__device__ __inline__ int sep_advance_wrapped_index(const int x2, const int n)
int sphi_work_size(const int lmax, const int max_nsgf_set)
Size (in doubles) of the work arrays of the two step spherical <-> cartesian transformations of calcu...
__inline__ __host__ __device__ double3 compute_coordinates(const double *__restrict__ dh_, const double x, const double y, const double z)
const task_info & glb_task
static void init_constant_memory()
Initializes the device's constant memory.
__inline__ __device__ unsigned int block_index()
__global__ const int ld
static collocate_separable_kernel_t select_collocate_separable(const bool distributed, const bool axis_aligned)
__device__ __inline__ void setup_task_cube_center(const kernel_params &dev_, smem_task_reduced< T, T3 > &task)
Shifts task.cube_center into the local grid's coordinate system and, for distributed grids,...
__global__ const int const int const int erows
__device__ __inline__ void prepare_pab(const enum grid_func func, const orbital a, const orbital b, const T zeta, const T zetb, const T pab_val, const int n, T *cab)
Transforms a given element of the density matrix according to func.
__device__ __inline__ T calculate_ymix_ymax_boundaries(smem_task_reduced< T, T3 > &task, const int z, int &ymin, int &ymax)
static int collocate_separable_erows(const int mc, const int lp_max, const size_t smem_max)
__shared__ double sep_smem[]
__device__ __inline__ void fill_smem_task_coef(const kernel_params &dev, const int task_id, smem_task< T > &task)
Copies a task from global to shared memory and does precomputations.
constexpr int nrow_groups
__inline__ __device__ unsigned int thread_global_index()
static int collocate_separable_max_edge(const int lp_max, const bool axis_aligned, const size_t smem_max)
__device__ __inline__ void cab_to_cxyz_factorized(const smem_task< T > &task, T *__restrict__ cab, T *__restrict__ cxyz)
Transforms the coefficients C_ab into C_xyz, cxyz[c] = prefactor sum_{a, b} alpha[a][b][c] cab[b][a] ...
static void init_separable_constant_memory()
Initializes the constant memory of integrate_separable_kernel.
__inline__ __device__ int wrap_grid_index(const int idx, const int full_size)
Wraps a cube-local coordinate into the periodic full grid along one axis. Single point of entry share...
__constant__ orbital coset_inv[1330]
size_t max_dynamic_smem(Kernel kernel, const size_t budget)
Largest dynamic shared memory the kernel can be launched with when its static and dynamic shared memo...
__host__ __device__ __inline__ int coset(int lx, int ly, int lz)
Maps three angular momentum components to a single zero based index.
__device__ __inline__ void block_to_cab(const kernel_params &params, const smem_task< T > &task, T *cab)
Decontracts the subblock, going from spherical to cartesian harmonics.
__global__ const int const int max_nsgf_set
static size_t collocate_separable_smem_size(const int lp_max, const int erows, const int ntab)
static const double * get_lattice_to_cartesian(grid_info< double > &grid, const int lmax)
Returns the lattice to cartesian matrix of a grid level on the device, (re)computing it when the latt...
__global__ const int mc
__device__ __inline__ bool sep_skip_task(const task_info &task, const int mc)
__global__ __launch_bounds__(64) void calculate_coefficients(const kernel_params dev_)
Same as calculate_coefficients, with the spherical to cartesian transformation of block_to_cab done i...
__global__ const int const int const int const double *__restrict__ M
__inline__ __device__ T * allocate_workspace(const kernel_params &dev_)
__host__ __device__ __inline__ int ncoset(const int l)
Number of Cartesian orbitals up to given angular momentum quantum.
void(*)(const kernel_params, const int, const int, const int, const double *) collocate_separable_kernel_t
__constant__ int sep_m_offset[GRID_SEP_MAX_L+1]
__device__ __inline__ void calculate_xmin_xmax_boundaries(smem_task_reduced< T, T3 > &task, const int y, const T kremain, int &xmin, int &xmax)
ldiffs_value prepare_get_ldiffs(const enum grid_func func)
Returns difference in angular momentum range for given func.
__global__ const int const int const int const double *__restrict__ lattice_to_cartesian
__shared__ smem_task_reduced< double, double3 > task
const double *__restrict__ coef
__constant__ char2 sep_pair_ab[(GRID_SEP_MAX_L+1) *(GRID_SEP_MAX_L+2)/2]
constexpr size_t grid_gpu_smem_budget
__global__ const int const int lp_max
Parameters of the collocate kernel.
Differences in angular momentum.
Orbital angular momentum.
data needed for collocate and integrate kernels
data needed for calculating the coefficients, forces, and stress
Internal representation of a task.