(git:3d5d2e5)
Loading...
Searching...
No Matches
offload_library.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#include "offload_library.h"
8#include "offload_runtime.h"
9
10#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#if defined(__OFFLOAD_CUDA)
16#include <cuda.h>
17#elif defined(__OFFLOAD_HIP)
18#include <hip/hip_runtime_api.h>
19#elif defined(__OFFLOAD_OPENCL)
20#include <libxstream/libxstream.h>
21#endif
22
23#if defined(__OFFLOAD_PROFILING)
24#if defined(__OFFLOAD_CUDA)
25#include <nvtx3/nvToolsExt.h>
26#elif defined(__OFFLOAD_HIP) && defined(__HIP_PLATFORM_AMD__)
27#include <roctracer/roctx.h>
28#endif
29#endif
30
31static int chosen_device_id = -1;
32
33const uint32_t colormap[] = {0xFFFFFF00, // Yellow
34 0xFFFF00FF, // Fuchsia
35 0xFFFF0000, // Red
36 0xFFC0C0C0, // Silver
37 0xFF808080, // Gray
38 0xFF808000, // Olive
39 0xFF800080, // Purple
40 0xFF800000, // Maroon
41 0xFF00FFFF, // Aqua
42 0xFF00FF00, // Lime
43 0xFF008080, // Teal
44 0xFF008000, // Green
45 0xFF0000FF, // Blue
46 0xFF000080}; // Navy
47
48/*******************************************************************************
49 * \brief Initialize runtime.
50 * \author Rocco Meli
51 ******************************************************************************/
52void offload_init(void) {
53#if defined(__OFFLOAD_CUDA)
54 CUresult error = cuInit(0);
55 if (error != CUDA_SUCCESS) {
56 fprintf(stderr, "ERROR: %s %d %s %d\n", "cuInit failed with error: ", error,
57 __FILE__, __LINE__);
58 abort();
59 }
60#elif defined(__OFFLOAD_HIP)
61 OFFLOAD_CHECK(hipInit(0));
62#elif defined(__OFFLOAD_OPENCL)
63 OFFLOAD_CHECK(libxstream_init());
64#endif
65}
66
67/*******************************************************************************
68 * \brief Returns the number of available devices.
69 * \author Ole Schuett
70 ******************************************************************************/
72 int count = 0;
73#if defined(__OFFLOAD_CUDA)
74 OFFLOAD_CHECK(cudaGetDeviceCount(&count));
75#elif defined(__OFFLOAD_HIP)
76 OFFLOAD_CHECK(hipGetDeviceCount(&count));
77#elif defined(__OFFLOAD_OPENCL)
78 OFFLOAD_CHECK(libxstream_device_count(&count));
79#endif
80 return count;
81}
82
83/*******************************************************************************
84 * \brief Selects the chosen device to be used.
85 * \author Ole Schuett
86 ******************************************************************************/
87void offload_set_chosen_device(int device_id) { chosen_device_id = device_id; }
88
89/*******************************************************************************
90 * \brief Returns the chosen device.
91 * \author Ole Schuett
92 ******************************************************************************/
94
95/*******************************************************************************
96 * \brief Activates the device selected via offload_set_chosen_device()
97 * \author Ole Schuett
98 ******************************************************************************/
100#if defined(__OFFLOAD_CUDA)
101 OFFLOAD_CHECK(cudaSetDevice(chosen_device_id));
102#elif defined(__OFFLOAD_HIP)
103 OFFLOAD_CHECK(hipSetDevice(chosen_device_id));
104#elif defined(__OFFLOAD_OPENCL)
105 if (chosen_device_id >= 0) {
106 OFFLOAD_CHECK(libxstream_device_set_active(chosen_device_id));
107 }
108#endif
109}
110
111/*******************************************************************************
112 * \brief Starts a timing range.
113 * \author Ole Schuett
114 ******************************************************************************/
115void offload_timeset(const char *message) {
116#if defined(__OFFLOAD_PROFILING)
117#if defined(__OFFLOAD_CUDA)
118 // colors are picked based on a (very simple) hash value of the message
119 int hash = 0;
120 for (size_t i = 0; i < strlen(message); i++) {
121 hash += i * message[i] * message[i];
122 }
123 nvtxEventAttributes_t eventAttrib = {0};
124 eventAttrib.version = NVTX_VERSION;
125 eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
126 eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
127 eventAttrib.message.ascii = message;
128 eventAttrib.colorType = NVTX_COLOR_ARGB;
129 eventAttrib.color = colormap[hash % 14];
130 eventAttrib.payloadType = NVTX_PAYLOAD_TYPE_INT64;
131 eventAttrib.payload.llValue = 123;
132 eventAttrib.category = 42;
133 nvtxRangePushEx(&eventAttrib);
134#elif defined(__OFFLOAD_HIP) && defined(__HIP_PLATFORM_AMD__)
135 roctxRangePushA(message);
136#endif
137#endif
138 (void)message; // mark argument as used
139}
140
141/*******************************************************************************
142 * \brief Ends a timing range.
143 * \author Ole Schuett
144 ******************************************************************************/
146#if defined(__OFFLOAD_PROFILING)
147#if defined(__OFFLOAD_CUDA)
148 nvtxRangePop();
149#elif defined(__OFFLOAD_HIP) && defined(__HIP_PLATFORM_AMD__)
150 roctxRangePop();
151#endif
152#endif
153}
154
155/*******************************************************************************
156 * \brief Gets free and total device memory.
157 * \author Ole Schuett
158 ******************************************************************************/
159void offload_mem_info(size_t *free, size_t *total) {
160#if defined(__OFFLOAD_CUDA)
161 OFFLOAD_CHECK(cudaMemGetInfo(free, total));
162#elif defined(__OFFLOAD_HIP)
163 OFFLOAD_CHECK(hipMemGetInfo(free, total));
164#elif defined(__OFFLOAD_OPENCL)
165 OFFLOAD_CHECK(libxstream_mem_info(free, total));
166#else
167 *free = *total = 0;
168#endif
169}
170
171/*******************************************************************************
172 * \brief Allocate pinned memory (or simple malloc when there is no gpu)
173 ******************************************************************************/
174int offload_host_malloc(void **ptr__, const size_t size__) {
175#if defined(__OFFLOAD)
176 offloadMallocHost(ptr__, size__); /* checked */
177 return offloadSuccess;
178#else
179 *ptr__ = malloc(size__);
180 return EXIT_SUCCESS;
181#endif
182}
183
184/*******************************************************************************
185 * \brief free pinned memory (or simple free when there is no gpu)
186 ******************************************************************************/
187int offload_host_free(void *ptr__) {
188#if defined(__OFFLOAD)
189 offloadFreeHost(ptr__); /* checked */
190 return offloadSuccess;
191#else
192 free(ptr__);
193 return EXIT_SUCCESS;
194#endif
195}
196
197/*******************************************************************************
198 * \brief Blocks until all outstanding device work has completed.
199 * \author Samal Bibek
200 ******************************************************************************/
202#if defined(__OFFLOAD_CUDA)
203 OFFLOAD_CHECK(cudaDeviceSynchronize());
204#elif defined(__OFFLOAD_HIP)
205 OFFLOAD_CHECK(hipDeviceSynchronize());
206#endif
207}
208
209// EOF
static unsigned int hash(const dbm_task_t task)
Private hash function based on Szudzik's elegant pairing. Using unsigned int to return a positive num...
static void const int const int i
const uint32_t colormap[]
int offload_get_device_count(void)
Returns the number of available devices.
void offload_mem_info(size_t *free, size_t *total)
Gets free and total device memory.
void offload_activate_chosen_device(void)
Activates the device selected via offload_set_chosen_device().
int offload_host_free(void *ptr__)
free pinned memory (or simple free when there is no gpu)
static int chosen_device_id
void offload_timestop(void)
Ends a timing range.
void offload_device_synchronize(void)
Blocks until all outstanding device work has completed.
void offload_timeset(const char *message)
Starts a timing range.
int offload_get_chosen_device(void)
Returns the chosen device.
void offload_set_chosen_device(int device_id)
Selects the chosen device to be used.
void offload_init(void)
Initialize runtime.
int offload_host_malloc(void **ptr__, const size_t size__)
Allocate pinned memory (or simple malloc when there is no gpu).