DOC PREVIEW
CU-Boulder CSCI 5828 - Thread Libraries & Scala Agents

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© University of Colorado, 2010Thread Libraries & Scala AgentsKenneth M. AndersonUniversity of Colorado, BoulderCSCI 5828 — Lecture 19 — 03/16/20101GoalsThreading LibrariesReview material from Chapter 5 of BreshearsImplicit ThreadingOpenMP, Intel Threading Building Blocks, Scala Agent Model, go’s goroutines, Clojure’s concurrency constructs...Explicit ThreadingPthreads, Windows Threads, JDK, ruby, python, etc.Introduce the Scala Agent Model2GoalsThreading LibrariesReview material from Chapter 5 of BreshearsImplicit threading libraries manage threads for youOpenMP, Intel Threading Building Blocks, …Explicit ThreadingPthreads, Windows Threads, …Introduce the Scala Agent Model3Implicit ThreadingImplicit threading libraries handle the task ofcreating,managing, andsynchronizing threadsIf your concurrency needs can be handled by the limited features of an implicit threading library thenyou can write concurrent programs and not bother with the details of thread managementWe will only show high-level examples in this lecture4ExamplesOpenMPA set of compiler directives, library routines and environment variables that specify shared-memory concurrency in FORTRAN, C, and C++Intel Threading Building BlocksA C++ template-based library for loop-level parallelism that concentrates on defining tasks rather than explicit threadsMany otherse.g. Scala agent model, go’s goroutines, Clojure’s refs, atoms & agents, etc.5OpenMP (I)OpenMP directives indicate code that can be executed in parallel; such sections are called parallel regionsThese directives control how code is assigned to threadsTo define a parallel region in C++, use a pragma#pragma omp parallelThis 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 control6OpenMP (II)The omp for construct will make a for loop concurrentThere are options for static and dynamic schedulingThere 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 region7Returning to PiWe return to the multithreaded program we saw earlier this semester that calculates an approximate value of PIThe example code demonstrates an OpenMP parallel region with thread local storage and an automatic reductionIn particular#pragma omp parallel for private(mid, height) reduction(+:sum)parallel section, parallel for loopprivate vars mid and heightautomatic reduction of sum variable across threads using the plus operator, storing result in sum8static 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;}9Work being assigned to both cores on this machinebut utilization never goes over 100% CPU for this processNot clear why performance is not higherbut, 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.10Results?Explicit ThreadingExplicit threading libraries require the programmer to control all aspects of threading, includingcreating threadsassigning taskssynchronizing/controlling interactions between threadsmanaging shared resources11ExamplesPthreadsStands for POSIX threads, available on a wide number of platformsWindow ThreadsSimilar library created by Microsoft for Windows platformBUT, 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.12PthreadsProvides basic concurrency primitives to C programspthread_t is core data structurepthread_create creates new threadspthread_join will join a thread to the main thread of controlpthread_mutex_lock provides mutual exclusionpthread_cond_wait() and pthread_cond_signal() provide functionality similar to Java’s wait() and notify() methodsDemonstration13Alternative 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 values14Example (I)• In python, strings are immutablea = “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 effects15Example (II)• Functions as values (in python)def Foo(x, y): return x + yadd = Fooadd(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 at16Example (III)• continuing from previous exampledef 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 programming17Relationship 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


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
Download Thread Libraries & Scala Agents
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?