Virtual Shared Memory

How can processes communicate with each other?

Message Passing

Shared Memory

Virtual Shared Memory: faking it on distributed memory machines.

When a process needs the value of a global (shared) variable or wants to set its value, it sends a request back to the scheduler. The scheduler maintains the variables, udpates their values, and sends back the requested values.

Declaring Shared Variables/Functions

A declaration sets up the slave-side definitions and transports them to all slaves, which, therefore, need no separate init code.

[Graphics:../Images/MSRI98_gr_95.gif]
[Graphics:../Images/MSRI98_gr_96.gif]

Examples

We should have at least two remote kernels here.

[Graphics:../Images/MSRI98_gr_97.gif]
[Graphics:../Images/MSRI98_gr_98.gif]

Declare shared variables.

[Graphics:../Images/MSRI98_gr_99.gif]

Basic tests.

[Graphics:../Images/MSRI98_gr_100.gif]
[Graphics:../Images/MSRI98_gr_101.gif]
[Graphics:../Images/MSRI98_gr_102.gif]
[Graphics:../Images/MSRI98_gr_103.gif]
[Graphics:../Images/MSRI98_gr_104.gif]
[Graphics:../Images/MSRI98_gr_105.gif]
[Graphics:../Images/MSRI98_gr_106.gif]
[Graphics:../Images/MSRI98_gr_107.gif]
[Graphics:../Images/MSRI98_gr_108.gif]

Atomic update:

[Graphics:../Images/MSRI98_gr_109.gif]
[Graphics:../Images/MSRI98_gr_110.gif]
[Graphics:../Images/MSRI98_gr_111.gif]
[Graphics:../Images/MSRI98_gr_112.gif]

No special syntax is required to use shared variables. Just do things the normal way. If a variable is shared, all accesses are transparently sent to the master kernel.

The prototypical synchronization problem: between reading and writing a shared variable y, another process can change the value or read an obsolete value.

[Graphics:../Images/MSRI98_gr_113.gif]
[Graphics:../Images/MSRI98_gr_114.gif]

A sequential execution (using Map instead of parallelMap) would ensure that the final value of y were 5. With concurrent access, it will usually be smaller.

[Graphics:../Images/MSRI98_gr_115.gif]
[Graphics:../Images/MSRI98_gr_116.gif]

Synchronization

Exclusive access to variables can be achieved with one new primitive operation, a conditional atomic write. In machine code, it is often called test and set or read-modify-write.

[Graphics:../Images/MSRI98_gr_117.gif]

Set s to e (just like s=e), but only if s is currently unset or its value is [Graphics:../Images/MSRI98_gr_118.gif]. Return the (old or new) value of s. The read-modify-write is atomic.

[Graphics:../Images/MSRI98_gr_119.gif]
[Graphics:../Images/MSRI98_gr_120.gif]

This attempt fails:

[Graphics:../Images/MSRI98_gr_121.gif]
[Graphics:../Images/MSRI98_gr_122.gif]

Release exclusive access.

[Graphics:../Images/MSRI98_gr_123.gif]

Now r2 succeees.

[Graphics:../Images/MSRI98_gr_124.gif]
[Graphics:../Images/MSRI98_gr_125.gif]

Critical Sections

Exclusive access to a variable or other resource can be controlled by a lock. Here is the typical code added to the previous example about asynchronous access to a variable.

