(git:ed6f26b)
Loading...
Searching...
No Matches
grid_dgemm_collocation_integration.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2025 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7
8#include <assert.h>
9#include <stdbool.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14
15#include "../common/grid_common.h"
19#include "grid_dgemm_utils.h"
20
22 struct collocation_integration_ *handle = NULL;
23 handle = (struct collocation_integration_ *)malloc(
24 sizeof(struct collocation_integration_));
25 assert(handle != NULL);
26 memset(handle, 0, sizeof(struct collocation_integration_));
27
28 handle->alpha.alloc_size_ = 8192;
29 handle->coef.alloc_size_ = 1024;
30 handle->pol.alloc_size_ = 1024;
31 /* it is a cube of size 32 x 32 x 32 */
32 handle->cube.alloc_size_ = 32768;
33
34 handle->cube_alloc_size = realloc_tensor(&handle->cube);
35 handle->alpha_alloc_size = realloc_tensor(&handle->alpha);
36 handle->coef_alloc_size = realloc_tensor(&handle->coef);
37 handle->pol_alloc_size = realloc_tensor(&handle->pol);
38
39 handle->scratch = malloc(32768 * sizeof(double));
40 assert(handle->scratch != NULL);
41 handle->scratch_alloc_size = 32768;
42 handle->T_alloc_size = 8192;
43 handle->W_alloc_size = 2048;
44 handle->blockDim[0] = 5;
45 handle->blockDim[1] = 5;
46 handle->blockDim[2] = 5;
47 handle->device_id = (int *)malloc(sizeof(double) * 12);
48 assert(handle->device_id != NULL);
49 handle->number_of_devices = 1;
50
51 /* to suppress when we remove the spherical cutoff */
52 handle->map = (int **)malloc(3 * sizeof(int *));
53 assert(handle->map != NULL);
54 handle->map[0] = (int *)malloc(sizeof(int) * 512 * 3);
55 assert(handle->map[0] != NULL);
56 handle->map[1] = handle->map[0] + 512;
57 handle->map[2] = handle->map[1] + 512;
58 handle->cmax = 512 * 3;
59 return handle;
60}
61
62void collocate_destroy_handle(void *gaussian_handle) {
63 struct collocation_integration_ *handle =
64 (struct collocation_integration_ *)gaussian_handle;
65 if (handle->Exp.data)
66 free(handle->Exp.data);
67
68 if (handle->grid.data)
69 free(handle->grid.data);
70
71 free(handle->scratch);
72 free(handle->pol.data);
73 free(handle->cube.data);
74 free(handle->alpha.data);
75 free(handle->coef.data);
76 free(handle->blocks_coordinates.data);
77 handle->alpha.data = NULL;
78 handle->coef.data = NULL;
79 handle->blocks_coordinates.data = NULL;
80 free(handle->device_id);
81 free(handle->map[0]);
82 free(handle->map);
83 free(handle);
84
85 handle = NULL;
86}
87
89 const tensor *cube, const tensor *coef) {
90 size_t tmp1 = compute_memory_space_tensor_3(coef->size[0] /* alpha */,
91 coef->size[1] /* gamma */,
92 cube->size[1] /* j */);
93
95 coef->size[0] /* gamma */, cube->size[1] /* j */, cube->size[2] /* i */);
96
97 const size_t mem_alloc_size_ =
98 imax(imax(tmp1 + tmp2, cube->alloc_size_), coef->alloc_size_);
99
100 handler->T_alloc_size = tmp1;
101 handler->W_alloc_size = tmp2;
102
103 if ((mem_alloc_size_ > handler->scratch_alloc_size) ||
104 (handler->scratch == NULL)) {
105 handler->scratch_alloc_size = mem_alloc_size_;
106
107 if (handler->scratch)
108 free(handler->scratch);
109 handler->scratch = malloc(sizeof(double) * handler->scratch_alloc_size);
110 assert(handler->scratch != NULL);
111 }
112}
113
115 const int num_block, const tensor *coef,
116 const tensor *block) {
117 /* T */
118 size_t tmp1 = compute_memory_space_tensor_4(num_block, block->size[0] /* k */,
119 block->size[1] /* j */,
120 coef->size[1] /* alpha */);
121
122 /* W */
123 size_t tmp2 = compute_memory_space_tensor_4(num_block, block->size[1] /* j */,
124 coef->size[1] /* alpha */,
125 coef->size[2] /* gamma */);
126
127 const size_t mem_alloc_size_ = tmp1 + tmp2;
128
129 handler->T_alloc_size = tmp1;
130 handler->W_alloc_size = tmp2;
131
132 if ((mem_alloc_size_ > handler->scratch_alloc_size) ||
133 (handler->scratch == NULL)) {
134 handler->scratch_alloc_size = mem_alloc_size_;
135
136 if (handler->scratch)
137 free(handler->scratch);
138 handler->scratch = malloc(sizeof(double) * handler->scratch_alloc_size);
139 assert(handler->scratch != NULL);
140 }
141}
142
144 const double dh[3][3],
145 const double dh_inv[3][3]) {
146 handler->dh[0][0] = dh[0][0];
147 handler->dh[0][1] = dh[0][1];
148 handler->dh[0][2] = dh[0][2];
149 handler->dh[1][0] = dh[1][0];
150 handler->dh[1][1] = dh[1][1];
151 handler->dh[1][2] = dh[1][2];
152 handler->dh[2][0] = dh[2][0];
153 handler->dh[2][1] = dh[2][1];
154 handler->dh[2][2] = dh[2][2];
155
156 handler->dh_inv[0][0] = dh_inv[0][0];
157 handler->dh_inv[0][1] = dh_inv[0][1];
158 handler->dh_inv[0][2] = dh_inv[0][2];
159 handler->dh_inv[1][0] = dh_inv[1][0];
160 handler->dh_inv[1][1] = dh_inv[1][1];
161 handler->dh_inv[1][2] = dh_inv[1][2];
162 handler->dh_inv[2][0] = dh_inv[2][0];
163 handler->dh_inv[2][1] = dh_inv[2][1];
164 handler->dh_inv[2][2] = dh_inv[2][2];
165
166 /* Only used when we are in the non orthorombic case */
167 handler->dx[2] = handler->dh[0][0] * handler->dh[0][0] +
168 handler->dh[0][1] * handler->dh[0][1] +
169 handler->dh[0][2] * handler->dh[0][2];
170 handler->dx[1] = handler->dh[1][0] * handler->dh[1][0] +
171 handler->dh[1][1] * handler->dh[1][1] +
172 handler->dh[1][2] * handler->dh[1][2];
173 handler->dx[0] = handler->dh[2][0] * handler->dh[2][0] +
174 handler->dh[2][1] * handler->dh[2][1] +
175 handler->dh[2][2] * handler->dh[2][2];
176}
static int imax(int x, int y)
Returns the larger of two given integers (missing from the C standard)
void initialize_basis_vectors(collocation_integration *const handler, const double dh[3][3], const double dh_inv[3][3])
void initialize_W_and_T_integrate(collocation_integration *const handler, const int num_block, const tensor *coef, const tensor *block)
void collocate_destroy_handle(void *gaussian_handle)
void initialize_W_and_T(collocation_integration *const handler, const tensor *cube, const tensor *coef)
struct collocation_integration_ * collocate_create_handle(void)
size_t realloc_tensor(tensor *t)
static size_t compute_memory_space_tensor_4(const int n1, const int n2, const int n3, const int n4)
static size_t compute_memory_space_tensor_3(const int n1, const int n2, const int n3)