5
$\begingroup$

I am developing a package in Mathematica for finding the conductivity of magnetic materials. The most important and time consuming processes are EigenSystem[] calls. I want to make the code modular so I am developing a separate package that construct the matrix which ultimately gets sent to EigenSystem[]. I have run into behavior that I do not understand when I try to parallelize my code. The parallelization is trivial, e.g., loop over multiple parameter values in parallel and run the expensive calculation in serial for each value.

Below is a sample code that shows the unexpected behavior. In the first example I define a function test0[size_] that builds the identity matrix with dimensions size^2. Next I find the Eigenvalues[] and return the first entry. Finally I evaluate the expression in parallel and the result is as expected.

In[0]: test0[size_]:=IdentityMatrix[size];
In[1]: Eigenvalues[test0[2]][[1]]
In[2]: ParallelEvaluate[Eigenvalues[test0[2]][[1]]]

Out[1]: 1

Out[2]: {1, 1}

In the second example I perform essentially the same operations, with one crucial difference: now when I define the function I do so in a different context. This is a crucial component of making nice Mathematica package. Now when I evaluate the code in serial I get the same behavior as the function test0[], but when I evaluate in parallel the behavior is quite strange. The output is simply the matrix itself.

In[3]: BeginPackage["testing`"];
         test1[size_]:=IdentityMatrix[size];
       EndPackage[];
In[4]: Eigenvalues[test1[2]][[1]]
In[5]: ParallelEvaluate[Eigenvalues[test1[2]][[1]]]

Out[4]: 1

Out[5]: {{{1, 0}, {0, 1}}, {{1, 0}, {0, 1}}}

What is even more strange to me is if I remove the [[1]] indexing of the Eigenvalues the behavior is as expected.

In[6]: ParallelEvaluate[Eigenvalues[test1[2]]]

Out[6]: {{1, 1}, {1, 1}}
$\endgroup$
  • $\begingroup$ If going down the package route ParallelNeeds is worth looking at (and/or possibly with some initialization finessing). $\endgroup$ – Ronald Monson Jul 20 '18 at 20:36
6
$\begingroup$

No idea if this is a bug, but you can work around it by using

ParallelEvaluate[Eigenvalues[test1[2]][[1]], 
 DistributedContexts -> {"testing`"}]
(* {1, 1, 1, 1} *)

Or, with the same effect, you can evaluate

$DistributedContexts = {"Global`", "testing`"}
ParallelEvaluate[Eigenvalues[test1[2]][[1]]]
(* {"Global`", "testing`"} *)
(* {1, 1, 1, 1} *)
$\endgroup$
  • $\begingroup$ Thanks! Everything works when I use DistributedContexts. I guess the surprising thing is that ParallelEvaluate[Eigenvalues[...]] works without DistributedContexts, but ParallelEvaluate[Eigenvalues[...][[1]]] fails without giving an error message and with an unexpected return value. $\endgroup$ – James Rowland Jul 21 '18 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.