1 / 21

Threading Libraries

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.

una
Télécharger la présentation

Threading Libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threading Libraries CET306 Harry R. Erwin University of Sunderland

  2. 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.

  3. 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

  4. 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.

  5. 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

  6. POSIX Threads (Pthreads) • Explicit—you have to do everything. • Almost the same functionality as Windows Threads.

  7. Windows Threads • Uses HANDLE to access threads. • Mutual exclusion via mutexes and critical sections.

  8. C# • You have already had some exposure to C# threads. • Works only in C#. • Review materials follow.

  9. Threading in C# • Creating and running threads • Managing threads • Synchronisation

  10. 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.

  11. 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));

  12. Example PseudoCode Namespace whatever{ class Foo { // various variables and constructors public void Dowhatever() {etc.} // the run method static void Main(String[] args){ Foo f = new Foo(whatever); Thread t = new Thread(new ThreadStart(f.Dowhatever); t.start(); } } }

  13. 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.

  14. 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—

  15. 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.

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

  17. 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.

  18. Resulting Pay Over Time

  19. 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.

  20. 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.

  21. Discussion

More Related