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.

Consider the following:

grid = Tuples[Range[0, 3.3083, 0.08468], 3];
k = Length[grid]; vdw = {0.15, 0.19, 0.19, 0.195, 0.195};
PeriodicDistance = 
  Compile[{{x, _Real, 1}, {y, _Real, 2}, {r, _Real}}, 
   Sqrt@Total[((x - y) - 3.3083*Round[(x - y)/3.3083])^2] - r, 
   CompilationTarget :> "C", RuntimeOptions -> "Speed"];

Now the following function:

Cavs[dat_?MatrixQ] := Module[{pos},
  Do[
   With[{tr = Transpose[dat[[v ;; -1 ;; 5]]]},
    pos[v] = 
     ParallelTable[
      Min[PeriodicDistance[grid[[i]], tr, vdw[[v]]]], {i, k}]
    ],
   {v, 1, 5}];
  pos[2]
  ]

with input file

dat = ReadList["filename.ext", {Word, Word} ~ Join ~ Table[Number, {4}]][[All, 4 ;; 6]];

Cavs[dat]

gives

Mathematica graphics

This answer is exactly the same answer I get when I replace ParallelTable with Table. Now, if I include this function in a Mathematica package I get all sorts of Part Specification errors. Here's a sample:

Mathematica graphics

I know ParallelTable is the culprit because if I replace it with Table in the package it works fine. I just don't know why it works in a normal Mathematica notebook session but fails when packaged. Any ideas?

share|improve this question
    
Have you distributed all needed FN and Var. definitions with SetSharedFunction , etc.? –  rasher May 6 at 1:32
    
@rasher, why should that make a difference between running the same code on the notebook vs. being packaged? –  RunnyKine May 6 at 2:31

1 Answer 1

up vote 2 down vote accepted

When you read the documentation carefully, you see why this happens:

The default value is DistributedContexts:>$DistributedContexts with $DistributedContexts:=$Context, which distributes definitions of all symbols in the current context, but does not distribute definitions of symbols from packages.

Therefore, what you want is (with the addition DistributedContexts :> Automatic):

BeginPackage["Test`"];

Cavs;

Begin["`Private`"];
grid = Tuples[Range[0, 3.3083, 0.08468], 3];
k = Length[grid]; vdw = {0.15, 0.19, 0.19, 0.195, 0.195};
PeriodicDistance = 
  Compile[{{x, _Real, 1}, {y, _Real, 2}, {r, _Real}}, 
   Sqrt@Total[((x - y) - 3.3083*Round[(x - y)/3.3083])^2] - r, 
   CompilationTarget :> "C", RuntimeOptions -> "Speed"];
Cavs[dat_?MatrixQ] := 
 Module[{pos}, 
  Do[With[{tr = Transpose[dat[[v ;; -1 ;; 5]]]}, 
    pos[v] = 
     ParallelTable[
      Min[PeriodicDistance[grid[[i]], tr, vdw[[v]]]], {i, k}, 
      DistributedContexts :> Automatic]], {v, 1, 5}];
  pos[2]]

End[];
share|improve this answer
    
Thanks a lot. I remember ParallelTable distributes definition automatically, never thought it would be different for packages so I never bothered to check. I guess lesson learned. –  RunnyKine May 6 at 16:39

Your Answer

 
discard

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

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