Matlab crashes when Intel MKL's function Pardiso is called

22 views (last 30 days)
Hello everyone!
I am currently working with Matlab R2021b on Ubuntu 22.04. I am writing a program that will solve linear sparse system of equations, and I am trying to solve it with Pardiso function from Intel MKL library.
I have successfully compiled Mex file in C (that is calling Pardiso function); however when I call Mex file, Matlab crashes without any output or message.
I am using GCC to compile my Mex file. This is the code for compilation:
mex CFLAGS="\$CFLAGS -m64" LDFLAGS="\$LDFLAGS -m64" pardiso_sparse.c -I/opt/intel/oneapi/mkl/2024.2/include -L/opt/intel/oneapi/mkl/2024.2/lib -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl
Here is the Mex file itself. It is quite short:
#include "mex.h"
#include "math.h"
#include "matrix.h"
#include "stdint.h"
#include "mkl.h"
/*#define PARDISO_MAX_SIZE 64*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *vals, *RG;
int32_t *rows, *cols;
int32_t n;
// input
vals = mxGetPr(prhs[0]);
rows = (int32_t*) mxGetData(prhs[1]);
cols = (int32_t*) mxGetData(prhs[2]);
RG = mxGetPr(prhs[3]);
n = (int32_t) mxGetScalar(prhs[4]);
// Create the output solution vector x (n x 1)
plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL);
double *sol = mxGetPr(plhs[0]);
// PARDISO control parameters
void *pt[64] = {0}; // Internal solver memory pointer
MKL_INT iparm[64] ; // Control parameters
MKL_INT maxfct = 1, mnum = 1, phase, error = 0, msglvl = 1;
MKL_INT mtype = 2; // Real symmetric matrix
MKL_INT n_mkl = (MKL_INT)n;
MKL_INT nrhs_mkl = 1; // Number of right-hand sides (for solving Ax=b)
MKL_INT idum; // Dummy integer used for PARDISO
double ddum;
// Initialize PARDISO control parameters to default
iparm[0] = 0; // Use default PARDISO parameters
// Step 1: Reordering and symbolic factorization
printf("Analysis is running!");
phase = 11;
pardiso(pt, &maxfct, &mnum, &mtype, &phase, &n_mkl, vals, (MKL_INT *)rows, (MKL_INT *)cols, &idum, &nrhs_mkl,
iparm, &msglvl, NULL, NULL, &error);
if (error != 0) {
mexErrMsgIdAndTxt("MATLAB:mexfile:pardisoError", "Error during symbolic factorization: %d", error);
}
printf("First phase done!");
// Step 2: Numerical factorization
phase = 22;
pardiso(pt, &maxfct, &mnum, &mtype, &phase, &n_mkl, vals, (MKL_INT *)rows, (MKL_INT *)cols, &idum, &nrhs_mkl,
iparm, &msglvl, &ddum, &ddum, &error);
if (error != 0) {
mexErrMsgIdAndTxt("MATLAB:mexfile:pardisoError", "Error during numerical factorization: %d", error);
}
printf("Second phase done!");
// Step 3: Solve and iterative refinement
phase = 33;
pardiso(pt, &maxfct, &mnum, &mtype, &phase, &n_mkl, vals, (MKL_INT *)rows, (MKL_INT *)cols, &idum, &nrhs_mkl,
iparm, &msglvl, RG, sol, &error);
if (error != 0) {
mexErrMsgIdAndTxt("MATLAB:mexfile:pardisoError", "Error during solution: %d", error);
}
printf("Thrid phase done!");
// Step 4: Release internal memory
phase = -1;
pardiso(pt, &maxfct, &mnum, &mtype, &phase, &n_mkl, &ddum, (MKL_INT *)rows, (MKL_INT *)cols, &idum, &nrhs_mkl,
iparm, &msglvl, &ddum, &ddum, &error);
}
Can anyone give me a solution to this? I've been trying to run this code for more than a week!
Ante

Answers (2)

Shivam Lahoti
Shivam Lahoti on 10 Mar 2025
Edited: Shivam Lahoti on 10 Mar 2025
Hi Ante,
I understand you are encountering a matlab crash when you are trying to call the mex file that you have compiled. The crash could be due to multiple reasons and the crash log would surely help you point to the cause of the same.
However, this could be caused due to a comman error "Invalid MEX File". Please refer to the following community posts for more details to understand possible solutions to resolve this issue:
Possible reasons for failure include:
  • MATLAB version incompatibility. For more information, see MEX Version Compatibility.
  • Missing compiler run-time libraries. If your system does not have the same compiler that built the MEX file, see the Microsoft® MSDN® website for information about Visual C++® Redistributable Packages.
  • Missing or incorrectly installed specialized run-time libraries. Contact your MEX file or library vendor.
I hope this would be helpful.

James Tursa
James Tursa on 21 Mar 2025
Edited: James Tursa on 21 Mar 2025
Just seeing this post. Too late for OP, but for future readers here are my comments:
----------------------------------------
First comment is that there is no argument checking, either for number of arguments or for type, size, complexity, sparsity, etc. So any little mismatch can cause a MATLAB crash.
----------------------------------------
int32_t *rows, *cols;
:
rows = (int32_t*) mxGetData(prhs[1]);
cols = (int32_t*) mxGetData(prhs[2]);
This requires that the user pass in the 2nd and 3rd arguments as int32 variables or you will get an incorrect result and potential MATLAB crash. Again pointing out the need for input argument checks.
----------------------------------------
pardiso(pt, &maxfct, &mnum, &mtype, &phase, &n_mkl, vals, (MKL_INT *)rows, (MKL_INT *)cols, &idum, &nrhs_mkl,
iparm, &msglvl, NULL, NULL, &error);
This raises major alarm bells, particularly the (MLK_INT *)row and (MLK_INT *)cols casts. Assuming that the user actually passed in int32 data and that rows and cols correctly point to this data, you can't suddenly turn this data into different length integer data just by applying a cast like this. E.g., if MLK_INT happens to be a 64-bit integer, the underlying data is still 32-bit integers. So this mismatch will cause major problems, most likely a run off the end of valid data resulting in a seg fault and MATLAB crash. The proper way to do this is to create a new memory block for 64-bit integers and copy the 32-bit integer data into the 64-bit integer data block. Then free the 64-bit data block after you are done with it.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!