Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Similar documents
CS61C : Machine Structures

Review: Multicore everywhere!

Lecture #27 Parallelism in Software

Chae, Summer 2008 UCB. manycore is coming. Chae, Summer 2008 UCB

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

Chapter 5: Achieving Good Performance

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Threads need to synchronize their activities to effectively interact. This includes:

POSIX Threads. HUJI Spring 2011

Synchronising Threads

Why We Wait. IPC, Threads, Races, Critical Sections. Correct Ordering. Approaches to Waiting. Condition Variables 4/23/2018

W4118 Operating Systems. Instructor: Junfeng Yang

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

TCSS 422: OPERATING SYSTEMS

Multithreading Programming II

CS 3723 Operating Systems: Final Review

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

THREADS. Jo, Heeseung

ANSI/IEEE POSIX Standard Thread management

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Introduction to parallel computing

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Distributed systems. Programming with threads

Operating systems and concurrency (B08)

Locks. Dongkun Shin, SKKU

COMP 3430 Robert Guderian

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

CS Lecture 3! Threads! George Mason University! Spring 2010!

Computer Systems Laboratory Sungkyunkwan University

High Performance Computing Course Notes Shared Memory Parallel Programming

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

Synchroniza+on II COMS W4118

Thread and Synchronization

Chap. 6 Part 1. CIS*3090 Fall Fall 2016 CIS*3090 Parallel Programming 1

Process Synchronization (Part I)

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Lecture 13 Condition Variables

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

More Shared Memory Programming

Lecture 19: Shared Memory & Synchronization

Operating Systems. Synchronization

Shared Memory Parallel Programming with Pthreads An overview

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

Threading and Synchronization. Fahd Albinali

Synchronization. Dr. Yingwu Zhu

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

Message-Passing Shared Address Space

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Concurrent Server Design Multiple- vs. Single-Thread

POSIX PTHREADS PROGRAMMING

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

CS 261 Fall Mike Lam, Professor. Threads

CMSC 132: Object-Oriented Programming II

Synchronization Primitives

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Concurrent Programming

CPSC 261 Sample Midterm 2 March 2016

CS 153 Design of Operating Systems Winter 2016

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

CS 153 Lab6. Kishore Kumar Pusukuri

Chapter 4 Concurrent Programming

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

TCSS 422: OPERATING SYSTEMS

Paralleland Distributed Programming. Concurrency

Concurrency: a crash course

Concurrent Programming

Synchronization Spinlocks - Semaphores

Solving the Producer Consumer Problem with PThreads

Operating systems. Lecture 3 Interprocess communication. Narcis ILISEI, 2018

A Brief Introduction to OS/2 Multithreading

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

CS 167 Midterm Exam. Two Hours; Closed Book; Please Use a Black Pen March 23, 2017

Real Time Operating Systems and Middleware

Thinking parallel. Decomposition. Thinking parallel. COMP528 Ways of exploiting parallelism, or thinking parallel

CS 105, Spring 2007 Ring Buffer

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

Introduction to Parallel Programming Part 4 Confronting Race Conditions

CS 105, Spring 2015 Ring Buffer

CMSC 330: Organization of Programming Languages

CMSC Computer Architecture Lecture 12: Multi-Core. Prof. Yanjing Li University of Chicago

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

CS533 Concepts of Operating Systems. Jonathan Walpole

Multithread Programming. Alexandre David

CMSC421: Principles of Operating Systems

740: Computer Architecture Memory Consistency. Prof. Onur Mutlu Carnegie Mellon University

Introduction to Parallel Computing

CS 3214 Midterm. Here is the distribution of midterm scores for both sections (combined). Midterm Scores 20. # Problem Points Score

Review: Easy Piece 1

12:00 13:20, December 14 (Monday), 2009 # (even student id)

Why We Wait. Mutual Exclusion, Async Completions. Correct Ordering. Approaches to Waiting. Condition Variables 4/15/2017

Transcription:

Motivation and definitions Processes Threads Synchronization constructs Speedup issues Overhead Caches Amdahl s Law CS550: Advanced Operating Systems 2

If task can be completely decoupled into independent sub-tasks, cooperation required is minimal Starting and stopping communication Trouble when they need to share data! Race conditions: Thread A Thread B Scenario 1 readx incx writex readx incx writex time --> We need to force some serialization Synchronization constructs do that! Scenario 2 Thread A readx incx writex Thread B readx incx writex time --> CS550: Advanced Operating Systems 3

