Example: Convert MKL Sparse Matrix-Vector Multiplication with CSR Matrices to AOCL-Sparse - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English

MKL call

mkl_?csrmv(const char *transa, const MKL_INT *m, const MKL_INT *k, const float *alpha,
  const char *matdescra, const float *val, const MKL_INT *indx, const MKL_INT *pntrb,
  const MKL_INT *pntre, const float *x, const float *beta, float *y)

Steps to convert into an AOCL-Sparse call

aoclsparse_?mv(aoclsparse_operation op, const float *alpha, aoclsparse_matrix A,
  const aoclsparse_mat_descr descr, const float *x, const float *beta, float *y)
  1. Create a aoclsparse_matrix handle, A, using aoclsparse_create_?csr()

    // Create matrix A from m, k, val, indx, pntrb, pntre
    aoclsparse_matrix A;
    aoclsparse_index_base base = /* refer to Table 3 */;
    aoclsparse_int nnz;
    aoclsparse_int *row_ptr;
    aoclsparse_int *col_idx;
    aoclsparse_int i;
    aoclsparse_status status;
    
    nnz = pntre[*m-1] - pntrb[0];
    col_idx = (aoclsparse_int *) indx;
    
    // convert pntrb and pntre to row_ptr
    // assuming there is no gap between pntre[i] and pntrb[i+1]
    row_ptr[0] = base;
    for (i = 0; i < m; i++)
      row_ptr[i+1] = row_ptr[i] + pntre[i] - pntrb[i];
    
    status = aoclsparse_create_scsr(&A, base, *m, *n, nnz, row_ptr, col_idx, val);
    if (status != aoclsparse_status_success)
      return status;
    
  2. Create a aoclsparse_mat_descr, descr, and configure it using aoclsparse_set_mat_type(), aoclsparse_set_mat_fill_mode(), aoclsparse_set_mat_diag_type(), and aoclsparse_set_mat_index_base(). Refer to Table 11.5 for parameter mappings.

    // create descr
    aoclsparse_mat_descr descr;
    status = aoclsparse_create_mat_descr(&descr);
    if (status != aoclsparse_status_success)
      return status;
    
    // fill in descr based on the values of the matdescra pointers
    aoclsparse_matrix_type mat_type = /* refer to Table 3 */;
    aoclsparse_fill_mode fill = /* refer to Table 3 */;
    aoclsparse_diag_type diag = /* refer to Table 3 */;
    aoclsparse_set_mat_index_base(descr, base);
    aoclsparse_set_mat_type(descr, mat_type);
    aoclsparse_set_mat_fill_mode(descr, fill);
    aoclsparse_set_mat_diag_type(descr, diag);
    
  3. Call aoclsparse_?mv() for sparse matrix-vector multiplication

    // Map op to an appropriate value
    aoclsparse_operation op = /* refer to Table 3 */;
    
    // Call sparse matrix vector multiplication
    status = aoclsparse_smv(op, alpha, A, descr, x, beta, y);
    if (status != aoclsparse_status_success)
      return status;
    
  4. Cleanup using aoclsparse_destroy_mat_descr() and aoclsparse_destroy()

    // descriptor
    aoclsparse_destroy_mat_descr(descr);
    
    // matrix
    aoclsparse_destroy(&A);