Download SALSA-Concurrency - Computer Science at RPI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

One-time pad wikipedia , lookup

Transcript
Concurrent Programming
Actors, SALSA, Coordination Abstractions
Carlos Varela
RPI
C. Varela
1
Actors/SALSA
•
Actor Model
– A reasoning framework to model concurrent
computations
– Programming abstractions for distributed open
systems
G. Agha, Actors: A Model of Concurrent Computation in Distributed
Systems. MIT Press, 1986.
•
SALSA
– Simple Actor Language System and
Architecture
– An actor-oriented language for mobile and
internet computing
– Programming abstractions for internet-based
concurrency, distribution, mobility, and
coordination
C. Varela and G. Agha, “Programming dynamically reconfigurable
open systems with SALSA”, ACM SIGPLAN Notices, OOPSLA
2001, 36(12), pp 20-34.
C. Varela
2
SALSA Support for Actors
•
•
•
•
•
Programmers define behaviors for actors.
Messages are sent asynchronously.
State is modeled as encapsulated objects/primitive types.
Messages are modeled as potential method invocations.
Continuation primitives are used for coordination.
C. Varela
3
Actor Creation
• To create an actor locally
TravelAgent a = new TravelAgent();
• To create an actor with a specified UAN and UAL:
TravelAgent a = new TravelAgent() at (uan, ual);
• At current location with a UAN:
TravelAgent a = new TravelAgent() at (uan);
C. Varela
4
Message Sending
TravelAgent a = new TravelAgent();
a <- book( flight );
C. Varela
5
Remote Message Sending
• Obtain a remote actor reference by name.
TravelAgent a =
getReferenceByName(“uan://myhost/ta”);
a <- printItinerary();
C. Varela
6
Migration
• Obtaining a remote actor reference and migrating
the actor.
TravelAgent a =
getReferenceByName(“uan://myhost/ta”);
a <- migrate( “rmsp://yourhost/travel” ) @
a <- printItinerary();
C. Varela
7
Token Passing Continuations
• Ensures that each message in the continuation expression is sent after
the previous message has been processed. It also enables the use of a
message handler return value as an argument for a later message
(through the token keyword).
– Example:
a1 <- m1() @
a2 <- m2( token );
Send m1 to a1 asking a1 to forward the result of processing m1 to a2
(as the argument of message m2).
C. Varela
8
Join Blocks
• Provide a mechanism for synchronizing the processing of a set of
messages.
• Set of results is sent along as a token.
– Example:
Actor[] actors = { searcher0, searcher1,
searcher2, searcher3 };
join { actors <- find( phrase ) } @
resultActor <- output( token );
Send the find( phrase ) message to each actor in actors[] then after all
have completed send the result to resultActor as the argument of an
output( … ) message.
C. Varela
9
Example: Acknowledged
Multicast
join{ a1 <- m1(); a2 <- m2; a3 <- m3(); … } @
cust <- n(token);
C. Varela
10
Lines of Code Comparison
Acknowledged Multicast
Java
Foundry
SALSA
168
100
31
C. Varela
11
First Class Continuations
• Enable actors to delegate computation to a third party independently of
the processing context.
• For example:
int m(…){
b <- n(…) @ currentContinuation;
}
Ask (delegate) actor b to respond to this message m on behalf of current actor
(self) by processing its own message n.
C. Varela
12
Fibonacci Example
module examples.fibonacci;
behavior Fibonacci {
int n;
Fibonacci(int n)
{ this.n = n; }
int add(int x, int y) { return x + y; }
int compute() {
if (n == 0)
return 0;
else if (n <= 2)
return 1;
else {
Fibonacci fib1 = new Fibonacci(n-1);
Fibonacci fib2 = new Fibonacci(n-2);
token x = fib1<-compute();
token y = fib2<-compute();
add(x,y) @ currentContinuation;
}
}
void act(String args[]) {
n = Integer.parseInt(args[0]);
compute() @ standardOutput<-println(token);
}
}
C. Varela
13
SALSA and Java
•
•
SALSA source files are compiled into Java source files before being compiled into
Java byte code.
SALSA programs may take full advantage of the Java API.
C. Varela
14
Hello World Example
module demo;
behavior HelloWorld {
void act( String[] argv ) {
standardOutput<-print( "Hello" ) @
standardOutput<-println( "World!" );
}
}
C. Varela
15
Hello World Example
• The act( String[] args ) message handler is
similar to the main(…) method in Java and is used to
bootstrap SALSA programs.
• When a SALSA program is executed, an actor of the given
behavior is created and an act(args) message is sent to
this actor with any given command-line arguments.
C. Varela
16
Migration Example
behavior Migrate {
void print() {
standardOutput<-println( "Migrate actor is here." );
}
void act( String[] args ) {
if (args.length != 3) {
standardOutput<-println("Usage: java migration.Migrate <UAN> <srcUAL> <destUAL>");
return;
}
UAN uan = new UAN(args[0]);
UAL ual = new UAL(args[1]);
Migrate migrateActor = new Migrate() at (uan, ual);
migrateActor<-print() @
migrateActor<-migrate( args[2] ) @
migrateActor<-print();
}
}
C. Varela
17
Migration Example
• The program must be given valid name and locations.
• After remotely creating the actor. It sends the print
message to itself before migrating to the second theater and
sending the message again.
C. Varela
18
Compilation
$ java SalsaCompiler demo/Migrate.salsa
SALSA Compiler Version 1.0: Reading from file demo/Migrate.salsa . .
.
SALSA Compiler Version 1.0: SALSA program parsed successfully.
SALSA Compiler Version 1.0: SALSA program compiled successfully.
$ javac demo/Migrate.java
$ java demo.Migrate
$ Usage: java migration.Migrate <uan> <ual> <ual>
• Compile Migrate.salsa file into Migrate.java.
• Compile Migrate.java file into Migrate.class.
• Execute Migrate
C. Varela
19
Migration Example
UAN
Server
theater
1
theater
2
The actor will print "Migrate actor just
migrated here." at theater 1 then at
C. Varela
theater 2.
20
World Migrating Agent Example
Host
Location
OS/JVM
Processor
yangtze.cs.uiuc.edu
Urbana IL, USA
Solaris 2.5.1 JDK 1.1.6
Ultra 2
vulcain.ecoledoc.lip6.fr
Paris, France
Linux 2.2.5 JDK 1.2pre2
Pentium II 350Mhz
solar.isr.co.jp
Tokyo, Japan
Solaris 2.6 JDK 1.1.6
Sparc 20
Local actor creation
386us
Local message sending
LAN message sending
WAN message sending
148 us
30-60 ms
2-3 s
LAN minimal actor migration
LAN 100Kb actor migration
WAN minimal actor migration
WAN 100Kb actor migration
150-160 ms
240-250 ms
3-7 s
25-30 s
C. Varela
21
Exercises
•
How would you implement the join continuation
linguistic abstraction in terms of message passing?
•
Download and execute the Migrate.salsa example.
•
Write a solution to the Flavius Josephus problem in
SALSA. A description of the problem is at VRH
Section7.8.3.
C. Varela
22