Threaded Programming. Lecture 9: Alternatives to OpenMP

Similar documents
Advanced OpenMP. Other threading APIs

CS 3305 Intro to Threads. Lecture 6

Introduction to pthreads

Threads. Threads (continued)

CSE 333 SECTION 9. Threads

PROGRAMOVÁNÍ V C++ CVIČENÍ. Michal Brabec

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Threads. studykorner.org

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CS333 Intro to Operating Systems. Jonathan Walpole

Operating systems fundamentals - B06

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency. Johan Montelius KTH

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

PRACE Autumn School Basic Programming Models

POSIX Threads. HUJI Spring 2011

Parallel Programming Libraries and implementations

LSN 13 Linux Concurrency Mechanisms

CSE 333 Section 9 - pthreads

Multicore and Multiprocessor Systems: Part I

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CS510 Operating System Foundations. Jonathan Walpole

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

CS370 Operating Systems

pthreads CS449 Fall 2017

Multithreaded Programming

Shared Memory Programming. Parallel Programming Overview

ECE 598 Advanced Operating Systems Lecture 23

POSIX PTHREADS PROGRAMMING

Recitation 14: Proxy Lab Part 2

CS 326: Operating Systems. Process Execution. Lecture 5

CS370 Operating Systems

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Parallel Programming with Threads

Parallelization, OpenMP

CS510 Operating System Foundations. Jonathan Walpole

Lecture 4 Threads. (chapter 4)

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

CS 261 Fall Mike Lam, Professor. Threads

CSE 4/521 Introduction to Operating Systems

Concurrent Server Design Multiple- vs. Single-Thread

CS420: Operating Systems

COMP4510 Introduction to Parallel Computation. Shared Memory and OpenMP. Outline (cont d) Shared Memory and OpenMP

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

CS370 Operating Systems

Thread Fundamentals. CSCI 315 Operating Systems Design 1

Shared Memory Programming: Threads and OpenMP. Lecture 6

Parallel Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Parallel Programming. Libraries and Implementations

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Lecture 19: Shared Memory & Synchronization

Introduction to PThreads and Basic Synchronization

CS444 1/28/05. Lab 03

User Space Multithreading. Computer Science, University of Warwick

4.8 Summary. Practice Exercises

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

TCSS 422: OPERATING SYSTEMS

High Performance Computing Course Notes Shared Memory Parallel Programming

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

Threads. CS3026 Operating Systems Lecture 06

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

ENCM 501 Winter 2019 Assignment 9

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Threads. Jo, Heeseung

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Multiprocessors 2007/2008

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Moore s Law. Multicore Programming. Vendor Solution. Power Density. Parallelism and Performance MIT Lecture 11 1.

Operating systems. Lecture 10

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

Shared memory parallel computing

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

PThreads in a Nutshell

Scientific Computing WS 2017/2018. Lecture 25. Jürgen Fuhrmann Lecture 25 Slide 1

C Grundlagen - Threads

Introduction to Threads

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

CS 6400 Lecture 11 Name:

Creating Threads. Programming Details. COMP750 Distributed Systems

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

CISC2200 Threads Spring 2015

Recitation 14: Proxy Lab Part 2

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Computer Systems Laboratory Sungkyunkwan University

There are, of course, many other possible solutions and, if done correctly, those received full credit.

Embedded Multicore Building Blocks (EMB²)

Carnegie Mellon Performance of Mul.threaded Code

CS533 Concepts of Operating Systems. Jonathan Walpole

Processes and Threads

CS Operating Systems: Threads (SGG 4)

CSE 333 Final Exam June 8, 2016 Sample Solution

Questions from last time

Transcription:

Threaded Programming Lecture 9: Alternatives to OpenMP

What s wrong with OpenMP? OpenMP is designed for programs where you want a fixed number of threads, and you always want the threads to be consuming CPU cycles. cannot arbitrarily start/stop threads cannot put threads to sleep and wake them up later OpenMP is good for programs where each thread is doing (more-or-less) the same thing. Although OpenMP supports C++, it s not especially OO friendly though it is gradually getting better. OpenMP doesn t support Java OpenMP programs don t run on distributed memory architectures (e.g. clusters of PCs)

What s wrong with OpenMP? (cont.) Can do this Can do this Can t do this

What are the alternatives? Threading libraries POSIX threads Boost threads Intel TBB Java threads Other programming models MPI PGAS languages Novelties

Threading libraries Pure library interfaces (no directives) Lower level of abstraction than OpenMP requires more changes to code harder to maintain some are less portable than OpenMP Free software

