Computing a sketch: dense data

Full matrix-matrix operations

template<typename T, typename SKOP>
inline void RandBLAS::sketch_general(blas::Layout layout, blas::Op opS, blas::Op opA, int64_t d, int64_t n, int64_t m, T alpha, SKOP &S, const T *A, int64_t lda, T beta, T *B, int64_t ldb)

Sketch from the left in a GEMM-like operation

\[\operatorname{mat}(B) = \alpha \cdot \underbrace{\operatorname{op}(S)}_{d \times m} \cdot \underbrace{\operatorname{op}(\operatorname{mat}(A))}_{m \times n} + \beta \cdot \underbrace{\operatorname{mat}(B)}_{d \times n}, \tag{$\star$}\]

where \(\alpha\) and \(\beta\) are real scalars, \(\operatorname{op}(X)\) either returns a matrix \(X\) or its transpose, and \(S\) is a sketching operator.

FAQ

What are \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B)\) ?

Their shapes are defined implicitly by \((d, m, n, \texttt{opA}).\) Their precise contents are determined by \((A, \texttt{lda}),\) \((B, \texttt{ldb}),\) and “layout”, following the same convention as the Level 3 BLAS function “GEMM.”

Full parameter descriptions
layout - [in]
  • Either Layout::ColMajor or Layout::RowMajor

  • Matrix storage for \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B).\)

opS - [in]
  • Either Op::Trans or Op::NoTrans.

  • If \(\texttt{opS}\) = NoTrans, then \(\operatorname{op}(S) = S.\)

  • If \(\texttt{opS}\) = Trans, then \(\operatorname{op}(S) = S^T.\)

opA - [in]
  • If \(\texttt{opA}\) == NoTrans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A).\)

  • If \(\texttt{opA}\) == Trans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A)^T.\)

d - [in]
  • A nonnegative integer.

  • The number of rows in \(\operatorname{mat}(B)\)

  • The number of rows in \(\operatorname{op}(\operatorname{mat}(S)).\)

n - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{mat}(B)\)

  • The number of columns in \(\operatorname{op}(\operatorname{mat}(A)).\)

m - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{op}(S).\)

  • The number of rows in \(\operatorname{op}(\operatorname{mat}(A)).\)

alpha - [in]
  • A real scalar.

  • If zero, then \(A\) is not accessed.

S - [in]
  • A DenseSkOp or SparseSkOp object.

  • Defines \(\operatorname{submat}(S).\)

A - [in]
  • Pointer to a 1D array of real scalars.

  • Defines \(\operatorname{mat}(A).\)

lda - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(A)\) when reading from \(A.\)

beta - [in]
  • A real scalar.

  • If zero, then \(B\) need not be set on input.

B - [in,out]
  • Pointer to 1D array of real scalars.

  • On entry, defines \(\operatorname{mat}(B)\) on the RIGHT-hand side of \((\star).\)

  • On exit, defines \(\operatorname{mat}(B)\) on the LEFT-hand side of \((\star).\)

ldb - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(B)\) when reading from \(B.\)

template<typename T, typename SKOP>
inline void RandBLAS::sketch_general(blas::Layout layout, blas::Op opA, blas::Op opS, int64_t m, int64_t d, int64_t n, T alpha, const T *A, int64_t lda, SKOP &S, T beta, T *B, int64_t ldb)

Sketch from the right in a GEMM-like operation

\[\operatorname{mat}(B) = \alpha \cdot \underbrace{\operatorname{op}(\operatorname{mat}(A))}_{m \times n} \cdot \underbrace{\operatorname{op}(S)}_{n \times d} + \beta \cdot \underbrace{\operatorname{mat}(B)}_{m \times d}, \tag{$\star$}\]

where \(\alpha\) and \(\beta\) are real scalars, \(\operatorname{op}(X)\) either returns a matrix \(X\) or its transpose, and \(S\) is a sketching operator.

FAQ

What are \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B)\) ?

Their shapes are defined implicitly by \((m, d, n, \texttt{opA}).\) Their precise contents are determined by \((A, \texttt{lda}),\) \((B, \texttt{ldb}),\) and “layout”, following the same convention as the Level 3 BLAS function “GEMM.”

Full parameter descriptions
layout - [in]
  • Either Layout::ColMajor or Layout::RowMajor

  • Matrix storage for \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B).\)

