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?
dyldknows 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"]. – ilian Dec 14 '15 at 02:11SetEnvironment[]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::libloaderror. – mmal Dec 14 '15 at 11:36DYLD_LIBRARY_PATHis changed as indicated before Mathematica is started looks to me like now Mathematica itself will also find and use your versions of mkl libraries, which could cause problems in internal functionality. I'd check whether that's the case. If it is the case I'd feel safer try to use the mkl libraries versions provided with Mathematica for my own functions than force Mathematica to use another version internally... – Albert Retey Dec 15 '15 at 06:30libWolframEngine.dylib. – ilian Dec 15 '15 at 15:02/Applications/Mathematica.app/Contents/SystemFiles/Libraries/MacOSX-x86-64/(libmkl_avx.dylib, $\ldots$,libmkl_intel_ilp64.dylib, $\ldots$). – mmal Dec 15 '15 at 17:18