2
$\begingroup$

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?

$\endgroup$
11
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Dec 14, 2015 at 1:36
  • $\begingroup$ 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"]. $\endgroup$
    – ilian
    Commented Dec 14, 2015 at 2:11
  • $\begingroup$ @ilian Thank you but it isn't working, which is confusing to me. $\endgroup$
    – mmal
    Commented Dec 14, 2015 at 8:39
  • 1
    $\begingroup$ @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. $\endgroup$
    – mmal
    Commented Dec 14, 2015 at 11:36
  • $\begingroup$ 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. $\endgroup$
    – ilian
    Commented Dec 14, 2015 at 20:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.