Parallel Computing


Parallel Computing

Maria
Let’s begin with a familiar work-rate problem: if one person takes ten days to finish a job, how long would ten people take if they worked together at the same rate? In the ideal case, one day. Each person does one-tenth of the work, and their efforts happen in parallel.

Anna
That idea is the starting point for parallelization in computing. A large job is divided into smaller pieces, and several computing entities work on those pieces at the same time. If the work divides neatly and the hardware can run the pieces simultaneously, the overall job can finish sooner.

Maria
Modern hardware can support this on a large scale. Graphics processing units, or GPUs, can perform thousands of tasks in parallel. Each task may do only a small part of a larger job, but doing many parts at once can greatly reduce the time needed for the whole job.

Anna
Our focus is one way to organize those pieces of work: threads. A thread is an execution entity, somewhat like a process, but much lighter weight. We’ll look at how threads differ from processes, how they can be created and managed, and how a program can use them to work in parallel.

Maria
Imagine a program that adds the first ten million positive integers by looping through them one at a time. A compiler such as GCC turns the program into an executable, and running that executable creates a process with its own memory space. On a machine with four processors, the operating system scheduler can assign that process to a processor.

Anna
But a single process with one execution path still does its loop sequentially. Even if the machine has four processors, that program uses just one processor at a time for this work, leaving the others idle or available for other tasks. The program’s long-running sum is therefore not making full use of the available processors.

Maria
One approach is to divide the ten million numbers into four ranges of about 2.5 million numbers each. Then create four processes: one sums the first range, another sums the second, and the other two sum the remaining ranges. If the four processes run on separate processors, they can calculate their partial sums in parallel.

Anna
In an ideal case, that could make the computation about four times faster, because each process handles one-quarter of the work at the same time. But this process-based approach comes with costs. Each process has its own instructions, data, heap, and stack, so the processes are isolated from one another.

Maria
That isolation means the processes need a way to exchange their partial results. They could use message passing, such as send and receive operations, or use shared memory created with help from system calls. Process creation, destruction, and other management operations also involve system calls.

Anna
System calls have overhead because they transfer control to the operating system, which handles the request. There can also be duplicated instructions and data: the four processes may all use the same program code and access the same global data, even though each process has its own copy in its separate process space.

Maria
Threads offer another way to divide the work while reducing some of that overhead. Instead of creating four processes, create one process and give it four threads. Each thread sums a different quarter of the numbers, and the threads can execute at the same time on different processors.

Anna
The key difference is that those four threads live inside the same process. They share the process’s code, global data, heap, and open files. Each thread has its own stack, though, which it needs for its local variables and function calls.

Maria
That private stack helps give each thread its own execution context. A thread also has its own register values, including registers that track instruction execution and the stack. The operating system can save that context when a thread pauses and restore it when the thread runs again.

Anna
Threads can be scheduled on separate processors, or on the same processor at different times. Either way, they are separate execution contexts within one process. Sharing the process’s global data and heap also makes it easier for threads in that process to communicate than for isolated processes.

Maria
That lighter-weight design has several advantages. Creating and managing threads can be much more efficient than creating and managing processes. For example, the source compares creating 50,000 processes with fork to creating 50,000 threads on the same machines; on one listed machine, the times are 8.1 and 0.9, respectively.

Anna
Communication can be more efficient, too. Processes generally need an inter-process communication mechanism, such as message passing or shared memory, and setting those up involves system calls. Threads in one process can communicate through the global data or heap that they already share.

Maria
Context switching between threads can also be faster than switching between processes. A thread’s context is smaller to save and restore. And because the threads belong to the same process, switching between them does not require changing to a different process’s page tables.

Anna
That means the existing page-table setup can stay in place, and cached translation lookaside buffer entries—the TLB entries—can remain valid. Switching between processes can involve changing page tables and refreshing the TLB, which adds work that a switch between threads in the same process avoids.

Maria
So let’s make the process-versus-thread distinction really clear. A process is an isolated execution entity with its own code, data, heap, and stack. A process can run with a single execution context, while a thread cannot run on its own: it must belong to a process.

Anna
Threads in the same process share its code, data, heap, and files, but each has its own stack. If one thread ends, its stack is reclaimed, while the other threads and the rest of the process can keep running. If the process itself terminates, all of its threads terminate with it.

Maria
There are multiple libraries for creating and managing threads. One popular library covered here is pthread. Its pthread_create function creates a new thread context in the process and starts that thread at a specified function.

Anna
The function takes four arguments. The first is a pointer where the new thread’s identifier, or TID, is stored. The second supplies thread attributes. The third is a pointer to the function the new thread should begin executing, and the fourth is a pointer to the arguments that function will receive.

Maria
When a thread has finished its work, pthread_exit can end it and pass along a return status. Another thread can use pthread_join to wait for a particular thread. The join call blocks the calling thread until the specified thread exits, then allows the caller to obtain that thread’s exit status.

Anna
Now apply those functions to the ten-million-number example. The program declares four pthread identifiers, creates four threads, and passes each one a different argument so it knows which range to sum. All four threads can begin at the same thread function, but the argument tells each thread which part of the calculation is its responsibility.

Maria
The example uses a shared global array with one slot for each partial sum. The thread assigned the first range writes its result to the first slot, the next thread writes to the second slot, and so on. After creating the threads, the main thread calls pthread_join for each one, waiting until all four have finished.

Anna
Once the joins complete, the program adds the four partial sums and prints the final result. That’s the whole strategy: split the big job, let each thread calculate a smaller piece, wait for the threads to finish, and combine their results.

Maria
To compile a pthread program with GCC on a Linux system, the example links the pthread library, using a command like gcc threads.c -lpthread. You can then compare a sequential version, a process-based version, and a thread-based version to see how their execution times differ.

Anna
Pthreads are not the only option. The material also mentions Windows threads for Windows applications, Boost for C++, and Linux threads, along with many other available thread libraries. The general idea stays the same: threads let a process contain multiple execution contexts that can share resources and perform work in parallel.

Ask anything
We've read this whole study set, so questions about it get the best answers.