Travis Desell [email protected] Department of Computer Science University of North Dakota Using...

27
Travis Desell [email protected] Department of Computer Science University of North Dakota Using Actors and the SALSA Programming Language to Introduce Concurrency in Computer Science II The Third NSF/TCPP Workshop on Parallel and Distributed Computing Education (EduPar-13), held in conjunction with the 27th IEEE International Parallel & Distributed Processing Symposium . Boston, Massachussets. May 20, 2013.

Transcript of Travis Desell [email protected] Department of Computer Science University of North Dakota Using...

Travis [email protected]

Department of Computer ScienceUniversity of North Dakota

Using Actors and the SALSA Programming Language to Introduce Concurrency in Computer Science II

The Third NSF/TCPP Workshop on Parallel and Distributed Computing Education (EduPar-13), held in conjunction with the 27th IEEE International Parallel & Distributed Processing Symposium.

Boston, Massachussets. May 20, 2013.

Overview

• Why the Actor Model?• SALSA Examples• Programming Assignments• Survey• Conclusions and Future Work

Why the Actor Model?

• Actors react to messages by:• Change their state• Create new actors• Send new messages

• Actors process one message at a time from their mailbox.

• Shared memory is prohibited.

• This model makes programming deadlocks very difficult, and prevents concurrent memory access memories.

SALSA Examples

SALSA

