CSCE 713 Advanced Computer Architecture Posix Threads Topics

  • Slides: 29
Download presentation
CSCE 713 Advanced Computer Architecture Posix Threads Topics n Pthreads Readings January 12, 2012

CSCE 713 Advanced Computer Architecture Posix Threads Topics n Pthreads Readings January 12, 2012

Overview Last Time n Power wall, ILP wall, to multicore n Seven Dwarfs Amdahl’s

Overview Last Time n Power wall, ILP wall, to multicore n Seven Dwarfs Amdahl’s Law, Gustaphson’s law n n Landscape of Parallel Computing Research Berkeley View EECS-2006 -183 Readings for today n n Chapter 25 Threads from Advanced Unix Programming 2 nd ed. Richard Stevens and … (emailed) http: //www. kohala. com/start/ New n n – 2– n Finish Slides from Lecture 1 NP-Completeness and the Dwarves Posix Pthreads CSCE 713 Spring 2012

Books by Richard Stevens UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, Prentice

Books by Richard Stevens UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, Prentice Hall, 1999. UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI, Prentice Hall, 1998. TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols, Addison-Wesley, 1996. TCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, 1995. TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, 1994. Advanced Programming in the UNIX Environment, Addison-Wesley, 1992. UNIX Network Programming, Prentice Hall, 1990. – 3– CSCE 713 Spring 2012

More Dwarves later – 4– CSCE 713 Spring 2012

More Dwarves later – 4– CSCE 713 Spring 2012

What are the Dwarves? • Benchmark suites SPEC 2006 • Very important problems •

What are the Dwarves? • Benchmark suites SPEC 2006 • Very important problems • Prototypical problems • … – 5– CSCE 713 Spring 2012

No Efficient Algorithms part 1 – 6– Computers and Intractability … NP-Completeness – Garey

No Efficient Algorithms part 1 – 6– Computers and Intractability … NP-Completeness – Garey and Johnson CSCE 713 Spring 2012

No Efficient Algorithms part 2 – 7– Computers and Intractability … NP-Completeness – Garey

No Efficient Algorithms part 2 – 7– Computers and Intractability … NP-Completeness – Garey and Johnson CSCE 713 Spring 2012

No Efficient Algorithms part 3 – 8– Computers and Intractability … NP-Completeness – Garey

No Efficient Algorithms part 3 – 8– Computers and Intractability … NP-Completeness – Garey and Johnson CSCE 713 Spring 2012

NP-Completeness Review P NP NP-P – intractable P transforms to Q P is NP-Complete

NP-Completeness Review P NP NP-P – intractable P transforms to Q P is NP-Complete if 1. P is in NP 2. For all other problems Q in NP, Q transforms to P – 9– Computers and Intractability … NP-Completeness – Garey and Johnson CSCE 713 Spring 2012

Proving NP-Completeness and Cooke’s Theorem To prove Problem P is NP-Complete 1. Show P

Proving NP-Completeness and Cooke’s Theorem To prove Problem P is NP-Complete 1. Show P is in NP and 2. Show some known NP-Complete problem Q transforms to P Cooke’s Theorem 1970 --- SAT (boolean satisfiability) is NP Complete. • http: //en. wikipedia. org/wiki/Cook%E 2%80%93 Levin_theorem – 10 – Computers and Intractability … NP-Completeness – Garey and Johnson CSCE 713 Spring 2012

Links: Threads, Unix Processes, 1. https: //computing. llnl. gov/tutorials/pthreads/ 2. http: //en. wikipedia. org/wiki/POSIX_Threads

Links: Threads, Unix Processes, 1. https: //computing. llnl. gov/tutorials/pthreads/ 2. http: //en. wikipedia. org/wiki/POSIX_Threads 3. http: //download. oracle. com/javase/tutorial/essential/concurren cy/procthread. html 4. http: //www. cis. temple. edu/~ingargio/cis 307/readings/systemcommands. html 5. http: //www. yolinux. com/TUTORIALS/Linux. Tutorial. Posix. Thread s. html – 11 – CSCE 713 Spring 2012

Unix System Related Commands ps, kill, lscpu top, nice, jobs, fg, bg /dev/proc –

Unix System Related Commands ps, kill, lscpu top, nice, jobs, fg, bg /dev/proc – 12 – https: //computing. llnl. gov/tutorials/pthreads/CSCE 713 Spring 2012

What is a Thread? . – 13 – https: //computing. llnl. gov/tutorials/pthreads/ CSCE 713

What is a Thread? . – 13 – https: //computing. llnl. gov/tutorials/pthreads/ CSCE 713 Spring 2012

Threads in the Unix Environment • Exists within a process and uses the process

Threads in the Unix Environment • Exists within a process and uses the process resources • Has its own independent flow of control as long as its parent process exists and the OS supports it • Duplicates only the essential resources it needs to be independently schedulable • May share the process resources with other threads that act equally independently (and dependently) • Dies if the parent process dies - or something similar • Is "lightweight" because most of the overhead has already been accomplished through the creation of its process. • Because threads within the same process share resources: • • • – 14 – Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. Two pointers having the same value point to the same data. Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer. https: //computing. llnl. gov/tutorials/pthreads/ CSCE 713 Spring 2012

