I am attempting to parallelize the construction of a table using MATLink
and ParallelTable
.
Let's consider a sample MATLAB
function defined by
function [out1,out2] = TestFunction(in1,in2,in3)
%TestFunction Example function
out1=in1;
out2=in2+in3;
end
Consider the non-parallel construction of the table
Needs["MATLink`"];
OpenMATLAB[];
TestFunction = MFunction["TestFunction", "OutputArguments" -> 2];
tab = Table[
{out1, out2} = TestFunction[i, 2 i, 3 i];
{out1, out2}
, {i, 1, 3}];
Which yields the output
{{1.,5.},{2.,10.},{3.,15.}}
My attempt to parallelize the construction is as follows:
First, launch kernels and load MATLink:
LaunchKernels[];
ParallelNeeds["MATLink`"];
ParallelEvaluate[
OpenMATLAB[];
TestFunction = MFunction["TestFunction", "OutputArguments" -> 2];
];
Which yields the following two errors for each of the kernels
Global`OpenMATLAB::shdw : Symbol OpenMATLAB appears in multiple contexts {Global`,MATLink`}; definitions in context Global` may shadow or be shadowed by other definitions.
Global`MFunction::shdw : Symbol MFunction appears in multiple contexts {Global`,MATLink`}; definitions in context Global` may shadow or be shadowed by other definitions.
and MATLAB is not opened on any kernals. As a result, the parallel table construction
tab = ParallelTable[
{out1, out2} = TestFunction[i, 2 i, 3 i];
{out1, out2}
, {i, 1, 3}];
yields the error
Set::shape : Lists {out1,out2} and Global`MFunction[TestFunction,OutputArguments->2][1,2,3] are not the same shape.
for each kernel. No output is generated.
What is the correct way to use MATLink
on multiple kernels?
Additional details:
The output of FindFile["Matlink
"]` is
In[1]:=FindFile["MATLink`"]
Out[1]=C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications\MATLink\Kernel\init.m
The output of ParallelEvaluate[FindFile["MATLINK
"]` is
In[2]:=ParallelEvaluate[FindFile["MATLink`"]]
Out[2]={C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications\MATLink\Kernel\init.m,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications\MATLink\Kernel\init.m,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications\MATLink\Kernel\init.m,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications\MATLink\Kernel\init.m}
The output of $Path
is
In[3]:= $Path
Out[3]= {C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Applications,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Links,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Kernel,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Autoload,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications,
C:\ProgramData\Mathematica\Kernel,
C:\ProgramData\Mathematica\Autoload,
C:\ProgramData\Mathematica\Applications,
.,
C:\Users\VHYDAN001,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Packages,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Autoload,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Autoload,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Applications,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\ExtraPackages,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Kernel\Packages,
C:\Program Files\Wolfram Research\Mathematica\11.1\Documentation\English\System,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Data\ICC}
The output of First@ParallelEvaluate[$Path]
is
In[4]:= First@ParallelEvaluate[$Path]
Out[4]= {C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Links,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Kernel,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Autoload,
C:\Users\VHYDAN001\AppData\Roaming\Mathematica\Applications,
C:\ProgramData\Mathematica\Kernel,
C:\ProgramData\Mathematica\Autoload,
C:\ProgramData\Mathematica\Applications,
.,
C:\Users\VHYDAN001,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Packages,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Autoload,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Autoload,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\Applications,
C:\Program Files\Wolfram Research\Mathematica\11.1\AddOns\ExtraPackages,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Kernel\Packages,
C:\Program Files\Wolfram Research\Mathematica\11.1\Documentation\English\System,
C:\Program Files\Wolfram Research\Mathematica\11.1\SystemFiles\Data\ICC}
Global
context makes it clear that the package was never loaded on subkernels. I will be EXTREMELY surprised if the situation is not what I described in my answer. $\endgroup$