Line data Source code
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 : #include <limits.h>
9 : #include <math.h>
10 : #include <stdbool.h>
11 : #include <stdio.h>
12 : #include <string.h>
13 :
14 : #ifdef __MKL
15 : #include <mkl.h>
16 : #endif
17 :
18 : #if defined(__LIBXSMM)
19 : #include <libxsmm.h>
20 : #endif
21 : #if defined(__LIBXS)
22 : #include <libxs/libxs_gemm.h>
23 : #endif
24 :
25 : #include "../common/grid_common.h"
26 : #include "grid_dgemm_tensor_local.h"
27 : #include "grid_dgemm_utils.h"
28 :
29 0 : void convert_to_lattice_coordinates(const double dh_inv_[3][3],
30 : const double *restrict const rp,
31 : double *restrict rp_c) {
32 0 : rp_c[0] =
33 0 : dh_inv_[0][0] * rp[0] + dh_inv_[1][0] * rp[1] + dh_inv_[0][0] * rp[2];
34 0 : rp_c[1] =
35 0 : dh_inv_[0][1] * rp[0] + dh_inv_[1][1] * rp[1] + dh_inv_[1][1] * rp[2];
36 0 : rp_c[2] =
37 0 : dh_inv_[0][2] * rp[0] + dh_inv_[1][2] * rp[1] + dh_inv_[2][2] * rp[2];
38 0 : }
39 :
40 232236 : void dgemm_simplified(dgemm_params *const m) {
41 232236 : if (m == NULL)
42 0 : abort();
43 :
44 : #if defined(__LIBXS)
45 : {
46 232236 : const char col_transa = (m->op2 == 'N') ? 'N' : 'T';
47 232236 : const char col_transb = (m->op1 == 'N') ? 'N' : 'T';
48 464472 : const libxs_gemm_config_t *cfg = libxs_gemm_dispatch(
49 : LIBXS_DATATYPE_F64, col_transa, col_transb, m->n, m->m, m->k, m->ldb,
50 232236 : m->lda, m->ldc, &m->alpha, &m->beta, NULL);
51 232236 : if (NULL != cfg) {
52 232236 : libxs_gemm_call(cfg, m->b, m->a, m->c);
53 232236 : return;
54 : }
55 : }
56 : #endif
57 :
58 : #if defined(__MKL)
59 : if ((m->op1 == 'N') && (m->op2 == 'N'))
60 : cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m->m, m->n, m->k,
61 : m->alpha, m->a, m->lda, m->b, m->ldb, m->beta, m->c, m->ldc);
62 :
63 : if ((m->op1 == 'T') && (m->op2 == 'N'))
64 : cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m->m, m->n, m->k,
65 : m->alpha, m->a, m->lda, m->b, m->ldb, m->beta, m->c, m->ldc);
66 :
67 : if ((m->op1 == 'N') && (m->op2 == 'T'))
68 : cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m->m, m->n, m->k,
69 : m->alpha, m->a, m->lda, m->b, m->ldb, m->beta, m->c, m->ldc);
70 :
71 : if ((m->op1 == 'T') && (m->op2 == 'T'))
72 : cblas_dgemm(CblasRowMajor, CblasTrans, CblasTrans, m->m, m->n, m->k,
73 : m->alpha, m->a, m->lda, m->b, m->ldb, m->beta, m->c, m->ldc);
74 :
75 : #else
76 :
77 0 : if ((m->op1 == 'N') && (m->op2 == 'N'))
78 0 : dgemm_("N", "N", &m->n, &m->m, &m->k, &m->alpha, m->b, &m->ldb, m->a,
79 0 : &m->lda, &m->beta, m->c, &m->ldc);
80 :
81 0 : if ((m->op1 == 'T') && (m->op2 == 'N'))
82 0 : dgemm_("N", "T", &m->n, &m->m, &m->k, &m->alpha, m->b, &m->ldb, m->a,
83 0 : &m->lda, &m->beta, m->c, &m->ldc);
84 :
85 0 : if ((m->op1 == 'T') && (m->op2 == 'T'))
86 0 : dgemm_("T", "T", &m->n, &m->m, &m->k, &m->alpha, m->b, &m->ldb, m->a,
87 0 : &m->lda, &m->beta, m->c, &m->ldc);
88 :
89 0 : if ((m->op1 == 'N') && (m->op2 == 'T'))
90 0 : dgemm_("T", "N", &m->n, &m->m, &m->k, &m->alpha, m->b, &m->ldb, m->a,
91 0 : &m->lda, &m->beta, m->c, &m->ldc);
92 :
93 : #endif
94 : }
95 :
96 380820 : void extract_sub_grid(const int *lower_corner, const int *upper_corner,
97 : const int *position, const tensor *const grid,
98 : tensor *const subgrid) {
99 380820 : int position1[3] = {0, 0, 0};
100 :
101 380820 : if (position) {
102 380820 : position1[0] = position[0];
103 380820 : position1[1] = position[1];
104 380820 : position1[2] = position[2];
105 : }
106 :
107 380820 : const int sizex = upper_corner[2] - lower_corner[2];
108 380820 : const int sizey = upper_corner[1] - lower_corner[1];
109 380820 : const int sizez = upper_corner[0] - lower_corner[0];
110 :
111 4974324 : for (int z = 0; z < sizez; z++) {
112 61956638 : for (int y = 0; y < sizey; y++) {
113 57363134 : double *restrict src =
114 57363134 : &idx3(grid[0], lower_corner[0] + z - grid->window_shift[0],
115 : lower_corner[1] + y - grid->window_shift[1],
116 : lower_corner[2] - grid->window_shift[2]);
117 57363134 : double *restrict dst =
118 57363134 : &idx3(subgrid[0], position1[0] + z, position1[1] + y, position1[2]);
119 57363134 : GRID_PRAGMA_SIMD((dst, src), 8)
120 57363134 : for (int x = 0; x < sizex; x++) {
121 778255433 : dst[x] = src[x];
122 : }
123 : }
124 : }
125 :
126 380820 : return;
127 : }
128 :
129 414166 : void add_sub_grid(const int *lower_corner, const int *upper_corner,
130 : const int *position, const tensor *subgrid, tensor *grid) {
131 414166 : int position1[3] = {0, 0, 0};
132 :
133 414166 : if (position) {
134 414166 : position1[0] = position[0];
135 414166 : position1[1] = position[1];
136 414166 : position1[2] = position[2];
137 : }
138 :
139 414166 : const int sizex = upper_corner[2] - lower_corner[2];
140 414166 : const int sizey = upper_corner[1] - lower_corner[1];
141 414166 : const int sizez = upper_corner[0] - lower_corner[0];
142 :
143 5315394 : for (int z = 0; z < sizez; z++) {
144 4901228 : double *restrict dst =
145 4901228 : &idx3(grid[0], lower_corner[0] + z, lower_corner[1], lower_corner[2]);
146 4901228 : double *restrict src =
147 4901228 : &idx3(subgrid[0], position1[0] + z, position1[1], position1[2]);
148 59167776 : for (int y = 0; y < sizey - 1; y++) {
149 : GRID_PRAGMA_SIMD((dst, src), 8)
150 : for (int x = 0; x < sizex; x++) {
151 731179166 : dst[x] += src[x];
152 : }
153 :
154 54266548 : dst += grid->ld_;
155 54266548 : src += subgrid->ld_;
156 : }
157 :
158 : // #pragma omp simd linear(dst, src) simdlen(8)
159 4901228 : GRID_PRAGMA_SIMD((dst, src), 8)
160 : for (int x = 0; x < sizex; x++) {
161 59381511 : dst[x] += src[x];
162 : }
163 : }
164 414166 : return;
165 : }
166 :
167 90334 : int compute_cube_properties(const bool ortho, const double radius,
168 : const double dh[3][3], const double dh_inv[3][3],
169 : const double *rp, double *disr_radius,
170 : double *roffset, int *cubecenter, int *lb_cube,
171 : int *ub_cube, int *cube_size) {
172 90334 : int cmax = 0;
173 :
174 : /* center of the gaussian in the lattice coordinates */
175 90334 : double rp1[3];
176 :
177 : /* it is in the lattice vector frame */
178 361336 : for (int i = 0; i < 3; i++) {
179 : double dh_inv_rp = 0.0;
180 1084008 : for (int j = 0; j < 3; j++) {
181 813006 : dh_inv_rp += dh_inv[j][i] * rp[j];
182 : }
183 271002 : rp1[2 - i] = dh_inv_rp;
184 271002 : cubecenter[2 - i] = floor(dh_inv_rp);
185 : }
186 :
187 90334 : if (ortho) {
188 : /* seting up the cube parameters */
189 42228 : const double dx[3] = {dh[2][2], dh[1][1], dh[0][0]};
190 42228 : const double dx_inv[3] = {dh_inv[2][2], dh_inv[1][1], dh_inv[0][0]};
191 : /* cube center */
192 :
193 : /* lower and upper bounds */
194 :
195 : // Historically, the radius gets discretized.
196 42228 : const double drmin = fmin(dh[0][0], fmin(dh[1][1], dh[2][2]));
197 42228 : *disr_radius = drmin * fmax(1.0, ceil(radius / drmin));
198 :
199 168912 : for (int i = 0; i < 3; i++) {
200 126684 : roffset[i] = rp[2 - i] - ((double)cubecenter[i]) * dx[i];
201 : }
202 :
203 168912 : for (int i = 0; i < 3; i++) {
204 126684 : lb_cube[i] = ceil(-1e-8 - *disr_radius * dx_inv[i]);
205 : }
206 :
207 : // Symmetric interval
208 168912 : for (int i = 0; i < 3; i++) {
209 126684 : ub_cube[i] = 1 - lb_cube[i];
210 : }
211 :
212 : } else {
213 192424 : for (int idir = 0; idir < 3; idir++) {
214 144318 : lb_cube[idir] = INT_MAX;
215 144318 : ub_cube[idir] = INT_MIN;
216 : }
217 :
218 : /* compute the size of the box. It is a fairly trivial way to compute
219 : * the box and it may have far more point than needed */
220 192424 : for (int i = -1; i <= 1; i++) {
221 577272 : for (int j = -1; j <= 1; j++) {
222 1731816 : for (int k = -1; k <= 1; k++) {
223 1298862 : double x[3] = {/* rp[0] + */ ((double)i) * radius,
224 1298862 : /* rp[1] + */ ((double)j) * radius,
225 1298862 : /* rp[2] + */ ((double)k) * radius};
226 : /* convert_to_lattice_coordinates(dh_inv, x, y); */
227 5195448 : for (int idir = 0; idir < 3; idir++) {
228 3896586 : const double resc = dh_inv[0][idir] * x[0] +
229 3896586 : dh_inv[1][idir] * x[1] + dh_inv[2][idir] * x[2];
230 3896586 : lb_cube[2 - idir] = imin(lb_cube[2 - idir], floor(resc));
231 3896586 : ub_cube[2 - idir] = imax(ub_cube[2 - idir], ceil(resc));
232 : }
233 : }
234 : }
235 : }
236 :
237 : /* compute the offset in lattice coordinates */
238 :
239 192424 : for (int i = 0; i < 3; i++) {
240 144318 : roffset[i] = rp1[i] - cubecenter[i];
241 : }
242 :
243 48106 : *disr_radius = radius;
244 : }
245 :
246 : /* compute the cube size ignoring periodicity */
247 :
248 : /* the +1 is normal here */
249 90334 : cube_size[0] = ub_cube[0] - lb_cube[0] + 1;
250 90334 : cube_size[1] = ub_cube[1] - lb_cube[1] + 1;
251 90334 : cube_size[2] = ub_cube[2] - lb_cube[2] + 1;
252 :
253 361336 : for (int i = 0; i < 3; i++) {
254 271002 : cmax = imax(cmax, cube_size[i]);
255 : }
256 :
257 90334 : return cmax;
258 : }
259 :
260 0 : void return_cube_position(const int *const lb_grid,
261 : const int *const cube_center,
262 : const int *const lower_boundaries_cube,
263 : const int *const period, int *const position) {
264 0 : for (int i = 0; i < 3; i++)
265 0 : position[i] = modulo(cube_center[i] - lb_grid[i] + lower_boundaries_cube[i],
266 0 : period[i]);
267 0 : }
268 :
269 1280 : void verify_orthogonality(const double dh[3][3], bool orthogonal[3]) {
270 1280 : double norm1, norm2, norm3;
271 :
272 1280 : norm1 = dh[0][0] * dh[0][0] + dh[0][1] * dh[0][1] + dh[0][2] * dh[0][2];
273 1280 : norm2 = dh[1][0] * dh[1][0] + dh[1][1] * dh[1][1] + dh[1][2] * dh[1][2];
274 1280 : norm3 = dh[2][0] * dh[2][0] + dh[2][1] * dh[2][1] + dh[2][2] * dh[2][2];
275 :
276 1280 : norm1 = 1.0 / sqrt(norm1);
277 1280 : norm2 = 1.0 / sqrt(norm2);
278 1280 : norm3 = 1.0 / sqrt(norm3);
279 :
280 : /* x z */
281 1280 : orthogonal[0] =
282 1280 : ((fabs(dh[0][0] * dh[2][0] + dh[0][1] * dh[2][1] + dh[0][2] * dh[2][2]) *
283 1280 : norm1 * norm3) < 1e-12);
284 : /* y z */
285 1280 : orthogonal[1] =
286 1280 : ((fabs(dh[1][0] * dh[2][0] + dh[1][1] * dh[2][1] + dh[1][2] * dh[2][2]) *
287 1280 : norm2 * norm3) < 1e-12);
288 : /* x y */
289 1280 : orthogonal[2] =
290 1280 : ((fabs(dh[0][0] * dh[1][0] + dh[0][1] * dh[1][1] + dh[0][2] * dh[1][2]) *
291 1280 : norm1 * norm2) < 1e-12);
292 1280 : }
293 :
294 : #ifndef __MKL
295 : extern void dger_(const int *M, const int *N, const double *alpha,
296 : const double *X, const int *incX, const double *Y,
297 : const int *incY, double *A, const int *lda);
298 : extern void dgemv_(const char *Trans, const int *M, const int *N,
299 : const double *alpha, const double *A, const int *lda,
300 : const double *X, const int *incX, const double *beta,
301 : double *Y, const int *incY);
302 :
303 80 : void cblas_daxpy(const int N, const double alpha, const double *X,
304 : const int incX, double *Y, const int incY) {
305 80 : if ((incX == 1) && (incY == 1)) {
306 776 : for (int i = 0; i < N; i++)
307 696 : Y[i] += alpha * X[i];
308 : return;
309 : }
310 :
311 0 : if (incX == 1) {
312 0 : for (int i = 0; i < N; i++)
313 0 : Y[i + incY] += alpha * X[i];
314 : return;
315 : }
316 :
317 0 : if (incY == 1) {
318 0 : for (int i = 0; i < N; i++)
319 0 : Y[i] += alpha * X[i + incX];
320 : return;
321 : }
322 :
323 0 : for (int i = 0; i < N; i++)
324 0 : Y[i + incY] += alpha * X[i + incX];
325 : return;
326 : }
327 :
328 9280 : double cblas_ddot(const int N, const double *X, const int incX, const double *Y,
329 : const int incY) {
330 9280 : if ((incX == incY) && (incY == 1)) {
331 : double res = 0.0;
332 :
333 230673 : for (int i = 0; i < N; i++) {
334 221393 : res += X[i] * Y[i];
335 : }
336 : return res;
337 : }
338 :
339 0 : if (incX == 1) {
340 : double res = 0.0;
341 :
342 0 : for (int i = 0; i < N; i++) {
343 0 : res += X[i] * Y[i + incY];
344 : }
345 : return res;
346 : }
347 :
348 0 : if (incY == 1) {
349 : double res = 0.0;
350 :
351 0 : for (int i = 0; i < N; i++) {
352 0 : res += X[i + incX] * Y[i];
353 : }
354 : return res;
355 : }
356 :
357 : double res = 0.0;
358 :
359 0 : for (int i = 0; i < N; i++) {
360 0 : res += X[i + incX] * Y[i + incY];
361 : }
362 : return res;
363 : }
364 :
365 69226 : void cblas_dger(const CBLAS_LAYOUT Layout, const int M, const int N,
366 : const double alpha, const double *X, const int incX,
367 : const double *Y, const int incY, double *A, const int lda) {
368 69226 : if (Layout == CblasRowMajor) {
369 69226 : dger_(&N, &M, &alpha, Y, &incY, X, &incX, A, &lda);
370 : } else {
371 0 : dger_(&N, &M, &alpha, X, &incX, Y, &incY, A, &lda);
372 : }
373 69226 : }
374 :
375 : /* code taken from gsl_cblas. We really need to use a proper cblas interface and
376 : * build system.... */
377 18560 : void cblas_dgemv(const CBLAS_LAYOUT order, const CBLAS_TRANSPOSE TransA,
378 : const int M, const int N, const double alpha, const double *A,
379 : const int lda, const double *X, const int incX,
380 : const double beta, double *Y, const int incY) {
381 :
382 18560 : if (order == CblasColMajor) {
383 0 : if (TransA == CblasTrans)
384 0 : dgemv_("T", &M, &N, &alpha, A, &lda, X, &incX, &beta, Y, &incY);
385 : else {
386 0 : dgemv_("N", &M, &N, &alpha, A, &lda, X, &incX, &beta, Y, &incY);
387 : }
388 : } else {
389 18560 : if (TransA == CblasTrans)
390 0 : dgemv_("N", &N, &M, &alpha, A, &lda, X, &incX, &beta, Y, &incY);
391 : else {
392 18560 : dgemv_("T", &N, &M, &alpha, A, &lda, X, &incX, &beta, Y, &incY);
393 : }
394 : }
395 18560 : }
396 : #endif
397 :
398 1321626 : void compute_interval(const int *const map, const int full_size, const int size,
399 : const int cube_size, const int x1, int *x,
400 : int *const lower_corner, int *const upper_corner,
401 : const Interval window) {
402 1321626 : if (size == full_size) {
403 : /* we have the full grid in that direction */
404 : /* lower boundary is within the window */
405 1321626 : *lower_corner = x1;
406 : /* now compute the upper corner */
407 : /* needs to be as large as possible. basically I take [x1..
408 : * min(grid.full_size, cube_size - x)] */
409 :
410 1321626 : *upper_corner = compute_next_boundaries(x1, *x, full_size, cube_size);
411 :
412 : /* { */
413 : /* Interval tz = create_interval(*lower_corner, *upper_corner); */
414 : /* Interval res = intersection_interval(tz, window); */
415 : /* *lower_corner = res.xmin; */
416 : /* *upper_corner = res.xmax; */
417 : /* } */
418 : } else {
419 0 : *lower_corner = x1;
420 0 : *upper_corner = x1 + 1;
421 :
422 : // the map is always increasing by 1 except when we cross the boundaries of
423 : // the grid and pbc are applied. Since we are only interested in by a
424 : // subwindow of the full table we check that the next point is inside the
425 : // window of interest and is also equal to the previous point + 1. The last
426 : // check is pointless in practice.
427 :
428 0 : for (int i = *x + 1; (i < cube_size) && (*upper_corner == map[i]) &&
429 0 : is_point_in_interval(map[i], window);
430 0 : i++) {
431 0 : (*upper_corner)++;
432 : }
433 : }
434 1321626 : }
|