opA - [in]
  • If \(\texttt{opA}\) == NoTrans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A).\)

  • If \(\texttt{opA}\) == Trans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A)^T.\)

opS - [in]
  • Either Op::Trans or Op::NoTrans.

  • If \(\texttt{opS}\) = NoTrans, then \(\operatorname{op}(S) = S.\)

  • If \(\texttt{opS}\) = Trans, then \(\operatorname{op}(S) = S^T.\)

m - [in]
  • A nonnegative integer.

  • The number of rows in \(\operatorname{mat}(B).\)

  • The number of rows in \(\operatorname{op}(\operatorname{mat}(A)).\)

d - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{mat}(B).\)

  • The number of columns in \(\operatorname{op}(\operatorname{mat}(S)).\)

n - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{op}(\operatorname{mat}(A)).\)

  • The number of rows in \(\operatorname{op}(S).\)

alpha - [in]
  • A real scalar.

  • If zero, then \(A\) is not accessed.

A - [in]
  • Pointer to a 1D array of real scalars.

  • Defines \(\operatorname{mat}(A).\)

lda - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(A)\) when reading from \(A.\)

S - [in]
  • A DenseSkOp or SparseSkOp object.

beta - [in]
  • A real scalar.

  • If zero, then \(B\) need not be set on input.

B - [in,out]
  • Pointer to 1D array of real scalars.

  • On entry, defines \(\operatorname{mat}(B)\) on the RIGHT-hand side of \((\star).\)

  • On exit, defines \(\operatorname{mat}(B)\) on the LEFT-hand side of \((\star).\)

ldb - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(B)\) when reading from \(B.\)

Full matrix-vector operations

template<typename T, typename SKOP>
inline void RandBLAS::sketch_vector(blas::Op opS, T alpha, SKOP &S, const T *x, int64_t incx, T beta, T *y, int64_t incy)

Perform a GEMV-like operation:

\[\operatorname{mat}(y) = \alpha \cdot \operatorname{op}(S) \cdot \operatorname{mat}(x) + \beta \cdot \operatorname{mat}(y), \tag{$\star$}\]

where \(\alpha\) and \(\beta\) are real scalars and \(S\) is a sketching operator.

FAQ

What are \(\operatorname{mat}(x)\) and \(\operatorname{mat}(y)\) ?

Their shapes are defined as tall vectors of dimension \((\operatorname{mat}(x), L_x \times 1),\) \((\operatorname{mat}(y), L_y \times 1),\) where \((L_x, L_y)\) are lengths so that \(\texttt{opS}(S) \operatorname{mat}(x)\) is well-defined and the same shape as \(\operatorname{mat}(y).\) Their precise contents are determined in a way that is identical to the Level 2 BLAS function “GEMV.”

Why no “layout” argument?

The GEMV in CBLAS accepts a parameter that specifies row-major or column-major layout of the matrix. Since our matrix is a sketching operator, and since RandBLAS has no notion of the layout of a sketching operator, we do not have a layout parameter.

Full parameter descriptions
opS - [in]
  • Either Op::Trans or Op::NoTrans.

  • If \(\texttt{opS}\) = NoTrans, then \(\operatorname{op}(S) = S.\)

  • If \(\texttt{opS}\) = Trans, then \(\operatorname{op}(S) = S^T.\)

alpha - [in]
  • A real scalar.

  • If zero, then \(x\) is not accessed.

S - [in]
  • A DenseSkOp or SparseSkOp object.

x - [in]
  • Pointer to a 1D array of real scalars.

  • Defines \(\operatorname{mat}(x).\)

incx - [in]
  • A positive integer.

  • Stride between elements of x.

beta - [in]
  • A real scalar.

  • If zero, then \(y\) need not be set on input.

y - [in, out]
  • Pointer to 1D array of real scalars.

  • On entry, defines \(\operatorname{mat}(y)\) on the RIGHT-hand side of \((\star).\)

  • On exit, defines \(\operatorname{mat}(y)\) on the LEFT-hand side of the same.

incy - [in]
  • A positive integer.

  • Stride between elements of y.

Submatrix operations

