Reputation: 41
Can anyone give me an example of multiplying a sparse matrix by a vector using mkl_sparse_?_mv
? Specifically, how to define the sparse matrix A?
int main(int argc, char** argv) {
printf("Testing sparse blas\n");
sparse_operation_t operation = SPARSE_OPERATION_TRANSPOSE;
float alpha = 1.0;
float beta = 0.0;
sparse_matrix_type_t A_type = SPARSE_MATRIX_TYPE_GENERAL;
int m = (int) pow(2,3);
int n = (int) pow(2,5);
float * x = calloc(n,sizeof(float));
int i;
for (i=0;i<n;i++) {
x[i] = i*2+1;
}
float * y = calloc(m*n,sizeof(float));
// How to define A?
mkl_sparse_s_mv(operation, alpha, A, SPARSE_MATRIX_TYPE_GENERAL, x, beta, y);
return 0;
}
Upvotes: 1
Views: 1477
Reputation: 26342
Matrix A
should be created via one of mkl_sparse_?_create_?
routines. For example, if you have a CSR sparse matrix, you should call mkl_sparse_?_create_csr
to convert it into the internal representation (sparse_matrix_t
):
constexpr MKL_INT M = 5;
constexpr MKL_INT N = 5;
constexpr MKL_INT NNZ = 13;
float csrVal[NNZ] = { /* ... */ };
MKL_INT csrColInd[NNZ] = { /* ... */ };
MKL_INT csrRowPtr[M + 1] = { /* ... */ };
sparse_matrix_t csrA;
mkl_sparse_s_create_csr(&csrA, SPARSE_INDEX_BASE_ZERO, N, M,
csrRowPtr, csrRowPtr + 1, csrColInd, csrVal);
and then use it for mkl_sparse_s_mv
:
float x[N] = { /* ... */ };
float y[N] = { /* ... */ };
float alpha = /* ... */, beta = /* ... */;
matrix_descr descrA;
descrA.type = SPARSE_MATRIX_TYPE_GENERAL;
mkl_sparse_optimize(csrA);
mkl_sparse_s_mv(SPARSE_OPERATION_NON_TRANSPOSE, alpha, csrA, descrA, x, beta, y);
mkl_sparse_destroy(csrA);
This code was taken from examples provided within MKL distribution. You can find it in $MKLROOT/examples/examples_core_c.tgz/spblasc/source/sparse_csrmv.c
.
Upvotes: 2