3
$\begingroup$

Background

I have a function I would like to execute inside a ParallelTable. One of the arguments of this function is another function. So it looks something like this:

myfunction[function_,param1_,someotherparameter_]:=(x=function[param1];
(*some evaluation using x and someotherparameter*))

(*I have several functions that I use as argument, but the one that causes the error is:*)
Needs["NumericalDifferentialEquationAnalysis`"]
gaussiangridandweights[np_] := (gaw = GaussianQuadratureWeights[np, 0, 10]; 
{gaw[[All, 1]], gaw[[All, 2]]})

Table[myfunction[gaussiangridandweights,n,otherparameter],{otherparameter,0,1}]
(*no error here*)
ParallelTable[myfunction[gaussiangridandweights,n,otherparameter],{otherparameter,0,1}]
(*error and different result*)

I have had no errors when using a Table, but now that I'm using ParallelTable, I get "Part specification is longer than depth of object." and I'm a bit baffled what causes this. The results are also different between using Table and using ParallelTable.

Drilled-down Problem

So I tried to nail down the problem and found that just evaluating the offending function gaussiangridandweights inside a Paralleltable also gives the same error (but interestingly does not change the result)

Table[gaussiangridandweights[2], {i, 0, 2}]
(*no error*)
ParallelTable[gaussiangridandweights[2], {i, 0, 2}]
(*same error as before, but same result as with Table*)

Just doing

Table[GaussianQuadratureWeights[2, 0, 10], {i, 0, 2}]
ParallelTable[GaussianQuadratureWeights[2, 0, 10], {i, 0, 2}]

does not cause errors, so there must be something wrong with the {gaw[[All, 1]], gaw[[All, 2]]}, but I have no idea what to do to fix this.

The exact error messages are

Part::partd :  Part specification NumericalDifferentialEquationAnalysis`GaussianQuadratureWeights[2,0,10][[All,1]] is longer than depth of object.
Part::partd :  Part specification NumericalDifferentialEquationAnalysis`GaussianQuadratureWeights[2,0,10][[All,2]] is longer than depth of object.

I searched for other people with the same error message "Part specification is longer than depth of object.", but their problems did not seem to apply to my case.

Questions

  1. What causes the error message and why do I only get it in ParallelTable?

  2. Any ideas why I get different results for myfunction in ParallelTable, even though gaussiangridandweights still seems to return the correct results (together with the error message)? (I do realise that this question might vanish as soon as question 1 is answered, but then again it might not. This part of the question is more out of theoretical interest)

$\endgroup$

1 Answer 1

7
$\begingroup$

You needs to use ParallelNeeds to load the package on the parallel kernels:

ParallelNeeds["NumericalDifferentialEquationAnalysis`"]

ParallelTable[gaussiangridandweights[2], {i, 0, 2}]
(* {{{2.11325, 7.88675}, {5., 5.}}, {{2.11325, 7.88675}, {5., 
   5.}}, {{2.11325, 7.88675}, {5., 5.}}} *)

To understand why it still "works" without it, look at the following: (evaluate it on a fresh kernel without calling ParallelNeeds)

ParallelTable[Echo@gaussiangridandweights[2], {i, 0, 2}]
(*same error as before,but same result as with Table*)

(* Part::partd :  Part specification NumericalDifferentialEquationAnalysis`GaussianQuadratureWeights[2,0,10][[All,1]] is longer than depth of object. *)

(* ... *)

(* >> {NumericalDifferentialEquationAnalysis`GaussianQuadratureWeights[2,0,10][[All,1]],NumericalDifferentialEquationAnalysis`GaussianQuadratureWeights[2,0,10][[All,2]]} *)

(* ... *)

{{{2.11325, 7.88675}, {5., 5.}}, {{2.11325, 7.88675}, {5., 
   5.}}, {{2.11325, 7.88675}, {5., 5.}}}

The Echo shows you what is returned by the parallel kernels: As you can see, they simply return the unevaluated expression, which is then evaluated on the main kernel. Of course, this defeats the entire purpose of parallelization and will not work if the evaluated result is needed on the parallel kernels to continue the computation.

$\endgroup$
1
  • $\begingroup$ Good thing I asked, I didn't even know something like ParallelNeeds existed. This explains everything. Thank you very much! $\endgroup$
    – fifaltra
    Commented Aug 14, 2020 at 8:41

Your Answer

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

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