1

Consider the two cases:

ClearAll["Global`*"]
j = Reap[Do[{Sow[{i, i}, a]}; Sow[{i, i^2}, b], {i, 100}], _, Rule];
a /. Last@j
h = Reap[ParallelDo[{Sow[{i, i}, a]}; Sow[{i, i^2}, b], {i, 100}], _, 
   Rule];
a /. Last@h

The output of the first is a matrix, while the output of the second is a. I am obviously using ParallelDo, Sow and Reap in the wrong way.

Which is the correct way to use Reap and Sow with a ParallelDo?

mattiav27
  • 6,677
  • 3
  • 28
  • 64
  • 3
    Are you sure you want to be using the parallelization functions with Sow and Reap? From what I read in https://mathematica.stackexchange.com/questions/84534 they're not likely to perform well with each other to begin with. Normally divergent results between Do and ParallelDo is going to be an inter-kernel communication issue or a failure to share definitions, but I'm not sure you should expect any performance benefit even if you fix that issue. – eyorble Mar 13 '20 at 22:25
  • 1
    I have the same concerns as eyorble, but I am also not sure. It might be a better choice to collect results per kernel first (maybe even in an Association (per kernel, of course)) and to Merge the results afterwards. – Henrik Schumacher Mar 14 '20 at 07:41
  • In order to obtain the desired output, you have to execute SetSharedFunction[Sow] in advance. – user688486 Sep 17 '23 at 09:21

0 Answers0