Distributed Programming in Java - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Distributed Programming in Java

Description:

Forms of parallelism. Supervisor-Worker pattern as an example of agenda parallelism ... Specialist parallelism. work units organized around specialists ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 46
Provided by: peopleScs
Category:

less

Transcript and Presenter's Notes

Title: Distributed Programming in Java


1
Distributed Programming in Java
  • Space-Based Computing (2)

2
Overview
  • Review of Space-Based Computing
  • Forms of parallelism
  • Supervisor-Worker pattern as an example of agenda
    parallelism
  • Design of the sample tuple space system

3
Space-Based Computing
4
Space-Based Computing
  • A space is a high-level coordination tool for
    glueing agents into an application
  • Different programming paradigm
  • Instead of relying on message-passing,
  • applications cooperate through the flow of
    objects in and out of spaces
  • Space-based models (JavaSpaces, TSpaces) based on
    tuple space concepts

5
Tuple Spaces (Linda)
  • Originally designed as a persistent buffer for
    communicating among parallel programs
  • Design goals include
  • Anonymous communication
  • Universal associative addressing
  • Persistent data repository
  • Applications post and read unstructured tuples to
    a shared tuple space

6
JavaSpaces
  • Adds new ideas to Linda
  • Distributed leasing curbs resource consumption
    due to failures
  • Distributed event notification event sources,
    listeners, and objects
  • Distributed transactions ensures operation
    ordering (2PC)
  • Basis of the Jini architecture for service
    discovery and invocation

7
Simple Set of Operations
  • Basic operations on a tuple space (Linda)
  • out adds a tuple to the tuple space
  • in reads a matching tuple and deletes it from the
    tuple space
  • in tuple also known as template fields are
    expressions, or formal parameters
  • read is like in, but tuples not deleted

out("tag", expr-1, ..., expr-n)
in("tag", field-1, ..., field-n)
8
Tuple Matching
9
Tuples
  • A tag (eg "camera")
  • Typically, a collection of named fields
  • A tuple becomes a template, if it contains formal
    fields, otherwise it is actual
  • Templates match another tuple, if
  • the tags are the same
  • number of fields is the same
  • all of the fields match

10
Fields
  • Each field has
  • name (eg make)
  • type (a Java class, eg String.class)
  • value (eg Leica)
  • A field is formal if its value is a null token
    (eg null), otherwise a field is actual
  • A formal field matches a field in another tuple,
    if its name and type are the same

11
Forms of Parallelism
12
Result parallelism
  • work units are organized around data structures

Blue workers operate on 1st element of data
structure Red workers operate on 2nd element Eg.
Image processing, vector computing
Source http//www.cs.bath.ac.uk/amb/UQC007H3/DES
IGN/DESGN300.HTMCONTENTS
13
Approach
  • Build a data structure to represent the shape of
    the result
  • Assign a process to each element of the result
  • Identify the data sources needed to compute each
    element
  • Determine how to obtain the values of all
    elements simultaneously
  • Arrange termination when all values are known

14
Agenda parallelism
  • work units are organized around activities

Master
Workers
15
Approach
  • Identify the job to be performed by the workers
  • Provide a master to manage the jobs
  • Identify the granularity of the jobs

16
Specialist parallelism
  • work units organized around specialists
  • decompose into appropriate specialists

Specialist
17
Supervisor-Worker
  • Task divided into n identical subtasks
  • Supervisor creates n subtask tuples and writes
    them into the tuple space
  • Workers read subtasks from the space and execute
    them, then put the results back
  • Supervisor reads the space for partial results
    and combines them, then finishes

18
Supervisor-Worker (1)
19
Supervisor-Worker (2)
20
Supervisor-Worker (3)
21
Supervisor
  • Pseudocode for the supervisor
  • Supervisor posts tasks, then collects results

public class Supervisor for (int i0
ilttotalTasks i) Tuple task new
Tuple("task") task.add(new Field("param1",
"value1")) space.out(task) for (int i0
ilttotalTasks i) Tuple template new
Tuple("result") template.add(new
Field("value", Value.class)) Tuple result
space.in(template)
22
Worker
  • Pseudocode for each worker
  • Repeatedly takes task, and posts result

