Mathematica Q&A: Sow, Reap, and Parallel Programming
Got questions about Mathematica? The Wolfram Blog has answers! We’ll regularly answer selected questions from users around the web. You can submit your question directly to the Q&A Team using this form.
This week’s question comes from Patrick, a student:
How can I use Sow & Reap across parallel kernels?
The first part of the list is the regular result of the computation. The second part is everything that was “sown”.
Sow and Reap are ideally suited to situations in which you don’t know in advance how many results you will get. For example, suppose that you want to find simple initial conditions that lead to “interesting” results in Conway’s game of life, the famous two-dimensional cellular automaton:
We’ll call a result “interesting” if it has at least n cells alive (live cells have value 1):
Now we’ll check 1,000 random 6X6 initial conditions for the “interesting” property of having at least 200 live cells after 100 steps. Whenever the result is InterestingQ, we create and Sow a simple visualization:
One interesting result was found and sown. If we want to conduct a larger search, we ought to take advantage of Mathematica‘s automatic parallelization.
In this case the answer is particularly simple: define a version of Sow that always runs on the master kernel.
SetSharedFunction specifies that a function should always run on the master kernel.
Now we can apply Parallelize to our search. Here’s a search of 10,000 random initial conditions:
Click here to download this post as a Computable Document Format (CDF) file.
If you have a question you’d like answered in this blog, you can submit it to the Q&A Team using this form.