PRODUCTS
Products Overview
Mathematica
Mathematica for Students
Mathematica Home Edition
Wolfram
CDF Player
(free download)
Computable Document Format (CDF)
web
Mathematica
grid
Mathematica
Wolfram
Workbench
Mathematica
Add-Ons
Wolfram|Alpha Products
SOLUTIONS
Solutions Overview
Engineering
Aerospace Engineering & Defense
Chemical Engineering
Control Systems
Electrical Engineering
Image Processing
Industrial Engineering
Materials Science
Mechanical Engineering
Operations Research
Optics
Petroleum Engineering
Biotechnology & Medicine
Bioinformatics
Medical Imaging
Finance, Statistics & Business Analysis
Actuarial Sciences
Data Analysis & Mining
Econometrics
Economics
Financial Engineering & Mathematics
Financial Risk Management
Statistics
Software Engineering & Content Delivery
Authoring & Publishing
Interface Development
Software Engineering
Web Development
Science
Astronomy
Biological Sciences
Chemistry
Environmental Sciences
Geosciences
Social & Behavioral Sciences
Design, Arts & Entertainment
Game Design, Special Effects & Generative Art
Education
STEM Education Initiative
Higher Education
Community & Technical College Education
Primary & Secondary Education
Students
Technology
Computable Document Format (CDF)
High-Performance & Parallel Computing (HPC)
See Also: Technology Guide
PURCHASE
Online Store
Other Ways to Buy
Volume & Site Licensing
Contact Sales
Software
Service
Upgrades
Training
Books
SUPPORT
Support Overview
Knowledge Base
Learning Center
Community & Forums
Training & Free Seminars
Does My Site Have a License?
Wolfram User Portal
COMPANY
About Wolfram Research
News & Events
Wolfram Blog
Partnerships
Employment Opportunities
History of
Mathematica
Stephen Wolfram's Home Page
Contact Us
OUR SITES
All Sites
Wolfram|Alpha
Demonstrations Project
MathWorld
Integrator
Wolfram Functions Site
Mathematica Journal
Wolfram Media
Wolfram
Tones
Wolfram Science
Stephen Wolfram
DOCUMENTATION CENTER SEARCH
New to
Mathematica
?
Find your learning path
»
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
>
BUILT-IN MATHEMATICA SYMBOL
$SharedFunctions
UnsetShared
SetSharedVariable
DistributeDefinitions
DownValues
See Also »
|
Concurrency
Parallel Computation Setup & Configuration
Parallel Computing
Resource Sharing in Parallel Computing
Summary of New Features in 7.0
New in 7.0: Alphabetical Listing
More About »
SetSharedFunction
SetSharedFunction
declares the symbols
as shared functions whose downvalues are synchronized among all parallel kernels.
MORE INFORMATION
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
.
EXAMPLES
CLOSE ALL
Basic Examples
(1)
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]:=
Each subkernel makes a definition for the same shared variable:
In[2]:=
Out[2]=
In[3]:=
Out[3]=
Without sharing, each subkernel has its own (local) copy of the function:
In[4]:=
Out[4]=
In[5]:=
Out[5]=
In[6]:=
Scope
(4)
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:
Generalizations & Extensions
(1)
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:
Applications
(3)
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
:
Possible Issues
(3)
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:
Neat Examples
(1)
A parallel version of
Sow
:
SEE ALSO
$SharedFunctions
UnsetShared
SetSharedVariable
DistributeDefinitions
DownValues
MORE ABOUT
Concurrency
Parallel Computation Setup & Configuration
Parallel Computing
Resource Sharing in Parallel Computing
Summary of New Features in 7.0
New in 7.0: Alphabetical Listing
New in 7