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_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 : ******************************************************************************/
24 : void 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 : ******************************************************************************/
33 0 : static 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 0 : 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 : ******************************************************************************/
50 65671406 : static inline unsigned int hash(const dbm_task_t task) {
51 65671406 : const unsigned int m = task.m, n = task.n, k = task.k;
52 65671406 : const unsigned int mn = (m >= n) ? m * m + m + n : m + n * n;
53 65671406 : const unsigned int mnk = (mn >= k) ? mn * mn + mn + k : mn + k * k;
54 65671406 : return mnk;
55 : }
56 :
57 : /*******************************************************************************
58 : * \brief Private routine for mapping a task to its bucket (by shape).
59 : * \author Hans Pabst
60 : ******************************************************************************/
61 65671406 : static inline int task_bucket(const dbm_task_t task) {
62 65671406 : 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 : ******************************************************************************/
70 343927 : void dbm_multiply_cpu_task_order(const int ntasks,
71 : const dbm_task_t batch[ntasks],
72 : int order[ntasks]) {
73 343927 : int buckets[DBM_BATCH_NUM_BUCKETS] = {0};
74 33179630 : for (int itask = 0; itask < ntasks; ++itask) {
75 32835703 : ++buckets[task_bucket(batch[itask])];
76 : }
77 343927000 : for (int i = 1; i < DBM_BATCH_NUM_BUCKETS; ++i) {
78 343583073 : buckets[i] += buckets[i - 1];
79 : }
80 343927 : cp_assert(0 >= ntasks || buckets[DBM_BATCH_NUM_BUCKETS - 1] == ntasks);
81 33179630 : for (int itask = 0; itask < ntasks; ++itask) {
82 32835703 : const int i = task_bucket(batch[itask]);
83 32835703 : --buckets[i];
84 32835703 : order[buckets[i]] = itask;
85 : }
86 343927 : }
87 :
88 : /*******************************************************************************
89 : * \brief Internal routine for executing the tasks in given batch on the CPU.
90 : * \author Ole Schuett
91 : ******************************************************************************/
92 419628 : void 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 419628 : dbm_shard_t *shard_c, int options) {
96 419628 : const int ntasks = batch->ntasks;
97 419628 : const dbm_task_t *const tasks = batch->tasks;
98 419628 : if (0 >= ntasks) { // nothing to do
99 75701 : return;
100 : }
101 343927 : dbm_shard_allocate_promised_blocks(shard_c);
102 :
103 343927 : int batch_order[ntasks];
104 343927 : if (DBM_MULTIPLY_TASK_REORDER & options) {
105 343927 : dbm_multiply_cpu_task_order(ntasks, tasks, batch_order);
106 : } else {
107 0 : for (int itask = 0; itask < ntasks; ++itask) {
108 0 : batch_order[itask] = itask;
109 : }
110 : }
111 :
112 : #if defined(__LIBXS)
113 343927 : const libxs_gemm_config_t *gemm_config = NULL;
114 343927 : int kernel_m = 0, kernel_n = 0, kernel_k = 0;
115 : #endif
116 :
117 : // Loop over tasks.
118 343927 : dbm_task_t task_next = tasks[batch_order[0]];
119 33179630 : for (int itask = 0; itask < ntasks; ++itask) {
120 32835703 : const dbm_task_t task = task_next;
121 32835703 : task_next = tasks[batch_order[(itask + 1) < ntasks ? (itask + 1) : itask]];
122 :
123 : #if defined(__LIBXS)
124 32835703 : if (0 == (DBM_MULTIPLY_BLAS_LIBRARY & options) &&
125 31875418 : (task.m != kernel_m || task.n != kernel_n || task.k != kernel_k)) {
126 2102655 : const double beta = 1.0;
127 2102655 : gemm_config = libxs_gemm_dispatch(LIBXS_DATATYPE_F64, 'N', 'T', task.m,
128 : task.n, task.k, task.m, task.n, task.m,
129 : &alpha, &beta, NULL);
130 2102655 : kernel_m = task.m;
131 2102655 : kernel_n = task.n;
132 2102655 : kernel_k = task.k;
133 : }
134 : #endif
135 :
136 32835703 : double *const data_a = pack_a->data + task.offset_a;
137 32835703 : double *const data_b = pack_b->data + task.offset_b;
138 32835703 : double *const data_c = shard_c->data + task.offset_c;
139 :
140 : #if defined(__LIBXS)
141 32835703 : if (NULL != gemm_config) {
142 32835703 : libxs_gemm_call(gemm_config, data_a, data_b, data_c);
143 : } else
144 : #endif
145 : {
146 0 : dbm_dgemm('N', 'T', task.m, task.n, task.k, alpha, data_a, task.m, data_b,
147 : task.n, 1.0, data_c, task.m);
148 : }
149 : }
150 : }
151 :
152 : // EOF
|