2021-04-23 11:25:45 +02:00
|
|
|
#+TITLE: QMCkl dgemm
|
|
|
|
|
|
|
|
#+NAME: header
|
|
|
|
#+BEGIN_SRC text
|
|
|
|
Generated from qmckl_dgemm.org
|
|
|
|
#+END_SRC
|
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
#+BEGIN_SRC c :noweb yes :tangle qmckl_dgemm.c
|
|
|
|
/* <<header>> */
|
|
|
|
|
|
|
|
#include <cblas.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
<<tasks_init>>
|
|
|
|
|
|
|
|
<<dgemm_args>>
|
|
|
|
|
|
|
|
<<dgemm_rec>>
|
|
|
|
|
|
|
|
<<dgemm>>
|
|
|
|
|
|
|
|
<<tasks_run>>
|
|
|
|
|
|
|
|
#+END_SRC
|
|
|
|
|
2021-04-23 11:25:45 +02:00
|
|
|
* Fortran interface
|
|
|
|
|
|
|
|
#+BEGIN_SRC f90 :noweb yes :tangle qmckl_blas_f.f90
|
|
|
|
! <<header>>
|
|
|
|
|
|
|
|
module qmckl_blas
|
|
|
|
use :: iso_c_binding
|
|
|
|
|
|
|
|
interface
|
|
|
|
subroutine qmckl_dgemm(transa, transb, m, n, k, &
|
2021-04-23 23:35:06 +02:00
|
|
|
alpha, A, lda, B, ldb, beta, C, ldc, res) bind(C)
|
2021-04-23 11:25:45 +02:00
|
|
|
use :: iso_c_binding
|
|
|
|
implicit none
|
|
|
|
character(kind=c_char ), value :: transa, transb
|
|
|
|
integer (kind=c_int ), value :: m, n, k, lda, ldb, ldc
|
|
|
|
real (kind=c_double), value :: alpha, beta
|
|
|
|
real (kind=c_double) :: A(lda,*), B(ldb,*), C(ldc,*)
|
2021-04-23 23:35:06 +02:00
|
|
|
integer (kind=c_int64_t) :: res
|
2021-04-23 11:25:45 +02:00
|
|
|
end subroutine qmckl_dgemm
|
|
|
|
end interface
|
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
interface
|
|
|
|
subroutine qmckl_tasks_run(gemms, ngemms) bind(C)
|
|
|
|
use :: iso_c_binding
|
|
|
|
implicit none
|
|
|
|
integer (kind=c_int32_t), value :: ngemms
|
|
|
|
integer (kind=c_int64_t) :: gemms(ngemms)
|
|
|
|
end subroutine qmckl_tasks_run
|
|
|
|
end interface
|
|
|
|
|
2021-04-23 11:25:45 +02:00
|
|
|
end module qmckl_blas
|
|
|
|
#+END_SRC
|
|
|
|
|
2021-04-23 14:18:59 +02:00
|
|
|
* TODO C code
|
2021-04-23 11:25:45 +02:00
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
|
|
|
|
The main function packs the arguments in the struct and returns
|
|
|
|
the struct as a result.
|
|
|
|
|
|
|
|
#+NAME: dgemm_args
|
|
|
|
#+BEGIN_SRC c
|
2021-04-23 11:25:45 +02:00
|
|
|
struct dgemm_args {
|
2021-04-23 14:18:59 +02:00
|
|
|
double alpha;
|
2021-04-23 11:25:45 +02:00
|
|
|
double beta;
|
|
|
|
double* A;
|
|
|
|
double* B;
|
|
|
|
double* C;
|
|
|
|
int m;
|
|
|
|
int n;
|
|
|
|
int k;
|
|
|
|
int lda;
|
|
|
|
int ldb;
|
|
|
|
int ldc;
|
|
|
|
CBLAS_LAYOUT transa;
|
|
|
|
CBLAS_LAYOUT transb;
|
2021-04-23 14:18:59 +02:00
|
|
|
};
|
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
#+END_SRC
|
2021-04-23 11:25:45 +02:00
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
#+NAME: dgemm
|
|
|
|
#+BEGIN_SRC c
|
2021-04-23 11:25:45 +02:00
|
|
|
void qmckl_dgemm(char transa, char transb,
|
|
|
|
int m, int n, int k,
|
2021-04-23 14:18:59 +02:00
|
|
|
double alpha,
|
2021-04-23 11:25:45 +02:00
|
|
|
double* A, int lda,
|
|
|
|
double* B, int ldb,
|
|
|
|
double beta,
|
2021-04-23 23:35:06 +02:00
|
|
|
double* C, int ldc,
|
|
|
|
int64_t* result)
|
2021-04-23 11:25:45 +02:00
|
|
|
{
|
2021-04-23 23:35:06 +02:00
|
|
|
struct dgemm_args* args = (struct dgemm_args*) malloc (sizeof(struct dgemm_args));
|
|
|
|
assert (args != NULL);
|
|
|
|
*result = (int64_t) args;
|
|
|
|
|
|
|
|
args->alpha = alpha;
|
|
|
|
args->beta = beta ;
|
|
|
|
args->A = A;
|
|
|
|
args->B = B;
|
|
|
|
args->C = C;
|
|
|
|
args->m = m;
|
|
|
|
args->n = n;
|
|
|
|
args->k = k;
|
|
|
|
args->lda = lda;
|
|
|
|
args->ldb = ldb;
|
|
|
|
args->ldc = ldc;
|
2021-04-23 11:25:45 +02:00
|
|
|
|
|
|
|
if (transa == 'T' || transa == 't') {
|
2021-04-23 23:35:06 +02:00
|
|
|
args->transa = CblasTrans;
|
2021-04-23 11:25:45 +02:00
|
|
|
} else {
|
2021-04-23 23:35:06 +02:00
|
|
|
args->transa = CblasNoTrans;
|
2021-04-23 11:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (transa == 'T' || transa == 't') {
|
2021-04-23 23:35:06 +02:00
|
|
|
args->transb = CblasTrans;
|
2021-04-23 11:25:45 +02:00
|
|
|
} else {
|
2021-04-23 23:35:06 +02:00
|
|
|
args->transb = CblasNoTrans;
|
2021-04-23 11:25:45 +02:00
|
|
|
}
|
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
To run the dgemms as tasks, pass all the dgemms to do to the
|
|
|
|
following function. It call task-based the recursive dgemm defined below.
|
|
|
|
|
|
|
|
#+NAME: tasks_run
|
|
|
|
#+BEGIN_SRC c
|
|
|
|
void qmckl_tasks_run(struct dgemm_args** gemms, int ngemms)
|
|
|
|
{
|
2021-04-23 14:30:34 +02:00
|
|
|
#pragma omp parallel
|
|
|
|
{
|
|
|
|
#pragma omp single
|
|
|
|
{
|
2021-04-23 23:35:06 +02:00
|
|
|
for (int i=0 ; i<ngemms ; ++i)
|
|
|
|
{
|
|
|
|
qmckl_dgemm_rec(*(gemms[i]));
|
|
|
|
}
|
2021-04-23 14:30:34 +02:00
|
|
|
}
|
|
|
|
#pragma omp taskwait
|
|
|
|
}
|
2021-04-23 11:25:45 +02:00
|
|
|
}
|
2021-04-23 23:35:06 +02:00
|
|
|
#+END_SRC
|
|
|
|
|
2021-04-23 11:25:45 +02:00
|
|
|
|
2021-04-23 23:35:06 +02:00
|
|
|
#+NAME: dgemm_rec
|
|
|
|
#+BEGIN_SRC c
|
|
|
|
#define MIN_SIZE 512
|
2021-04-23 14:18:59 +02:00
|
|
|
#include<stdio.h>
|
|
|
|
|
2021-04-23 11:25:45 +02:00
|
|
|
static void qmckl_dgemm_rec(struct dgemm_args args) {
|
|
|
|
|
2021-04-23 14:18:59 +02:00
|
|
|
// printf("%5d %5d\n", args.m, args.n);
|
|
|
|
|
|
|
|
if ( (args.m <= MIN_SIZE) || (args.n <= MIN_SIZE)) {
|
2021-04-23 14:30:34 +02:00
|
|
|
#pragma omp task
|
|
|
|
{
|
2021-04-23 23:35:06 +02:00
|
|
|
printf("BLAS %5d %5d %5d\n", args.m, args.n, args.k);
|
2021-04-23 14:30:34 +02:00
|
|
|
cblas_dgemm(CblasColMajor, args.transa, args.transb,
|
|
|
|
args.m, args.n, args.k, args.alpha,
|
|
|
|
args.A, args.lda, args.B, args.ldb,
|
|
|
|
args.beta, args.C, args.ldc);
|
|
|
|
}
|
2021-04-23 14:18:59 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
int m1 = args.m / 2;
|
|
|
|
int m2 = args.m - m1;
|
|
|
|
int n1 = args.n / 2;
|
|
|
|
int n2 = args.n - n1;
|
2021-04-23 14:30:34 +02:00
|
|
|
|
|
|
|
#pragma omp task
|
|
|
|
{
|
|
|
|
struct dgemm_args args_1 = args;
|
|
|
|
args_1.m = m1;
|
|
|
|
args_1.n = n1;
|
|
|
|
qmckl_dgemm_rec(args_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma omp task
|
|
|
|
{
|
|
|
|
// TODO: assuming 'N', 'N' here
|
|
|
|
struct dgemm_args args_2 = args;
|
|
|
|
args_2.B = args.B + args.ldb*n1;
|
|
|
|
args_2.C = args.C + args.ldc*n1;
|
|
|
|
args_2.m = m1;
|
|
|
|
args_2.n = n2;
|
|
|
|
qmckl_dgemm_rec(args_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma omp task
|
|
|
|
{
|
|
|
|
struct dgemm_args args_3 = args;
|
|
|
|
args_3.A = args.A + m1;
|
|
|
|
args_3.C = args.C + m1;
|
|
|
|
args_3.m = m2;
|
|
|
|
args_3.n = n1;
|
|
|
|
qmckl_dgemm_rec(args_3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma omp task
|
|
|
|
{
|
|
|
|
struct dgemm_args args_4 = args;
|
|
|
|
args_4.A = args.A + m1;
|
|
|
|
args_4.B = args.B + args.ldb*n1;
|
|
|
|
args_4.C = args.C + m1 + args.ldc*n1;
|
|
|
|
args_4.m = m2;
|
|
|
|
args_4.n = n2;
|
|
|
|
qmckl_dgemm_rec(args_4);
|
|
|
|
}
|
2021-04-23 14:18:59 +02:00
|
|
|
}
|
2021-04-23 11:25:45 +02:00
|
|
|
|
|
|
|
}
|
2021-04-23 23:35:06 +02:00
|
|
|
#+END_SRC
|
2021-04-23 11:25:45 +02:00
|
|
|
|
|
|
|
|