17#include <libxs/libxs_gemm.h>
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,
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,
38 dgemm_(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c,
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;
74 for (
int itask = 0; itask < ntasks; ++itask) {
78 buckets[
i] += buckets[
i - 1];
81 for (
int itask = 0; itask < ntasks; ++itask) {
84 order[buckets[
i]] = itask;
96 const int ntasks = batch->
ntasks;
103 int batch_order[ntasks];
107 for (
int itask = 0; itask < ntasks; ++itask) {
108 batch_order[itask] = itask;
113 const libxs_gemm_config_t *gemm_config = NULL;
114 int kernel_m = 0, kernel_n = 0, kernel_k = 0;
119 for (
int itask = 0; itask < ntasks; ++itask) {
121 task_next = tasks[batch_order[(itask + 1) < ntasks ? (itask + 1) : itask]];
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;
130 (task.m != kernel_m || task.n != kernel_n || task.k != kernel_k)) {
131 const double beta = 1.0;
133 libxs_gemm_dispatch(LIBXS_DATATYPE_F64, ta, tb, task.m, task.n,
134 task.k, lda, ldb, task.m, &alpha, &beta, NULL);
143 double *
const data_c = shard_c->
data + task.offset_c;
146 if (NULL != gemm_config) {
147 libxs_gemm_call(gemm_config, data_a, data_b, data_c);
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);
#define cp_assert(condition)
#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.
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...
Internal struct for storing a pack - essentially a shard for MPI.
Internal struct for storing a matrix shard.
Internal struct for storing a task, ie. a single block multiplication.