2

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?

mmal
  • 3,508
  • 2
  • 18
  • 38
  • 1
    I've not used OS X so I can't answer you, but I also can't help thinking that it's probably a good idea to link your own library statically with MKL to avoid the problem of your code and Mathematica both trying to load different versions of the same libraries into the same process. It might work, but it just sounds like asking for trouble. – Oleksandr R. Dec 14 '15 at 01:36
  • It should work fine provided dyld knows where to look for the MKL libraries, so you may want to try something like SetEnvironment[ "DYLD_LIBRARY_PATH" -> "/opt/intel/mkl/lib:/opt/intel/lib"]. – ilian Dec 14 '15 at 02:11
  • @ilian Thank you but it isn't working, which is confusing to me. – mmal Dec 14 '15 at 08:39
  • 1
    @ilian You are right, its a linking issue. Setting environment variable with 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 by GetEnvironment["DYLD_LIBRARY_PATH"]) and only then I'm able to load my functions without getting the LibraryFunction::libload error. – mmal Dec 14 '15 at 11:36
  • Yes, I guess there can be subtle differences in how environment variables are treated for processes launched from a shell or in another way. But that, along with how dynamic libraries work, is really an operating system issue, not a Mathematica one. – ilian Dec 14 '15 at 20:06
  • Not sure as I don't know OSX very well, but when it works (only) when DYLD_LIBRARY_PATH is 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:30
  • @AlbertRetey There are no MKL libraries provided with Mathematica on OS X; they are statically linked in libWolframEngine.dylib. – ilian Dec 15 '15 at 15:02
  • @ilian: as I said, don't know OSX very well. So these are statically linked into another dynamic library? Is this so just on OSX? Interesting construct, certainly outside of the limits where I understand what actually will be used and why :-) – Albert Retey Dec 15 '15 at 15:13
  • @AlbertRetey Interesting, I will take a look at this later. @ilian So what about the content of /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
  • @mmal Right, I was talking about the current state of things, that is, for version 10.1.0 and later. – ilian Dec 15 '15 at 20:58
  • @ilian Of course, so for more recent versions there should be no problems which AlbertRetey mentioned? – mmal Dec 15 '15 at 22:24

0 Answers0