Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Systems Interfaces & Deployment > Parallel Computing > Resource Sharing in Parallel Computing > SetSharedFunction >
Mathematica > Core Language > Tuning & Debugging > Parallel Computing > Resource Sharing in Parallel Computing > SetSharedFunction >

SetSharedFunction

SetSharedFunction
declares the symbols as shared functions whose downvalues are synchronized among all parallel kernels.
  • Downvalues for a shared function defined on any kernel are maintained by the master kernel and every access on a parallel subkernel is synchronized through the master kernel.
  • Expressions of the form that would remain unevaluated give Null.
Make fs be a (global) shared function:
Each subkernel makes a definition for the same shared variable:
Without sharing, each subkernel has its own (local) copy of the function:
Make fs be a (global) shared function:
In[1]:=
Click for copyable input
Each subkernel makes a definition for the same shared variable:
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
Without sharing, each subkernel has its own (local) copy of the function:
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
Define and share a function:
When called from each subkernel, the function updates next on the master kernel:
Set up a shared function with a default rule:
Make additional definitions on the subkernels:
Since the function is shared, the master kernel knows about the additional rules:
Maintain indexed shared variables with a default value:
Define a shared function to manipulate the local variable results on the master kernel:
Call the function from the subkernels:
The variable on the master has been modified:
A delayed definition for a shared function made on the master kernel:
Such definitions are always evaluated on the master kernel, whichever kernel evaluates it:
A definition for a shared function made on one of the parallel kernels:
Such definitions are always evaluated on the kernel that asks for the value:
Shared functions can be used for synchronization:
Less efficient is the use of a shared variable and critical section:
A constructor for a simple queue data type:
Create two shared queues:
Fill the input queue:
Work on the elements of the input queue in parallel and put results into the output queue:
Use a single shared function to communicate both input and result:
Set up a search and display its progress until it is manually aborted:
The results found so far—a list of the number of factors of :
A shared function is inefficient for mere code distribution and leads to sequential evaluation:
Simply distribute the definitions of any function needed on the parallel kernels:
Separate read and write operations on a shared variable are not thread-safe:
Use a shared function to synchronize access to an (unshared) variable:
Alternatively, use CriticalSection to make a whole code section atomic:
Parallel dynamic programming:
A parallel version of Sow:
New in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF