300 likes | 395 Views
Explore the landscape of parallel computing, Amdahl’s Law, and more in this overview. Learn about threads in Unix environments and the importance of Pthreads for program performance gains.
E N D
Posix Threads CSCE 713 Advanced Computer Architecture • Topics • Pthreads • Readings January 12, 2012
Overview • Last Time • Power wall, ILP wall, to multicore • Seven Dwarfs • Amdahl’s Law, Gustaphson’s law • Landscape of Parallel Computing Research Berkeley View EECS-2006-183 • Readings for today • Chapter 25 Threads from Advanced Unix Programming 2nd ed. Richard Stevens and … (emailed) • http://www.kohala.com/start/ • New • Finish Slides from Lecture 1 • NP-Completeness and the Dwarves • PosixPthreads
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.
What are the Dwarves? • Benchmark suites SPEC 2006 • Very important problems • Prototypical problems • …
No Efficient Algorithms part 1 Computers and Intractability … NP-Completeness – Garey and Johnson
No Efficient Algorithms part 2 Computers and Intractability … NP-Completeness – Garey and Johnson
No Efficient Algorithms part 3 Computers and Intractability … NP-Completeness – Garey and Johnson
NP-Completeness Review • P • NP • NP-P – intractable • P transforms to Q • P is NP-Complete if • P is in NP • For all other problems Q in NP, Q transforms to P Computers and Intractability … NP-Completeness – Garey and Johnson
Proving NP-Completeness and Cooke’s Theorem • To prove Problem P is NP-Complete • Show P is in NP and • Show some known NP-Complete problem Q transforms to P • Cooke’s Theorem 1970 --- SAT (booleansatisfiability) is NP Complete. • http://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem Computers and Intractability … NP-Completeness – Garey and Johnson
Links: Threads, Unix Processes, • https://computing.llnl.gov/tutorials/pthreads/ • http://en.wikipedia.org/wiki/POSIX_Threads • http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html • http://www.cis.temple.edu/~ingargio/cis307/readings/system-commands.html • http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Unix System Related Commands • ps, kill, • top, nice, jobs, fg, bg • lscpu • /dev/proc https://computing.llnl.gov/tutorials/pthreads/
What is a Thread? • . https://computing.llnl.gov/tutorials/pthreads/
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: • 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/
Threads Sharing of Data • Because threads within the same process share resources: • 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/
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. https://computing.llnl.gov/tutorials/pthreads/
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 • pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread a • ... • matthews@saluda$ man -s 7 pthreads
More Unix • Includes • Libraries
Pthread Create • #include <pthread.h> • intpthread_create (pthread_t=tid , constpthread_attr_t*attr, • void * (*func) (void *), void *argv ); • Returns: 0 if OK, positive Exxxvalue on error APUE – Stevens et al Chapter 25
pthread_join Function • #include <pthread.h> • intpthread_join(pthread_t, tidvoid **status); • Returns: 0 if OK, positive Exxxvalue on error APUE – Stevens et al Chapter 25
thread_self Function • #include <pthread.h> • pthread_tpthread_self(void); • Returns: thread ID of calling thread APUE – Stevens et al Chapter 25
pthread_detach Function • #include <pthread.h> • intpthread_detach(pthread_ttid); • Returns: 0 if OK, positive Exxxvalue on error APUE – Stevens et al Chapter 25
pthread_exit Function • #include <pthread.h> • void pthread_exi t (void *status); • Does not return to caller
threads/tcpserv01.c - APUE Code • 1 #include "unpthread.h" • 2 static void *doit(void *); /* each thread executes this function */ • 3 int • 4 main(intargc, char **argv) • intlistenfd, connfd; • pthread_ttid; • socklen_taddrlen, len; • structsockaddr *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: tcpserv01 [ <host> ] <service or port>"); APUE – Stevens et al Chapter 25
16 cliaddrMalloc (addrlen) ; • 17 for (; ) { • 18 lenaddrlen; • 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);