Using Actors and the SALSA Programming Language to

  • Slides: 1
Download presentation
Using Actors and the SALSA Programming Language to Introduce Concurrency in Computer Science II

Using Actors and the SALSA Programming Language to Introduce Concurrency in Computer Science II Travis Desell (tdesell@cs. und. edu) Department of Computer Science, University of North Dakota Results Fibonacci Example SALSA’s syntax is extremely similar to Java, and it can utilize all of Java’s libraries (lines 7, 19). The new command creates a (concurrent) actor (lines 14 and 15), and <- sends asynchronous messages (lines 8, 14, 15). If a message or result of a message requires the result of another message (lines 8, 14, 15) it will not be sent until the required result has been sent with the pass statement (lines 12, 13, 14), similar to a return statement. The constructor taking a array of arguments serves as an actor’s main method (there are no static methods/fields, simplifying code for students. This example is also a simple introduction to concepts of divide & conquer, recursion and reduction. 1. behavior Fibonacci { 2. int n; 3. 4. Fibonacci(int n) { self. n = n; } 5. 6. Fibonacci(String[] arguments) { 7. n = Integer. parse. Int(arguments[0]); 8. self<-finish( self<-compute() ); 9. } 10. 11. int compute() { 12. if (n == 0) pass 0; 13. else if (n <= 2) pass 1; 14. else pass new Fibonacci(n-1)<-compute() 15. + new Fibonacci(n-2)<-compute(); 16. } 17. 18. ack finish(int value) { 19. System. out. println(value); 20. } 21. } 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 preand 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). Concurrency: 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. Trapezoidal Example 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. 1. behavior Trapezoidal. Worker { 2. double f(double x) { 3. pass 5 * x * x; 4. } 5. 6. Trapezoidal. Worker() { 7. } 8. 9. double calculate. Partial. Integral(double left. Endpoint, 10. double right. Endpoint, 11. int number. Trapezoids) { 12. double trapezoid. Width = (right. Endpoint - left. Endpoint) / number. Trapezoids; 13. 14. System. out. println("Calculating integral from: " + 15. left. Endpoint + " to " + right. Endpoint); 16. 17. double partial. Integral = 0. 0; 18. for (int i = 0; i < number. Trapezoids; i++) { 19. double left = left. Endpoint + (i * trapezoid. Width); 20. double right = left. Endpoint + ((i + 1) * trapezoid. Width); 21. 22. //The following works: 23. partial. Integral += trapezoid. Width * (self. f(right) + self. f(left)) / 2. 0; 24. } 25. 26. System. out. println("Partial integral: " + partial. Integral); 27. 28. pass partial. Integral; 29. } 30. } 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. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. import salsa_lite. language. Join. Director; import salsa_lite. runtime. io. Standard. Output; behavior Trapezoidal. Master { double integral; double get. Integral() { pass self. integral; } ack accumulate(double value) { self. integral += value; } Trapezoidal. Master(String[] arguments){ if (arguments. length != 4) { System. err. println("Error: wrong arguments. "); System. err. println("Usage: "); System. err. println("tjava Trapezoidal. Worker "); System. exit(0); } double left. Endpoint = Double. parse. Double(arguments[0]); double right. Endpoint = Double. parse. Double(arguments[1]); int number. Trapezoids = Integer. parse. Int(arguments[2]); int number. Actors = Integer. parse. Int(arguments[3]); if (number. Trapezoids % number. Actors > 0) { System. err. println("Error: number of trapezoids (" + number. Trapezoids + ") is not evenly divisible by number of actors (" + number. Actors + ")"); System. exit(0); } Asynchrony vs 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. self. integral = 0. 0; double slice. Size = (right. Endpoint - left. Endpoint) / number. Actors; int trapezoids. Per. Worker = number. Trapezoids / number. Actors; System. out. println("Slice size: " + slice. Size); System. out. println("Trapezeoids per worker: " + trapezoids. Per. Worker); Join. Director join. Director = new Join. Director(); for (int i = 0; i < number. Actors; i++) { double left = left. Endpoint + (i * slice. Size); double right = left. Endpoint + ((i + 1) * slice. Size); Trapezoidal. Worker worker = new Trapezoidal. Worker() on (i); 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). 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 move 1 meter to the left. Robot 2 will repeat this right-left process until it can move forward. 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). 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. self<-accumulate( worker<-calculate. Partial. Integral(left, right, trapezoids. Per. Worker ) ) @ join. Director<-join(); } join. Director<-resolve. After(number. Actors) @ new Standard. Output()<-println("The integral of f(x) = 5 x^3 from " + left. Endpoint + " to " + right. Endpoint + " is: " + self<-get. Integral()); 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. } } SALSA Information 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. Distributed vs Shared 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. Acknowledgements I would like to thank the students of the fall 2012 class of Computer Science II at the University of North Dakota for their patience and enthusiasm in participating in this research. This work was funded by the NSF/IEEE-TCPP Curriculum Initiative Early Adopter Program. The Third NSF/TCPP Workshop on Parallel and Distributed Computing Education (Edu. Par-13), held in conjunction with the 27 th IEEE International Parallel & Distributed Processing Symposium. Boston, Massachussets. May 20, 2013.