Group Abstract Group Abstract

Message Boards Message Boards

0
|
10.2K Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[?] How it works ParallelTable[Print...]?

Dear Community, I have a very simple question relating to ParallelTable[] function. The following code works as it must.

In[1]:= ParallelTable[k, {k, 10}]
Out[1]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

But, if I use Print[], it works as it follows:

In[2]:= ParallelTable[Print[k], {k, 10}];
(kernel 2) 1
(kernel 1) 4
(kernel 2) 2
(kernel 1) 5
(kernel 2) 3
(kernel 1) 6
(kernel 2) 7
(kernel 1) 9
(kernel 2) 8
(kernel 1) 10

Can someone explain how it works?

One additional code, the result of which looks very strange:

In[3]:= i = 0; ParallelTable[Print["k = ", k, " \[Rule] i = ", ++i], {k, 10}];
 (kernel 2) k = 1 -> i = 26
 (kernel 1) k = 4 -> i = 26
 (kernel 2) k = 2 -> i = 27
 (kernel 1) k = 5 -> i = 27
 (kernel 2) k = 3 -> i = 28
 (kernel 1) k = 6 -> i = 28
 (kernel 2) k = 7 -> i = 29
 (kernel 1) k = 9 -> i = 29
 (kernel 2) k = 8 -> i = 30
 (kernel 1) k = 10 -> i = 30

Why the values of variable i are so strange?

3 Replies
POSTED BY: Kevin Daily

Thank you @Szabolcs and @Kevin for your valuable answers!

Can someone explain how it works?

It works in parallel, therefore there are no guarantees about the order of evaluation.

Why the values of variable i are so strange?

Because each subkernel (paralell thread) has its own private copy of i. The value 0 is distributed to them only once.


These are basic problems that come up in all forms of parallel programming. As you learn more about the topic, you will find countless discussions (even books) on them. At this point it may be a good idea to read through the Parallel Computing Tools User Guide, though I do not remember to what extent it addresses these problems.

http://reference.wolfram.com/language/ParallelTools/tutorial/Overview.html

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard