Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7196 Discussions

Segmentation fault in LAPACKE_dgesv for large matrices

szp
Beginner
742 Views

Hello! I'm learning usage of MKL these days. When I tried to use LAPACKE_dgesv to solve linear equations, I find that it works for small matrices but fails for large matrices (segmentation fault (core dumped)). My CPU is AMD, operating system is Rockylinux 9, and OneMKL version is 2023.2.0. I have also tried 2024.1, but it has the same problem. I have also set ulimit to unlimit. Here is  my code.

#include <iostream>
#include <string>
#include <iomanip>
#include <random>
#include <mkl.h>

using std::cout;
using std::endl;
using std::string;
using std::setw;
using std::setprecision;
using std::fixed;

double generateRandomDouble(double min, double max) {
    static std::random_device rd;
    static std::mt19937 gen(rd());
    std::uniform_real_distribution<double> dis(min, max);
    return dis(gen);
}

int linsol(double *A,double *B, int lda, int ldb, int i) {
    lapack_int *ipiv = new lapack_int[lda];
    int info = 100;
    if (i == 0)
        info = LAPACKE_dgesv(LAPACK_ROW_MAJOR, lda, ldb, A, lda, ipiv, B, ldb);
    else {
        info = LAPACKE_dgesv(LAPACK_COL_MAJOR, lda, ldb, A, lda, ipiv, B, lda);
    }
    delete[] ipiv;
    return info;
}

void print(double *A, int row, int col) {
    for(int i = 0; i < row; ++i) {
        for(int j = 0; j < col; ++j)
            cout << setw(8) << fixed << setprecision(4) << A[i*col+j] << " ";
        cout << endl;
    }
            
}

int main(int argc, char *argv[])
{
    int n = std::stoi(argv[1]);
    double *matrix_a = (double*)mkl_malloc(n * n * sizeof(double), 64);
    double *matrix_b = (double*)mkl_malloc(n * n * sizeof(double), 64);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            matrix_a[i * n + j] = generateRandomDouble(0.0,1.0);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            matrix_b[i * n + j] = generateRandomDouble(0.0, 1.0);
    }

    print(matrix_a, n, n);
    cout << endl;
    print(matrix_b, n, n);
   
    cout << linsol(matrix_a, matrix_b, n, n, 1) << endl;
        
    print(matrix_b, n, n);

    mkl_free(matrix_a);
    mkl_free(matrix_b);
    return 0;    
}

 And my CMakelists.txt is as follows (MKLROOT has been set as environment variable).

cmake_minimum_required(VERSION 3.10)
project(mkl LANGUAGES CXX)
file(GLOB SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
set(CMAKE_CXX_STANDARD 17)
list(APPEND CMAKE_CXX_FLAGS -O3)
find_package(MKL REQUIRED)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(1 ${SRC})
target_link_libraries(1 iomp5 ${MKL_LIBRARIES})

The configuration is as follows.

-- The CXX compiler identification is GNU 11.4.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- MKL_ARCH: None, set to ` intel64` by default
-- MKL_LINK: None, set to ` dynamic` by default
-- MKL_INTERFACE_FULL: None, set to ` intel_ilp64` by default
-- MKL_THREADING: None, set to ` intel_thread` by default
-- Found MKL: /opt/intel/oneapi/mkl/2023.2.0
-- Found MKL: /opt/intel/oneapi/mkl/2023.2.0/lib/intel64/libmkl_intel_ilp64.so
-- Found MKL: /opt/intel/oneapi/mkl/2023.2.0/lib/intel64/libmkl_intel_thread.so
-- Found MKL: /opt/intel/oneapi/mkl/2023.2.0/lib/intel64/libmkl_core.so
-- Found MKL: /opt/intel/oneapi/compiler/latest/linux/compiler/lib/intel64_lin/libiomp5.so
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /path/to/my/dir

On my computer, when n >=25, it appears "Segmentation fault (core dumped)", and the returned value of LAPACKE_dgesv is not printed on the screen, so I think the error occurs in the function. So what is the problem? How to solve it? Very thanks!

0 Kudos
1 Solution
szp
Beginner
659 Views

Very thanks! The problem is solved after I link my program to libmkl_intel_lp64.so. 

View solution in original post

0 Kudos
2 Replies
Gennady_F_Intel
Moderator
668 Views

At the first glance the problem is that you link against ilp64 libraries (libmkl_intel_ilp64.so ) without using compiler option -DMKL_ILP64. Please check the MKL Linker adviser recommendation.

You might try to relink against libmkl_intel_lp64.so without modification you code and check how is will go.

 

0 Kudos
szp
Beginner
660 Views

Very thanks! The problem is solved after I link my program to libmkl_intel_lp64.so. 

0 Kudos
Reply