Programming model Similar to OpenMP in many respects shared and private data similar synchronisation issues Routine to create a new thread pass as arguments a function for the new thread to execute, plus some arguments thread dies when the passed function exits Routines for synchronising threads wait for a thread to exit locks, barriers, semaphores, condition variables,. Some libraries have the notion of tasks pass a function to a thread pool: task gets executed by one of the threads sometime in the future

POSIX threads POSIX threads (or Pthreads) is a standard library for shared memory programming without directives. Part of the ANSI/IEEE 1003.1 standard (1996) Interface is a C library no standard Fortran interface can be used with C++, but not OO friendly Widely available even for Windows typically installed as part of OS code is pretty portable Lots of low-level control over behaviour of threads

Thread forking #include <pthread.h> int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine, void*), void *arg) Creates a new thread: first argument returns a pointer to a thread descriptor. can set attributes. new thread will execute start_routine(arg) return value is error code.

Thread joining #include <pthread.h> int pthread_join( pthread_t thread, void **value_ptr) Waits for the specified thread to finish. thread finishes when start_routine exits second argument holds return value from start_routine

Synchronisation Barriers Mutex locks Behaviour is essentially the same as the OpenMP lock routines. Condition variables Behaviour is essentially the same as wait/notify in Java

Hello World #include <pthread.h> #define NTHREADS 5 int i, threadnum[nthreads]; pthread_t tid[nthreads]; for (i=0; i<nthreads; i++) { threadnum[i]=i; pthread_create(&tid[i], NULL, hello, &threadnum[i]); } for (i=0; i<nthreads; i++) pthread_join(tid[i], NULL);

Hello World (cont.) void* hello (void *arg) { int myid; myid = *(int *)arg; printf( Hello world from thread %d\n, myid); } return (0);

BOOST threads C++ library for multithreaded programming Similar functionality to POSIX threads but with a proper OO interface Closest thing to a C++ standard new version of C++ standard (C++11) contains a standard thread library which is similar to BOOST threads. will take some time for stable compilers to appear Reasonably portable need to install and build library should be OK for most common C++ compilers http://www.boost.org/doc/libs/1_40_0/doc/html/thread.html

Hello world #include <boost/thread/thread.hpp> #include <iostream> void hello() { std::cout << "Hello world << std::endl; } int main(int argc, char* argv[]) { boost::thread thrd(&hello); thrd.join(); return 0; }

Intel Thread Building Blocks (TBB) C++ library for multithreaded programming Offers somewhat higher level of abstraction that POSIX/ BOOST threads notion of tasks rather that explicit threads support for parallel loops and reductions support for concurrency on containers Moderately portable support for Intel and gcc compilers on Linux and Mac OS X, Intel and Visual C++ on Windows no build required to install http://www.threadingbuildingblocks.org

Hello World #include <iostream> #include <tbb/parallel_for.h> using namespace tbb; class Hello { public: void operator()(int x) const { std::cout << "Hello world\n"; } }; int main() { // parallelizing: // for(int i = 0; i < 2; ++i) {... } parallel_for(0, 2, 1, Hello()); return 0; }

Java threads Threads are an inbuilt part of the Java language Very portable (available in every Java VM) Java has lots of nice properties as a programming language high performance isn t necessarily one of them! Well integrated into Java s OO model Both explicit thread creation and task models Synchronisation methods every object contains a mutex lock condition variables, barriers, explicit lock objects http://java.sun.com/docs/books/tutorial/essential/concurrency/

MPI Message Passing Interface (MPI) Principal method used for programming distributed memory architectures library interface for Fortran, C, C++ MPI programs will run just fine on multicore systems Need to install an MPI library and configure it to use shared memory to pass messages MPI programs typically take much longer to develop than OpenMP programs (months not weeks) extremely portable and scalable solution Hybrid OpenMP/MPI becoming a common solution for very large systems

PGAS languages Partitioned Global Address Space Genuine language extensions Co-array Fortran Unified Parallel C Designed to work on distributed memory systems and be easier to write than MPI will also work on shared memory Lack of mature, robust implementations that actually deliver good performance Not terribly portable at the moment

Novelties As multicore programming becomes mainstream, a lot of new programming languages/apis have appeared some commercial, some academic Intel ArBB, Cilk, OpenCL, HMPP, OpenACC, StarSs, X10, Chapel,. Some are designed to address accelerator devices such as GPUs as well as traditional multicore. Unclear which (if any) will become popular/mature/widely implemented. Probably best avoided in the short term, unless you wish to experience a lot of pain!

What to do? OpenMP is a good solution for many programs in the scientific computing area. However, there may be very sound reasons for not using OpenMP. If you are going to use one of the alternatives, you should convince yourself carefully of why you are not using OpenMP!