(git:5a3db73)
Loading...
Searching...
No Matches
dbm_multiply_cpu.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 "dbm_multiply_cpu.h"
8#include "dbm_hyperparams.h"
9
10#include "../base/cp_assert.h"
11#include <stddef.h>
12
13#if defined(__LIBXSMM)
14#include <libxsmm.h>
15#endif
16#if defined(__LIBXS)
17#include <libxs/libxs_gemm.h>
18#endif
19
20/*******************************************************************************
21 * \brief Prototype for BLAS dgemm.
22 * \author Ole Schuett
23 ******************************************************************************/
24void dgemm_(const char *transa, const char *transb, const int *m, const int *n,
25 const int *k, const double *alpha, const double *a, const int *lda,
26 const double *b, const int *ldb, const double *beta, double *c,
27 const int *ldc);
28
29/*******************************************************************************
30 * \brief Private convenient wrapper to hide Fortran nature of dgemm_.
31 * \author Ole Schuett
32 ******************************************************************************/
33static inline void dbm_dgemm(const char transa, const char transb, const int m,
34 const int n, const int k, const double alpha,
35 const double *a, const int lda, const double *b,
36 const int ldb, const double beta, double *c,
37 const int ldc) {
38 dgemm_(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c,
39 &ldc);
40}
41
42/*******************************************************************************
43 * \brief Private hash function based on Szudzik's elegant pairing.
44 * Using unsigned int to return a positive number even after overflow.
45 * https://en.wikipedia.org/wiki/Pairing_function#Other_pairing_functions
46 * https://stackoverflow.com/a/13871379
47 * http://szudzik.com/ElegantPairing.pdf
48 * \author Ole Schuett
49 ******************************************************************************/
50static inline unsigned int hash(const dbm_task_t task) {
51 const unsigned int m = task.m, n = task.n, k = task.k;
52 const unsigned int mn = (m >= n) ? m * m + m + n : m + n * n;
53 const unsigned int mnk = (mn >= k) ? mn * mn + mn + k : mn + k * k;
54 return mnk;
55}
56
57/*******************************************************************************
58 * \brief Private routine for mapping a task to its bucket (by shape).
59 * \author Hans Pabst
60 ******************************************************************************/
61static inline int task_bucket(const dbm_task_t task) {
62 return (int)(hash(task) % DBM_BATCH_NUM_BUCKETS);
63}
64
65/*******************************************************************************
66 * \brief Internal routine for ordering the tasks of a batch approximately by
67 * shape (bucket counting).
68 * \author Hans Pabst
69 ******************************************************************************/
70void dbm_multiply_cpu_task_order(const int ntasks,
71 const dbm_task_t batch[ntasks],
72 int order[ntasks]) {
73 int buckets[DBM_BATCH_NUM_BUCKETS] = {0};
74 for (int itask = 0; itask < ntasks; ++itask) {
75 ++buckets[task_bucket(batch[itask])];
76 }
77 for (int i = 1; i < DBM_BATCH_NUM_BUCKETS; ++i) {
78 buckets[i] += buckets[i - 1];
79 }
80 cp_assert(0 >= ntasks || buckets[DBM_BATCH_NUM_BUCKETS - 1] == ntasks);
81 for (int itask = 0; itask < ntasks; ++itask) {
82 const int i = task_bucket(batch[itask]);
83 --buckets[i];
84 order[buckets[i]] = itask;
85 }
86}
87
88/*******************************************************************************
89 * \brief Internal routine for executing the tasks in given batch on the CPU.
90 * \author Ole Schuett
91 ******************************************************************************/
92void dbm_multiply_cpu_process_batch(const dbm_batch_t *batch, double alpha,
93 const dbm_pack_t *pack_a,
94 const dbm_pack_t *pack_b,
95 dbm_shard_t *shard_c, int options) {
96 const int ntasks = batch->ntasks;
97 const dbm_task_t *const tasks = batch->tasks;
98 if (0 >= ntasks) { // nothing to do
99 return;
100 }
102
103 int batch_order[ntasks];
104 if (DBM_MULTIPLY_TASK_REORDER & options) {
105 dbm_multiply_cpu_task_order(ntasks, tasks, batch_order);
106 } else {
107 for (int itask = 0; itask < ntasks; ++itask) {
108 batch_order[itask] = itask;
109 }
110 }
111
112#if defined(__LIBXS)
113 const libxs_gemm_config_t *gemm_config = NULL;
114 int kernel_m = 0, kernel_n = 0, kernel_k = 0;
115#endif
116
117 // Loop over tasks.
118 dbm_task_t task_next = tasks[batch_order[0]];
119 for (int itask = 0; itask < ntasks; ++itask) {
120 const dbm_task_t task = task_next;
121 task_next = tasks[batch_order[(itask + 1) < ntasks ? (itask + 1) : itask]];
122
123 const char ta = pack_a->transposed ? 'T' : 'N';
124 const char tb = pack_b->transposed ? 'N' : 'T';
125 const int lda = ('T' == ta) ? task.k : task.m;
126 const int ldb = ('N' == tb) ? task.k : task.n;
127
128#if defined(__LIBXS)
129 if (0 == (DBM_MULTIPLY_BLAS_LIBRARY & options) &&
130 (task.m != kernel_m || task.n != kernel_n || task.k != kernel_k)) {
131 const double beta = 1.0;
132 gemm_config =
133 libxs_gemm_dispatch(LIBXS_DATATYPE_F64, ta, tb, task.m, task.n,
134 task.k, lda, ldb, task.m, &alpha, &beta, NULL);
135 kernel_m = task.m;
136 kernel_n = task.n;
137 kernel_k = task.k;
138 }
139#endif
140
141 double *const data_a = dbm_pack_data(pack_a, task.offset_a);
142 double *const data_b = dbm_pack_data(pack_b, task.offset_b);
143 double *const data_c = shard_c->data + task.offset_c;
144
145#if defined(__LIBXS)
146 if (NULL != gemm_config) {
147 libxs_gemm_call(gemm_config, data_a, data_b, data_c);
148 } else
149#endif
150 {
151 dbm_dgemm(ta, tb, task.m, task.n, task.k, alpha, data_a, lda, data_b, ldb,
152 1.0, data_c, task.m);
153 }
154 }
155}
156
157// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
#define DBM_BATCH_NUM_BUCKETS
static double * dbm_pack_data(const dbm_pack_t *pack, const int offset)
Internal routine for getting the data pointer of a block in a pack. Hides whether the pack is copied ...
static void dbm_dgemm(const char transa, const char transb, const int m, const int n, const int k, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
Private convenient wrapper to hide Fortran nature of dgemm_.
void dbm_multiply_cpu_process_batch(const dbm_batch_t *batch, double alpha, const dbm_pack_t *pack_a, const dbm_pack_t *pack_b, dbm_shard_t *shard_c, int options)
Internal routine for executing the tasks in given batch on the CPU.
static int task_bucket(const dbm_task_t task)
Private routine for mapping a task to its bucket (by shape).
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...
void dgemm_(const char *transa, const char *transb, const int *m, const int *n, const int *k, const double *alpha, const double *a, const int *lda, const double *b, const int *ldb, const double *beta, double *c, const int *ldc)
Prototype for BLAS dgemm.
void dbm_multiply_cpu_task_order(const int ntasks, const dbm_task_t batch[ntasks], int order[ntasks])
Internal routine for ordering the tasks of a batch approximately by shape (bucket counting).
@ DBM_MULTIPLY_BLAS_LIBRARY
@ DBM_MULTIPLY_TASK_REORDER
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
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.
bool transposed
Internal struct for storing a matrix shard.
Definition dbm_shard.h:30
double * data
Definition dbm_shard.h:42
Internal struct for storing a task, ie. a single block multiplication.