public class Worker for () Tuple task
new Tuple("task") task.add(new Field("param1",
String.class)) Tuple result
compute(task) space.out(result)
23
Mandelbrot Sets
  • The Mandelbrot set is a fractal that has become
    popular far outside of mathematics both for its
    aesthetic appeal and its complicated structure,
    arising from a simple definition. This is largely
    due to the efforts of Benoît Mandelbrot and
    others, who worked hard to communicate this area
    of mathematics to the general public.

24
(No Transcript)
25
Mandelbrot Sets
  • Image of Mandelbrot set is a mapping from (x,y)
    coordinates to color values
  • Each point represents a complex numberC x y
    i
  • Iterate over the following sequenceZ(0)
    0Z(n1) Z(n)2 C
  • Run for a given number of iterations, or stop if
    Z(n) does not converge (for Z(n) gt 2)

26
Run the Example
  • Start the workers first, then the supervisor

gt java -Djava.security.policyall.policy
dpj.ts.TupleSpaceServer \ -debug mandelbrot gt
java -Djava.security.policyall.policy
dpj.ts.mandelbrot.Worker \ localhost mandelbrot gt
java -Djava.security.policyall.policy
dpj.ts.mandelbrot.Worker \ localhost mandelbrot gt
java -Djava.security.policyall.policy
dpj.ts.mandelbrot.Worker \ localhost
mandelbrot gt appletviewer Supervisor.html
27
Screenshot
28
Supervisor
public class Supervisor extends Applet implements
Runnable, MouseListener, MouseMotionListener
protected TupleSpace space protected int
xsize // dimensions of window protected int
ysize protected long job // id for this
computation protected int tasks // number of
tasks to generate protected int lines // of
scan lines per task protected Thread
supervisor // initial region for which
Mandelbrot is being computed protected double x1
-2.25 protected double x2 3.00 protected
double y1 -1.80 protected double y2 3.30
29
private void generateTasks() job
System.currentTimeMillis() Tuple task new
Tuple("task") task.add(new Field("job",
job)) // job identifier // region for which
application is computing Mandelbrot task.add(new
Field("x1", x1)) // starting x task.add(new
Field("x2", x2)) // ending x task.add(new
Field("y1", y1)) // starting y task.add(new
Field("y2", y1)) // ending y // slice of
region this tasks computes task.add(new
Field("width", xsize)) task.add(new
Field("height", ysize)) task.add(new
Field("lines", lines)) for (int i0 iltysize
ilines) task.add(new Field("start",
i)) space.out(task)
30
private void collectResults() while (true)
while (done) // sleep for 500
ms Tuple template new
Tuple("result") template.add(new Field("job",
job)) template.add(new Field("start",
Integer.class)) template.add(new
Field("points", Region.class)) Tuple
result result space.in(template) display
(result) progress progress lines if
(progress gt ysize-lines) done
true repaint() // graphics
code (see full code)
31
Worker
public class Worker protected TupleSpace
space public void run() throws
RemoteException for () Tuple task
space.in(taskTemplate()) Tuple result
compute(task) space.out(result) prot
ected Tuple taskTemplate() throws RemoteException
Tuple template new Tuple("task") template
.add(new Field("job", Long.class)) ...
32
public Tuple compute(Tuple task) double x,
y, xx, a, b int start ((Integer)
task.getField("start").getValue()).intValue()
// ... double da ((Double)
task.getField("x2").getValue()).doubleValue()/widt
h double db ((Double)
task.getField("y2").getValue()).doubleValue()/heig
ht b ((Double) task.getField("y1").getValue()
).doubleValue() int points new
intwidthlines for (int i 0 i lt start
i) b b db int k
0 for (int i start i lt end i, k)
a ((Double) task.getField("x1").getValue()).do
ubleValue() for (int j 0 j lt width
j) byte n 0 x 0.0
y 0.0
33
while ( (n lt 100) ( (xx)(yy) lt
4.0) ) xx x x - y y a
y 2 x y b x
xx n
pointsjk n // in range 0..100
a a da b b
db Tuple result new
Tuple("result") result.add(new Field("job",
task.getField("job").getValue())) result.add(new
Field("start", task.getField("start").getValue())
) result.add(new Field("points", new
Region(points))) return result
34
Design
  • Now for a closer look at the internals that were
    operating behind the scenes
  • Overall design
  • Representation of tuples
  • Management of tuple space (an opportunity to
    review concurrency mechanisms)
  • Use of Adapter pattern (ako Wrapper) to make
    tuple space a remote object

