MEX error: "The specified module could not be found."

25 views (last 30 days)
Hello, I would like to know how to address the following problem: MATLAB gives "The specified module could not be found." when I run a MEX file. Let us divide in a few quick sections my problem to explain it better.
  • Original code (stand alone, no MEX): I compiled with the Intel compiler using the following command on the intel oneAPI cmd:
ifort -O3 -Qmkl=parallel test.for -o test
When I run "test.exe", it produces the expected result
[MEX files] I compiled a simple gateway routine that calls the subroutines compiled above. I will show the steps I wook and the solutions I found so far:
  • PROMP T1:
mex test.f90
ERROR 1:
"error #7002: Error in opening the compiled module file. Check INCLUDE paths. [MKL_SERVICE]"
SOLUTION 1: Add complementary flag "-Qmkl"
  • PROMPT 2
mex COMPFLAGS='$COMPFLAGS -O2 -Qmkl' test.f90
I used the -O2 optimization flag here instead of -O3 (as in the first code snippet) because it seems MATLAB only supports that.
The previous error was solved, but...
ERROR 2:
"LINK : fatal error LNK1104: cannot open file 'mkl_intel_ilp64_dll.lib'"
SOLUTION 2: Include folder with .lib files.
  • PROMPT 3
mex COMPFLAGS='$COMPFLAGS -O2 -Qmkl' test.f90 '-LC:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\lib\intel64\'
Now, the code does compile (the added path contains the needed .lib files)!
ERROR 3: When I call the function in the form y = test(x), it throws the error:
"The specified module could not be found."
CAUSE OF ERROR 3: I debbuged line by line and found that the problem is when I calls the function pardiso (in the FORTRAN code):
CALL pardiso (pt,... other input arguments
SOLUTION 3 (not working yet): I googled this error, and there are many who had similar problems, but I could not make it work, some of which I show below:
  • This person had a similar problem, but without answers
  • The same person posted a solution, but it did not work for me (also, he used a previous version of the Intel compiler, with a slightly different syntax)
  • The accepted answer to this question suggested using Dependency Walker, which I tried, but my computer is taking over 1 hour to load everything with no success (should I keep trying this method?)
Lastly, I want to mention that I tried also using the flag "-I" to include the "include" folders that contain the "mkl_service" module, in the form:
mex COMPFLAGS='$COMPFLAGS -O2 -Qopenmp -Qmkl' ...
'-IC:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\include' ...
'-LC:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\lib\intel64' ...
'-IC:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\include\intel64\ilp64' ...
'-LC:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\lib\intel64\' ...
test.f90
But all yield the same problem (module could not be found).
I apologize for the long question and appreciate any support!

Accepted Answer

Vinicius Fontes
Vinicius Fontes on 5 Jan 2024
I found the solution to this problem and will share here for anyone else who find this problem.
The issue is essentially that, though I compiled correctly the MEX file, the .dll files needed to run pardiso are not found.
  • To be clear, during compilation, the .lib and .mod files are required to be included during compilation
  • This is done by adding the -Qmkl flag and telling the compiler where the libraries and modules are with "-L"
  • However, during execution, you still need to have the .dll accessible.
  • This can be done by simply adding the path that contain them to the environment variables
In this snippet of code, I restore the original path and add the path with the .dll needed for pardiso. Adjust accordingly to your situation:
restoredefaultpath
dllPath = 'C:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\redist\intel64';
currentPath = getenv('PATH');
setenv('PATH', [dllPath, pathsep, currentPath]);
I got this solution with the help of ChatGPT, but it did solve the problem (after many tries!).
Lastly, I would like to point out that using dependency walker (like other users suggested in this and other similar questions) may work, but it takes a LONG time to run. Try the aforementioned solution first!

More Answers (1)

Swaraj
Swaraj on 2 Jan 2024
Hi Vinicius
This error can be caused when there is a dependency issue.
To identify the missing library dependencies, please download "dependency walker" from the link below http://www.mathworks.com/help/matlab/matlab_external/invalid-mex-file-error.
I suggest using the "dependency walker", it can help in resolving the issue you are facing although it might take some time.
Also, after Simulink generates code and compiles executables for the first time, it makes temporary files for faster compilation the second time. In case it ran successfully once before, try deleting the generated files (YourModel_ert or YourModel_grt, slprj, and the mexed files) and run the process again.
Hope it helps!!
  2 Comments
Vinicius Fontes
Vinicius Fontes on 2 Jan 2024
Hi Swaraj, thank you for your reply.
The dependency walker takes too long to run on my laptop and I do not know if it works correctly here.
Also, I am not using Simulink, just a normal mex file run from a MATLAB script.
I am deleting the generated MEX file every time to make sure I am running the newly generated one.
My question is mainly if I am missing a flag on the mex command line, and/or which file should I be linking (is it the .mod module or something else?).
Thank you!
Vinicius Fontes
Vinicius Fontes on 5 Jan 2024
Hi @Swaraj, thank you again for your support. I found the solution and posted as an answer to this question for future reference. Best regards.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!