[Graphics:../Images/MSRI98_gr_126.gif]
[Graphics:../Images/MSRI98_gr_127.gif]
[Graphics:../Images/MSRI98_gr_128.gif]
[Graphics:../Images/MSRI98_gr_129.gif]
[Graphics:../Images/MSRI98_gr_130.gif]
[Graphics:../Images/MSRI98_gr_131.gif]
[Graphics:../Images/MSRI98_gr_132.gif]
[Graphics:../Images/MSRI98_gr_133.gif]
[Graphics:../Images/MSRI98_gr_134.gif]
[Graphics:../Images/MSRI98_gr_135.gif]
[Graphics:../Images/MSRI98_gr_136.gif]
[Graphics:../Images/MSRI98_gr_137.gif]
[Graphics:../Images/MSRI98_gr_138.gif]
[Graphics:../Images/MSRI98_gr_139.gif]
[Graphics:../Images/MSRI98_gr_140.gif]
[Graphics:../Images/MSRI98_gr_141.gif]
[Graphics:../Images/MSRI98_gr_142.gif]
[Graphics:../Images/MSRI98_gr_143.gif]
[Graphics:../Images/MSRI98_gr_144.gif]
[Graphics:../Images/MSRI98_gr_145.gif]
[Graphics:../Images/MSRI98_gr_146.gif]
[Graphics:../Images/MSRI98_gr_147.gif]
[Graphics:../Images/MSRI98_gr_148.gif]
[Graphics:../Images/MSRI98_gr_149.gif]
[Graphics:../Images/MSRI98_gr_150.gif]
[Graphics:../Images/MSRI98_gr_151.gif]
[Graphics:../Images/MSRI98_gr_152.gif]
[Graphics:../Images/MSRI98_gr_153.gif]
[Graphics:../Images/MSRI98_gr_154.gif]
[Graphics:../Images/MSRI98_gr_155.gif]
[Graphics:../Images/MSRI98_gr_156.gif]
[Graphics:../Images/MSRI98_gr_157.gif]
[Graphics:../Images/MSRI98_gr_158.gif]
[Graphics:../Images/MSRI98_gr_159.gif]
[Graphics:../Images/MSRI98_gr_160.gif]
[Graphics:../Images/MSRI98_gr_161.gif]
[Graphics:../Images/MSRI98_gr_162.gif]
[Graphics:../Images/MSRI98_gr_163.gif]
[Graphics:../Images/MSRI98_gr_164.gif]
[Graphics:../Images/MSRI98_gr_165.gif]
[Graphics:../Images/MSRI98_gr_166.gif]
[Graphics:../Images/MSRI98_gr_167.gif]
[Graphics:../Images/MSRI98_gr_168.gif]
[Graphics:../Images/MSRI98_gr_169.gif]
[Graphics:../Images/MSRI98_gr_170.gif]
[Graphics:../Images/MSRI98_gr_171.gif]
[Graphics:../Images/MSRI98_gr_172.gif]
[Graphics:../Images/MSRI98_gr_173.gif]
[Graphics:../Images/MSRI98_gr_174.gif]
[Graphics:../Images/MSRI98_gr_175.gif]
[Graphics:../Images/MSRI98_gr_176.gif]
[Graphics:../Images/MSRI98_gr_177.gif]
[Graphics:../Images/MSRI98_gr_178.gif]
[Graphics:../Images/MSRI98_gr_179.gif]
[Graphics:../Images/MSRI98_gr_180.gif]
[Graphics:../Images/MSRI98_gr_181.gif]
[Graphics:../Images/MSRI98_gr_182.gif]
[Graphics:../Images/MSRI98_gr_183.gif]

The Dining Philosophers

The classic example for access to shared resources, in this case forks placed between philosophers sitting at a round table. A philosopher needs the two forks to his left and right to eat.

The variables forks[1],...,forks[n] serve to reserve forks by conditionally setting them to a unique value.

[Graphics:../Images/MSRI98_gr_184.gif]
[Graphics:../Images/MSRI98_gr_185.gif]

Each dining philosopher is one process.

[Graphics:../Images/MSRI98_gr_186.gif]

Here's the behaviour of a philosopher: either thinking or eating.

[Graphics:../Images/MSRI98_gr_187.gif]
[Graphics:../Images/MSRI98_gr_188.gif]

We want essentially ParallelMap, but need a way to stop them cleanly.

[Graphics:../Images/MSRI98_gr_189.gif]
[Graphics:../Images/MSRI98_gr_190.gif]

After an interrupt, clean up:

[Graphics:../Images/MSRI98_gr_191.gif]

Document converted by Mathematica of Wolfram Research