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 : #include "dbm_shard.h"
8 : #include "../offload/offload_mempool.h"
9 : #include "dbm_hyperparams.h"
10 :
11 : #include "../base/cp_assert.h"
12 : #include <omp.h>
13 : #include <stdbool.h>
14 : #include <stddef.h>
15 : #include <stdlib.h>
16 : #include <string.h>
17 :
18 : /*******************************************************************************
19 : * \brief Internal routine for finding a power of two greater than given number.
20 : * \author Ole Schuett
21 : ******************************************************************************/
22 5390042 : static int next_power2(const int start) {
23 5390042 : int candidate = 2;
24 15665608 : while (candidate < start) {
25 10275566 : candidate *= 2;
26 : }
27 5390042 : return candidate;
28 : }
29 :
30 : /*******************************************************************************
31 : * \brief Internal routine for finding a prime greater equal than given number.
32 : * \author Ole Schuett
33 : ******************************************************************************/
34 5390042 : static int next_prime(const int start) {
35 5390042 : int candidate = start;
36 : // Divisors up to the square root suffice (division avoids an overflow).
37 33536957 : for (int divisor = 2; divisor <= candidate / divisor; divisor++) {
38 28146915 : if (candidate % divisor == 0) { // try the next candidate
39 8173850 : ++candidate;
40 8173850 : divisor = 1;
41 : }
42 : }
43 5390042 : return candidate;
44 : }
45 :
46 : /*******************************************************************************
47 : * \brief Internal routine for initializing a shard's hashtable.
48 : * \author Ole Schuett
49 : ******************************************************************************/
50 5390042 : static void hashtable_init(dbm_shard_t *shard) {
51 : // Choosing size as power of two allows to replace modulo with bitwise AND.
52 10780084 : shard->hashtable_size =
53 5390042 : next_power2(DBM_HASHTABLE_FACTOR * shard->nblocks_allocated);
54 5390042 : shard->hashtable_prime = next_prime(shard->hashtable_size);
55 5390042 : shard->hashtable = calloc(shard->hashtable_size, sizeof(int));
56 5390042 : cp_assert(shard->hashtable != NULL);
57 5390042 : }
58 :
59 : /*******************************************************************************
60 : * \brief Internal routine for initializing a shard.
61 : * \author Ole Schuett
62 : ******************************************************************************/
63 2155702 : void dbm_shard_init(dbm_shard_t *shard) {
64 2155702 : shard->nblocks = 0;
65 2155702 : shard->nblocks_allocated = 0;
66 2155702 : shard->blocks = NULL;
67 2155702 : hashtable_init(shard);
68 2155702 : shard->data_size = 0;
69 2155702 : shard->data_promised = 0;
70 2155702 : shard->data_allocated = 0;
71 2155702 : shard->data = NULL;
72 2155702 : omp_init_lock(&shard->lock);
73 2155702 : }
74 :
75 : /*******************************************************************************
76 : * \brief Internal routine for copying content of shard_b into shard_a.
77 : * \author Ole Schuett
78 : ******************************************************************************/
79 216328 : void dbm_shard_copy(dbm_shard_t *shard_a, const dbm_shard_t *shard_b) {
80 216328 : cp_assert(shard_a != NULL && shard_b != NULL);
81 :
82 216328 : if (shard_a->nblocks_allocated < shard_b->nblocks) {
83 32496 : free(shard_a->blocks);
84 32496 : shard_a->blocks = malloc(shard_b->nblocks * sizeof(dbm_block_t));
85 32496 : shard_a->nblocks_allocated = shard_b->nblocks;
86 32496 : cp_assert(shard_a->blocks != NULL);
87 : }
88 216328 : shard_a->nblocks = shard_b->nblocks;
89 :
90 216328 : if (shard_a->hashtable_size < shard_b->hashtable_size) {
91 42428 : free(shard_a->hashtable);
92 42428 : shard_a->hashtable = malloc(shard_b->hashtable_size * sizeof(int));
93 42428 : cp_assert(shard_a->hashtable != NULL);
94 : }
95 216328 : shard_a->hashtable_size = shard_b->hashtable_size;
96 216328 : shard_a->hashtable_prime = shard_b->hashtable_prime;
97 :
98 216328 : if (shard_a->data_allocated < shard_b->data_size) {
99 33776 : offload_mempool_host_free(shard_a->data);
100 67552 : shard_a->data =
101 33776 : offload_mempool_host_malloc(shard_b->data_size * sizeof(double));
102 33776 : shard_a->data_allocated = shard_b->data_size;
103 33776 : cp_assert(shard_a->data != NULL);
104 : }
105 216328 : shard_a->data_size = shard_b->data_size;
106 216328 : shard_a->data_promised = shard_b->data_size;
107 :
108 216328 : if (shard_b->nblocks != 0) {
109 151553 : cp_assert(shard_a->blocks != NULL && shard_b->blocks != NULL);
110 151553 : memcpy(shard_a->blocks, shard_b->blocks,
111 151553 : shard_b->nblocks * sizeof(dbm_block_t));
112 : }
113 216328 : if (shard_b->hashtable_size != 0) {
114 216328 : cp_assert(shard_a->hashtable != NULL && shard_b->hashtable != NULL);
115 216328 : memcpy(shard_a->hashtable, shard_b->hashtable,
116 216328 : shard_b->hashtable_size * sizeof(int));
117 : }
118 216328 : if (shard_b->data_size != 0) {
119 151553 : cp_assert(shard_a->data != NULL && shard_b->data != NULL);
120 151553 : memcpy(shard_a->data, shard_b->data, shard_b->data_size * sizeof(double));
121 : }
122 216328 : }
123 :
124 : /*******************************************************************************
125 : * \brief Internal routine for releasing a shard.
126 : * \author Ole Schuett
127 : ******************************************************************************/
128 2155702 : void dbm_shard_release(dbm_shard_t *shard) {
129 2155702 : free(shard->blocks);
130 2155702 : free(shard->hashtable);
131 2155702 : offload_mempool_host_free(shard->data);
132 2155702 : omp_destroy_lock(&shard->lock);
133 2155702 : }
134 :
135 : /*******************************************************************************
136 : * \brief Private routine for inserting a block into a shard's hashtable.
137 : * \author Ole Schuett
138 : ******************************************************************************/
139 75829298 : static void hashtable_insert(dbm_shard_t *shard, const int block_idx) {
140 75829298 : cp_assert(0 <= block_idx && block_idx < shard->nblocks);
141 75829298 : const dbm_block_t *blk = &shard->blocks[block_idx];
142 : // Bounded probe: at most hashtable_size steps (load factor < 1 guarantees
143 : // termination). Avoids unbounded scans when the slot variable wrapped around
144 : // via the mask but the inner iteration continued past the table end.
145 75829298 : int slot = dbm_shard_slot(shard, blk->row, blk->col);
146 79765727 : for (int i = 0; i < shard->hashtable_size; ++i) { // linear probing
147 79765727 : if (shard->hashtable[slot] == 0) { // 0 means empty
148 75829298 : shard->hashtable[slot] = block_idx + 1; // 1-based
149 75829298 : return;
150 : }
151 3936429 : slot = dbm_shard_nextslot(shard, slot);
152 : }
153 75829298 : cp_assert(false);
154 : }
155 :
156 : /*******************************************************************************
157 : * \brief Internal routine for looking up a block from a shard.
158 : * \author Ole Schuett
159 : ******************************************************************************/
160 130899730 : dbm_block_t *dbm_shard_lookup(const dbm_shard_t *shard, const int row,
161 : const int col) {
162 : // Bounded probe count prevents scanning the entire table on a miss when
163 : // clusters exist (previous code could re-enter via slot wrap and re-scan).
164 130899730 : int slot = dbm_shard_slot(shard, row, col);
165 137196496 : for (int i = 0; i < shard->hashtable_size; ++i) { // linear probing
166 137196496 : const int block_idx = shard->hashtable[slot];
167 137196496 : if (block_idx == 0) { // 1-based, 0 means empty
168 : return NULL; // block not found
169 : }
170 88323872 : cp_assert(0 < block_idx && block_idx <= shard->nblocks);
171 88323872 : dbm_block_t *blk = &shard->blocks[block_idx - 1];
172 88323872 : if (blk->row == row && blk->col == col) {
173 : return blk;
174 : }
175 6296766 : slot = dbm_shard_nextslot(shard, slot);
176 : }
177 : return NULL;
178 : }
179 :
180 : /*******************************************************************************
181 : * \brief Internal routine for growing the block metadata of a shard such that
182 : * it holds nblocks blocks in total without growing again.
183 : * \author Ole Schuett and Hans Pabst
184 : ******************************************************************************/
185 62970559 : void dbm_shard_reserve(dbm_shard_t *shard, const int nblocks) {
186 : // Grow blocks array if necessary.
187 62970559 : if (shard->nblocks_allocated < nblocks) {
188 4110298 : shard->nblocks_allocated = DBM_ALLOCATION_FACTOR * nblocks;
189 4110298 : cp_assert(nblocks <= shard->nblocks_allocated);
190 4110298 : shard->blocks =
191 4110298 : realloc(shard->blocks, shard->nblocks_allocated * sizeof(dbm_block_t));
192 4110298 : cp_assert(shard->blocks != NULL);
193 : }
194 :
195 : // A copied hash table can hold fewer blocks than the block array.
196 62970559 : if (shard->hashtable_size < DBM_HASHTABLE_FACTOR * nblocks) {
197 3234340 : free(shard->hashtable);
198 3234340 : hashtable_init(shard);
199 18042446 : for (int i = 0; i < shard->nblocks; i++) {
200 14808106 : hashtable_insert(shard, i);
201 : }
202 : }
203 62970559 : }
204 :
205 : /*******************************************************************************
206 : * \brief Internal routine for allocating the metadata of a new block.
207 : * \author Ole Schuett
208 : ******************************************************************************/
209 61021192 : dbm_block_t *dbm_shard_promise_new_block(dbm_shard_t *shard, const int row,
210 : const int col, const int block_size) {
211 61021192 : dbm_shard_reserve(shard, shard->nblocks + 1);
212 :
213 61021192 : const int new_block_idx = shard->nblocks;
214 61021192 : shard->nblocks++;
215 61021192 : dbm_block_t *new_block = &shard->blocks[new_block_idx];
216 61021192 : new_block->row = row;
217 61021192 : new_block->col = col;
218 61021192 : new_block->offset = shard->data_promised;
219 61021192 : shard->data_promised += block_size;
220 : // The data_size will be increased after the memory is allocated and zeroed.
221 61021192 : hashtable_insert(shard, new_block_idx);
222 61021192 : return new_block;
223 : }
224 :
225 : /*******************************************************************************
226 : * \brief Internal routine for allocating and zeroing any promised block's data.
227 : * \author Ole Schuett
228 : ******************************************************************************/
229 16006691 : void dbm_shard_allocate_promised_blocks(dbm_shard_t *shard) {
230 : // Reallocate data array if necessary.
231 16006691 : if (shard->data_allocated < shard->data_promised) {
232 2784554 : const double *data = shard->data;
233 2784554 : shard->data_allocated = DBM_ALLOCATION_FACTOR * shard->data_promised;
234 2784554 : cp_assert(shard->data_promised <= shard->data_allocated);
235 5569108 : shard->data =
236 2784554 : offload_mempool_host_malloc(shard->data_allocated * sizeof(double));
237 2784554 : cp_assert(shard->data != NULL);
238 2784554 : if (data != NULL) {
239 976822 : memcpy(shard->data, data, shard->data_size * sizeof(double));
240 976822 : offload_mempool_host_free(data);
241 : }
242 : }
243 :
244 : // Zero new blocks.
245 : // The following memset is usually the first touch of the memory, which leads
246 : // to frequent page faults. The executing thread determines the NUMA location
247 16006691 : if (shard->data_size < shard->data_promised) {
248 15645134 : const int tail = shard->data_promised - shard->data_size;
249 15645134 : memset(shard->data + shard->data_size, 0, tail * sizeof(double));
250 15645134 : shard->data_size = shard->data_promised;
251 : }
252 16006691 : }
253 :
254 : /*******************************************************************************
255 : * \brief Internal routine for getting block or promising a new one.
256 : * \author Ole Schuett
257 : ******************************************************************************/
258 30563742 : dbm_block_t *dbm_shard_get_or_promise_block(dbm_shard_t *shard, const int row,
259 : const int col,
260 : const int block_size) {
261 30563742 : dbm_block_t *existing_blk = dbm_shard_lookup(shard, row, col);
262 30563742 : if (existing_blk != NULL) {
263 : return existing_blk;
264 : } else {
265 26867842 : return dbm_shard_promise_new_block(shard, row, col, block_size);
266 : }
267 : }
268 :
269 : /*******************************************************************************
270 : * \brief Internal routine for getting block or allocating a new one.
271 : * \author Ole Schuett
272 : ******************************************************************************/
273 43207961 : dbm_block_t *dbm_shard_get_or_allocate_block(dbm_shard_t *shard, const int row,
274 : const int col,
275 : const int block_size) {
276 43207961 : dbm_block_t *existing_blk = dbm_shard_lookup(shard, row, col);
277 43207961 : if (existing_blk != NULL) {
278 : return existing_blk;
279 : }
280 :
281 : // Create a new block.
282 13704740 : dbm_block_t *new_blk =
283 13704740 : dbm_shard_promise_new_block(shard, row, col, block_size);
284 13704740 : dbm_shard_allocate_promised_blocks(shard);
285 :
286 13704740 : return new_blk;
287 : }
288 :
289 : // EOF
|