4
$\begingroup$

Possible Duplicate:
How to run mathlink external commands in parallel?

Abstract

I have a binary file compiled to work with MathLink. I can install the file and run the component functions from within Mathematica without any problems. But, when I try to use Mathematica's inbuilt parallelization with calls to my binary, I get an error that says

LinkObject::linkn: Argument LinkObject[/path/to/binary/file.binary,14,8] in
 LinkWrite[LinkObject[/path/to/binary/file.binary,14,8],CallPacket[1,{1,2066455,5.,200000}]]
 has an invalid LinkObject number; the link may be closed.

I understand this to mean that MathLink cannot set up links to my binary for some reason? How does one get this to work?

System Details

I am on a system with 8 cores running x86_64-redhat-linux-gnu.

Mathematica Code that Produces the Error

Assume SwitchTimes is a function defined inside my binary. It takes a number of inputs, but I want to loop over values for one particular input.

f[x_] := Module[{A, ma}, 
   A = SwitchTimes[x, RandomInteger[10000000], 5.0, 200000];
   ma = Mean[A];
   ma
   ];

B = ParallelMap[f, Table[i, {i, 1, 100, 2}]];

Code that runs fine

f[x_] := Module[{A, ma}, 
   A = SwitchTimes[x, RandomInteger[10000000], 5.0, 200000];
   ma = Mean[A];
   ma
   ];

B = Map[f, Table[i, {i, 1, 100, 2}]];
$\endgroup$
  • $\begingroup$ Maybe try DistributeDefinitions[SwitchTimes] before use it? $\endgroup$ – Silvia Jul 26 '12 at 20:46
  • $\begingroup$ Ok, so I add the following to the top of the mathematica notebook: link = Install[myPath<>"file.binary"]; LaunchKernels[]; DistributeDefinitions[SwitchTimes]; And then use ParallelMap. I still get the same errors. $\endgroup$ – Y.P. Jul 26 '12 at 21:09
  • 2
    $\begingroup$ This may be related: mathematica.stackexchange.com/q/33/877 $\endgroup$ – jmlopez Jul 26 '12 at 22:37
  • 1
    $\begingroup$ I recommend you use LibraryLink instead of MathLink if you are planning to run parallel code. $\endgroup$ – Szabolcs Jul 27 '12 at 7:37
  • 1
    $\begingroup$ @jmlopez actually it seems like a duplicate., $\endgroup$ – Szabolcs Jul 27 '12 at 7:40