Programming with Skeletons: Skandium
Patrizio Dazzi (ISTI-CNR)Distributed systems: paradigms and models
1mercoledì 14 marzo 12
How to contact mePatrizio DazziMail: [email protected]: +39 050 315 3074Web: http://hpc.isti.cnr.it/~dazzi
2mercoledì 14 marzo 12
What will we see ?
Frameworks for Programming in Parallel with Skeletons
Today SkandiumFriday SkeTo
3mercoledì 14 marzo 12
Today OutlineSkandium framework Overview
Future conceptUsing Skandium SkeletonsSample ApplicationsDemo on my machine
4mercoledì 14 marzo 12
Framework conceived by Mario LeytonINRIA, FranceUniversity of Chile
Previously implemented in ProActive
Strongly relies on Future - Wait-By-Necessity semantics
Java based framework
Home page: http://skandium.niclabs.cl/
Skandium History
5mercoledì 14 marzo 12
Traditional control flow
Method A calls Method BA execution is stopped until B ends its execution.... is this always the behavior we need ?NO! let’s see and example
6mercoledì 14 marzo 12
Example simple sequential computation
A computer C needs statistics about a set S of computersStatistics are computed locally by each computer belonging to SEach statistics requires T secondsTotal time required (T x #S) seconds + (Tcomm(req) x #S) + (Tcomm(answ) x #S)
7mercoledì 14 marzo 12
Showing the exampleObject A Object B
RMI invocation without Futures
res = B.method();method(){ ... return value;}
Now value is available
value
8mercoledì 14 marzo 12
FuturesNatively supported by Java from release 5.0
Result Placeholder
Needed to achieve Asynchrony with standard Javawithout callbacks!!!
E.g.Future<Integer> fi1 = methodCall(A);Future<Integer> fi2 = methodCall(B);
Suppose each call requires 3 seconds and youhave 2 cores
9mercoledì 14 marzo 12
A Future exampleObject A Object B
RMI invocation with Futures
method(){ ... return value;}
put value in the container already sent
res = B.method();empty container
If the res variable is not accessed the computation can continue
10mercoledì 14 marzo 12
How is it set up in Java ?
Typically used in RMI or RMI-like based communication modelsA Future represents the result of an asynchronous computation. Typically, it defines proper methods
to check if the computation has been completedto wait for its completion, and to retrieve the result of the computation.
From a more theoretical point of view this inter-object synchronization policy is known as wait-by-necessity
11mercoledì 14 marzo 12
Wait-by-necessity
Asynchronous execution of sequential code
the client waits when it logically needs to
12mercoledì 14 marzo 12
Skeletons in SkandiumA lot of available SkeletonsWe will focus on a subset:
Task Parallelfarmpipeline
Data Parallelmap
13mercoledì 14 marzo 12
Additional Entities
StreamsExecute interfaceFuture collections
14mercoledì 14 marzo 12
Farm in Skandium (1)Elaborations performed by multiple “workers”Each worker computes the same application code on different, independent tasks
Tasks Computed Tasks
15mercoledì 14 marzo 12
Farm in Skandium (2)
class Farm
Two Constructors
public class Farm<P,R> extends AbstractSkeleton<P,R>public abstract class AbstractSkeleton<P,R> implements Skeleton<P,R>
public Farm(Skeleton<P,R> skeleton)public Farm(Execute<P,R> execute)
16mercoledì 14 marzo 12
Sample Farm usage (1)
public class Worker implements Execute<Integer, Integer> {
@Override public Integer execute(Integer arg0) throws Exception { Thread.sleep(3000); return new Integer(((int)Math.pow(2, arg0))); }
}
Worker
17mercoledì 14 marzo 12
Sample Farm usage (2)public class FirstFarm {
public static void main(String[] args) {
Skeleton<Integer, Integer> farm = new Farm<Integer, Integer>(new Worker()); Vector<Future<Integer>> futures = new Vector<Future<Integer>>(); futures.add(farm.input(new Integer(2))); futures.add(farm.input(new Integer(3))); futures.add(farm.input(new Integer(4))); futures.add(farm.input(new Integer(5))); long currentTime = System.currentTimeMillis(); for(Future<Integer> future:futures){ Integer res = null; try { res = future.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println(res); } System.out.println("Time Elapsed: "+(System.currentTimeMillis()-currentTime)); System.exit(0); }}
18mercoledì 14 marzo 12
Sample Farm Usage (2)
Skeleton<Integer, Integer> farm = new Farm<Integer, Integer>(new Worker());
19mercoledì 14 marzo 12
Map in Skandium (1)Elaborations performed by multiple Processing elements (PEs)Each PE computes the same application code on a fragment of the input data
Data Computed Data
Split Merge
20mercoledì 14 marzo 12
Map in Skandium (2)
class Map
Two Constructors
public class Map<P,R> extends AbstractSkeleton<P,R> public abstract class AbstractSkeleton<P,R> implements Skeleton<P,R>
public <X,Y> Map(Split<P,X> split, Execute<X,Y> execute, Merge<Y,R> merge)public <X,Y> Map(Split<P,X> split, Skeleton<X,Y> skeleton, Merge<Y,R> merge)
21mercoledì 14 marzo 12
Sample Map usage (1)
public class CharProcessor implements Execute<Character, Character> {
@Override public Character execute(Character param) throws Exception { char c = param.charValue(); char upC = Character.toUpperCase(c); return new Character(upC); }
}
Computation executed by each processing element
22mercoledì 14 marzo 12
Sample Map usage (2)public class FirstMap {
public static void main(String[] args) throws Exception { int PARTS = Runtime.getRuntime().availableProcessors()*4; Skeleton<String, String> string = new Map<String, String>( new SplitInterval(), new CharProcessor(), new MergeResults());
long init = System.currentTimeMillis(); Future<String> future = string.input(new String("hello world today I feel good!"));
String result = future.get(); System.out.println((System.currentTimeMillis() - init)+"[ms]: "+result); }}
23mercoledì 14 marzo 12
Sample Map Usage (2)
Skeleton<String, String> map = new Map<String, String>(new SplitInterval(),new CharProcessor(),new MergeResults());
24mercoledì 14 marzo 12
Pipeline in Skandium (1)Applications organized in Stages
Each Stage performs a specific computation
Tasks Computed Tasks
25mercoledì 14 marzo 12
Pipeline in Skandium (2)Class Pipeline
Two Constructors
public class Pipe<P,R> extends AbstractSkeleton<P,R>
public Pipe(Skeleton<P,R> stage1, Skeleton<P,R> stage2)public Pipe(Execute<P,R> stage1,Execute<P,R> stage2)
26mercoledì 14 marzo 12
Sample Pipeline Usage (1)
public class Incr implements Execute<Integer, Integer> { public Integer execute(Integer arg0) throws Exception { return new Integer(arg0+1); }}
public class Power implements Execute<Integer, Integer> { public Integer execute(Integer arg0) throws Exception {
return arg0*arg0; }}
First Stage
Second Stage
27mercoledì 14 marzo 12
Sample Pipeline Usage (2)
public static void main(String[] args) throws Exception {
Skeleton<Integer, Integer> pipeline = new Pipe<Integer, Integer>(new Incr(), new Power());
Vector<Future<Integer>> futures = new Vector<Future<Integer>>(); try { futures.add(pipeline.input(new Integer(2))); futures.add(pipeline.input(new Integer(3))); } catch (Exception e1) { e1.printStackTrace(); } for(Future<Integer> future:futures){ Integer res = future.get(); System.out.println(res); } System.exit(0); }
28mercoledì 14 marzo 12
Sample Pipeline Usage (3)
Skeleton<Integer, String> pipeline = new Pipe<Integer, String>(new Incr(), new StringMaker());
29mercoledì 14 marzo 12
Compiling a Skandium Application
Sun Java Compiler, version >= 5
Add to the Java CLASSPATH the jar file in dist/lib
Use javac <source code>
30mercoledì 14 marzo 12
Questions ?
31mercoledì 14 marzo 12