LCOV - code coverage report
Current view: top level - src/grid/dgemm - grid_dgemm_context.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 72.2 % 277 200
Test Date: 2026-11-24 01:22:08 Functions: 61.9 % 21 13

            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 <math.h>
       9              : #include <omp.h>
      10              : #include <stdio.h>
      11              : #include <stdlib.h>
      12              : #include <string.h>
      13              : 
      14              : #include "../../base/cp_assert.h"
      15              : #include "../common/grid_library.h"
      16              : #include "grid_dgemm_collocate.h"
      17              : #include "grid_dgemm_collocation_integration.h"
      18              : #include "grid_dgemm_context.h"
      19              : #include "grid_dgemm_private_header.h"
      20              : #include "grid_dgemm_task_list.h"
      21              : #include "grid_dgemm_tensor_local.h"
      22              : #include "grid_dgemm_utils.h"
      23              : 
      24            0 : void return_dh(void *const ptr, const int level, double *const dh) {
      25            0 :   grid_context *const ctx = (grid_context *)ptr;
      26              : 
      27            0 :   cp_assert(ctx->checksum == ctx_checksum);
      28            0 :   dh[0] = ctx->grid[level].dh[0][0];
      29            0 :   dh[1] = ctx->grid[level].dh[0][1];
      30            0 :   dh[2] = ctx->grid[level].dh[0][2];
      31            0 :   dh[3] = ctx->grid[level].dh[1][0];
      32            0 :   dh[4] = ctx->grid[level].dh[1][1];
      33            0 :   dh[5] = ctx->grid[level].dh[1][2];
      34            0 :   dh[6] = ctx->grid[level].dh[2][0];
      35            0 :   dh[7] = ctx->grid[level].dh[2][1];
      36            0 :   dh[8] = ctx->grid[level].dh[2][2];
      37            0 : }
      38              : 
      39            0 : void return_dh_inv(void *const ptr, const int level, double *const dh_inv) {
      40            0 :   grid_context *const ctx = (grid_context *)ptr;
      41              : 
      42            0 :   cp_assert(ctx->checksum == ctx_checksum);
      43            0 :   dh_inv[0] = ctx->grid[level].dh_inv[0][0];
      44            0 :   dh_inv[1] = ctx->grid[level].dh_inv[0][1];
      45            0 :   dh_inv[2] = ctx->grid[level].dh_inv[0][2];
      46            0 :   dh_inv[3] = ctx->grid[level].dh_inv[1][0];
      47            0 :   dh_inv[4] = ctx->grid[level].dh_inv[1][1];
      48            0 :   dh_inv[5] = ctx->grid[level].dh_inv[1][2];
      49            0 :   dh_inv[6] = ctx->grid[level].dh_inv[2][0];
      50            0 :   dh_inv[7] = ctx->grid[level].dh_inv[2][1];
      51            0 :   dh_inv[8] = ctx->grid[level].dh_inv[2][2];
      52            0 : }
      53              : 
      54            0 : int return_num_devs(void *const ptr) {
      55            0 :   grid_context *const ctx = (grid_context *)ptr;
      56            0 :   cp_assert(ctx->checksum == ctx_checksum);
      57              : 
      58            0 :   return ctx->number_of_devices;
      59              : }
      60              : 
      61            0 : int return_device_id(void *const ptr, const int device) {
      62            0 :   grid_context *const ctx = (grid_context *)ptr;
      63            0 :   cp_assert(ctx->checksum == ctx_checksum);
      64              : 
      65            0 :   return ctx->device_id[device];
      66              : }
      67              : 
      68            0 : int is_grid_orthorhombic(void *const ptr) {
      69            0 :   grid_context *const ctx = (grid_context *)ptr;
      70            0 :   cp_assert(ctx->checksum == ctx_checksum);
      71            0 :   return ctx->orthorhombic;
      72              : }
      73              : 
      74            0 : void update_queue_length(void *const ptr, const int queue_length) {
      75            0 :   grid_context *const ctx = (grid_context *)ptr;
      76            0 :   cp_assert(ctx->checksum == ctx_checksum);
      77            0 :   ctx->queue_length = queue_length;
      78            0 : }
      79              : 
      80           20 : void update_atoms_position(const int natoms,
      81              :                            const double atoms_positions[natoms][3],
      82              :                            grid_context *data) {
      83           20 :   cp_assert(data != NULL);
      84              : 
      85           20 :   if (natoms == 0)
      86              :     return;
      87              : 
      88           20 :   if (data->atom_positions == NULL) {
      89            8 :     data->atom_positions = malloc(3 * natoms * sizeof(double));
      90              :   } else {
      91           12 :     if (natoms > data->natoms) {
      92            0 :       data->atom_positions =
      93            0 :           realloc(data->atom_positions, 3 * natoms * sizeof(double));
      94              :     }
      95              :   }
      96           20 :   cp_assert(data->atom_positions != NULL);
      97              : 
      98           20 :   data->natoms = natoms;
      99              : 
     100           20 :   if (data->atom_positions) {
     101           78 :     for (int i = 0; i < natoms; i++) {
     102           58 :       data->atom_positions[3 * i] = atoms_positions[i][0];
     103           58 :       data->atom_positions[3 * i + 1] = atoms_positions[i][1];
     104           58 :       data->atom_positions[3 * i + 2] = atoms_positions[i][2];
     105              :     }
     106              :   }
     107              : }
     108              : 
     109           20 : void update_atoms_kinds(const int natoms, const int *atoms_kinds,
     110              :                         grid_context *data) {
     111           20 :   cp_assert(data != NULL);
     112              : 
     113              :   // data->atom_kinds is a table that give the type of a given atom.
     114           20 :   if (natoms == 0)
     115              :     return;
     116              : 
     117           20 :   if (data->atom_kinds == NULL) {
     118            8 :     data->atom_kinds = malloc(natoms * sizeof(int));
     119              :   } else {
     120           12 :     if ((natoms > data->natoms) && (data->natoms > 0)) {
     121            0 :       data->atom_kinds = realloc(data->atom_kinds, natoms * sizeof(int));
     122              :     }
     123              :   }
     124           20 :   cp_assert(data->atom_kinds != NULL);
     125              :   // data->natoms is initialized before calling this function
     126           20 :   if (data->natoms)
     127           20 :     memcpy(data->atom_kinds, atoms_kinds, sizeof(int) * natoms);
     128              : 
     129           78 :   for (int i = 0; i < natoms; i++) {
     130           58 :     data->atom_kinds[i] -= 1;
     131              :   }
     132              : }
     133              : 
     134           20 : void update_block_offsets(const int nblocks, const int *const block_offsets,
     135              :                           grid_context *data) {
     136           20 :   cp_assert(data != NULL);
     137              : 
     138           20 :   if (nblocks == 0)
     139              :     return;
     140              : 
     141           20 :   if (data->block_offsets == NULL) {
     142            8 :     data->block_offsets = malloc(nblocks * sizeof(int));
     143              :   } else {
     144           12 :     if ((nblocks > data->nblocks_total) && (data->nblocks_total > 0)) {
     145            0 :       data->block_offsets = realloc(data->block_offsets, sizeof(int) * nblocks);
     146              :     }
     147              :   }
     148           20 :   cp_assert(data->block_offsets != NULL);
     149              : 
     150           20 :   data->nblocks = nblocks;
     151           20 :   data->nblocks_total = imax(data->nblocks_total, nblocks);
     152           20 :   if (nblocks)
     153           20 :     memcpy(data->block_offsets, block_offsets, nblocks * sizeof(int));
     154              : }
     155              : 
     156           20 : void update_basis_set(const int nkinds, const grid_basis_set **const basis_sets,
     157              :                       grid_context *data) {
     158           20 :   if (nkinds > data->nkinds_total) {
     159            8 :     if (data->basis_sets == NULL) {
     160            8 :       data->basis_sets = malloc(nkinds * sizeof(grid_basis_set *));
     161              :     } else {
     162            0 :       data->basis_sets =
     163            0 :           realloc(data->basis_sets, nkinds * sizeof(grid_basis_set *));
     164              :     }
     165              :   }
     166           20 :   cp_assert(data->basis_sets != NULL);
     167           20 :   data->nkinds = nkinds;
     168           20 :   data->nkinds_total = imax(data->nkinds_total, nkinds);
     169           20 :   memcpy(data->basis_sets, basis_sets, nkinds * sizeof(grid_basis_set *));
     170           20 : }
     171              : 
     172           20 : void update_task_lists(const int nlevels, const int ntasks,
     173              :                        const int *const level_list, const int *const iatom_list,
     174              :                        const int *const jatom_list, const int *const iset_list,
     175              :                        const int *const jset_list, const int *const ipgf_list,
     176              :                        const int *const jpgf_list,
     177              :                        const int *const border_mask_list,
     178              :                        const int *block_num_list,
     179              :                        const double *const radius_list,
     180              :                        const double rab_list[ntasks][3], grid_context *ctx) {
     181              : 
     182           20 :   cp_assert(ctx->checksum == ctx_checksum);
     183              : 
     184           20 :   if (nlevels == 0)
     185              :     return;
     186              : 
     187           20 :   if (ctx->ntasks == 0) {
     188              :     // Count tasks per level.
     189            8 :     size_t size = nlevels * sizeof(int);
     190            8 :     ctx->tasks_per_level = malloc(size);
     191            8 :     ctx->tasks = malloc(nlevels * sizeof(_task *));
     192              :     /* memset(ctx->tasks, 0, nlevels * sizeof(_task *)); */
     193            8 :     if (ntasks)
     194            8 :       ctx->tasks[0] = malloc(ntasks * sizeof(_task));
     195              :     else
     196            0 :       ctx->tasks[0] = NULL;
     197              :   } else {
     198           12 :     if (ctx->nlevels_total < nlevels) {
     199              :       /* save the address of the full task list. NULL when completly empty */
     200            0 :       ctx->tasks = realloc(ctx->tasks, nlevels * sizeof(_task *));
     201            0 :       cp_assert(ctx->tasks != NULL);
     202              :     }
     203           12 :     if (ctx->ntasks_total < ntasks) {
     204            0 :       ctx->tasks[0] = realloc(ctx->tasks[0], ntasks * sizeof(_task));
     205            0 :       cp_assert(ctx->tasks[0] != NULL);
     206              :     }
     207              :   }
     208              : 
     209           20 :   memset(ctx->tasks_per_level, 0, nlevels * sizeof(int));
     210           20 :   ctx->nlevels = nlevels;
     211           20 :   ctx->nlevels_total = imax(ctx->nlevels_total, nlevels);
     212           20 :   ctx->ntasks_total = imax(ctx->ntasks_total, ntasks);
     213           20 :   ctx->ntasks = ntasks;
     214              : 
     215         5793 :   for (int i = 0; i < ntasks; i++) {
     216         5773 :     ctx->tasks_per_level[level_list[i] - 1]++;
     217         5773 :     cp_assert(i == 0 ||
     218              :               level_list[i] >= level_list[i - 1]); // expect ordered list
     219              :   }
     220              : 
     221           80 :   for (int i = 1; i < ctx->nlevels; i++) {
     222           60 :     ctx->tasks[i] = ctx->tasks[i - 1] + ctx->tasks_per_level[i - 1];
     223              :   }
     224              : 
     225           20 :   _task *const tasks = ctx->tasks[0];
     226           20 : #pragma omp parallel for schedule(static) if (ntasks > GRID_OMP_MIN_ITERATIONS)
     227              :   for (int i = 0; i < ntasks; i++) {
     228              :     _task *const task = &tasks[i];
     229              :     task->level = level_list[i] - 1;
     230              :     task->iatom = iatom_list[i] - 1;
     231              :     task->jatom = jatom_list[i] - 1;
     232              :     task->iset = iset_list[i] - 1;
     233              :     task->jset = jset_list[i] - 1;
     234              :     task->ipgf = ipgf_list[i] - 1;
     235              :     task->jpgf = jpgf_list[i] - 1;
     236              :     task->border_mask = border_mask_list[i];
     237              :     task->block_num = block_num_list[i] - 1;
     238              :     task->radius = radius_list[i];
     239              :     task->rab[0] = rab_list[i][0];
     240              :     task->rab[1] = rab_list[i][1];
     241              :     task->rab[2] = rab_list[i][2];
     242              :     const int iatom = task->iatom;
     243              :     const int jatom = task->jatom;
     244              :     const int iset = task->iset;
     245              :     const int jset = task->jset;
     246              :     const int ipgf = task->ipgf;
     247              :     const int jpgf = task->jpgf;
     248              :     const int ikind = ctx->atom_kinds[iatom];
     249              :     const int jkind = ctx->atom_kinds[jatom];
     250              :     const grid_basis_set *ibasis = ctx->basis_sets[ikind];
     251              :     const grid_basis_set *jbasis = ctx->basis_sets[jkind];
     252              :     const int ncoseta = ncoset(ibasis->lmax[iset]);
     253              :     const int ncosetb = ncoset(jbasis->lmax[jset]);
     254              : 
     255              :     task->zeta[0] = ibasis->zet[iset * ibasis->maxpgf + ipgf];
     256              :     task->zeta[1] = jbasis->zet[jset * jbasis->maxpgf + jpgf];
     257              : 
     258              :     const double *ra = &ctx->atom_positions[3 * iatom];
     259              :     const double zetp = task->zeta[0] + task->zeta[1];
     260              :     const double f = task->zeta[1] / zetp;
     261              :     const double rab2 = task->rab[0] * task->rab[0] +
     262              :                         task->rab[1] * task->rab[1] +
     263              :                         task->rab[2] * task->rab[2];
     264              : 
     265              :     task->prefactor = exp(-task->zeta[0] * f * rab2);
     266              :     task->zetp = zetp;
     267              : 
     268              :     for (int i = 0; i < 3; i++) {
     269              :       task->ra[i] = ra[i];
     270              :       task->rp[i] = ra[i] + f * task->rab[i];
     271              :       task->rb[i] = ra[i] + task->rab[i];
     272              :     }
     273              : 
     274              :     task->lmax[0] = ibasis->lmax[iset];
     275              :     task->lmax[1] = jbasis->lmax[jset];
     276              :     task->lmin[0] = ibasis->lmin[iset];
     277              :     task->lmin[1] = jbasis->lmin[jset];
     278              : 
     279              :     task->update_block_ = i == 0 || level_list[i] != level_list[i - 1] ||
     280              :                           block_num_list[i] != block_num_list[i - 1] ||
     281              :                           iset_list[i] != iset_list[i - 1] ||
     282              :                           jset_list[i] != jset_list[i - 1];
     283              : 
     284              :     task->offset[0] = ipgf * ncoseta;
     285              :     task->offset[1] = jpgf * ncosetb;
     286              :   }
     287              : 
     288              :   // Find largest Cartesian subblock size.
     289           20 :   ctx->maxco = 0;
     290           56 :   for (int i = 0; i < ctx->nkinds; i++) {
     291           36 :     ctx->maxco = imax(ctx->maxco, ctx->basis_sets[i]->maxco);
     292              :   }
     293              : }
     294              : 
     295           20 : void update_layouts(const int nlevels, const int npts_global[nlevels][3],
     296              :                     const int npts_local[nlevels][3],
     297              :                     const int shift_local[nlevels][3],
     298              :                     const int border_width[nlevels][3],
     299              :                     const double dh[nlevels][3][3],
     300              :                     const double dh_inv[nlevels][3][3], grid_context *ctx) {
     301              : 
     302           20 :   cp_assert(ctx != NULL);
     303           20 :   cp_assert(ctx->checksum == ctx_checksum);
     304              : 
     305           20 :   if (ctx->layouts != NULL) {
     306           12 :     free(ctx->layouts);
     307              :   }
     308              : 
     309           20 :   ctx->layouts = malloc(sizeof(_layout) * nlevels);
     310              : 
     311          100 :   for (int level = 0; level < nlevels; level++) {
     312          320 :     for (int i = 0; i < 3; i++) {
     313          240 :       ctx->layouts[level].npts_global[i] = npts_global[level][i];
     314          240 :       ctx->layouts[level].npts_local[i] = npts_local[level][i];
     315          240 :       ctx->layouts[level].shift_local[i] = shift_local[level][i];
     316          240 :       ctx->layouts[level].border_width[i] = border_width[level][i];
     317          960 :       for (int j = 0; j < 3; j++) {
     318          720 :         ctx->layouts[level].dh[i][j] = dh[level][i][j];
     319          720 :         ctx->layouts[level].dh_inv[i][j] = dh_inv[level][i][j];
     320              :       }
     321              :     }
     322              :   }
     323           20 : }
     324              : 
     325           20 : void update_grid(const int nlevels, grid_context *ctx) {
     326           20 :   cp_assert(ctx != NULL);
     327           20 :   cp_assert(ctx->checksum == ctx_checksum);
     328              : 
     329           20 :   if (nlevels == 0)
     330              :     return;
     331              : 
     332           20 :   if (ctx->grid == NULL) {
     333            8 :     ctx->grid = malloc(sizeof(tensor) * nlevels);
     334              :   } else {
     335           12 :     if (ctx->nlevels_total < nlevels) {
     336            0 :       ctx->grid = realloc(ctx->grid, sizeof(tensor) * nlevels);
     337              :     }
     338              :   }
     339           20 :   cp_assert(ctx->grid != NULL);
     340              : 
     341           20 :   ctx->nlevels_total = imax(ctx->nlevels_total, nlevels);
     342           20 :   ctx->nlevels = nlevels;
     343              : }
     344              : 
     345            8 : void *create_grid_context_dgemm(
     346              :     const bool orthorhombic, const int ntasks, const int nlevels,
     347              :     const int natoms, const int nkinds, const int nblocks,
     348              :     const int *block_offsets, const double atom_positions[natoms][3],
     349              :     const int *const atom_kinds, const grid_basis_set **const basis_sets,
     350              :     const int *const level_list, const int *const iatom_list,
     351              :     const int *jatom_list, const int *const iset_list,
     352              :     const int *const jset_list, const int *const ipgf_list,
     353              :     const int *const jpgf_list, const int *const border_mask_list,
     354              :     const int *block_num_list, const double *const radius_list,
     355              :     const double rab_list[ntasks][3], const int npts_global[nlevels][3],
     356              :     const int npts_local[nlevels][3], const int shift_local[nlevels][3],
     357              :     const int border_width[nlevels][3], const double dh[nlevels][3][3],
     358              :     const double dh_inv[nlevels][3][3]) {
     359              : 
     360            8 :   grid_context *ctx = malloc(sizeof(grid_context));
     361              : 
     362            8 :   memset(ctx, 0, sizeof(grid_context));
     363              : 
     364            8 :   ctx->checksum = ctx_checksum;
     365            8 :   ctx->orthorhombic = orthorhombic;
     366            8 :   update_block_offsets(nblocks, block_offsets, ctx);
     367            8 :   update_atoms_position(natoms, atom_positions, ctx);
     368            8 :   update_atoms_kinds(natoms, atom_kinds, ctx);
     369            8 :   update_basis_set(nkinds, basis_sets, ctx);
     370            8 :   update_task_lists(nlevels, ntasks, level_list, iatom_list, jatom_list,
     371              :                     iset_list, jset_list, ipgf_list, jpgf_list,
     372              :                     border_mask_list, block_num_list, radius_list, rab_list,
     373              :                     ctx);
     374            8 :   update_layouts(nlevels, npts_global, npts_local, shift_local, border_width,
     375              :                  dh, dh_inv, ctx);
     376            8 :   update_grid(nlevels, ctx);
     377              : 
     378            8 :   const int max_threads = omp_get_max_threads();
     379              : 
     380            8 :   ctx->handler =
     381            8 :       malloc(sizeof(struct collocation_integration_ *) * max_threads);
     382              : 
     383           16 :   for (int i = 0; i < max_threads; i++) {
     384            8 :     ctx->handler[i] = collocate_create_handle();
     385              :   }
     386              : 
     387            8 :   ctx->number_of_handler = max_threads;
     388              : 
     389            8 :   return ctx;
     390              : }
     391              : 
     392           12 : void update_grid_context_dgemm(
     393              :     const bool orthorhombic, const int ntasks, const int nlevels,
     394              :     const int natoms, const int nkinds, const int nblocks,
     395              :     const int *block_offsets, const double atom_positions[natoms][3],
     396              :     const int *const atom_kinds, const grid_basis_set **const basis_sets,
     397              :     const int *const level_list, const int *const iatom_list,
     398              :     const int *jatom_list, const int *const iset_list,
     399              :     const int *const jset_list, const int *const ipgf_list,
     400              :     const int *const jpgf_list, const int *const border_mask_list,
     401              :     const int *block_num_list, const double *const radius_list,
     402              :     const double rab_list[ntasks][3], const int npts_global[nlevels][3],
     403              :     const int npts_local[nlevels][3], const int shift_local[nlevels][3],
     404              :     const int border_width[nlevels][3], const double dh[nlevels][3][3],
     405              :     const double dh_inv[nlevels][3][3], void *ptr) {
     406              : 
     407           12 :   cp_assert(ptr != NULL);
     408           12 :   grid_context *ctx = (grid_context *)ptr;
     409           12 :   cp_assert(ctx->checksum == ctx_checksum);
     410              : 
     411           12 :   ctx->orthorhombic = orthorhombic;
     412           12 :   update_block_offsets(nblocks, block_offsets, ctx);
     413           12 :   update_atoms_position(natoms, atom_positions, ctx);
     414           12 :   update_atoms_kinds(natoms, atom_kinds, ctx);
     415           12 :   update_basis_set(nkinds, basis_sets, ctx);
     416           12 :   update_task_lists(nlevels, ntasks, level_list, iatom_list, jatom_list,
     417              :                     iset_list, jset_list, ipgf_list, jpgf_list,
     418              :                     border_mask_list, block_num_list, radius_list, rab_list,
     419              :                     ctx);
     420           12 :   update_layouts(nlevels, npts_global, npts_local, shift_local, border_width,
     421              :                  dh, dh_inv, ctx);
     422           12 :   update_grid(nlevels, ctx);
     423              : 
     424              :   // Find largest Cartesian subblock size.
     425           12 :   ctx->maxco = 0;
     426           36 :   for (int i = 0; i < nkinds; i++) {
     427           24 :     ctx->maxco = imax(ctx->maxco, ctx->basis_sets[i]->maxco);
     428              :   }
     429           12 : }
     430              : 
     431            0 : void initialize_grid_context_on_gpu(void *ptr, const int number_of_devices,
     432              :                                     const int *device_id) {
     433            0 :   cp_assert(ptr != NULL);
     434            0 :   grid_context *ctx = (grid_context *)ptr;
     435            0 :   cp_assert(ctx->checksum == ctx_checksum);
     436            0 :   ctx->work_on_gpu = false;
     437            0 :   if (number_of_devices <= 0) {
     438              :     return;
     439              :   }
     440              : 
     441            0 :   ctx->number_of_devices = number_of_devices;
     442            0 :   ctx->queue_length = 8192;
     443            0 :   if (ctx->device_id == NULL) {
     444            0 :     ctx->device_id = malloc(sizeof(int) * number_of_devices);
     445              :   } else {
     446            0 :     ctx->device_id = realloc(ctx->device_id, sizeof(int) * number_of_devices);
     447              :   }
     448            0 :   cp_assert(ctx->device_id != NULL);
     449              : 
     450            0 :   memcpy(ctx->device_id, device_id, sizeof(int) * number_of_devices);
     451              : }
     452              : 
     453            8 : void destroy_grid_context_dgemm(void *ptr) {
     454            8 :   cp_assert(ptr);
     455            8 :   grid_context *ctx = (grid_context *)ptr;
     456            8 :   cp_assert(ctx->checksum == ctx_checksum);
     457            8 :   free(ctx->block_offsets);
     458            8 :   free(ctx->atom_positions);
     459            8 :   free(ctx->atom_kinds);
     460            8 :   free(ctx->basis_sets);
     461            8 :   free(ctx->tasks[0]);
     462            8 :   free(ctx->tasks);
     463            8 :   free(ctx->tasks_per_level);
     464            8 :   free(ctx->layouts);
     465            8 :   free(ctx->grid);
     466            8 :   if (ctx->device_id)
     467            0 :     free(ctx->device_id);
     468              : 
     469            8 :   if (ctx->handler) {
     470           16 :     for (int i = 0; i < ctx->number_of_handler; i++) {
     471            8 :       collocate_destroy_handle(ctx->handler[i]);
     472              :     }
     473            8 :     free(ctx->handler);
     474              :   }
     475              : 
     476            8 :   free(ctx);
     477            8 : }
     478              : 
     479            0 : void apply_cutoff(void *ptr) {
     480            0 :   cp_assert(ptr);
     481            0 :   grid_context *ctx = (grid_context *)ptr;
     482            0 :   cp_assert(ctx->checksum == ctx_checksum);
     483            0 :   ctx->apply_cutoff = true;
     484            0 : }
     485              : 
     486         1280 : void set_grid_parameters(
     487              :     tensor *grid, const bool orthorhombic,
     488              :     const int grid_full_size[3],  /* size of the full grid */
     489              :     const int grid_local_size[3], /* size of the local grid block */
     490              :     const int shift_local[3],     /* coordinates of the lower coordinates of the
     491              :                                      local grid window */
     492              :     const int border_width[3],    /* width of the borders */
     493              :     const double
     494              :         dh[3][3], /* displacement vectors of the grid (cartesian) -> (ijk) */
     495              :     const double dh_inv[3][3], /* (ijk) -> (x,y,z) */
     496              :     offload_buffer *grid_) {
     497         1280 :   memset(grid, 0, sizeof(tensor));
     498         1280 :   initialize_tensor_3(grid, grid_local_size[2], grid_local_size[1],
     499              :                       grid_local_size[0]);
     500              : 
     501         1280 :   grid->data = grid_->host_buffer;
     502         1280 :   grid->ld_ = grid_local_size[0];
     503              : 
     504         1280 :   setup_global_grid_size(grid, &grid_full_size[0]);
     505              : 
     506              :   /* the grid is divided over several ranks or not periodic */
     507         1280 :   if ((grid_local_size[0] != grid_full_size[0]) ||
     508         1280 :       (grid_local_size[1] != grid_full_size[1]) ||
     509         1280 :       (grid_local_size[2] != grid_full_size[2])) {
     510            0 :     setup_grid_window(grid, shift_local, border_width, 0);
     511              :   } else {
     512         1280 :     grid->window_shift[0] = 0;
     513         1280 :     grid->window_shift[1] = 0;
     514         1280 :     grid->window_shift[2] = 0;
     515              : 
     516         1280 :     grid->window_size[0] = grid->size[0];
     517         1280 :     grid->window_size[1] = grid->size[1];
     518         1280 :     grid->window_size[2] = grid->size[2];
     519              :   }
     520              : 
     521         1280 :   grid->dh[0][0] = dh[0][0];
     522         1280 :   grid->dh[0][1] = dh[0][1];
     523         1280 :   grid->dh[0][2] = dh[0][2];
     524         1280 :   grid->dh[1][0] = dh[1][0];
     525         1280 :   grid->dh[1][1] = dh[1][1];
     526         1280 :   grid->dh[1][2] = dh[1][2];
     527         1280 :   grid->dh[2][0] = dh[2][0];
     528         1280 :   grid->dh[2][1] = dh[2][1];
     529         1280 :   grid->dh[2][2] = dh[2][2];
     530              : 
     531         1280 :   grid->dh_inv[0][0] = dh_inv[0][0];
     532         1280 :   grid->dh_inv[0][1] = dh_inv[0][1];
     533         1280 :   grid->dh_inv[0][2] = dh_inv[0][2];
     534         1280 :   grid->dh_inv[1][0] = dh_inv[1][0];
     535         1280 :   grid->dh_inv[1][1] = dh_inv[1][1];
     536         1280 :   grid->dh_inv[1][2] = dh_inv[1][2];
     537         1280 :   grid->dh_inv[2][0] = dh_inv[2][0];
     538         1280 :   grid->dh_inv[2][1] = dh_inv[2][1];
     539         1280 :   grid->dh_inv[2][2] = dh_inv[2][2];
     540              : 
     541         1280 :   verify_orthogonality(dh, grid->orthogonal);
     542              : 
     543         1280 :   if (orthorhombic) {
     544          672 :     grid->orthogonal[0] = true;
     545          672 :     grid->orthogonal[1] = true;
     546          672 :     grid->orthogonal[2] = true;
     547              :   }
     548         1280 : }
     549              : 
     550              : /*******************************************************************************
     551              :  * \brief Allocates a task list for the dgemm backend.
     552              :  *        See grid_task_list.h for details.
     553              :  ******************************************************************************/
     554           20 : void grid_dgemm_create_task_list(
     555              :     const bool orthorhombic, const int ntasks, const int nlevels,
     556              :     const int natoms, const int nkinds, const int nblocks,
     557              :     const int block_offsets[nblocks], const double atom_positions[natoms][3],
     558              :     const int atom_kinds[natoms], const grid_basis_set *basis_sets[nkinds],
     559              :     const int level_list[ntasks], const int iatom_list[ntasks],
     560              :     const int jatom_list[ntasks], const int iset_list[ntasks],
     561              :     const int jset_list[ntasks], const int ipgf_list[ntasks],
     562              :     const int jpgf_list[ntasks], const int border_mask_list[ntasks],
     563              :     const int block_num_list[ntasks], const double radius_list[ntasks],
     564              :     const double rab_list[ntasks][3], const int npts_global[nlevels][3],
     565              :     const int npts_local[nlevels][3], const int shift_local[nlevels][3],
     566              :     const int border_width[nlevels][3], const double dh[nlevels][3][3],
     567              :     const double dh_inv[nlevels][3][3], grid_dgemm_task_list **task_list) {
     568              : 
     569           20 :   if (*task_list == NULL) {
     570            8 :     *task_list = create_grid_context_dgemm(
     571              :         orthorhombic, ntasks, nlevels, natoms, nkinds, nblocks, block_offsets,
     572              :         atom_positions, atom_kinds, basis_sets, level_list, iatom_list,
     573              :         jatom_list, iset_list, jset_list, ipgf_list, jpgf_list,
     574              :         border_mask_list, block_num_list, radius_list, rab_list, npts_global,
     575              :         npts_local, shift_local, border_width, dh, dh_inv);
     576              :   } else {
     577           12 :     update_grid_context_dgemm(
     578              :         orthorhombic, ntasks, nlevels, natoms, nkinds, nblocks, block_offsets,
     579              :         atom_positions, atom_kinds, basis_sets, level_list, iatom_list,
     580              :         jatom_list, iset_list, jset_list, ipgf_list, jpgf_list,
     581              :         border_mask_list, block_num_list, radius_list, rab_list, npts_global,
     582              :         npts_local, shift_local, border_width, dh, dh_inv, *task_list);
     583              :   }
     584              : 
     585           20 :   const grid_library_config config = grid_library_get_config();
     586           20 :   if (config.apply_cutoff) {
     587            0 :     apply_cutoff(*task_list);
     588              :   }
     589           20 : }
     590              : 
     591              : /*******************************************************************************
     592              :  * \brief Deallocates given task list, basis_sets have to be freed separately.
     593              :  ******************************************************************************/
     594            8 : void grid_dgemm_free_task_list(grid_dgemm_task_list *task_list) {
     595            8 :   destroy_grid_context_dgemm(task_list);
     596            8 : }
        

Generated by: LCOV version 2.0-1