BLAS functions
1. Introduction
This module defines the vector, matrix and tensor types used inside QMCkl,
the functions that allocate, reshape and copy them, and the matrix operations
the kernels are built on: qmckl_dgemm, qmckl_dgemv and qmckl_dger,
which wrap BLAS, qmckl_adjugate, which uses LAPACK for the general case,
and the helpers qmckl_matmul and qmckl_transpose. qmckl_dgemm and
qmckl_adjugate also have a _safe form, which takes the declared sizes of
the arrays and checks them.
Matrices and tensors are stored in Fortran order: two elements that differ by
one in the first dimension are contiguous in memory. The structures hold their
dimensions next to the data pointer, so an accessor like qmckl_mat(A,i,j)
knows the shape of what it reads.
The types are private. They are tangle'd into the headers used to compile the
library, but not into the public qmckl.h: the arrays that cross the API
boundary are plain double* buffers, which leaves an implementation free to
store its own data differently.
Most of the functions are private too, and are declared in
qmckl_blas_private_func.h. Only six of them appear in the public qmckl.h:
qmckl_dgemm, qmckl_dgemm_safe, qmckl_dgemv, qmckl_dger,
qmckl_adjugate and qmckl_adjugate_safe.
2. Data types
The library defines standard mathematical objects as structured data types with associated operations. These types encapsulate both the data and its metadata (such as dimensions).
These data types are used internally in QMCkl, so the current section is intended for QMCkl developers.
2.1. Vector
A vector represents a one-dimensional array of double-precision floating-point numbers. The structure includes both the data pointer and the size, ensuring that operations on vectors can verify dimensional compatibility.
| Variable | Type | Description |
|---|---|---|
size |
int64_t |
Dimension of the vector |
data |
double* |
Elements |
typedef struct qmckl_vector {
double* restrict data;
int64_t size;
} qmckl_vector;
2.1.1. Allocation and deallocation
qmckl_vector
qmckl_vector_alloc( qmckl_context context,
const int64_t size);
Allocates a new vector of the specified size. The function returns a
qmckl_vector structure containing the allocated memory and size information.
If the allocation fails, the returned vector will have a size of zero,
which can be checked by the caller to detect allocation failures.
qmckl_exit_code
qmckl_vector_free( qmckl_context context,
qmckl_vector* vector);
2.2. Matrix
| Variable | Type | Description |
|---|---|---|
size |
int64_t[2] |
Dimension of each component |
data |
double* |
Elements |
The dimensions use Fortran ordering: two elements differing by one in the first dimension are consecutive in memory.
typedef struct qmckl_matrix {
double* restrict data;
int64_t size[2];
} qmckl_matrix;
qmckl_matrix
qmckl_matrix_alloc( qmckl_context context,
const int64_t size1,
const int64_t size2);
Allocates a new matrix. If the allocation failed the sizes are zero.
qmckl_exit_code
qmckl_matrix_free( qmckl_context context,
qmckl_matrix* matrix);
2.3. Tensor
| Variable | Type | Description |
|---|---|---|
order |
int32_t |
Order of the tensor |
size |
int64_t[QMCKL_TENSOR_ORDER_MAX] |
Dimension of each component |
data |
double* |
Elements |
The dimensions use Fortran ordering: two elements differing by one in the first dimension are consecutive in memory.
#define QMCKL_TENSOR_ORDER_MAX 16
typedef struct qmckl_tensor {
double* restrict data;
int32_t order;
int32_t __space__;
int64_t size[QMCKL_TENSOR_ORDER_MAX];
} qmckl_tensor;
qmckl_tensor
qmckl_tensor_alloc( qmckl_context context,
const int32_t order,
const int64_t* size);
Allocates memory for a tensor. If the allocation failed, the size is zero.
qmckl_exit_code
qmckl_tensor_free (qmckl_context context,
qmckl_tensor* tensor);
2.4. Reshaping
Reshaping occurs in-place and the pointer to the data is copied.
2.4.1. Vector -> Matrix
qmckl_matrix
qmckl_matrix_of_vector(const qmckl_vector vector,
const int64_t size1,
const int64_t size2);
Reshapes a vector into a matrix.
2.4.2. Vector -> Tensor
qmckl_tensor
qmckl_tensor_of_vector(const qmckl_vector vector,
const int32_t order,
const int64_t* size);
Reshapes a vector into a tensor.
2.4.3. Matrix -> Vector
qmckl_vector
qmckl_vector_of_matrix(const qmckl_matrix matrix);
Reshapes a matrix into a vector.
2.4.4. Matrix -> Tensor
qmckl_tensor
qmckl_tensor_of_matrix(const qmckl_matrix matrix,
const int32_t order,
const int64_t* size);
Reshapes a matrix into a tensor.
2.4.5. Tensor -> Vector
qmckl_vector
qmckl_vector_of_tensor(const qmckl_tensor tensor);
Reshapes a tensor into a vector.
2.4.6. Tensor -> Matrix
qmckl_matrix
qmckl_matrix_of_tensor(const qmckl_tensor tensor,
const int64_t size1,
const int64_t size2);
Reshapes a tensor into a matrix.
2.5. Access macros
Macros are provided to ease the access to vectors, matrices and tensors. Matrices use column-major ordering, as in Fortran.
double qmckl_vec (qmckl_vector v, int64_t i); // v[i]
double qmckl_mat (qmckl_matrix m, int64_t i, int64_t j) // m[j][i]
double qmckl_ten3(qmckl_tensor t, int64_t i, int64_t j, int64_t k); // t[k][j][i]
double qmckl_ten4(qmckl_tensor t, int64_t i, int64_t j, int64_t k, int64_t l);
double qmckl_ten5(qmckl_tensor t, int64_t i, int64_t j, int64_t k, int64_t l, int64_t m);
double qmckl_ten6(qmckl_tensor t, int64_t i, int64_t j, int64_t k, int64_t l, int64_t m, int64_t n);
...
2.6. Set all elements
2.6.1. Vector
qmckl_vector
qmckl_vector_set(qmckl_vector vector, double value);
2.6.2. Matrix
qmckl_matrix
qmckl_matrix_set(qmckl_matrix matrix, double value);
2.6.3. Tensor
qmckl_tensor
qmckl_tensor_set(qmckl_tensor tensor, double value);
2.7. Copy to and from double*
qmckl_exit_code
qmckl_double_of_vector(const qmckl_context context,
const qmckl_vector vector,
double* const target,
const int64_t size_max);
Converts a vector to a double*.
qmckl_exit_code
qmckl_double_of_matrix(const qmckl_context context,
const qmckl_matrix matrix,
double* const target,
const int64_t size_max);
Converts a matrix to a double*.
qmckl_exit_code
qmckl_double_of_tensor(const qmckl_context context,
const qmckl_tensor tensor,
double* const target,
const int64_t size_max);
Converts a tensor to a double*.
qmckl_exit_code
qmckl_vector_of_double(const qmckl_context context,
const double* target,
const int64_t size_max,
qmckl_vector* vector);
Converts a double* to a vector.
qmckl_exit_code
qmckl_matrix_of_double(const qmckl_context context,
const double* target,
const int64_t size_max,
qmckl_matrix* matrix);
Converts a double* to a matrix.
qmckl_exit_code
qmckl_tensor_of_double(const qmckl_context context,
const double* target,
const int64_t size_max,
qmckl_tensor* tensor);
Converts a double* to a tensor.
2.8. Allocate and copy to double*
double* qmckl_alloc_double_of_vector(const qmckl_context context,
const qmckl_vector vector);
double* qmckl_alloc_double_of_matrix(const qmckl_context context,
const qmckl_matrix matrix);
double* qmckl_alloc_double_of_tensor(const qmckl_context context,
const qmckl_tensor tensor);
3. Matrix operations
3.1. qmckl_dgemm
Matrix multiplication with a BLAS interface:
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
TransA |
char |
in | 'T' or 't' transposes A |
TransB |
char |
in | 'T' or 't' transposes B |
m |
int64_t |
in | Number of rows of the product |
n |
int64_t |
in | Number of columns of the product |
k |
int64_t |
in | Inner dimension of the product |
alpha |
double |
in | Scaling factor of the product |
A |
double[][lda] |
in | Array containing the matrix A |
lda |
int64_t |
in | Leading dimension of array A |
B |
double[][ldb] |
in | Array containing the matrix B |
ldb |
int64_t |
in | Leading dimension of array B |
beta |
double |
in | Scaling factor of the input C |
C |
double[][ldc] |
out | Array containing the matrix C |
ldc |
int64_t |
in | Leading dimension of array C |
Requirements:
contextis notQMCKL_NULL_CONTEXTm > 0n > 0k > 0lda >= mifTransA == 'N', otherwiselda >= kldb >= kifTransB == 'N', otherwiseldb >= nldc >= mAis allocated with at leastlda*kelements ifTransA == 'N', andlda*melements otherwiseBis allocated with at leastldb*nelements ifTransB == 'N', andldb*kelements otherwiseCis allocated with at leastldc*nelementsm,n,k,lda,ldbandldcare smaller thanhuge(0_4), as they are passed to BLAS as 32-bit integers
The function checks the context and that m, n, k, lda, ldb and
ldc are positive. The rest of the list is the caller's responsibility;
qmckl_dgemm_safe checks the same conditions against the buffer sizes it
is given.
qmckl_exit_code qmckl_dgemm (
const qmckl_context context,
const char TransA,
const char TransB,
const int64_t m,
const int64_t n,
const int64_t k,
const double alpha,
const double* A,
const int64_t lda,
const double* B,
const int64_t ldb,
const double beta,
double* const C,
const int64_t ldc );
3.2. qmckl_dgemm_safe
"Size-safe" proxy function with the same functionality as qmckl_dgemm
but with 3 additional arguments. These arguments size_max_M (where M is a matrix)
let the caller declare the allocated size of each array, so that the sizes
are checked instead of being inferred from the leading dimensions.
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
TransA |
char |
in | 'T' or 't' transposes A |
TransB |
char |
in | 'T' or 't' transposes B |
m |
int64_t |
in | Number of rows of the product |
n |
int64_t |
in | Number of columns of the product |
k |
int64_t |
in | Inner dimension of the product |
alpha |
double |
in | Scaling factor of the product |
A |
double[][lda] |
in | Array containing the matrix A |
size_max_A |
int64_t |
in | Size of the matrix A |
lda |
int64_t |
in | Leading dimension of array A |
B |
double[][ldb] |
in | Array containing the matrix B |
size_max_B |
int64_t |
in | Size of the matrix B |
ldb |
int64_t |
in | Leading dimension of array B |
beta |
double |
in | Scaling factor of the input C |
C |
double[][ldc] |
out | Array containing the matrix C |
size_max_C |
int64_t |
in | Size of the matrix C |
ldc |
int64_t |
in | Leading dimension of array C |
Requirements:
contextis notQMCKL_NULL_CONTEXTm > 0n > 0k > 0lda >= mifTransA == 'N', otherwiselda >= kldb >= kifTransB == 'N', otherwiseldb >= nldc >= mTransAandTransBare'N','n','T'or't'Ais allocated with at leastlenaelements, wherelenaislda*kifTransA == 'N'andlda*motherwiseBis allocated with at leastlenbelements, wherelenbisldb*nifTransB == 'N'andldb*kotherwiseCis allocated with at leastldc*nelementssize_max_A >= lenasize_max_B >= lenbsize_max_C >= ldc * nm,n,k,lda,ldbandldcare smaller thanhuge(0_4), as they are passed to BLAS as 32-bit integers
qmckl_exit_code qmckl_dgemm_safe (
const qmckl_context context,
const char TransA,
const char TransB,
const int64_t m,
const int64_t n,
const int64_t k,
const double alpha,
const double* A,
const int64_t size_max_A,
const int64_t lda,
const double* B,
const int64_t size_max_B,
const int64_t ldb,
const double beta,
double* const C,
const int64_t size_max_C,
const int64_t ldc );
3.3. qmckl_matmul
Matrix multiplication using the qmckl_matrix data type:
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
TransA |
char |
in | 'T' transposes A |
TransB |
char |
in | 'T' transposes B |
alpha |
double |
in | Scaling factor of the product |
A |
qmckl_matrix |
in | Matrix A |
B |
qmckl_matrix |
in | Matrix B |
beta |
double |
in | Scaling factor of the input C |
C |
qmckl_matrix |
out | Matrix C |
The dimensions of C are set by qmckl_matmul; its data buffer must
already be large enough for the result.
Requirements:
contextis notQMCKL_NULL_CONTEXTTransAandTransBare'N'or'T'AandBhave compatible dimensions for the requested transpositionsCis notNULLandC->datais allocated with room for the result
qmckl_exit_code qmckl_matmul (
const qmckl_context context,
const char TransA,
const char TransB,
const double alpha,
const qmckl_matrix A,
const qmckl_matrix B,
const double beta,
qmckl_matrix* const C );
3.4. qmckl_adjugate
Given a square matrix A, its adjugate is the transpose of its matrix
of cofactors.
See also: https://en.wikipedia.org/wiki/Adjugate_matrix
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
n |
int64_t |
in | Number of rows and columns of the input matrix |
A |
double[][lda] |
in | Array containing the n*n matrix A |
lda |
int64_t |
in | Leading dimension of array A |
B |
double[][ldb] |
out | Adjugate of A |
ldb |
int64_t |
in | Leading dimension of array B |
det_l |
double |
inout | Determinant of A |
Requirements:
contextis notQMCKL_NULL_CONTEXTn > 0lda >= nAis allocated with at leastlda*nelementsldb >= nBis allocated with at leastldb*nelements
qmckl_exit_code qmckl_adjugate (
const qmckl_context context,
const int64_t n,
const double* A,
const int64_t lda,
double* const B,
const int64_t ldb,
double* det_l );
For matrices of size 5 or less, the adjugate is computed by a specialized routine. For larger sizes, we rely on the LAPACK library.
subroutine adjugate_general(context, na, A, LDA, B, LDB, det_l)
use qmckl
implicit none
integer(qmckl_context), intent(in) :: context
integer*8, intent(in) :: LDA
integer*8, intent(in) :: LDB
integer*8, intent(in) :: na
double precision, intent(in) :: A (LDA,na)
double precision, intent(out) :: B (LDB,na)
double precision, intent(inout) :: det_l
double precision :: work(LDA*max(na,64))
integer :: inf
integer :: ipiv(LDA)
integer :: lwork
integer(8) :: i, j
We first copy the array A into array B.
B(1:na,1:na) = A(1:na,1:na)
Then, we compute the LU factorization A = LU using the dgetrf
routine.
call dgetrf(na, na, B, LDB, ipiv, inf )
By convention, the determinant of L is one, so the determinant of
A is the product of the diagonal elements of U, up to the sign
introduced by the row interchanges.
det_l = 1.d0
j=0_8
do i=1,na
j = j+min(abs(ipiv(i)-i),1)
det_l = det_l*B(i,i)
enddo
As dgetrf returns PLU = A, where P is a permutation matrix,
that sign is (-1)^p, where p is the number of row interchanges.
if (iand(j,1_8) /= 0_8) then
det_l = -det_l
endif
Then, the inverse of A is computed using dgetri:
lwork = SIZE(work)
call dgetri(na, B, LDB, ipiv, work, lwork, inf )
and the adjugate matrix is computed as the product of the determinant with the inverse:
B(1:na,1:na) = B(1:na,1:na)*det_l
end subroutine adjugate_general
3.5. qmckl_adjugate_safe
"Size-safe" proxy function with the same functionality as qmckl_adjugate
but with 2 additional arguments. These arguments size_max_M (where M is a matrix)
let the caller declare the allocated size of each array, so that the sizes
are checked instead of being inferred from the leading dimensions.
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
n |
int64_t |
in | Number of rows and columns of the input matrix |
A |
double[][lda] |
in | Array containing the n*n matrix A |
size_max_A |
int64_t |
in | Size of the matrix A |
lda |
int64_t |
in | Leading dimension of array A |
B |
double[][ldb] |
out | Adjugate of A |
size_max_B |
int64_t |
in | Size of the matrix B |
ldb |
int64_t |
in | Leading dimension of array B |
det_l |
double |
inout | Determinant of A |
Requirements:
contextis notQMCKL_NULL_CONTEXTn > 0lda >= nAis allocated with at leastlda*nelementsldb >= nBis allocated with at leastldb*nelementssize_max_A >= lda * nsize_max_B >= ldb * nn,ldaandldbare smaller thanhuge(0_4), as they are passed to LAPACK as 32-bit integers
qmckl_exit_code qmckl_adjugate_safe (
const qmckl_context context,
const int64_t n,
const double* A,
const int64_t size_max_A,
const int64_t lda,
double* const B,
const int64_t size_max_B,
const int64_t ldb,
double* det_l );
For matrices of size 5 or less, the adjugate is computed by a specialized routine. For larger sizes, we rely on the LAPACK library.
3.6. qmckl_transpose
Computes the transpose of a real matrix: the element of At at row
i and column j is the element of A at row j and column i.
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
A |
qmckl_matrix |
in | Input matrix |
At |
qmckl_matrix |
out | Transposed matrix |
At is allocated by the caller with the transposed dimensions;
qmckl_transpose only writes into its data.
Requirements:
contextis notQMCKL_NULL_CONTEXTA.size[0] >= 1At.datais notNULLAt.size[0] == A.size[1]andAt.size[1] == A.size[0]
3.7. qmckl_dgemm_sparse_nn
Sparse matrix multiplication using CSR (Compressed Sparse Row) format for both matrices:
This function performs matrix multiplication where both A and B are stored
in CSR (Compressed Sparse Row) format. When the density of A or of B is
below 0.20, the nonzeros are multiplied directly; otherwise qmckl_dgemm
is called.
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
m |
int64_t |
in | Number of rows of matrix A and C |
n |
int64_t |
in | Number of columns of matrix B and C |
k |
int64_t |
in | Number of columns of A and rows of B |
alpha |
double |
in | Scaling factor of the product |
A |
CSRMatrix* |
in | CSR format sparse matrix A (m*k) |
B |
CSRMatrix* |
in | CSR format sparse matrix B (k*n) |
beta |
double |
in | Scaling factor of the input C |
C |
double* |
out | Dense output matrix C (m*n) |
ldc |
int64_t |
in | Leading dimension of array C |
Requirements:
contextis notQMCKL_NULL_CONTEXTm > 0n > 0k > 0Ais notNULLand properly initialized as CSR matrixBis notNULLand properly initialized as CSR matrixCis allocated with at leastldc*nelementsldc >= m
3.8. qmckl_dgemm_sparse_b_nn
Sparse matrix multiplication where A is dense and B is in CSC (Compressed Sparse Column) format:
This function performs matrix multiplication where A is a dense matrix and B
is stored in CSC (Compressed Sparse Column) format. When the density of B is
below 0.25, the nonzeros are multiplied directly; otherwise qmckl_dgemm
is called.
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
m |
int64_t |
in | Number of rows of matrix A and C |
n |
int64_t |
in | Number of columns of matrix B and C |
k |
int64_t |
in | Number of columns of A and rows of B |
alpha |
double |
in | Scaling factor of the product |
A |
double* |
in | Dense matrix A (m*k) |
LDA |
int64_t |
in | Leading dimension of array A |
B |
CSCMatrix* |
in | CSC format sparse matrix B (k*n) |
beta |
double |
in | Scaling factor of the input C |
C |
double* |
out | Dense output matrix C (m*n) |
ldc |
int64_t |
in | Leading dimension of array C |
Requirements:
contextis notQMCKL_NULL_CONTEXTm > 0n > 0k > 0Ais notNULLand allocated with at leastLDA*kelementsLDA >= mBis notNULLand properly initialized as CSC matrixCis allocated with at leastldc*nelementsldc >= m
3.9. qmckl_dgemv
Matrix-vector multiplication with a BLAS interface:
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
Trans |
char |
in | 'T' or 't' transposes the matrix |
m |
int64_t |
in | Number of rows of the input matrix |
n |
int64_t |
in | Number of columns of the input matrix |
alpha |
double |
in | Scaling factor of the product |
A |
double[][lda] |
in | Array containing the matrix A |
lda |
int64_t |
in | Leading dimension of array A |
X |
double[] |
in | Array containing the vector X |
incx |
int64_t |
in | Increment of the elements of X |
beta |
double |
in | Scaling factor of the input Y |
Y |
double[] |
out | Array containing the vector Y |
incy |
int64_t |
in | Increment of the elements of Y |
Requirements:
contextis notQMCKL_NULL_CONTEXTm > 0n > 0lda >= mifTrans == 'N', otherwiselda >= nincx > 0andincy > 0Ais allocated with at leastlda*nelements ifTrans == 'N', andlda*melements otherwiseXis allocated with at least1+(n-1)*incxelements ifTrans == 'N', and1+(m-1)*incxelements otherwiseYis allocated with at least1+(m-1)*incyelements ifTrans == 'N', and1+(n-1)*incyelements otherwisem,n,lda,incxandincyare smaller thanhuge(0_4), as they are passed to BLAS as 32-bit integers
The function checks the context and that m, n, lda, incx and
incy are positive. The rest of the list, the lower bound on lda
included, is the caller's responsibility.
qmckl_exit_code qmckl_dgemv (
const qmckl_context context,
const char Trans,
const int64_t m,
const int64_t n,
const double alpha,
const double* A,
const int64_t lda,
const double* X,
const int64_t incx,
const double beta,
double* const Y,
const int64_t incy );
3.10. qmckl_dger
Rank-1 update
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
m |
int64_t |
in | Number of rows of the input matrix |
n |
int64_t |
in | Number of columns of the input matrix |
alpha |
double |
in | Scaling factor of the rank-1 update |
X |
double[] |
in | Array containing the vector X |
incx |
int64_t |
in | Increment of the elements of X |
Y |
double[] |
in | Array containing the vector Y |
incy |
int64_t |
in | Increment of the elements of Y |
A |
double[][lda] |
inout | Array containing the matrix A |
lda |
int64_t |
in | Leading dimension of array A |
Requirements:
contextis notQMCKL_NULL_CONTEXTm > 0n > 0lda >= mincx > 0andincy > 0Xis allocated with at least1+(m-1)*incxelementsYis allocated with at least1+(n-1)*incyelementsAis allocated with at leastlda*nelementsm,n,lda,incxandincyare smaller thanhuge(0_4), as they are passed to BLAS as 32-bit integers
The function checks the context and that m, n, lda, incx and
incy are positive. The rest of the list, lda >= m included, is the
caller's responsibility.
qmckl_exit_code qmckl_dger (
const qmckl_context context,
const int64_t m,
const int64_t n,
const double alpha,
const double* X,
const int64_t incx,
const double* Y,
const int64_t incy,
double* A,
const int64_t lda );