This is my first time working with the MKL library. After referring to the official documentation, I obtained the latest version of the MKL library via the apt package manager in the WSL2 Ubuntu system. I correctly configured the header and library paths in the CMake file. Then, I tested cblas_dgemv
first and it worked fine. However, when I switched to using the CSR-related interfaces, I encountered an unusual error during runtime: the process terminated without any error message, and the exit code was 139. I will present the C++ code and CMake configuration I used for testing below:
#include <stdio.h>
#include <stdlib.h>
#include "mkl.h"
#define N 4 // Matrix dimension
#define NNZ 6 // Number of non-zero elements
// Auxiliary function: print a vector
void print_vector(const char* desc, int n, double* vec) {
int i;
printf("%sn", desc);
for (i = 0; i < n; i++) {
printf("%f ", vec[i]);
}
printf("n");
}
int main() {
// Matrix and vector dimensions
MKL_INT n = N; // Matrix dimension
MKL_INT nnz = NNZ; // Number of non-zero elements
// CSR matrix data: values, columns, rowIndex
double values[NNZ] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; // Non-zero elements
MKL_INT columns[NNZ] = {0, 1, 2, 1, 2, 3}; // Column indices of non-zero elements
MKL_INT rowIndex[N + 1] = {0, 2, 3, 4, 6}; // Row start indices
// Input vector (size N)
double x[N] = {1.0, 1.0, 1.0, 1.0}; // Assume all elements are 1
// Output vector (where results will be stored)
double y[N] = {0.0, 0.0, 0.0, 0.0};
// Create sparse_matrix_t object
sparse_matrix_t A;
sparse_status_t status;
// Create CSR format matrix
status = mkl_sparse_d_create_csr(&A, SPARSE_INDEX_BASE_ZERO, n, n, rowIndex, rowIndex +1, columns, values);
if (status != SPARSE_STATUS_SUCCESS) {
printf("Error creating sparse matrix: %dn", status);
return 1;
}
// Perform matrix-vector multiplication
double alpha = 1.0, beta = 0.0;
// Create matrix descriptor and explicitly assign values
matrix_descr descr{};
descr.type = SPARSE_MATRIX_TYPE_GENERAL; // Matrix type is general (non-symmetric)
// Perform matrix-vector multiplication
status = mkl_sparse_d_mv(SPARSE_OPERATION_NON_TRANSPOSE, alpha, A, descr, x, beta, y);
if (status != SPARSE_STATUS_SUCCESS) {
printf("Error in matrix-vector multiplication: %dn", status);
return 1;
}
// Print result
print_vector("Resulting vector y =", N, y);
// Release resources
mkl_sparse_destroy(A);
return 0;
}
Next is my CMake configuration:
cmake_minimum_required(VERSION 3.27)
project(testMKL)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-O3")
# Include header files
include_directories(/opt/intel/oneapi/mkl/2025.0/include)
# Link library directories
link_directories(/opt/intel/oneapi/mkl/2025.0/lib)
add_executable(testMKL main.cpp)
# Link the specific libraries: libmkl_rt
target_link_libraries(testMKL -m64 -L /opt/intel/oneapi/mkl/2025.0/lib -Wl,--no-as-needed -lmkl_intel_ilp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl)
Finally, here is a screenshot about the error message I encountered during debugging:
It seems like there is an issue with the mkl_avx2
library, but my computer actually supports AVX instructions. How can I resolve this? I could not find official test code. The code above is written based on my own understanding. If there are any official versions of debugging and testing code or more specific code examples, I would appreciate it if someone could share them with me.