diff --git a/src/main.c b/src/main.c index dd936d3..da883bd 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ typedef float F32; typedef double F64; typedef int32_t B32; +static int g_spmv_runs = 16; typedef struct CSRMatrix CSRMatrix; struct CSRMatrix { @@ -303,12 +304,73 @@ static void free_csr(CSRMatrix *A) { A->nnz = 0; } +static sparse_matrix_t csr_to_mkl_handle(const CSRMatrix *A) { + sparse_matrix_t H = NULL; + + // oneMKL CSR creation takes row_start and row_end arrays. + // With standard CSR row_ptr, these are row_ptr[i] and row_ptr[i+1]. + sparse_status_t st = mkl_sparse_d_create_csr( + &H, + SPARSE_INDEX_BASE_ZERO, + A->rows, + A->cols, + A->row_ptr, + A->row_ptr + 1, + A->col_ind, + A->values + ); + if (st != SPARSE_STATUS_SUCCESS) panic("mkl_sparse_d_create_csr failed"); + + return H; +} + +static void example_spmv(const CSRMatrix *A) { + sparse_matrix_t H = csr_to_mkl_handle(A); + + struct matrix_descr descr; + descr.type = SPARSE_MATRIX_TYPE_GENERAL; + descr.mode = SPARSE_FILL_MODE_FULL; + descr.diag = SPARSE_DIAG_NON_UNIT; + + // Optional optimization path recommended by oneMKL. + mkl_sparse_set_mv_hint(H, SPARSE_OPERATION_NON_TRANSPOSE, descr, 1000); + mkl_sparse_optimize(H); + + double *x = (double *)xmalloc((size_t)A->cols * sizeof(double)); + double *y = (double *)calloc((size_t)A->rows, sizeof(double)); + if (!y) panic("out of memory"); + + for (MKL_INT i = 0; i < A->cols; i++) x[i] = 1.0; + + for (int i = 0; i < g_spmv_runs; i += 1) { + sparse_status_t st = mkl_sparse_d_mv( + SPARSE_OPERATION_NON_TRANSPOSE, + 1.0, + H, + descr, + x, + 0.0, + y + ); + if (st != SPARSE_STATUS_SUCCESS) panic("mkl_sparse_d_mv failed"); + } + + printf("SpMV done for %d runs. y[0] = %.6g\n", g_spmv_runs, (A->rows > 0 ? y[0] : 0.0)); + + free(x); + free(y); + mkl_sparse_destroy(H); +} + int main() { - const char *thermal2Path = "E:\\dev\\go_matmul_perf\\suitesparse_test_matrices\\FEM_3D_thermal2.mtx"; - printf("Reading market matrix %s \n", thermal2Path); + //const char *thermal2Path = "E:\\dev\\go_matmul_perf\\suitesparse_test_matrices\\FEM_3D_thermal2.mtx"; + const char *bigPath = "E:\\dev\\go_matmul_perf\\suitesparse_test_matrices\\nlpkkt200.mtx"; + printf("Reading market matrix %s \n", bigPath); CSRMatrix A; - A = read_matrix_market_to_csr(thermal2Path); + A = read_matrix_market_to_csr(bigPath); printf("Read matrix with size %d x %d and nnz = %d \n", A.rows, A.cols, A.nnz); + example_spmv(&A); + free_csr(&A);