template<typename T, typename SKOP>
inline void RandBLAS::sketch_general(blas::Layout layout, blas::Op opS, blas::Op opA, int64_t d, int64_t n, int64_t m, T alpha, SKOP &S, int64_t ro_s, int64_t co_s, const T *A, int64_t lda, T beta, T *B, int64_t ldb)

Sketch from the left in a GEMM-like operation

\[\operatorname{mat}(B) = \alpha \cdot \underbrace{\operatorname{op}(\operatorname{submat}(S))}_{d \times m} \cdot \underbrace{\operatorname{op}(\operatorname{mat}(A))}_{m \times n} + \beta \cdot \underbrace{\operatorname{mat}(B)}_{d \times n}, \tag{$\star$}\]

where \(\alpha\) and \(\beta\) are real scalars, \(\operatorname{op}(X)\) either returns a matrix \(X\) or its transpose, and \(S\) is a sketching operator.

FAQ

What are \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B)\) ?

Their shapes are defined implicitly by \((d, m, n, \texttt{opA}).\) Their precise contents are determined by \((A, \texttt{lda}),\) \((B, \texttt{ldb}),\) and “layout”, following the same convention as GEMM from BLAS.

If layout == ColMajor, then

\[\operatorname{mat}(A)[i, j] = A[i + j \cdot \texttt{lda}].\]

In this case, \(\texttt{lda}\) must be \(\geq\) the length of a column in \(\operatorname{mat}(A).\)

If layout == RowMajor, then

\[\operatorname{mat}(A)[i, j] = A[i \cdot \texttt{lda} + j].\]

In this case, \(\texttt{lda}\) must be \(\geq\) the length of a row in \(\operatorname{mat}(A).\)

What is \(\operatorname{submat}(S)\) ?

Its shape is defined implicitly by \((\texttt{opS}, d, m).\)

If \({\operatorname{submat}(S)}\) is of shape \(r \times c,\) then it is the \(r \times c\) submatrix of \({S}\) whose upper-left corner appears at index \((\texttt{ro_s}, \texttt{co_s})\) of \({S}.\)

Full parameter descriptions
layout - [in]
  • Either Layout::ColMajor or Layout::RowMajor

  • Matrix storage for \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B).\)

opS - [in]
  • Either Op::Trans or Op::NoTrans.

  • If \(\texttt{opS}\) = NoTrans, then \(\operatorname{op}(\operatorname{submat}(S)) = \operatorname{submat}(S).\)

  • If \(\texttt{opS}\) = Trans, then \(\operatorname{op}(\operatorname{submat}(S)) = \operatorname{submat}(S)^T.\)

opA - [in]
  • If \(\texttt{opA}\) == NoTrans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A).\)

  • If \(\texttt{opA}\) == Trans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A)^T.\)

d - [in]
  • A nonnegative integer.

  • The number of rows in \(\operatorname{mat}(B)\)

  • The number of rows in \(\operatorname{op}(\operatorname{submat}(S)).\)

n - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{mat}(B)\)

  • The number of columns in \(\operatorname{op}(\operatorname{mat}(A)).\)

m - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{op}(\operatorname{submat}(S))\)

  • The number of rows in \(\operatorname{op}(\operatorname{mat}(A)).\)

alpha - [in]
  • A real scalar.

  • If zero, then \(A\) is not accessed.

S - [in]
  • A DenseSkOp or SparseSkOp object.

  • Defines \(\operatorname{submat}(S).\)

ro_s - [in]
  • A nonnegative integer.

  • The rows of \(\operatorname{submat}(S)\) are a contiguous subset of rows of \(S.\)

  • The rows of \(\operatorname{submat}(S)\) start at \(S[\texttt{ro_s}, :].\)

co_s - [in]
  • A nonnnegative integer.

  • The columns of \(\operatorname{submat}(S)\) are a contiguous subset of columns of \(S.\)

  • The columns of \(\operatorname{submat}(S)\) start at \(S[:,\texttt{co_s}].\)

A - [in]
  • Pointer to a 1D array of real scalars.

  • Defines \(\operatorname{mat}(A).\)

lda - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(A)\) when reading from \(A.\)

beta - [in]
  • A real scalar.

  • If zero, then \(B\) need not be set on input.

B - [in,out]
  • Pointer to 1D array of real scalars.

  • On entry, defines \(\operatorname{mat}(B)\) on the RIGHT-hand side of \((\star).\)

  • On exit, defines \(\operatorname{mat}(B)\) on the LEFT-hand side of \((\star).\)

