Table of Contents

OpenMP Sections

Sections is the OpenMP construct for distributing a fixed set of independent code blocks across threads at compile time, without requiring a loop structure. This is useful for distinct operations like compressing a buffer, encrypting a header, and writing a log simultaneously, where each block is wrapped in #pragma omp section and shares no data with the others.

#pragma omp parallel sections
{
    #pragma omp section
    compress(data);
 
    #pragma omp section
    encrypt(header);
 
    #pragma omp section
    write_log(meta);
}

If there are more section blocks than threads, the excess blocks are queued and picked up as threads become free. If there are fewer blocks than threads, the extra threads sit idle. For a large or dynamic number of tasks, tasks are more flexible; sections is simpler when the count is small and fixed.