Programming with Hyper-Threading Technology: How to Write Multithreaded Software for Intel IA-32 Processors

OpenMP provides an easy method for threading applications without burdening the programmer with the complications of creating, synchronizing, load balancing, and destroying threads. To achieve these requirements, the designers of OpenMP developed a platform-independent set of compiler pragmas, directives, function calls, and environment variables that explicitly instruct the compiler how and where to use parallelism in the application. Many loops can be threaded by inserting only one pragma right before the loop, as demonstrated in the following example. By leaving the nitty-gritty details to the compiler and OpenMP, you can spend more time determining which loops should be threaded and how to best restructure the algorithms for performance. The full potential of OpenMP is realized when it is used to thread the most time-consuming loops, that is, the hot spots.
The power and simplicity of OpenMP is best demonstrated by looking at an example. Figure 2.4 presented one example; now let's examine another. The following loop converts each 32-bit RGB (red, green, blue) pixel in an array into an 8-bit gray-scale pixel. The one pragma, which has been inserted immediately before the loop, is all that is needed for parallel execution under OpenMP.
#pragma omp parallel for for ( i = 0; i < numPixels; i++ ) { pGrayScaleBitmap[i] = (unsigned BYTE) ...