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
C
typedef struct qmckl_vector {
  double* restrict data;
  int64_t size;
} qmckl_vector;

2.1.1. Allocation and deallocation

C
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.

C
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.

C
typedef struct qmckl_matrix {
  double* restrict data;
  int64_t size[2];
} qmckl_matrix;
C
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.

C
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.

C
#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;
C
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.

C
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

C
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

C
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

C
qmckl_vector
qmckl_vector_of_matrix(const qmckl_matrix matrix);

Reshapes a matrix into a vector.

2.4.4. Matrix -> Tensor

C
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

C
qmckl_vector
qmckl_vector_of_tensor(const qmckl_tensor tensor);

Reshapes a tensor into a vector.

2.4.6. Tensor -> Matrix

C
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.

C
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

C
qmckl_vector
qmckl_vector_set(qmckl_vector vector, double value);

2.6.2. Matrix

C
qmckl_matrix
qmckl_matrix_set(qmckl_matrix matrix, double value);

2.6.3. Tensor

C
qmckl_tensor
qmckl_tensor_set(qmckl_tensor tensor, double value);

2.7. Copy to and from double*

C
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*.

C
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*.

C
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*.

C
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.

C
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.

C
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*

C
double* qmckl_alloc_double_of_vector(const qmckl_context context,
                                     const qmckl_vector vector);
C
double* qmckl_alloc_double_of_matrix(const qmckl_context context,
                                     const qmckl_matrix matrix);
C
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:

Cij=βCij+α∑kAik·Bkj

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:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • k > 0
  • lda >= m if TransA == 'N', otherwise lda >= k
  • ldb >= k if TransB == 'N', otherwise ldb >= n
  • ldc >= m
  • A is allocated with at least lda*k elements if TransA == 'N', and lda*m elements otherwise
  • B is allocated with at least ldb*n elements if TransB == 'N', and ldb*k elements otherwise
  • C is allocated with at least ldc*n elements
  • m, n, k, lda, ldb and ldc are smaller than huge(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.

C
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:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • k > 0
  • lda >= m if TransA == 'N', otherwise lda >= k
  • ldb >= k if TransB == 'N', otherwise ldb >= n
  • ldc >= m
  • TransA and TransB are 'N', 'n', 'T' or 't'
  • A is allocated with at least lena elements, where lena is lda*k if TransA == 'N' and lda*m otherwise
  • B is allocated with at least lenb elements, where lenb is ldb*n if TransB == 'N' and ldb*k otherwise
  • C is allocated with at least ldc*n elements
  • size_max_A >= lena
  • size_max_B >= lenb
  • size_max_C >= ldc * n
  • m, n, k, lda, ldb and ldc are smaller than huge(0_4), as they are passed to BLAS as 32-bit integers
C
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:

Cij=βCij+α∑kAik·Bkj

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:

  • context is not QMCKL_NULL_CONTEXT
  • TransA and TransB are 'N' or 'T'
  • A and B have compatible dimensions for the requested transpositions
  • C is not NULL and C->data is allocated with room for the result
C
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.

B=adj(A)=det(A)A−1

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:

  • context is not QMCKL_NULL_CONTEXT
  • n > 0
  • lda >= n
  • A is allocated with at least lda*n elements
  • ldb >= n
  • B is allocated with at least ldb*n elements
C
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.

Fortran
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.

Fortran
B(1:na,1:na) = A(1:na,1:na)

Then, we compute the LU factorization A = LU using the dgetrf routine.

Fortran
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.

Fortran
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.

Fortran
if (iand(j,1_8) /= 0_8)  then
  det_l = -det_l
endif

Then, the inverse of A is computed using dgetri:

Fortran
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:

Fortran
  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:

  • context is not QMCKL_NULL_CONTEXT
  • n > 0
  • lda >= n
  • A is allocated with at least lda*n elements
  • ldb >= n
  • B is allocated with at least ldb*n elements
  • size_max_A >= lda * n
  • size_max_B >= ldb * n
  • n, lda and ldb are smaller than huge(0_4), as they are passed to LAPACK as 32-bit integers
C
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:

  • context is not QMCKL_NULL_CONTEXT
  • A.size[0] >= 1
  • At.data is not NULL
  • At.size[0] == A.size[1] and At.size[1] == A.size[0]

3.7. qmckl_dgemm_sparse_nn

Sparse matrix multiplication using CSR (Compressed Sparse Row) format for both matrices:

Cij=βCij+α∑kAik·Bkj

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:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • k > 0
  • A is not NULL and properly initialized as CSR matrix
  • B is not NULL and properly initialized as CSR matrix
  • C is allocated with at least ldc*n elements
  • ldc >= m

3.8. qmckl_dgemm_sparse_b_nn

Sparse matrix multiplication where A is dense and B is in CSC (Compressed Sparse Column) format:

Cij=βCij+α∑kAik·Bkj

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:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • k > 0
  • A is not NULL and allocated with at least LDA*k elements
  • LDA >= m
  • B is not NULL and properly initialized as CSC matrix
  • C is allocated with at least ldc*n elements
  • ldc >= m

3.9. qmckl_dgemv

Matrix-vector multiplication with a BLAS interface:

Yi=∑jαAijXj+βYi

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:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • lda >= m if Trans == 'N', otherwise lda >= n
  • incx > 0 and incy > 0
  • A is allocated with at least lda*n elements if Trans == 'N', and lda*m elements otherwise
  • X is allocated with at least 1+(n-1)*incx elements if Trans == 'N', and 1+(m-1)*incx elements otherwise
  • Y is allocated with at least 1+(m-1)*incy elements if Trans == 'N', and 1+(n-1)*incy elements otherwise
  • m, n, lda, incx and incy are smaller than huge(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.

C
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

Aij=αXiYj+Aij

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:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • lda >= m
  • incx > 0 and incy > 0
  • X is allocated with at least 1+(m-1)*incx elements
  • Y is allocated with at least 1+(n-1)*incy elements
  • A is allocated with at least lda*n elements
  • m, n, lda, incx and incy are smaller than huge(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.

C
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 );