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

Issues compiling MATLAB mex with Intel MKL PARDISO

Zaluski__Dylan
Beginner
682 Views

I am quite new to writing mex files, and to the MKL. I need to interface the PARDISO solver with a MATLAB code of mine, for solving of large finite element matrices. When compiling my mex code, I am having some issues. MATLAB gives me the error message:

Error using mex
   Creating library fem_mex_pardiso.lib and object fem_mex_pardiso.exp
fem_mex_pardiso.obj : error LNK2019: unresolved external symbol pardiso referenced in function mexFunction
fem_mex_pardiso.mexw64 : fatal error LNK1120: 1 unresolved externals

This occurs when I submit the command

mex -R2018a fem_mex_pardiso.c -I'C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019.2.190\windows\mkl\include\'

I am sure that it is that I am not linking something properly, but I honestly am quite new to interfacing MATLAB with external libraries.

Here is the c - code that gives the errors:

/*==========================================================
 * fem_mex_pardiso.ca
 *==========================================================*/
 
 // Include headers
 #include "mex.h"
 #include "mkl.h"
 #include "mkl_pardiso.h"
 #include "mkl_types.h"

 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
 {
    // Define variables
    double *a, *rf_glob, *u_qct, ddum;
    int num_rows, i;
    void *pt[64];
    MKL_INT *ia, *ja, n, iparm[64], maxfct, mnum, phase, error, msglvl, mtype, idum;
    
    // Argument checks
    if (nlhs > 1)
    {
        mexErrMsgTxt("Too many outputs!");
    }
    if (nrhs != 4) // Should be 3 matrices for the CSR3 format of k_glob, and one column vector for rf_glob
    {
        mexErrMsgTxt("Need four inputs! (3 vectors for the sparse stiffness matrix, and one for the load vector)");
    }
    
    // Get the sparse matrix inputs
    ia = mxGetInt32s(prhs[0]); // Contains the indices of the first non-zero element in each row of a. Note, the last index of ia is the number of non-zero elements in a, plus one
    ja = mxGetInt32s(prhs[1]); // Contains the global column indices of every element in a
    a = mxGetDoubles(prhs[2]); // Non-zero elements of stiffness matrix
    
    // Number of equations is equal to length of the reaction force vector
    rf_glob = mxGetDoubles(prhs[3]);
    num_rows = mxGetM(prhs[3]);
    n = num_rows;
    
    // Configure out-of-core settings
    //setenv('MKL_PARDISO_OOC_PATH', 'C:\Users\dylan\Documents\School\Masters\01_Main_MSc\02_Codes\13_INTEL_MKL_Matlab\OutOfCore_Memory\ooc_file');
    //setenv('MKL_PARDISO_OOC_MAX_CORE_SIZE', '12288');
    //setenv('MKL_PARDISO_OOC_MAX_SWAP_SIZE', '8192');
    //setenv('MKL_PARDISO_OOC_KEEP_FILE', '0');
    
    // Setup solver
        // Iniitialize values for solver parameters
    maxfct = 1; // Maximal number of factors in memory
    mnum = 1; // Number of matrices to solve
    mtype = -2; // Matrix type (Here, real and symmetric positive indefinite. For real and symmetric positive definite, use 2)
    // Initialize the parameters
    for (i = 0; i < 64; i++)
    {
        iparm = 0;
    }    
    iparm[0] = 1; // Not using default solver parameters
    iparm[1] = 3; // Fill-in reducing ordering from METIS, parallelized with OpenMP
    iparm[3] = 0; // Not using an iterative algorithm
    iparm[4] = 0; // Not using a user-specified fill-in reducing permutation
    iparm[5] = 0; // Write solution into u_qct
    iparm[7] = 2; // Maximum number of iterative refinement steps
    iparm[9] = 13; // Perturb pivot elements by 1E-13 to deal with near-zero or zero pivots
    iparm[10] = 0; // No scaling (because we will be reording to a symmetric matrix)
    iparm[12] = 0; // No symmetric weighted matching
    iparm[13] = 0; // Zero perturbed pivots
    iparm[17] = -1; // Number of non-zeros in the factor LU
    iparm[18] = -1; // Mfloprs for LU factorization
    iparm[19] = 0; // Number of CG iterations
    iparm[34] = 1; // Use C-style indexing for ia and ja arrays
    iparm[60] = 1; // Use out-of-core memory if out of RAM
    msglvl = 0; // Do not print statistical information file
    error = 0; // Initialize error flag
        // Initialize the solver pointer
    for (i = 0; i < 64; i++)
    {
        pt = 0;
    }    
    
    // Begin solving
    
    // Step 1 - Reordering and symbolic factorization
    phase = 11;
    PARDISO (pt, &maxfct, &mnum, &mtype, &phase, &n, a, ia, ja, &idum, &nrhs, iparm, &msglvl, &ddum, &ddum, &error);
    if (error != 0)
    {
        printf("\n ERROR during symbolic factorization: %d", error);
        exit(1);
    }
    printf("\nReordering completed ... ");
    printf("\nNumber of nonzeros in factors = %d", iparm[17]);
    printf("\nNumber of factorization MFLOPS = %d", iparm[18]);
 }

 

Any help is greatly appreciated. Thankyou!

0 Kudos
1 Reply
Gennady_F_Intel
Moderator
682 Views

Dylan, I guess, the cause of the problem with regard to how do you link this case.I would recommend you to look at this article (https://software.intel.com/en-us/articles/intel-math-kernel-library-intel-mkl-for-windows-using-intel-mkl-in-matlab-executable-mex-files) .This is pretty obsolete (we should update the article. tbd) but you may catch some info how to link and call..

0 Kudos
Reply