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.

I would like to copy some parameter from the main Kernel of Mathematica to the parallel kernels available. So I'm running now a command like:

LaunchKernels[]
ParallelEvaluate[var = 10, #] & /@ Kernels[]

Now running this is not a problem. But say I have another variable var2 and I want to copy its content to all other kernels or a specific kernel. How can I do this?

share|improve this question

closed as off-topic by bobthechemist, rasher, Oleksandr R., Michael E2, RunnyKine Apr 29 at 5:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – bobthechemist, rasher, Michael E2, RunnyKine
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Maybe SetSharedVariable ? –  b.gatessucks Apr 22 at 10:09
    
@b.gatessucks Thank you. But I still am looking for a better way to control this, rather than sharing variables. –  The Quantum Physicist Apr 22 at 10:26
1  
I can't answer your question and honestly I'm not completely sure what you are trying to do, but for the second snippet of code you would use something along the lines of With[{var2=var2},ParallelEvaluate[var=var2]]. Also note that mapping over Kernels[] is not necessary as by default the command is evaluated on all available kernels. –  sebhofer Apr 22 at 10:46
    
What do you mean by "a better way to control this"? What constitutes better or what effect(s) of SetSharedVariable are you trying to avoid? With regard to a reference to "the main kernel", aren't you in it? When you write var2 that IS the copy of var2 that is in the main kernel - unless you explicitly specify another kernel. –  Ymareth Apr 22 at 10:54
1  
Then you can still do what I suggested above, just with a subset of Kernels[] –  sebhofer Apr 22 at 11:31

1 Answer 1

up vote 1 down vote accepted

This is not really an answer to the original question but it seems to do what the OP wanted. So as nobody else answered, I'll just go ahead and post what I suggested in the comments above.

With can be conveniently used to substitute constants into expressions such as ParallelEvaluate. The second snippet of code from the OP can therefore be written as

With[{var2=var2},ParallelEvaluate[var=var2]]

which takes the local value of var2 and substitutes it into ParallelEvaluate, which subsequently evaluates the assignment on all available kernels. To do this on a subset of Kernels[] one can just use

ker = Kernels[][[{1,2}]]
With[{var2=var2},ParallelEvaluate[var=var2,ker]]
share|improve this answer
    
Thank you. Please vote up my question, because I can't vote until I have 125 points :) –  The Quantum Physicist Apr 24 at 11:47
1  
@TheQuantumPhysicist So in this case I would suggest that you edit your question to make it clear what you are really looking for, i.e. a way to set a variable on specific kernels. It would also be worth a look if there are any duplicates out there. I did a quick search but didn't find anything appropriate. –  sebhofer Apr 24 at 11:56

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