(git:5a3db73)
Loading...
Searching...
No Matches
dbm_multiply_gpu.c
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
9#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
10
13#include "dbm_hyperparams.h"
14#include "dbm_multiply_gpu.h"
16
17#include "../base/cp_assert.h"
18#include <stdio.h>
19
20/*******************************************************************************
21 * \brief Internal routine for initializing the gpu backend.
22 * \author Ole Schuett
23 ******************************************************************************/
24void dbm_multiply_gpu_start(const int max_batch_size, const int nshards,
25 dbm_shard_t *shards_c_host,
26 dbm_multiply_gpu_context_t *ctx) {
27 // Select GPU device.
29
30 ctx->nshards = nshards;
31 ctx->max_batch_size = max_batch_size;
32 offloadStreamCreate(&ctx->main_stream);
33 offloadEventCreate(&ctx->upload_event);
34
35#if !defined(__OFFLOAD_UNIFIED_MEMORY)
36 // Allocate device storage for batches.
37 const size_t size = nshards * max_batch_size * sizeof(dbm_task_t);
38 ctx->batches_dev = offload_mempool_device_malloc(size);
39#endif
40
41 // Allocate and upload shards of result matrix C.
42 ctx->shards_c_dev = malloc(nshards * sizeof(dbm_shard_gpu_t));
43 cp_assert(ctx->shards_c_dev != NULL || nshards == 0);
44 for (int i = 0; i < nshards; i++) {
45 const dbm_shard_t *const shard_c_host = &shards_c_host[i];
46 dbm_shard_gpu_t *const shard_g = &ctx->shards_c_dev[i];
47 shard_g->data_size = shard_c_host->data_size;
48 offloadStreamCreate(&shard_g->stream);
49 offloadEventCreate(&shard_g->event);
50 // only allocate data_size on device rather than data_allocated
51 shard_g->data_allocated = shard_c_host->data_size;
52 shard_g->data =
53 offload_mempool_device_malloc(shard_g->data_allocated * sizeof(double));
54 offloadMemcpyAsyncHtoD(shard_g->data, shard_c_host->data,
55 shard_g->data_size * sizeof(double),
56 shard_g->stream);
57 }
58}
59
60/*******************************************************************************
61 * \brief Private routine for uploading a single pack onto the device.
62 * \author Ole Schuett
63 ******************************************************************************/
64static void upload_pack(const dbm_pack_t *pack_host, dbm_pack_gpu_t *pack_dev,
65 const offloadStream_t stream) {
66 // Reallocate only when the device buffer is too small; data_allocated tracks
67 // the device capacity independently from the host-side data_size that changes
68 // each tick (previous code compared against the wrong field).
69 const size_t size = pack_host->data_size * sizeof(double);
70 if (pack_dev->data_allocated < pack_host->data_size) {
71 offload_mempool_device_free(pack_dev->data);
72 pack_dev->data = offload_mempool_device_malloc(size);
73 pack_dev->data_allocated = pack_host->data_size;
74 }
75 offloadMemcpyAsyncHtoD(pack_dev->data, pack_host->data, size, stream);
76}
77
78/*******************************************************************************
79 * \brief Internal routine for uploading newly arrived packs onto the device.
80 * \author Ole Schuett and Hans Pabst
81 ******************************************************************************/
82bool dbm_multiply_gpu_upload_packs(const dbm_pack_t *pack_a,
83 const dbm_pack_t *pack_b,
84 dbm_multiply_gpu_context_t *ctx) {
85 // Assume GPU device was activated earlier.
86 // Wait for all c-streams to complete before overwriting old packs.
87 for (int i = 0; i < ctx->nshards; i++) {
88 offloadEventRecord(ctx->upload_event, ctx->shards_c_dev[i].stream);
89 offloadStreamWaitEvent(ctx->main_stream, ctx->upload_event);
90 }
91 // Record event to check if all c-streams already completed.
92 offloadEventRecord(ctx->upload_event, ctx->main_stream);
93 // A growing pack frees its buffer on the host while kernels may still read
94 // it (the waits above are device-side only).
95 if (ctx->pack_a_dev.data_allocated < pack_a->data_size ||
96 ctx->pack_b_dev.data_allocated < pack_b->data_size) {
97 offloadEventSynchronize(ctx->upload_event);
98 }
99
100 bool uploaded = false;
101 /*if (offloadEventQuery(ctx->upload_event))*/
102 {
103 upload_pack(pack_a, &ctx->pack_a_dev, ctx->main_stream);
104 upload_pack(pack_b, &ctx->pack_b_dev, ctx->main_stream);
105
106 // Have all c-streams wait until new packs are uploaded.
107 offloadEventRecord(ctx->upload_event, ctx->main_stream);
108 for (int i = 0; i < ctx->nshards; i++) {
109 offloadStreamWaitEvent(ctx->shards_c_dev[i].stream, ctx->upload_event);
110 }
111 uploaded = true;
112 }
113
114 return uploaded;
115}
116
117/*******************************************************************************
118 * \brief Internal routine for concluding a turn of the main loop, i.e., before
119 * the packs are reused or released and before batches are refilled.
120 * \author Hans Pabst
121 ******************************************************************************/
122void dbm_multiply_gpu_end_turn(dbm_multiply_gpu_context_t *ctx) {
123 // A turn without tasks does not otherwise wait for the asynchronous uploads.
124 offloadEventSynchronize(ctx->upload_event);
125#if defined(__OFFLOAD_UNIFIED_MEMORY)
126 // The next turn refills the batches the previous turn's kernels read.
127 for (int i = 0; i < ctx->nshards; i++) {
128 offloadEventSynchronize(ctx->shards_c_dev[i].event);
129 offloadEventRecord(ctx->shards_c_dev[i].event, ctx->shards_c_dev[i].stream);
130 }
131#endif
132}
133
134/*******************************************************************************
135 * \brief Internal routine for executing the tasks in given batch on the GPU.
136 * \author Ole Schuett
137 ******************************************************************************/
138void dbm_multiply_gpu_process_batch(const dbm_batch_t *batch,
139 const double alpha, dbm_shard_t *shard_c,
140 const int kshard, const bool finish,
141 dbm_multiply_gpu_context_t *ctx) {
142 // Assume GPU device was activated earlier.
143 dbm_shard_gpu_t *const shard_g = &ctx->shards_c_dev[kshard];
144 const int ntasks = batch->ntasks;
145 double *old_data_dev = NULL;
146
147 if (0 < ntasks) {
148 cp_assert(NULL != shard_c && NULL != shard_g);
149
150#if defined(__OFFLOAD_UNIFIED_MEMORY)
151 const dbm_task_t *const batch_dev = batch->tasks; // read where written
152#else
153 // Upload new batch.
154 dbm_task_t *batch_dev = &ctx->batches_dev[kshard * ctx->max_batch_size];
155 const size_t size = ntasks * sizeof(dbm_task_t);
156 offloadMemcpyAsyncHtoD(batch_dev, batch->tasks, size, shard_g->stream);
157#endif
158
159 // Reallocate shard_g->data if necessary.
160 if (shard_c->data_promised > shard_g->data_allocated) {
161 shard_g->data_allocated = DBM_ALLOCATION_FACTOR * shard_c->data_promised;
162 cp_assert(shard_c->data_promised <= shard_g->data_allocated);
163 old_data_dev = shard_g->data;
164 shard_g->data = offload_mempool_device_malloc(shard_g->data_allocated *
165 sizeof(double));
166 // Omit to wait for copy before freeing old buffer.
167 offloadMemcpyAsyncDtoD(shard_g->data, old_data_dev,
168 shard_g->data_size * sizeof(double),
169 shard_g->stream);
170 }
171 offloadEventRecord(shard_g->event, shard_g->stream);
172
173 // Zero new blocks if necessary.
174 if (shard_c->data_promised > shard_g->data_size) {
175 const int tail = shard_c->data_promised - shard_g->data_size;
176 offloadMemsetAsync(&shard_g->data[shard_g->data_size], 0,
177 tail * sizeof(double), shard_g->stream);
178 shard_g->data_size = shard_c->data_promised;
179 }
180
181 OFFLOAD_CHECK(offloadGetLastError());
182 cp_assert(0 != shard_g->data_size);
183
184 // Launch kernel.
185 dbm_multiply_gpu_launch_kernel(shard_g->stream, alpha, batch, batch_dev,
186 ctx->pack_a_dev.data, ctx->pack_b_dev.data,
187 shard_g->data);
188 OFFLOAD_CHECK(offloadGetLastError());
189#if defined(__OFFLOAD_UNIFIED_MEMORY)
190 if (!finish) { // the next batch of the shard refills what the kernel reads
191 offloadStreamSynchronize(shard_g->stream);
192 }
193#endif
194 }
195
196 if (finish) { // Start downloading the current shard of matrix_c.
197 // Grow host buffer if necessary.
199 // Download results from device.
200 cp_assert(shard_c->data_size == shard_g->data_size);
201 offloadMemcpyAsyncDtoH(shard_c->data, shard_g->data,
202 shard_g->data_size * sizeof(double),
203 shard_g->stream);
204 }
205
206 if (0 < ntasks) {
207 // Wait for:
208 // - Batch to be uploaded (before refilling it).
209 // - Safely freeing device buffer (if resized).
210 offloadEventSynchronize(shard_g->event);
211
212 if (NULL != old_data_dev) {
213 offload_mempool_device_free(old_data_dev);
214 }
215 }
216}
217
218/*******************************************************************************
219 * \brief Internal routine for shutting down the gpu backend.
220 * \author Ole Schuett
221 ******************************************************************************/
222void dbm_multiply_gpu_stop(dbm_multiply_gpu_context_t *ctx) {
223 // Assume GPU device was activated earlier.
224 // Wait for completion, then free gpu ressources.
225#pragma omp parallel for DBM_OMP_SCHEDULE
226 for (int i = 0; i < ctx->nshards; i++) {
227 dbm_shard_gpu_t *const shard_g = &ctx->shards_c_dev[i];
228 offloadStreamSynchronize(shard_g->stream);
229 offloadStreamDestroy(shard_g->stream);
230 offloadEventDestroy(shard_g->event);
231 offload_mempool_device_free(shard_g->data);
232 }
233 free(ctx->shards_c_dev);
234
235 offload_mempool_device_free(ctx->pack_a_dev.data);
236 offload_mempool_device_free(ctx->pack_b_dev.data);
237#if !defined(__OFFLOAD_UNIFIED_MEMORY)
238 offload_mempool_device_free(ctx->batches_dev);
239#endif
240 offloadStreamDestroy(ctx->main_stream);
241 offloadEventDestroy(ctx->upload_event);
242}
243
244#endif // defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
245
246// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
#define DBM_ALLOCATION_FACTOR
void dbm_shard_allocate_promised_blocks(dbm_shard_t *shard)
Internal routine for allocating and zeroing any promised block's data.
Definition dbm_shard.c:229
static void const int const int i
void offload_activate_chosen_device(void)
Activates the device selected via offload_set_chosen_device().
void offload_mempool_device_free(const void *memory)
Internal routine for releasing memory back to the pool.
void * offload_mempool_device_malloc(const size_t size)
Internal routine for allocating device memory from the pool.
Internal struct for storing a batch of tasks along with its shape, which is tracked while the batch i...
dbm_task_t * tasks
Internal struct for storing a pack - essentially a shard for MPI.
double * data
Internal struct for storing a matrix shard.
Definition dbm_shard.h:30
double * data
Definition dbm_shard.h:42
int data_size
Definition dbm_shard.h:41
int data_promised
Definition dbm_shard.h:39
Internal struct for storing a task, ie. a single block multiplication.