Programming with Shared Memory

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Threads. Readings r Silberschatz et al : Chapter 4.
8a-1 Programming with Shared Memory Threads Accessing shared data Critical sections ITCS4145/5145, Parallel Programming B. Wilkinson Jan 4, 2013 slides8a.ppt.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
1 ITCS4145/5145, Parallel Programming B. Wilkinson Feb 21, 2012 Programming with Shared Memory Introduction to OpenMP.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Threads, Thread management & Resource Management.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Thread Libraries  Pthreads  Windows.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
CSCI-455/552 Introduction to High Performance Computing Lecture 19.
CS333 Intro to Operating Systems Jonathan Walpole.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
Operating Systems Process Creation
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
1 Programming with Shared Memory - 2 Issues with sharing data ITCS 4145 Parallel Programming B. Wilkinson Jan 22, _Prog_Shared_Memory_II.ppt.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Chapter 4 – Thread Concepts
Realizing Concurrency using the thread model
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Chapter 4: Multithreaded Programming
Protection of System Resources
Day 12 Threads.
Chapter 4 – Thread Concepts
Threads in C Caryl Rahn.
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
Multithreading Tutorial
Process Management Presented By Aditya Gupta Assistant Professor
Computer Engg, IIT(BHU)
Chapter 4: Threads Overview Multithreading Models Thread Libraries
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Chapter 4: Threads.
Programming with Shared Memory
Realizing Concurrency using the thread model
System Structure and Process Model
Multithreading Tutorial
CSCE 513 Computer Architecture
CS510 Operating System Foundations
Operating System Concepts
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Chapter 4: Threads & Concurrency
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
CS510 Operating System Foundations
Tutorial 4.
Programming with Shared Memory - 2 Issues with sharing data
Shared Memory Programming with Pthreads
Presentation transcript:

Programming with Shared Memory Multiprocessors and Multicore Processors ITCS4145/5145, Parallel Programming B. Wilkinson Aug 5, 2014 slides8a.ppt

Shared memory multiprocessor/multicore processor system Processors or processor cores Single address space exists – each memory location given unique address within single range of addresses. Any memory location can be accessible by any of the processors. Programming can take advantage of shared memory for holding data. However access to shared data by different processors needs to be carefully controlled, usually explicitly by programmer. Address 1 2 3 Memory locations

Methods for Programming Shared Memory Multiprocessors Using heavyweight processes – rarely used in shared memory (used in message passing systems) Using threads explicitly - e.g. Pthreads, Java threads Using a sequential programming language such as C supplemented with compiler directives and libraries for specifying parallelism. e.g. OpenMP. Underlying mechanism in OpenMP is thread-based. Using a “parallel programming” language, e.g. Ada, UPC - not popular. We will look mostly at thread API’s and OpenMP, but first review processes.

(Heavyweight) Processes Basically a self-contained program having its own allocation of memory, stack, registers, instruction pointer, and other resources. Operating systems often based upon notion of a process. Processor time shares between processes, switching from one process to another. Might occur at regular intervals or when an active process becomes delayed. Offers opportunity to de-schedule processes blocked from proceeding for some reason, e.g. waiting for an I/O operation to complete. Process is the basic execution unit in message-passing MPI.

As used to dynamically create a process from a process Fork pattern As used to dynamically create a process from a process fork Both main program and forked program sequence execute at the same time if resources available “Forked” child program sequence   parent program sequence Time Parent process Child process Although general concept of a fork does not require it, child process created by the Linux fork is a replica of parent program with same instructions and variable declarations even prior to fork. However, child process only starts at fork and both parent and child process execute onwards together.

Multiple and nested fork patterns Main program Parent program sequence   “Forked” child program sequence Both main program and forked program sequence execute at the same time if resources available   “Forked” grandchild program sequence  

“Forked” program sequence Fork-join pattern fork join Main program “Forked” program sequence Both main program and forked program sequence execute at the same time if resources available   Explicit “join” placed in calling parent program. Parent will not proceed past this point until child has terminated. Join acts a barrier synchronization point for both sequences. Child can terminate before join is reached, but if not, parent will wait for it terminate.

UNIX System Calls to Create Fork-Join Pattern No join routine – use exit() to exit from process and wait() to wait for child to complete: . pid = fork(); // returns 0 to child and positive # to parent (-1 if error) if (pid == 0) { // code to be executed by child } else { //code to be executed by parent } if (pid == 0) exit(0); else wait (0); // join

Using processes in shared memory programming Concept could be used for shared memory parallel programming but not much used because of overhead of process creation and not being able to share data directly between processes

Threads A separate program sequence that can be executed separately by a processor core, usually within a process. Threads share memory space and global variables but have their own instruction pointer and stack. An OS will manage the threads within each process. Example my destop i7-3770 quad core processor. Supports 8 threads simultaneously (hyperthreading)

Threads in shared memory programming A common approach, either directly creating threads (a low level approach) or indirectly.

Really low level -- Pthreads IEEE Portable Operating System Interface, POSIX standard. proc1(*arg) Fork-join “pattern”

// based upon wikipedia entry "POSIX Threads" http://en. wikipedia #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *slave(void *argument) { int tid = *((int *) argument); printf("Hello World! It's me, thread %d!\n", tid); return NULL; } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS]; int i; for (i = 0; i < NUM_THREADS; i++) { // create threads thread_args[i] = i; printf("In main: creating thread %d\n", i); if ( pthread_create(&threads[i], NULL, slave, (void *) &thread_args[i] ) != 0) perror("Pthread_create fails"); for (i = 0; i < NUM_THREADS; i++) { // join threads if ( pthread_join(threads[i], NULL) != 0 ) perror("Pthread_join fails"); printf("In main: thread %d is complete\n", i); printf("In main: All threads completed successfully\n"); return 0;

Sample Output Program on VM in directory Pthreads as hello.c Compile: cc –o hello hello.c -lpthread Very simple to compile, Just add pthread library, but Pthreads very low level programming In main: creating thread 0 In main: creating thread 1 In main: creating thread 2 In main: creating thread 3 In main: creating thread 4 Hello World! It's me, thread 4! Hello World! It's me, thread 0! Hello World! It's me, thread 1! Hello World! It's me, thread 2! In main: thread 0 is complete In main: thread 1 is complete In main: thread 2 is complete Hello World! It's me, thread 3! In main: thread 3 is complete In main: thread 4 is complete In main: All threads completed successfully Sample Output

Pthreads detached threads Threads not joined are called detached threads. When detached threads terminate, they are destroyed and their resource released. Fork pattern

Thread pool pattern Common to need group of threads to be used together from one execution point. Group of threads readied to be allocated work and are brought into service. Whether threads actually exist or are created just for then is an implementation detail. Thread pool implies threads already created. Probably best as eliminates thread creation overhead. Pool of threads waiting to allocated Activated threads sequences Main program Generally a synchronization point as fork-join pattern. Thread pool pattern or the thread team pattern is the underlying structure of OpenMP, see next.

Questions