Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to call a C function in parallel through LibraryLink. Here is an example kind of demonstrates that library function can be used in parallel. My question is what happened in the library function call? Does each kernel have its own copy of the library function? Are there some pitfalls we need to watch out when using library functions in parallel?

Needs["CCompilerDriver`"];

src = "
  #include \"WolframLibrary.h\"
  #include \"time.h\"

  DLLEXPORT mint WolframLibrary_getVersion(){
    return WolframLibraryVersion;
  }
  DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData){
    return 0;
  }

  DLLEXPORT int pause(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {
    timer(MArgument_getReal(Args[0]));
    return 0;
  }

  void timer(double seconds)
  {
    clock_t x;
    x=clock()+seconds*CLOCKS_PER_SEC;
    while(clock()<x){}
  }

  ";

test1 = CreateLibrary[src, "test1"];
pauseLib = LibraryFunctionLoad[test1, "pause", {Real}, Integer];

Table[pauseLib[1.],{8}];//AbsoluteTiming
(* {8.021739,Null} *)
ParallelTable[pauseLib[1.],{8}];//AbsoluteTiming
(* {2.350266,Null} *)
share|improve this question
    
Yes, each kernel has its own independent copy of the library function. –  Szabolcs Feb 25 at 12:09
    
I did use LibraryLink functions with the parallel tools. I think there are no pitfalls if you are using LibraryLink (not MathLink). No other pitfalls than those that apply to parallelization in Mathematica in general, at least. The functions will live in separate kernels (separate processes). –  Szabolcs Feb 25 at 12:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.