CS 284a, 8 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 8 October, 1997
CS 284a, 8 October 1997 Copyright (c) , John Thornley2 Processes and Threads Process includes address space. Thread is program counter and stack pointer. Process may have many threads. All the threads share the same address space. Processes are heavyweight, threads are lightweight. Processes/threads need not map one-to-one onto processors.
CS 284a, 8 October 1997 Copyright (c) , John Thornley3 Three Threads Within a Process function f function g code data heap PC 1 PC 2 PC 3 stack 1 stack 2 stack 3 SP 1 SP 2 SP 3
CS 284a, 8 October 1997 Copyright (c) , John Thornley4 Thread Execution Model pool of threads pool of processors
CS 284a, 8 October 1997 Copyright (c) , John Thornley5 Thread Execution Model: Key Points Pool of processors, pool of threads. Threads are peers. Dynamic thread creation. Can support many more threads than processors. Threads dynamically switch between processors. Threads share access to memory. Synchronization needed between threads.
CS 284a, 8 October 1997 Copyright (c) , John Thornley6 Why Use Threads? Representing Concurrent Entities –Concurrency is part of the problem specification. –Examples: systems programming and user interfaces. –Single or multiple processors. –This kind of multithreaded programming is difficult. Multiprocessing for Performance –Concurrency is under programmer’s control. –Programs could be written sequentially. –This kind of multithreaded programming should be easier.
CS 284a, 8 October 1997 Copyright (c) , John Thornley7 Commercial Thread Libraries Win32 threads (Windows NT and Windows 95). Pthreads (POSIX Thread Interface). (SGI IRIX, Sun Solaris, HP-UX, IBM AIX, Linux, etc.). Solaris threads (SunOS 5.x). All designed primarily for systems programming.
CS 284a, 8 October 1997 Copyright (c) , John Thornley8 Example: Win32 Threads Thread Management: CreateThread(), SuspendThread(), ResumeThread(), ExitThread(), TerminateThread(), WaitForSingleObject(), WaitForMultipleObjects(), CloseHandle() Critical Sections: InitializeCriticalSection(), DeleteCriticalSection(), EnterCriticalSection(), LeaveCriticalSection() Mutexes: CreateMutex(), OpenMutex(), WaitForSingleObject(), WaitForMultipleObjects(), ReleaseMutex(), CloseHandle() Semaphores: CreateSemaphore(), WaitForSingleObject(), WaitForMultipleObjects() ReleaseSemaphore(), CloseHandle() Events: CreateEvent(), SetEvent(), ResetEvent(), PulseEvent(), WaitForSingleObject(), WaitForMultipleObjects(), CloseHandle() Interlocked Operations: InterlockedIncrement(), InterlockedDecrement(), InterlockedExchange() Thread Priorities: SetThreadPriority(), GetThreadPriority()
CS 284a, 8 October 1997 Copyright (c) , John Thornley9 A Simple Example: Array Summation int array_sum(int n, int data[]) { int mid; int low_sum, high_sum; mid = n/2; low_sum = 0; high_sum = 0; #pragma multithreadable { for (int i = 0; i < mid; i++) low_sum = low_sum + data[i]; for (int j = mid; j < n; j++) high_sum = high_sum + data[j]; } return low_sum + high_sum; }
CS 284a, 8 October 1997 Copyright (c) , John Thornley10 typedef struct { int n, *data, mid; int *high_sum, *low_sum; } args_block; void sum_0(args_block *args) { for (int i = 0; i mid; i++) *args->low_sum = *args->low_sum + args->data[i]; } void sum_1(args_block *args) { for (int j = args->mid; j n; j++) *args->high_sum = *args->high_sum + args->data[j]; } int array_sum(int n, int data[]) { int mid; int low_sum, high_sum; args_block args; HANDLE threads[2]; DWORD thread_ID; mid = n/2; args.n = n; args.data = data; args.mid = mid; args.low_sum = &low_sum; args.high_sum = &high_sum; threads[0] = CreateThread(NULL, 0, sum_0, &args, 0, &thread_ID); threads[1] = CreateThread(NULL, 0, sum_1, &args, 0, &thread_ID); WaitForMultipleObjects(2, threads, TRUE, INFINITE); return low_sum + high_sum; } security attributes stack sizecreation status wait for all
CS 284a, 8 October 1997 Copyright (c) , John Thornley11 The Need for Synchronization Thread 1:... EnterCriticalSection(cs); sum = sum + f(x); LeaveCriticalSection(cs);... Thread 2:... EnterCriticalSection(cs); sum = sum + f(y); LeaveCriticalSection(cs);...
CS 284a, 8 October 1997 Copyright (c) , John Thornley12 Commodity Multithreaded Applications Example Problems: Spreadsheets, CAD/CAM, simulation, video/photo editing and production, games, voice/handwriting recognition, real-time 3D rendering, job scheduling, etc. etc. Need to run as fast as sequential on one processor. Need to run significantly faster on multiprocessors. No recompilation, no relinking, no reconfiguration. Need to adapt dynamically to changing resources. Need to be reliable and timely.
CS 284a, 8 October 1997 Copyright (c) , John Thornley13 The Challenge Ahead The challenge is not shared-memory versus message-passing. The challenge is multiprocessing versus no multiprocessing for commodity applications.
CS 284a, 8 October 1997 Copyright (c) , John Thornley14 An Aside: Automatic Parallelization Write a sequential program. Compiler transforms sequential program into efficient parallel (multithreaded) program (and pigs might fly). A very very very very very very very difficult problem. Decades of work on this problem. Some success with some regular scientific programs. Not a general solution (and probably never will be). Not applicable to large, irregular, dynamic programs.