Threads Sharing of Data • Because threads within the same process share resources: •

Threads Sharing of Data • Because threads within the same process share resources: • • • – 15 – Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. Two pointers having the same value point to the same data. Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer. https: //computing. llnl. gov/tutorials/pthreads/ CSCE 713 Spring 2012

Sharing of Data between Processes – 16 – CSCE 713 Spring 2012

Sharing of Data between Processes – 16 – CSCE 713 Spring 2012

What are Pthreads? Why Pthreads? • What? POSIX is an acronym for Portable Operating

What are Pthreads? Why Pthreads? • What? POSIX is an acronym for Portable Operating System Interface • Why? The primary motivation for using Pthreads is to realize potential program performance gains. – 17 – https: //computing. llnl. gov/tutorials/pthreads/ CSCE 713 Spring 2012

Finding Information of Unix Script started on Thu 12 Jan 2012 09: 12: 55

Finding Information of Unix Script started on Thu 12 Jan 2012 09: 12: 55 AM EST matthews@saluda$ man pthread_create No manual entry for pthread_create matthews@saluda$ man -k pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread a. . . matthews@saluda$ man -s 7 pthreads – 18 – CSCE 713 Spring 2012

More Unix Includes Libraries – 19 – CSCE 713 Spring 2012

More Unix Includes Libraries – 19 – CSCE 713 Spring 2012

Pthread Create #include <pthread. h> int pthread_create (pthread_t =tid , const pthread_attr_t *attr, void

Pthread Create #include <pthread. h> int pthread_create (pthread_t =tid , const pthread_attr_t *attr, void * (*func) (void *), void *argv ); Returns: 0 if OK, positive Exxx value on error – 20 – APUE – Stevens et al Chapter 25 CSCE 713 Spring 2012

pthread_join Function #include <pthread. h> int pthread_join(pthread_t, tid void **status); Returns: 0 if OK,

pthread_join Function #include <pthread. h> int pthread_join(pthread_t, tid void **status); Returns: 0 if OK, positive Exxx value on error – 21 – APUE – Stevens et al Chapter 25 CSCE 713 Spring 2012

thread_self Function #include <pthread. h> pthread_t pthread_self(void); Returns: thread ID of calling thread –

thread_self Function #include <pthread. h> pthread_t pthread_self(void); Returns: thread ID of calling thread – 22 – APUE – Stevens et al Chapter 25 CSCE 713 Spring 2012

pthread_detach Function #include <pthread. h> int pthread_detach(pthread_t tid); Returns: 0 if OK, positive Exxx

pthread_detach Function #include <pthread. h> int pthread_detach(pthread_t tid); Returns: 0 if OK, positive Exxx value on error – 23 – APUE – Stevens et al Chapter 25 CSCE 713 Spring 2012

pthread_exit Function #include <pthread. h> void pthread_exi t (void *status); Does not return to

pthread_exit Function #include <pthread. h> void pthread_exi t (void *status); Does not return to caller – 24 – CSCE 713 Spring 2012

threads/tcpserv 01. c - APUE Code 1 #include "unpthread. h" 2 static void *doit(void

threads/tcpserv 01. c - APUE Code 1 #include "unpthread. h" 2 static void *doit(void *); /* each thread executes this function */ 3 int 4 main(int argc, char **argv) int listenfd, connfd; pthread_t tid; socklen_t addrlen, len; struct sockaddr *cliaddr; 10 if (argc == 2) 11 listenfd = Tcp_Iisten(NULL, argv[1], &addrlen); 12 else if (argc == 3) 13 listenfd = Tcp_Iisten(argv[1], argv[2], &addrlen); 14 else 15 err_quit("usage: tcpserv 01 [ <host> ] <service or port>"); – 25 – APUE – Stevens et al Chapter 25 CSCE 713 Spring 2012

16 cliaddr Malloc (addrlen) ; 17 for (; ) { 18 len addrlen; 19

16 cliaddr Malloc (addrlen) ; 17 for (; ) { 18 len addrlen; 19 connfd = Accept (listenfd, cliaddr, &len); 20 Pthread_create(&tid, NULL, &doit, (void *) connfd); 21 23 static void * 24 doit(void *arg) { Pthread_detach(pthread_self()) ; str_echo( (int) arg); /* same function as before */ Close((int) arg); /* we are done with connected socket */ return (NULL); – 26 – CSCE 713 Spring 2012

Embarrassingly Parallel to Inherently Sequential – 27 – CSCE 713 Spring 2012

Embarrassingly Parallel to Inherently Sequential – 27 – CSCE 713 Spring 2012

– 28 – CSCE 713 Spring 2012

– 28 – CSCE 713 Spring 2012

– 29 – CSCE 713 Spring 2012

– 29 – CSCE 713 Spring 2012