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.

Let the symbol V denote an element of some vector space and nV its norm. Let lin be a linear function, i.e. pull out factors that are not vectors:

lin[c_*x_?CustomVectorQ]:=c*lin[x]
CustomVectorQ[x_]:=MatchQ[x,V]

Let f be a (for simplicity constant) function generating an expression using lin

f[dummy_]:=lin[V/nV]

and subst shall be a simplification subst[exp_]:=exp/.lin[V]->0.

Now, the odd thing is that if I apply sf=subst@f@#& to some list, the result differs depepnding on whether one uses Map or ParallelMap.

Map[sf,{dummy}]
ParallelMap[sf,{dummy}]

gives {0} and {lin[V]/nV}, respectively.

Adding a debugging function printf[exp_] := Module[{}, Print["Into subst: ", exp]; exp], subst@printf@f@#& seems to suggest, that the parallel computation does not use the downvalues of lin and thus subst does not work.

Does anyone know why this happens and what can be done about it?

share|improve this question
    
DistributeDefinitions[subst, f, lin, CustomVectorQ] –  Oleksandr R. Jul 9 '13 at 14:04
    
Hmm, but the documentation of DistributeDefinitions states that "Higher-level parallel commands automatically distribute definitions needed". So why would I need this? Bug? And how would I know when to use DistributeDefinitions? –  Berg Jul 9 '13 at 15:22
2  
@Berg for automatically distribute definitions you need to set the option DistributedContexts->Automatic. But you're right when you claim, that the documentation says that it does automatically distribute those definitions in the same context...are you always in the same context? my general advise is always to avoid any automatic settings when using parallel functionality, unless you really know what you're doing and how the system will behave. The best is probably to be specific everytime... –  Stefan Jul 9 '13 at 17:49
1  
@Stefan to be honest, I wish they'd never introduced the automatic distribution. It works (when it does) in an utterly unpredictable way and seems to cause more problems than it saves. Unfortunately, it cannot be completely disabled either. Call me a curmudgeon if you like, but I think the changes since the last version of the PCT (apart from the fact that it's now bundled with Mathematica) have mostly been less than useful. –  Oleksandr R. Jul 9 '13 at 18:27
    
@OleksandrR. i totally agree with you...it is always a big surprise how the system behaves with a fresh kernel and the same lines of code, all local...my opinion is the same as yours (i wanted to express that in my comment, but maybe i failed)...why i often advise to use Automatic (and on the other hand immediatly advise against it), is that the user get their feeling of success and if they're more interested, maybe they turn that thing off and discover the peculiarities (gosh)...and, i've to confess they are really weird sometimes... –  Stefan Jul 9 '13 at 18:35
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.