(git:97501a3)
Loading...
Searching...
No Matches
dbm_multiply_opencl.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 "../offload/offload_runtime.h"
9#if defined(__OFFLOAD_OPENCL) && !defined(__NO_OFFLOAD_DBM)
10
12#include "dbm_multiply_opencl.cl.h"
13
14#if defined(__DBCSR_ACC)
15#include <smm/opencl_libsmm.h>
16#endif
17
18#define DBM_TIMER_DIFF(A, B) libxsmm_timer_duration(A, B)
19#define DBM_TIMER_TICK() libxsmm_timer_tick()
20#define DBM_TIMER_TICKINT libxsmm_timer_tickint
21
22int dbm_multiply_opencl_launch_kernel(void *stream, double alpha, int ntasks,
23 int param_format, const int *params_host,
24 const int *params,
25 const double *pack_a_data,
26 const double *pack_b_data,
27 double *shard_c_data);
28
29#if defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT)
30int dbm_multiply_opencl_initialized /*= 0*/;
31int dbm_multiply_opencl_smm /*= 0*/;
32
33LIBXSMM_ATTRIBUTE_CTOR static void dbm_multiply_opencl_initialize(void) {
34 const char *const smm_env = getenv("DBM_MULTIPLY_SMM");
35 const int smm = (NULL == smm_env ? 0 /*default*/ : atoi(smm_env));
36 dbm_multiply_opencl_smm =
37 LIBXSMM_MIN(1 != smm ? smm : 64, (1 << (OPENCL_LIBSMM_PFORMAT - 1)) - 1);
38 if (0 > dbm_multiply_opencl_smm) {
39 opencl_libsmm_acc_set_dbm_launch_fn(dbm_multiply_opencl_launch_kernel);
40 }
41 ++dbm_multiply_opencl_initialized;
42}
43#endif
44
45typedef struct {
46 int max_m, max_n, max_k, mnk_changes;
47} dbm_multiply_gpu_launch_info_t;
48
49static void dbm_multiply_gpu_launch_info(dbm_multiply_gpu_launch_info_t *info,
50 const int *params, int ntasks,
51 int param_format) {
52 if (0 == param_format) { /* native */
53 const int stride = sizeof(dbm_task_t) / sizeof(int);
54 int avg_m = params[0], avg_n = params[1], avg_k = params[2], i = stride;
55 info->max_m = avg_m;
56 info->max_n = avg_n;
57 info->max_k = avg_k;
58 for (info->mnk_changes = 0; i < (ntasks * stride); i += stride) {
59 const int m = params[i + 0], n = params[i + 1], k = params[i + 2];
60 info->max_m = imax(info->max_m, m);
61 info->max_n = imax(info->max_n, n);
62 info->max_k = imax(info->max_k, k);
63 if (m != avg_m || n != avg_n || k != avg_k) { /* approximation */
64 avg_m = (avg_m + m) / 2;
65 avg_n = (avg_n + n) / 2;
66 avg_k = (avg_k + k) / 2;
67 ++info->mnk_changes;
68 }
69 }
70 } else {
71#if defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT)
72 const int mask = (1 << OPENCL_LIBSMM_PFORMAT) - 1;
73 info->max_m = mask & (param_format);
74 info->max_n = mask & (param_format >> (OPENCL_LIBSMM_PFORMAT));
75 info->max_k = mask & (param_format >> (OPENCL_LIBSMM_PFORMAT * 2));
76 info->mnk_changes = 0; /* homogeneous */
77#else
78 assert(0);
79#endif
80 }
81}
82
83static void dbm_multiply_opencl_print(FILE *stream, const char *name, int val) {
84 if (0 != val) {
85 fprintf(stream, " %s=%i", name, val);
86 }
87}
88
89int dbm_multiply_opencl_launch_kernel(void *stream, double alpha, int ntasks,
90 int param_format, const int *params_host,
91 const int *params,
92 const double *pack_a_data,
93 const double *pack_b_data,
94 double *shard_c_data) {
95 const DBM_TIMER_TICKINT start = DBM_TIMER_TICK();
96 const c_dbcsr_acc_opencl_config_t *const config = &c_dbcsr_acc_opencl_config;
97 const int verbosity = config->verbosity,
98 info = (0 > verbosity || 2 < verbosity);
99 int result = EXIT_SUCCESS;
100 dbm_multiply_gpu_launch_info_t task = {0};
101 assert(NULL != pack_a_data && NULL != pack_b_data && NULL != shard_c_data);
102 assert(NULL != params_host || 0 == ntasks);
103 assert(NULL != params || 0 == ntasks);
104 if (0 == ntasks) {
105 return result;
106 }
107#if defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT)
108 if (0 == dbm_multiply_opencl_initialized) {
109 dbm_multiply_opencl_initialize();
110 }
111 if (0 != dbm_multiply_opencl_smm || 0 != info) {
112 dbm_multiply_gpu_launch_info(&task, params_host, ntasks, param_format);
113 }
114 if (0 > dbm_multiply_opencl_smm || dbm_multiply_opencl_smm < task.max_m ||
115 dbm_multiply_opencl_smm < task.max_n ||
116 dbm_multiply_opencl_smm < task.max_k || 0 == task.max_k || 1 != alpha)
117#endif
118 {
119#if defined(OPENCL_DBM_SOURCE_MULTIPLY)
120 /* creating/calling kernel must be consistent across threads */
121 static cl_kernel kernel_global = NULL;
122 static LIBXSMM_TLS cl_kernel kernel = NULL;
123 static int ndims = 1, clinear = 0;
124 static size_t wgsize[] = {0, 0, 0};
125 const c_dbcsr_acc_opencl_stream_t *const str = ACC_OPENCL_STREAM(stream);
126 const c_dbcsr_acc_opencl_device_t *const devinfo = &config->device;
127 ACC_OPENCL_LOCKTYPE *const lock_memory =
128 (NULL != devinfo->clSetKernelArgMemPointerINTEL ? NULL
129 : config->lock_memory);
130 c_dbcsr_acc_opencl_info_memptr_t adata, bdata, cdata, batch;
131 const int stride = (0 == param_format ? 6 : 3);
132 size_t work_size[] = {1, 1, 1}, ibatch = 0;
133 size_t iadata = 0, ibdata = 0, icdata = 0;
134 const size_t work_tasks = ntasks;
135 assert(NULL != str && NULL != str->queue);
136 if (NULL == kernel_global) { /* initial check if kernel is present */
137 ACC_OPENCL_ACQUIRE(config->lock_main);
138 if (NULL == kernel_global) {
139 char flags[ACC_OPENCL_BUFFERSIZE] =
140 "-cl-fast-relaxed-math -cl-denorms-are-zero";
141 const char *const gen_env = getenv("DBM_MULTIPLY_GEN");
142 const char *const lin_env = getenv("DBM_MULTIPLY_LIN");
143 const char *const fp_env = getenv("DBM_MULTIPLY_FP");
144 const char *const bn_env = getenv("DBM_MULTIPLY_BN");
145 const char *const sm_env = getenv("DBM_MULTIPLY_SM");
146 const char *const wg_env = getenv("DBM_MULTIPLY_WG");
147 const char *const lu_env = getenv("DBM_MULTIPLY_LU");
148 const char *const xf_env = getenv("DBM_MULTIPLY_XF");
149 int sm = (NULL == sm_env ? 0 /*default*/ : atoi(sm_env));
150 const int bn0 = (0 == devinfo->nv ? (0 == devinfo->amd ? 4 : 8) : 2);
151 const int bn1 = ((0 == sm && 0 == clinear) ? bn0 : (bn0 * 2));
152 int bn = LIBXSMM_CLMP(NULL == bn_env ? bn1 : atoi(bn_env), 1, 32);
153 int lu = LIBXSMM_CLMP(NULL == lu_env ? 0 : atoi(lu_env), -2, 1);
154 int gen = ((NULL == fp_env && NULL == bn_env && NULL == sm_env &&
155 NULL == wg_env && NULL == lu_env && NULL == lin_env &&
156 0 == param_format)
157 ? (NULL == gen_env ? 1 /*default*/ : atoi(gen_env))
158 : 0);
159 const int precision = (NULL == fp_env ? 0 /*default*/ : atoi(fp_env));
160 const int gpu = (CL_DEVICE_TYPE_GPU == devinfo->type);
161 const int xf = (NULL == xf_env ? -1 /*default*/ : atoi(xf_env));
162 const char *extensions[] = {NULL, NULL}, *options = NULL;
163 size_t nextensions = sizeof(extensions) / sizeof(*extensions);
164 size_t sgsize = devinfo->wgsize[2];
165 size_t offset =
166 ((0 == config->debug && 0 == config->dump) ? strlen(flags) : 0);
167 offset += (size_t)c_dbcsr_acc_opencl_flags_atomics(
168 devinfo, c_dbcsr_acc_opencl_atomic_fp_64, extensions, &nextensions,
169 flags + offset, sizeof(flags) - offset);
170 if (2 <= gen ||
171 (0 != gen && 1 < sgsize /*subgroups*/ && 2 <= *devinfo->std_level &&
172 NULL != extensions[1] &&
173 NULL != strstr(extensions[1], "cl_ext_float_atomics"))) {
174 offset +=
175 (size_t)LIBXSMM_SNPRINTF(flags + offset, sizeof(flags) - offset,
176 " -DDBM_MULTIPLY_OPENCL_GEN");
177 wgsize[1] = wgsize[2] = 1;
178 wgsize[0] = 16;
179 lu = bn = 0;
180 ndims = 3;
181 } else {
182 wgsize[0] = (NULL == wg_env ? (unsigned long int)LIBXSMM_ABS(sm)
183 : strtoul(wg_env, NULL, 10));
184 if (1 < sgsize && 0 < wgsize[0]) { /* subgroups */
185 if (LIBXSMM_DELTA(wgsize[0], devinfo->wgsize[1]) <=
186 LIBXSMM_DELTA(wgsize[0], sgsize)) { /* select SG-size */
187 sgsize = devinfo->wgsize[1];
188 }
189 wgsize[0] = LIBXSMM_UP(wgsize[0], sgsize);
190 } else {
191 wgsize[0] = LIBXSMM_UP(wgsize[0], devinfo->wgsize[1]);
192 sgsize = 0;
193 }
194 wgsize[0] = LIBXSMM_CLMP(wgsize[0], 0, devinfo->wgsize[0]);
195 sm = ((0 != sm && 0 != wgsize[0])
196 ? (LIBXSMM_ISPOT(bn * sizeof(double)) + 1)
197 : 0);
198 clinear = (NULL == lin_env ? 0 /*default*/ : atoi(lin_env));
199 offset += (size_t)LIBXSMM_SNPRINTF(
200 flags + offset, sizeof(flags) - offset,
201 " %s %s -DBN=%i -DSM=%i -DLU=%i -DWG=%i -DSG=%i",
202 0 != gpu ? "-DGPU" : "", 0 == clinear ? "" : "-DCLINEAR", bn, sm,
203 lu, (int)wgsize[0], (int)sgsize);
204 if (0 != precision) {
205 offset +=
206 (size_t)LIBXSMM_SNPRINTF(flags + offset, sizeof(flags) - offset,
207 " -DPRECISION=%i", precision);
208 }
209 gen = 0;
210 }
211 if (0 != devinfo->intel && 0 < xf) {
212 options = "-cl-intel-256-GRF-per-thread";
213 }
214 result |= (sizeof(flags) > offset ? EXIT_SUCCESS : EXIT_FAILURE);
215 if (2 <= verbosity || 0 > verbosity || EXIT_SUCCESS != result) {
216 const char *const kind = (EXIT_SUCCESS == result ? "INFO" : "ERROR");
217 fprintf(stderr, "%s ACC/LIBDBM: DBM-kernel gpu=%i", kind, gpu);
218 dbm_multiply_opencl_print(stderr, "gen", gen); /* generated */
219 dbm_multiply_opencl_print(stderr, "lin", clinear);
220 dbm_multiply_opencl_print(stderr, "fp", precision);
221 dbm_multiply_opencl_print(stderr, "bn", bn);
222 dbm_multiply_opencl_print(stderr, "sm", sm);
223 dbm_multiply_opencl_print(stderr, "wg", (int)wgsize[0]);
224 dbm_multiply_opencl_print(stderr, "sg", (int)sgsize);
225 dbm_multiply_opencl_print(stderr, "lu", lu);
226 fprintf(stderr, " -> ");
227 }
228 result |= c_dbcsr_acc_opencl_kernel(
229 0 /*source_is_file*/, OPENCL_DBM_SOURCE_MULTIPLY, "dbm_multiply",
230 flags, options, NULL /*try*/, NULL /*try_ok*/, extensions,
231 nextensions, &kernel_global);
232 if (2 <= verbosity || 0 > verbosity || EXIT_SUCCESS != result) {
233 if (EXIT_SUCCESS == result) {
234 const double ds = DBM_TIMER_DIFF(start, DBM_TIMER_TICK());
235 fprintf(stderr, "%.1f ms\n", 1E3 * ds);
236 } else {
237 fprintf(stderr, "FAILED!\n");
238 }
239 }
240 }
241 kernel = clCloneKernel(kernel_global, &result); /* always clone */
242 ACC_OPENCL_RELEASE(config->lock_main);
243 } else if (NULL == kernel) {
244 kernel = clCloneKernel(kernel_global, &result);
245 }
246#else
247#error "OpenCL kernel code not found!"
248#endif
249 if (NULL != lock_memory) {
250 ACC_OPENCL_ACQUIRE(lock_memory);
251 }
252 result |= c_dbcsr_acc_opencl_info_devptr_lock(&adata, NULL /*lock*/,
253 pack_a_data, 1 /*esize*/,
254 NULL /*amount*/, &iadata);
255 result |= c_dbcsr_acc_opencl_info_devptr_lock(&bdata, NULL /*lock*/,
256 pack_b_data, 1 /*esize*/,
257 NULL /*amount*/, &ibdata);
258 result |= c_dbcsr_acc_opencl_info_devptr_lock(&cdata, NULL /*lock*/,
259 shard_c_data, 1 /*esize*/,
260 NULL /*amount*/, &icdata);
261 result |= c_dbcsr_acc_opencl_info_devptr_lock(
262 &batch, NULL /*lock*/, params /*batch*/, sizeof(int) * stride,
263 &work_tasks, &ibatch);
264 if (NULL != lock_memory) {
265 ACC_OPENCL_RELEASE(lock_memory);
266 }
267 assert(0 == iadata && 0 == ibdata && 0 == icdata);
268 result |= clSetKernelArg(kernel, 0, sizeof(cl_double), &alpha);
269 result |= clSetKernelArg(kernel, 1, sizeof(cl_int), &ibatch);
270 if (1 < ndims) { /* DBM_MULTIPLY_GEN */
271 const cl_uint zero = 0;
272 assert(0 != wgsize[1] && 0 != wgsize[1] && 0 != wgsize[2]);
273 work_size[0] = 16;
274 assert(1 == work_size[1]);
275 work_size[2] = work_tasks;
276 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 2, batch.memory);
277 result |= clSetKernelArg(kernel, 3, sizeof(cl_uint), &zero /*shape*/);
278 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 4, adata.memory);
279 result |= clSetKernelArg(kernel, 5, sizeof(cl_uint), &zero /*A_shape0*/);
280 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 6, bdata.memory);
281 result |= clSetKernelArg(kernel, 7, sizeof(cl_uint), &zero /*B_shape0*/);
282 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 8, cdata.memory);
283 result |= clSetKernelArg(kernel, 9, sizeof(cl_uint), &zero /*C_shape0*/);
284#if !(defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT))
285 if (0 != info) {
286 dbm_multiply_gpu_launch_info(&task, params_host, ntasks, param_format);
287 }
288#endif
289 } else {
290 size_t size = work_tasks;
291#if defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT)
292 if (0 == dbm_multiply_opencl_smm && 0 == info)
293#endif
294 {
295 dbm_multiply_gpu_launch_info(&task, params_host, ntasks, param_format);
296 }
297 size *= (0 == clinear ? task.max_m : task.max_n);
298 /* fixup to be a multiple of the WG-size */
299 work_size[0] = (0 < wgsize[0] ? LIBXSMM_UP(size, wgsize[0]) : size);
300 result |= clSetKernelArg(kernel, 2, sizeof(cl_int), &ntasks);
301 result |= clSetKernelArg(kernel, 3, sizeof(cl_int), &size);
302 result |= clSetKernelArg(kernel, 4, sizeof(cl_int), &param_format);
303 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 5, batch.memory);
304 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 6, adata.memory);
305 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 7, bdata.memory);
306 result |= c_dbcsr_acc_opencl_set_kernel_ptr(kernel, 8, cdata.memory);
307 }
308 result |= clEnqueueNDRangeKernel(str->queue, kernel, ndims, NULL, work_size,
309 0 < wgsize[0] ? wgsize : NULL,
310 0 /*num_wait*/, NULL /*wait_list*/, NULL);
311 }
312#if defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT)
313 else { /* homogeneous */
314 result |= opencl_libsmm_acc_process(
315 params_host, params, ntasks, dbcsr_type_real_8, pack_a_data,
316 pack_b_data, shard_c_data, task.max_m, task.max_n, task.max_k,
317 dbm_multiply_opencl_smm, 1 /*homogeneous*/, stream, NULL /*c_stream*/,
318 task.max_m | task.max_n << OPENCL_LIBSMM_PFORMAT |
319 (task.max_k << (OPENCL_LIBSMM_PFORMAT * 2)),
320 NULL);
321 }
322#endif
323 if (0 != info && EXIT_SUCCESS == result) {
324 static LIBXSMM_TLS DBM_TIMER_TICKINT start2 = 0;
325 const DBM_TIMER_TICKINT stop = DBM_TIMER_TICK();
326 const double dhost = DBM_TIMER_DIFF(start, stop);
327 const double diter = (0 < start2 ? DBM_TIMER_DIFF(start, start2) : dhost);
328#if defined(OPENCL_LIBSMM_PFORMAT) && (0 < OPENCL_LIBSMM_PFORMAT)
329 const char *const kind = (0 >= dbm_multiply_opencl_smm ? "DBM" : "SMM");
330#else
331 const char *const kind = "DBM";
332#endif
333 const int pure = (100 * (ntasks - task.mnk_changes) + ntasks - 1) / ntasks;
334 const double dtotl = LIBXSMM_MAX(diter, dhost);
335 start2 = stop;
336 fprintf(stderr,
337 "INFO ACC/LIBDBM: %s-kernel mnk=%ix%ix%i "
338 "pure=%i%% ntasks=%i ms=%.1f\n",
339 kind, task.max_m, task.max_n, task.max_k, pure, ntasks,
340 1E+3 * dtotl);
341 }
342 return result;
343}
344
345void dbm_multiply_gpu_launch_kernel(offloadStream_t stream, double alpha,
346 int ntasks, const dbm_task_t *tasks_host,
347 const dbm_task_t *tasks,
348 const double *pack_a_data,
349 const double *pack_b_data,
350 double *shard_c_data) {
351 const int result = dbm_multiply_opencl_launch_kernel(
352 stream, alpha, ntasks, 0 /*param_format*/, &tasks_host->m, &tasks->m,
353 pack_a_data, pack_b_data, shard_c_data);
354 OFFLOAD_CHECK(result);
355}
356
357#endif // defined(__OFFLOAD_OPENCL) && !defined(__NO_OFFLOAD_DBM)
358
359// EOF
static int imax(int x, int y)
Returns the larger of two given integers (missing from the C standard)
static void const int const int i
static grid_library_config config
real(kind=dp), parameter zero
Definition erf_complex.F:57
Internal struct for storing a task, ie. a single block multiplication.