35
Tuple Space Design
36
Tuple
public class Tuple implements Serializable
protected String tag "" protected
Hashtable fields new Hashtable() public
Tuple(String tag) this.tag tag public
Tuple(String tag, Field field0)
this(tag) add(field0) public void
add(Field field) fields.put(field.getName(),
field)
37
Tuple
public boolean matches(Tuple other) if
(other null) return false // null
tuple if (!tag.equals(other.getTag())) return
false // not same tag if (size() !
other.size()) return false // not same number
of fields Enumeration e fields.elements() w
hile (e.hasMoreElements()) Field field
(Field) e.nextElement() Field otherField
other.getField(field.getName()) if
(!field.matches(otherField)) return false //
fields don't match return true
38
Field
public class Field implements Serializable
protected String name "" protected Object
value null protected Class type null //
Actual public Field(String name, Class type,
Object value) this.name name this.type
type this.value value //
Formal public Field(String name, Class type)
this.name name this.type type
39
Field
public boolean isFormal() return value
null public boolean matches(Field other)
if (other null) return false // null
tuple if (!other.getName().equals(name)) retu
rn false // not same name if
(!type.isAssignableFrom(other.getType())) retur
n false // not same type if (!isFormal()
!other.getValue().equals(value)) return false
// not same value // matches formal field of
same name and type return true
40
LocalTupleSpace
public class LocalTupleSpace protected
Hashtable bag new Hashtable() public
synchronized Tuple in(Tuple template) throws
InterruptedException Tuple tuple while
((tuple get(template, true)) null)
wait() return tuple public
synchronized Tuple inNoBlock(Tuple template)
return get(template, true)
41
LocalTupleSpace
  • Unlike in(), read() does not delete tuples

public synchronized Tuple read(Tuple
template) throws InterruptedException Tuple
tuple while ((tuple get(template, false))
null) wait() return
tuple public synchronized Tuple
readNoBlock(Tuple template) return
get(template, false)
42
LocalTupleSpace
private Tuple get(Tuple template, boolean
remove) String tag template.getTag() Vect
or v (Vector) bag.get(tag) if (v null
v.size() 0) return null // get the first
tuple from v that matches template Enumeration
e v.elements() while (e.hasMoreElements())
Tuple tuple (Tuple) e.nextElement() if
(template.matches(tuple)) if
(remove) v.remove(tuple) return
tuple return null // no matching
tuple in subbag
43
LocalTupleSpace
  • Out() unblocks in() and read()

public synchronized void out(Tuple tuple)
String tag tuple.getTag() // new tuples
are appended at the end of a vector // for a
tag, and removed in FIFO order Vector v
(Vector) bag.get(tag) if (v null) v
new Vector() bag.put(tag, v) v.addEleme
nt(tuple) notifyAll()
44
TupleSpaceImpl
public class TupleSpaceImpl extends
UnicastRemoteObject implements TupleSpace
protected LocalTupleSpace space //
target public TupleSpaceImpl() throws
RemoteException space new
LocalTupleSpace() public Tuple in(Tuple
template) throws RemoteException try //
delegate to target return space.in(template)
catch (InterruptedException e) throw new
RemoteException(e.toString()) ...
45
TupleSpaceServer
public class TupleSpaceServer public
static void main(String args) try
System.setSecurityManager(new
RMISecurityManager()) if (args0.startsWith("-d
")) TupleSpace server new
DebugTupleSpaceImpl() Naming.rebind(args1,
server) else TupleSpace server new
TupleSpaceImpl() Naming.rebind(args0,
server) catch (Exception e)
System.err.println(e)
Write a Comment
User Comments (0)
About PowerShow.com