threading libraries

21
Threading Libraries CET306 Harry R. Erwin University of Sunderland

Upload: una

Post on 06-Jan-2016

71 views

Category:

Documents


2 download

DESCRIPTION

Threading Libraries. CET306 Harry R. Erwin University of Sunderland. Texts. Clay Breshears (2009) The Art of Concurrency: A Thread Monkey's Guide to Writing Parallel Applications , O'Reilly Media. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Threading Libraries

Threading Libraries

CET306Harry R. Erwin

University of Sunderland

Page 2: Threading Libraries

Texts

• Clay Breshears (2009) The Art of Concurrency: A Thread Monkey's Guide to Writing Parallel Applications, O'Reilly Media.

• Mordechai Ben-Ari (2006) Principles of Concurrent and Distributed Programming, Second edition, Addison-Wesley.

Page 3: Threading Libraries

Libraries

• Implicit Threading (handle the details for you)– OpenMP– Intel Threading Building Blocks

• Explicit Threading (you do the work)– Posix Threads (Pthreads)– Windows Threads– C#

• GPU Threading (specialised)– CUDA– Ct– OpenCL

Page 4: Threading Libraries

OpenMP

• Uses special pragmas and directives inserted into your source code to define segments for concurrent execution.

• Used in FORTRAN, C, and C++.• All major compilers are compliant.• Uses a fork/join model, so it’s low-level.• #pragma omp command.• You can design looping for parallelization.• Tasks can also be specified.

Page 5: Threading Libraries

Intel Threading Building Blocks

• A C++ template-based library• Focuses on loop-level parallelism.• Works at a task rather than a thread level.• Provides thread-safe container classes. These

can be used independently of the TBB threading.

• Also provides mutexes

Page 6: Threading Libraries

POSIX Threads (Pthreads)

• Explicit—you have to do everything.• Almost the same functionality as Windows

Threads.

Page 7: Threading Libraries

Windows Threads

• Uses HANDLE to access threads.• Mutual exclusion via mutexes and critical

sections.

Page 8: Threading Libraries

C#

• You have already had some exposure to C# threads.

• Works only in C#.• Review materials follow.

Page 9: Threading Libraries

Threading in C#

• Creating and running threads• Managing threads• Synchronisation

Page 10: Threading Libraries

General

• To work with threads in C#, you need to import System and System.Threading. The code is:using System;using System.Threading;

• Some key classes are Thread and ThreadStart.

Page 11: Threading Libraries

Creating and Running Threads

• In C# you use a ThreadStart delegate to create a Thread. The argument to ThreadStart is the name of a method that returns void and will be the run() method of the thread.

• The Thread constructor can implicitly wrap its argument in a ThreadStart delegate.

• Thread can also take a lambda expression as an argument.– Thread t = new Thread( ()=>aMethod(arguments));

Page 12: Threading Libraries

Example PseudoCode

Namespace whatever{class Foo {// various variables and constructorspublic void Dowhatever() {etc.} // the run methodstatic void Main(String[] args){Foo f = new Foo(whatever);Thread t = new Thread(new ThreadStart(f.Dowhatever);t.start();}}

}

Page 13: Threading Libraries

Managing Threads

• This is where the various Thread methods come in. (next slide)

• The ThreadPool class is available and is used to make efficient use of multiple threads. You queue work items in the ThreadPool and provide callback delegates to be executed when the work items complete.

• We’ll get into this in more detail later.

Page 14: Threading Libraries

Thread

• Interesting members include:– Thread.Sleep(msec);– Name(string); —give it a name—– Priority(ThreadPriority); —give it a priority—– Abort(); —die—– Interrupt(); —wake immediately—– Join(); —have the calling thread wait until exit—– Resume(); —unsuspend—– Start(); —start the thread—– Suspend(); —take a break—

Page 15: Threading Libraries

Synchronisation

• If threads share data, access must be managed to avoid conflicts.

• One class used to manage this is the Monitor class. If you want to lock an Object, call Monitor.Enter(Object). When you’re done with it, remember to call Monitor.Exit(Object). This needs to be packaged in a try{} finally{} construct to avoid errors making locks permanent.

• The easiest way to do this is lock(this){etc.}

which hides the hassle from you.

Page 16: Threading Libraries

GPU Threading

• CUDA• Ct• OpenCL• Used in PC-based games• Specialised.

Page 17: Threading Libraries

Economics

• An aside to help you understand why the pay distribution has become so biased.

• Pay, like prices, is driven by supply and demand.• If a given skill package is widely available, it will

receive low pay.• If a skill package is hard to find, the pay will be high.• Common skill packages are easier to find than they

used to be and high skill packages are much rarer. Thus the increasing disparity.

Page 18: Threading Libraries

Resulting Pay Over Time

Low Pay Factory Skilled Professional0

0.5

1

1.5

2

2.5

CurrentTraditional

Page 19: Threading Libraries

Implications

• Specialise in skill packages that are rare and in demand.

• In the 1960s, math skills became very much in demand.– The first programming group I worked for was mostly

female, non-white, or other minority groups.• This bias is still the case. Most high-paid jobs demand

a level of numeracy that was uncommon before 1960.• The next slide is about graduate-level maths applied

to computing.

Page 20: Threading Libraries

Domain-Specific Libraries

• Intel Math Kernel Library– BLAS (Basic Linear Algebra Subroutines)– LAPACK (Linear Algebra Package)– DFT (Discrete Fourier Transforms)– VML (Vector Math Library)– VSL (Vector Statistics Library)

• Intel Integrated Performance Primitives– A broad range of mathematical functionality, all thread-safe.

• These libraries are mathematically sophisticated, which is the reason people who understand this stuff make millions.

Page 21: Threading Libraries

Discussion