ldb - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(B)\) when reading from \(B.\)

template<typename T, typename SKOP>
inline void RandBLAS::sketch_general(blas::Layout layout, blas::Op opA, blas::Op opS, int64_t m, int64_t d, int64_t n, T alpha, const T *A, int64_t lda, SKOP &S, int64_t ro_s, int64_t co_s, T beta, T *B, int64_t ldb)

Sketch from the right in a GEMM-like operation

\[\operatorname{mat}(B) = \alpha \cdot \underbrace{\operatorname{op}(\operatorname{mat}(A))}_{m \times n} \cdot \underbrace{\operatorname{op}(\operatorname{submat}(S))}_{n \times d} + \beta \cdot \underbrace{\operatorname{mat}(B)}_{m \times d}, \tag{$\star$}\]

where \(\alpha\) and \(\beta\) are real scalars, \(\operatorname{op}(X)\) either returns a matrix \(X\) or its transpose, and \(S\) is a sketching operator.

FAQ

What are \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B)\) ?

Their shapes are defined implicitly by \((m, d, n, \texttt{opA}).\) Their precise contents are determined by \((A, \texttt{lda}),\) \((B, \texttt{ldb}),\) and “layout”, following the same convention as the Level 3 BLAS function “GEMM.”

What is \(\operatorname{submat}(S)\) ?

Its shape is defined implicitly by \((\texttt{opS}, n, d).\) If \({\operatorname{submat}(S)}\) is of shape \(r \times c,\) then it is the \(r \times c\) submatrix of \({S}\) whose upper-left corner appears at index \((\texttt{ro_s}, \texttt{co_s})\) of \({S}.\)

Full parameter descriptions
layout - [in]
  • Either Layout::ColMajor or Layout::RowMajor

  • Matrix storage for \(\operatorname{mat}(A)\) and \(\operatorname{mat}(B).\)

opA - [in]
  • If \(\texttt{opA}\) == NoTrans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A).\)

  • If \(\texttt{opA}\) == Trans, then \(\operatorname{op}(\operatorname{mat}(A)) = \operatorname{mat}(A)^T.\)

opS - [in]
  • Either Op::Trans or Op::NoTrans.

  • If \(\texttt{opS}\) = NoTrans, then \(\operatorname{op}(\operatorname{submat}(S)) = \operatorname{submat}(S).\)

  • If \(\texttt{opS}\) = Trans, then \(\operatorname{op}(\operatorname{submat}(S)) = \operatorname{submat}(S)^T.\)

m - [in]
  • A nonnegative integer.

  • The number of rows in \(\operatorname{mat}(B).\)

  • The number of rows in \(\operatorname{op}(\operatorname{mat}(A)).\)

d - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{mat}(B)\)

  • The number of columns in \(\operatorname{op}(\operatorname{submat}(S)).\)

n - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{op}(\operatorname{mat}(A)).\)

  • The number of rows in \(\operatorname{op}(\operatorname{submat}(S)).\)

alpha - [in]
  • A real scalar.

  • If zero, then \(A\) is not accessed.

A - [in]
  • Pointer to a 1D array of real scalars.

  • Defines \(\operatorname{mat}(A).\)

lda - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(A)\) when reading from \(A.\)

S - [in]
  • A DenseSkOp or SparseSkOp object.

  • Defines \(\operatorname{submat}(S).\)

  • Defines \(\operatorname{submat}(S).\)

ro_s - [in]
  • A nonnegative integer.

  • The rows of \(\operatorname{submat}(S)\) are a contiguous subset of rows of \(S.\)

  • The rows of \(\operatorname{submat}(S)\) start at \(S[\texttt{ro_s}, :].\)

co_s - [in]
  • A nonnegative integer.

  • The columns of \(\operatorname{submat}(S)\) are a contiguous subset of columns of \(S.\)

  • The columns \(\operatorname{submat}(S)\) start at \(S[:,\texttt{co_s}].\)

beta - [in]
  • A real scalar.

  • If zero, then \(B\) need not be set on input.

B - [in,out]
  • Pointer to 1D array of real scalars.

  • On entry, defines \(\operatorname{mat}(B)\) on the RIGHT-hand side of \((\star).\)

  • On exit, defines \(\operatorname{mat}(B)\) on the LEFT-hand side of \((\star).\)