A lock (mutual exclusion, mutex) guards a critical section in code so that only one thread at a time runs its corresponding section acquire a lock before entering crit. section releases the lock when exiting crit. section Threads share locks, one per section to synchronize If a thread tries to acquire an in-use lock, that thread is put to sleep When the lock is released, the thread wakes up with the lock! (blocking call) CS550: Advanced Operating Systems 4

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int x; threada() { int temp = foo(x); pthread_mutex_lock(&lock); x = bar(x) + temp; pthread_mutex_unlock(&lock); // continue Thread } A readx acquirelock => SLEEP Thread B acquirelock readx time --> threadb() { int temp = foo(9000); pthread_mutex_lock(&lock); baz(x) + bar(x); x *= temp; pthread_mutex_unlock(&lock); // continue WAKE w/ LOCK releaselock } readx writex releaselock But locks don t solve everything Problem: potential deadlock! threada() { pthread_mutex_lock(&lock1); pthread_mutex_lock(&lock2); } threadb() { pthread_mutex_lock(&lock2); pthread_mutex_lock(&lock1); } CS550: Advanced Operating Systems 5

A condition variable (CV) is an object that threads can sleep on and be woken from Wait or sleep on a CV Signal a thread sleeping on a CV to wake Broadcast all threads sleeping on a CV to wake I like to think of them as thread pillows Always associated with a lock! Acquire a lock before touching a CV Sleeping on a CV releases the lock in the thread s sleep If a thread wakes from a CV it will have the lock Multiple CVs often share the same lock CS550: Advanced Operating Systems 6

Motivation and definitions Processes Threads Synchronization constructs Speedup issues Overhead Caches Amdahl s Law CS550: Advanced Operating Systems 7

More threads does not always mean better! I only have two cores Threads can spend too much time synchronizing (e.g. waiting on locks and condition variables) Synchronization is a form of overhead Also communication and creation/deletion overhead CS550: Advanced Operating Systems 8

Caches are often one of the largest considerations in performance For multicore, common to have independent L1 caches and shared L2 caches Can drive domain decomposition design CS550: Advanced Operating Systems 9

Applications can almost never be completely parallelized; some serial code remains 1 2 3 4 5 Number of Processors s is serial fraction of program, P is # of processors Amdahl s law: Parallel portion Speedup(P) = Time(1) / Time(P) 1 / ( s + ((1-s) / P) ), and as P 1/s Time Serial portion Even if the parallel portion of your application speeds up perfectly, your performance may be limited by the sequential portion CS550: Advanced Operating Systems 10

Super-linear speedup is possible Multicore is hard for architecture people, but pretty easy for software Multicore made it possible for Google to search the web CS550: Advanced Operating Systems 11

Super-linear speedup is possible True: more cores means simply more cache accessible (e.g. L1), so some problems may see super-linear speedup Multicore is hard for architecture people, but pretty easy for software False: parallel processors put the burden of concurrency largely on the SW side Multicore made it possible for Google to search the web False: web search and other Google problems have huge amounts of data. The performance bottleneck becomes RAM amounts and speeds! (CPU-RAM gap) CS550: Advanced Operating Systems 12

Threads can be awake and ready/running on a core or asleep for sync. (or blocking I/O) Use PThreads to thread C code and use your multicore processors to their full extent! pthread_create(), pthread_join(), pthread_exit() pthread_mutex_t, pthread_mutex_lock(), pthread_mutex_unlock() pthread_cond_t, pthread_cond_wait(), pthread_cond_signal(), pthread_cond_broadcast() Domain decomposition is a common technique for multithreading programs Watch out for Synchronization overhead Cache issues (for sharing data, decomposing) Amdahl s Law and algorithm parallelizability Reading Ch. 3 CS550: Advanced Operating Systems 13 Programming Assignment Part 1

Part 1: Peer-to-peer file sharing with centralized index Peer 1 1: registry(node 1,foo.avi) 4: obtain (foo.avi) Peer 2 Peer 3 2: search(foo.avi) 3: Node1, node2 Indexing server Foo.avi: Node1 Bar.c: Node 1 Foo.avi: Node 2 Mypic.gif: Node 3 CS550: Advanced Operating Systems 14

Two entities Central indexing server List of all files at peers Peer (both client and server) [client] Search for a file at the indexing server Download file from a peer, update indexing server [server] listen for download requests and service Provide concurrency at the central indexing server and peer Feel free to use any prog language and any mechanism (threads, RPC, RMI, sockets, semaphores ) CS550: Advanced Operating Systems 15

CS550: Advanced Operating Systems 16