Using OpenMP with GCC

OpenMP is a programming standard for shared-memory parallel systems, proposed by the OpenMP Architecture Review Board (ARB). Its not a new language but a compilation directive extension to base languages, supporting C/C++. Since OpenMP supports different languages, specifics may vary.

OpenMP defines directives, runtime libraries, and environment variables, allowing users to gradually parallelize existing serial programs. Directives extend programming languages, providing support for parallel regions, work sharing, and synchronization constructs. Programmers add special #pragma omp statements to indicate their intent. Compilers compliant with OpenMP automatically parallelize the program, inserting necessary synchronization, mutual exclusion, and communication. If these pragmas are ignored or the compiler does not support OpenMP, the program reverts to a normal (serial) program that still runs correctly, just without multi-threading acceleration.

OpenMP is simple, portable, and scalable, and is widely accepted. Major processor platforms, such as Intel, AMD, IBM, and Loongson, support OpenMP compilers. The open-source compiler GCC also supports OpenMP.

OpenMP employs a fork-join execution model. Initially, only a master thread exists. When parallel computation is needed, several forked threads are spawned to execute paralel tasks. After the parallel code completes, the forked threads join, and control returns to the single master thread.

OpenMP is a thread-based parallel programming model. An OpenMP process consists of multiple threads using the fork-join model. The program starts with a single master thread executing serially. When encountering a parallel region, parallel execution begins. The process is as follows:

  1. Fork: The master thread creates a team of parallel threads. The code in the parallel region is executed in parallel by the master thread and the spawned threads.
  2. Join: When the spawned threads finish executing in the parallel region, they are either blocked or terminated. The computed results are collected by the master thread, and only the master thread continues execution.

Fork-join model

A typical fork-join execution model is illustrated below:

Fork-join diagram

OpenMP's programming model is thread-based and uses compilation directives for parallelization. There are three programming elements to control parallelization: compile directives, API functions, and environment variables.

For more details, refer to this blog post:

OpenMP Basic Concepts - CSDN

Test Code:

#include <stdio.h>

int main(void) {
    #pragma omp parallel
    {
        int id;
        printf("Hello World\n");
        for (id = 0; id < 6; id++) {
            printf("Iter: %d\n", id);
        }
    }
    printf("GoodBye World\n");
    return 0;
}

Compilation:

gcc -o test openmp.c -fopenmp -Wl,-Map=openmp.map

The -fopenmp option enables OpenMP. For debugging and static linking, additional -static and -g options can be added.

Compilation output

From the map file, we can see that the libgomp library is linked:

czl@czl-VirtualBox:~/WorkSpace/cnn$ cat openmp.map 
Archive member included to satisfy reference by file (symbol)

/usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)
                              /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o (__libc_csu_init)

As-needed library included to satisfy reference by file (symbol)

libgomp.so.1                  /tmp/ccFkqztK.o (GOMP_parallel@@GOMP_4.0)
libc.so.6                     /tmp/ccFkqztK.o (puts@@GLIBC_2.2.5)

Discarded input sections

...

Tags: OpenMP GCC Parallel Programming C

Posted on Sun, 28 Jun 2026 16:23:00 +0000 by goldfiles