ldb - [in]
  • A nonnegative integer.

  • Leading dimension of \(\operatorname{mat}(B)\) when reading from \(B.\)

template<typename T, typename SKOP>
inline void RandBLAS::sketch_vector(blas::Op opS, int64_t d, int64_t m, T alpha, SKOP &S, int64_t ro_s, int64_t co_s, const T *x, int64_t incx, T beta, T *y, int64_t incy)

Perform a GEMV-like operation. If \({\texttt{opS}} = \texttt{NoTrans},\) then we perform

\[\operatorname{mat}(y) = \alpha \cdot \underbrace{\operatorname{submat}(S)}_{d \times m} \cdot \underbrace{\operatorname{mat}(x)}_{m \times 1} + \beta \cdot \underbrace{\operatorname{mat}(y)}_{d \times 1}, \tag{$\star$}\]

otherwise, we perform

\[\operatorname{mat}(y) = \alpha \cdot \underbrace{\operatorname{submat}(S)^T}_{m \times d} \cdot \underbrace{\operatorname{mat}(x)}_{d \times 1} + \beta \cdot \underbrace{\operatorname{mat}(y)}_{m \times 1}, \tag{$\diamond$}\]

where \(\alpha\) and \(\beta\) are real scalars and \(S\) is a sketching operator.

FAQ

What are \(\operatorname{mat}(x)\) and \(\operatorname{mat}(y)\) ?

Their shapes are defined as tall vectors of dimension \((\operatorname{mat}(x), L_x \times 1),\) \((\operatorname{mat}(y), L_y \times 1),\) where \((L_x, L_y)\) are lengths so that \(\texttt{opS}(\operatorname{submat}(S)) \operatorname{mat}(x)\) is well-defined and the same shape as \(\operatorname{mat}(y).\) Their precise contents are determined in a way that is identical to the Level 2 BLAS function “GEMV.”

Why no “layout” argument?

The GEMV in CBLAS accepts a parameter that specifies row-major or column-major layout of the matrix. Since our matrix is a sketching operator, and since RandBLAS has no notion of the layout of a sketching operator, we do not have a layout parameter.

Full parameter descriptions
opS - [in]
  • Either Op::Trans or Op::NoTrans.

  • If \(\texttt{opS}\) = NoTrans, then \(\operatorname{op}(\operatorname{submat}(S)) = \operatorname{submat}(S).\)

  • If \(\texttt{opS}\) = Trans, then \(\operatorname{op}(\operatorname{submat}(S)) = \operatorname{submat}(S)^T.\)

d - [in]
  • A nonnegative integer.

  • The number of rows in \(\operatorname{submat}(S).\)

m - [in]
  • A nonnegative integer.

  • The number of columns in \(\operatorname{submat}(S).\)

alpha - [in]
  • A real scalar.

  • If zero, then \(x\) is not accessed.

S - [in]
  • A DenseSkOp or SparseSkOp object.

  • Defines \(\operatorname{submat}(S).\)

ro_s - [in]
  • A nonnegative integer.

  • \(\operatorname{submat}(S)\) is a contiguous submatrix of \(S[\texttt{ro_s}:(\texttt{ro_s} + d), :].\)

co_s - [in]
  • A nonnegative integer.

  • \(\operatorname{submat}(S)\) is a contiguous submatrix of \(S[:,\texttt{co_s}:(\texttt{co_s} + m)].\)

x - [in]
  • Pointer to a 1D array of real scalars.

  • Defines \(\operatorname{mat}(x).\)

incx - [in]
  • A positive integer.

  • Stride between elements of x.

beta - [in]
  • A real scalar.

  • If zero, then \(y\) need not be set on input.

y - [in, out]
  • Pointer to 1D array of real scalars.

  • On entry, defines \(\operatorname{mat}(y)\) on the RIGHT-hand side of \((\star)\) (if \(\texttt{opS} = \texttt{NoTrans}\)) or \((\diamond)\) (if \(\texttt{opS} = \texttt{Trans}\))

  • On exit, defines \(\operatorname{mat}(y)\) on the LEFT-hand side of the same.

incy - [in]
  • A positive integer.

  • Stride between elements of y.