behavior HelloWorld { HelloWorld( String[] args ) { StandardOutput standardOutput = new StandardOutput();

//There is no synchronization between the two following //messages, given the actor programming model, this could //print out either ‘Hello World!’ or ‘ World!Hello’ standardOutput<-print( "Hello" ); standardOutput<-println( " World!" ); }}

Continuations

//Salsa introduces synchronization of messages using @//@ guarantees the method before the @ will be processed before//the message afterward.

//This makes for a simple and easy introduction asynchrony.

behavior HelloWorld { HelloWorld( String[] args ) { StandardOutput standardOutput = new StandardOutput();

standardOutput<-print( "Hello" ) @ standardOutput<-println( "World!" ); }}

Token Passingbehavior Sum { int getValue() { //You can use Java within SALSA code (with a couple //notable differences). return (int)(Math.random() * 100); }

int sum(int a, int b, int c) { return a + b + c; }

Sum( String[] args ) { //Tokens are similar to futures. They allow you to explicitly //define dependencies and order between messages.

token int a = new Sum()<-getValue(); token int b = new Sum()<-getValue(); token int c = new Sum()<-getValue(); token int sum = self<-sum(a,b,c);

new StandardOutput()<-println(“The sum is: “ + sum); }

}

Implicit Token Passing

behavior Sum { int getValue() { return (int)(Math.random() * 100); }

Sum( String[] args ) { //The SALSA compiler and runtime are smart enough //to determine the required dependencies and make sure //the messages are processed in order. token int totalSum = self<-sum(new Sum()<-getValue(), new Sum()<-getValue(), new Sum()<-getValue());

new StandardOutput()<-println(“The sum is: “ + totalSum); }}

Implicit Token Passing and Expressions

behavior Sum { int getValue() { return (int)(Math.random() * 100); }

Sum( String[] args ) { //The SALSA compiler and runtime are smart enough //to determine the required dependencies and make sure //the messages are processed in order, while providing as //much concurrency as possible. new StandardOutput()<-println(“The sum is: “ + (new Sum()<-getValue() + new Sum()<-getValue() + new Sum()<-getValue()) );

}

First Class Continuations1. behavior Fibonacci {2. int n;3.  4. Fibonacci(int n) {5. self.n = n;6. } 7.  8. Fibonacci(String[] arguments) {9. n = Integer.parseInt(arguments[0]);10. 11. self<-finish( self<-compute() );12. } 13. 14. //The pass statement delegates the result of a message to that15. //of another message (as on lines 20-21), allowing recursion.16. int compute() {17. if (n == 0) pass 0;18. else if (n <= 2) pass 1;19. else pass new Fibonacci(n-1)<-compute()20. + new Fibonacci(n-2)<-compute();21. } 22. 23. ack finish(int value) {24. System.out.println(value);25. System.exit(0);26. }27.}

Programming Assignments

Students were given skeleton code for a parallel trapezoidal rule solver and asked to implement a version which could run any number of worker actors which could calculate slices of the trapezoidal rule, whose results would be summed up by the master actor. This example code is the solution.

Concurrent Trapezoidal Rule

1. behavior TrapezoidalWorker {2. double f(double x) { 3. pass 5 * x * x * x;4. } 5.  6. TrapezoidalWorker() {7. } 8.  9. double calculatePartialIntegral(double leftEndpoint,10. double rightEndpoint,11. int numberTrapezoids) {12. double trapezoidWidth = (rightEndpoint - leftEndpoint) / numberTrapezoids;13.  14. System.out.println("Calculating integral from: " +15. leftEndpoint + " to " + rightEndpoint);16.  17. double partialIntegral = 0.0;18. for (int i = 0; i < numberTrapezoids; i++) {19. double left = leftEndpoint + (i * trapezoidWidth);20. double right = leftEndpoint + ((i + 1) * trapezoidWidth);21.  22. //The following works:23. partialIntegral += trapezoidWidth * (self.f(right) + self.f(left)) / 2.0;24. } 25.  26. System.out.println("Partial integral: " + partialIntegral);27.  28. pass partialIntegral;29. } 30. }

1. import salsa_lite.language.JoinDirector;2. import salsa_lite.runtime.io.StandardOutput;3.  4. behavior TrapezoidalMaster {5. double integral;6.  7. double getIntegral() { pass self.integral; }8.  9. ack accumulate(double value) { self.integral += value; }10.  11. TrapezoidalMaster(String[] arguments){12. if (arguments.length != 4) { /* Error Message and quit */ }13. 14. double leftEndpoint = Double.parseDouble(arguments[0]);15. double rightEndpoint = Double.parseDouble(arguments[1]);16. int numberTrapezoids = Integer.parseInt(arguments[2]);17. int numberActors = Integer.parseInt(arguments[3]);18. 19. if (numberTrapezoids % numberActors > 0) { /* Error and quit */ }20. 21. self.integral = 0.0;22. 23. double sliceSize = (rightEndpoint - leftEndpoint) / numberActors;24. int trapezoidsPerWorker = numberTrapezoids / numberActors;25. 26. System.out.println("Slice size: " + sliceSize);27. System.out.println("Trapezeoids per worker: " + trapezoidsPerWorker);28. 29. JoinDirector joinDirector = new JoinDirector();30. for (int i = 0; i < numberActors; i++) {31. double left = leftEndpoint + (i * sliceSize);32. double right = leftEndpoint + ((i + 1) * sliceSize);33. 34. TrapezoidalWorker worker = new TrapezoidalWorker() on (i);35. 36. self<-accumulate( worker<-calculatePartialIntegral(left, right, trapezoidsPerWorker) ) @37. joinDirector<-join();38. }39. 40. joinDirector<-resolveAfter(numberActors) @41. new StandardOutput()<-println("The integral of f(x) = 5x^3 from " + leftEndpoint + " to " +42. rightEndpoint + " is: " + self<-getIntegral());43. }44. }

Students were also asked to fix a broken dining philosophers program, which resulted in deadlock. Example code can be provided on request.

Student performance was highly bimodal, which I think was the result of students lack of motivation, given the fact the labs were given at the end of the semester,and there were 25 other labs with an equal grade percentage.

Dining Philosophers

Bimodal Grade Distribution

Knowledge and Confidence Survey

A survey was given to students before and after the module on programming in SALSA. The Computer Science II course consisted of 34 students (22 were Computer Science majors). Of these students, 24 students took the pre-survey and 27 students took the post-survey, with 21 students taking both the pre- and post-survey.

Students were asked to mark if the scenario was an example of the topic in question and to provide their confidence in each answer (from 0 for no confidence to 5 for extremely confident).

However, most students only reported their confidence for scenarios that were marked, which unfortunately makes the confidence data of limited use. It also made it unclear if a student left a scenario unmarked if they actually giving an answer, or not answering at all.

Data is presented for correct, wrong and no answers (as opposed to just correct and wrong). A response was scored wrong if the student checked the scenario when it was not an example of the topic. A response was scored correct if the student marked (or did not mark) the scenario correctly. A response was scored as no answer if it should have been marked and was not (and no confidence was given).

Students were asked to check which of the following scenarios were examples of concurrency: Having each member in a group programming project work on a different section of code before combining them all at the end. Having one roommate wash the dishes while the other dries the washed dishes. Answering the questions in this survey sequentially, one after the other. Answering the questions in any order, returning later to questions you may have skipped.

Students were asked to check which of the following scenarios were examples of asynchrony or synchrony: Invoking a method on a Java object.

A. Sending someone an email and then working on something else while waiting for the response.

B. Hitting the submit button on a webpage, which updates a database on the web server before displaying a success webpage.

Students were asked to check which of the following scenarios were examples of determinism: Making ten six-sided dice rolls and calculating their sum.

A. Having two computer processes continuously send messages between each other for 10 seconds, and calculating the total number of messages sent. Performing Quicksort where the pivot is chosen randomly, using a random seed. Performing Quicksort where the pivot is chosen randomly, using the same seed.

Students were asked to check which of the following scenarios were examples of concurrency defects:

A. Three philosophers sit at a circular table with three forks. One for is in between each philosopher. Each philosopher will attempt to pick up the fork to the left of them and hold it, while trying to pick up the fork to the right of them. After a philosopher has both forks they will eat their meal and put the forks down (so the other philosophers can pick them up). A group of athletes are running in a line around a track. Every minute, the athlete in the back of the line will sprint to the front of the line. Two robots are coming at each other from opposite directions down a hall. The first robot is programmed to move 1 meter to the left if something is in its way. If something is still in its way it will attempt to move 1 meter to the right. Robot 1 will repeat this left-right process until it can move forward. The second robot is programmed to move 1 meter to the right if something is in its way. If something is still in its way it will move1 meter to the left. Robot 2 will repeat this right-left process until it can move forward.

Students were asked to check which of the following scenarios were examples of shared or distributed memory:

A. Using software like Dropbox to save copies of your homework and class materials to a remote cloud storage service which will sync the same files on your laptop and desktop in case one breaks.

B. Running a parallel mergesort on a computer with 4 processors, all of which have access to the same memory which stores the array being sorted.

C. Two teaching assistants have a stack of papers to grade. One TA will grade the first half of the papers and the other TA will grade the other half of the papers.

Students were asked to self assess their experience and interest in concurrent and distributed programming.

There was a significant gain in self assessed experience, and a minor improvement in interest (perhaps due to the number of non-majors in the course).

Conclusions

The most important results of the survey was to note the importance of how these topics are taught to beginning computer science – a two week module was sufficient to some students for making up their minds about further studying this subject.

Students performed better on survey questions that directly related to the in class assignments (e.g., the dining philosophers question).

Questions?

Email: [email protected]: http://tdesell.cs.und.edu/

More information about SALSA can be found at the programming language’s webpage:

http://people.cs.und.edu/~tdesell/salsa.php

Which contains an in depth tutorial and many more examples.

Contact Information