Unformatted text preview:

Thread Libraries Scala Agents Kenneth M Anderson University of Colorado Boulder CSCI 5828 Lecture 19 03 16 2010 University of Colorado 2010 1 Goals Threading Libraries Review material from Chapter 5 of Breshears Implicit Threading OpenMP Intel Threading Building Blocks Scala Agent Model go s goroutines Clojure s concurrency constructs Explicit Threading Pthreads Windows Threads JDK ruby python etc Introduce the Scala Agent Model 2 Goals Threading Libraries Review material from Chapter 5 of Breshears Implicit threading libraries manage threads for you OpenMP Intel Threading Building Blocks Explicit Threading Pthreads Windows Threads Introduce the Scala Agent Model 3 Implicit Threading Implicit threading libraries handle the task of creating managing and synchronizing threads If your concurrency needs can be handled by the limited features of an implicit threading library then you can write concurrent programs and not bother with the details of thread management We will only show high level examples in this lecture 4 Examples 5 OpenMP A set of compiler directives library routines and environment variables that specify shared memory concurrency in FORTRAN C and C Intel Threading Building Blocks A C template based library for loop level parallelism that concentrates on defining tasks rather than explicit threads Many others e g Scala agent model go s goroutines Clojure s refs atoms agents etc OpenMP I OpenMP directives indicate code that can be executed in parallel such sections are called parallel regions These directives control how code is assigned to threads To define a parallel region in C use a pragma pragma omp parallel This pragma will be followed by a block of code or even a single statement which will be assigned to threads automatically executed in parallel and automatically joined back to the main thread of control 6 OpenMP II The omp for construct will make a for loop concurrent There are options for static and dynamic scheduling There are options to make statements atomic or to ensure that only a single thread executes a statement There are options for specifying reductions combining a set of values across multiple threads into a single value Finally OpenMP provides features for creating thread local storage for instance loop variables are made thread specific as are any variables declared inside a parallel region 7 Returning to Pi 8 We return to the multithreaded program we saw earlier this semester that calculates an approximate value of PI The example code demonstrates an OpenMP parallel region with thread local storage and an automatic reduction In particular pragma omp parallel for private mid height reduction sum parallel section parallel for loop private vars mid and height automatic reduction of sum variable across threads using the plus operator storing result in sum static long num rects 1000000 int main int argc char argv double mid height width sum 0 0 int i double area width 1 0 double num rects pragma omp parallel for private mid height reduction sum for i 0 i num rects i mid i 0 5 width height 4 0 1 0 mid mid sum height area width sum printf The value of PI is f n area return 0 9 Results Work being assigned to both cores on this machine but utilization never goes over 100 CPU for this process Not clear why performance is not higher but I didn t have to write a single line of code to create threads assign work to them worry about sharing values across threads synchronizing them etc 10 Explicit Threading Explicit threading libraries require the programmer to control all aspects of threading including creating threads assigning tasks synchronizing controlling interactions between threads managing shared resources 11 Examples 12 Pthreads Stands for POSIX threads available on a wide number of platforms Window Threads Similar library created by Microsoft for Windows platform BUT explicit threading libraries are available in any language in which thread creation management are the responsibility of the programmer Java ruby python C C etc Pthreads Provides basic concurrency primitives to C programs pthread t is core data structure pthread create creates new threads pthread join will join a thread to the main thread of control pthread mutex lock provides mutual exclusion pthread cond wait and pthread cond signal provide functionality similar to Java s wait and notify methods Demonstration 13 Alternative Approaches As a result of these concerns computer scientists have searched for other ways to exploit concurrency in particular using techniques from functional programming Functional programming is an approach to programming language design in which functions are first class values with the same status as int or string you can pass functions as arguments return them from functions and store them in variables and have no side effects they take input and produce output this typically means that they operate on immutable values 14 Example I In python strings are immutable a Ken b a replace b Ken a Ken replace is a function that takes an immutable value and produces a new immutable value with the desired transformation it has no side effects 15 Example II Functions as values in python def Foo x y return x y add Foo add 2 2 4 Here we defined a function stored it in a variable and then used the call syntax with that variable to invoke the function that it pointed at 16 Example III continuing from previous example def DoIt fun x y return fun x y DoIt add 2 2 4 Here we defined a function that accepts three values some other function and two arguments We then invoked that function by passing our add function along with two arguments DoIt is an example of higher order functions functions that take functions as parameters Higher order functions are a common idiom in functional programming 17 Relationship to Concurrency How does this relate to concurrency It offers a new model for designing concurrent systems Each thread operates on immutable data structures using functions with no side effects A thread s data structures are not shared with other threads Work is performed by passing messages between threads If one thread requires data from another that data is copied and then sent Such an approach allows each thread to act like a single threaded program no danger of interference 18 Map Filter Reduce Three common higher order functions are map filter reduce map fun list list Applies fun to each element of list returns results in new list filter fun list list Applies boolean


View Full Document

CU-Boulder CSCI 5828 - Thread Libraries & Scala Agents

Documents in this Course
Drupal

Drupal

31 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

22 pages

Load more
Loading Unlocking...
Login

Join to view Thread Libraries & Scala Agents and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Thread Libraries & Scala Agents and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?