I'm trying to load library funcitons to Mathematica which are using MKL functions but I'm constatly getting LibraryFunction::libload
on OSX (this works fine on Linux). I have simple C file (shorter version from here)
// test.c
#include <stdio.h>
#include <stdlib.h>
#include <WolframLibrary.h>
#include <mkl.h>
DLLEXPORT mint WolframLibrary_getVersion( ) {
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) {
return 0;
}
DLLEXPORT void WolframLibrary_uninitialize( WolframLibraryData libData) {
return;
}
DLLEXPORT int version(WolframLibraryData libData, mint argc, MArgument *args, MArgument res) {
char* buf = (char*)malloc(200*sizeof(char));
mkl_get_version_string(buf, 200);
MArgument_setUTF8String(res, buf);
return LIBRARY_NO_ERROR;
}
which is located in ~/tmp/MKLTest
. I'm using CreateLibrary
and LibraryFunctionLoad
to build dynamic library
SetDirectory["~/tmp/MKLTest"];
CreateLibrary[{"test.c"}, "test", "Debug" -> True,
"TargetDirectory" -> "~/tmp",
"IncludeDirectories" -> "/opt/intel/mkl/include",
"LibraryDirectories" -> {"/opt/intel/mkl/lib", "/opt/intel/lib"},
"CompileOptions" -> "-m64 -fPIC -lmkl_rt -lpthread -lm",
"Compiler" -> Automatic,
"ShellOutputFunction" -> Print,
"ShellCommandFunction" -> Print,
"CleanIntermediate" -> True, "CreateBinary" -> True,
"ExtraObjectFiles" -> {}]
(it creates test.dylib
file and gives no errors so I presume this step is correct) and I'm trying to load version
function
version = LibraryFunctionLoad["~/tmp/test", "version", {}, "UTF8String"]
This ends with failure and with message
LibraryFunction::libload: The function version was not loaded from the file /Users/user/tmp/test.dylib. >>
How to load to Mathematica functions linking to MKL with LibraryLink
?
dyld
knows where to look for the MKL libraries, so you may want to try something likeSetEnvironment[ "DYLD_LIBRARY_PATH" -> "/opt/intel/mkl/lib:/opt/intel/lib"]
. $\endgroup$SetEnvironment[]
does not work for me (why? it remainds me (39509)), but when I export it and run Mathematica from the command line$ export DYLD_LIBRARY_PATH=/opt/intel/mkl/lib:/opt/intel/lib && mathematica &
then the environment variable is defined (checked byGetEnvironment["DYLD_LIBRARY_PATH"]
) and only then I'm able to load my functions without getting theLibraryFunction::libload
error. $\endgroup$