Parallel Loop Example (Image Processing)

Multithreading can be used to speed up the execution of a particular task. One of the simplest (and biggest) opportunities for parallelism is to split the the iterations of large loop amongst multiple threads, so that each thread is responsible for executing some subset of the iterations.

The following videos illustrate the Parallel Loop pattern through a classic image processing example: inverting the colors of an image. An image can be though of a giant array of pixels. To invert the colors of an image, the color inversion operation is simply performed (in a loop) on each pixel. The Parallel Loop pattern therefore assigns a subset of unique pixels to each thread, and has each thread loop through its subset of pixels, inverting each pixel as it goes.

You can watch a combined video of all the examples below, or explore separate vidoes in the subsections below:

Serial Approach (1 Thread)

The following video illustrates image inversion on one thread. The single thread loops through the entire array of pixels, inverting each one as it goes.

Parallel Approach (2 Threads)

The following video illustrates image inversion using two threads. Each thread is assigned half the total pixels in the image, and then loops through its assigned pixels, inverting each one as it goes.

Parallel Approach (4 Threads)

The following video illustrates image inversion using four threads.

Parallel Approach (8 Threads)

The following video illustrates image inversion using eight threads.

Parallel Approach (16 Threads)

The following video illustrates image inversion using four threads.

You have attempted of activities on this page