Skip to content
systemcoding.dev
Concurrency

Choose OpenMP Schedules: static, dynamic and guided

Pick the right OpenMP loop schedule by looking at how iteration cost varies, with deterministic examples of static, dynamic, guided and runtime scheduling.

7 min read
Summarize in AI
Flat illustration of parallel threads meeting at a mutex, with the C, Go and Erlang logos.

A parallel loop is only as fast as its slowest thread, because the implicit barrier at the end waits for everyone. If iterations cost the same, giving each thread an equal block is perfect. If they do not, one thread finishes early and idles while another grinds on. The schedule clause is how you tell OpenMP how to hand out iterations, and the right choice comes from one question: how does the cost of iteration i vary with i? This post uses OpenMP 4.5 and GCC 14, shows exactly which thread gets which iteration under each schedule, and gives a rule for picking. It follows from parallelizing a loop with a pragma, where the default split was left unexamined.

See the assignment, not just the speed

Timing numbers depend on your machine. The assignment of iterations to threads does not, for the static schedules, so start there. This program records which thread ran each of 12 iterations and prints the table afterward.

schedule-static.cc
#include <stdio.h>
#include <omp.h>

#define N 12

static void show(const char *label, const int *owner) {
    printf("%-16s", label);
    for (int i = 0; i < N; i++) printf("%d ", owner[i]);
    printf("\n");
}

int main(void) {
    int owner[N];

    #pragma omp parallel for num_threads(4) schedule(static)
    for (int i = 0; i < N; i++) owner[i] = omp_get_thread_num();
    show("static", owner);

    #pragma omp parallel for num_threads(4) schedule(static, 1)
    for (int i = 0; i < N; i++) owner[i] = omp_get_thread_num();
    show("static,1", owner);

    #pragma omp parallel for num_threads(4) schedule(static, 2)
    for (int i = 0; i < N; i++) owner[i] = omp_get_thread_num();
    show("static,2", owner);

    return 0;
}
Text
static          0 0 0 1 1 1 2 2 2 3 3 3 
static,1        0 1 2 3 0 1 2 3 0 1 2 3 
static,2        0 0 1 1 2 2 3 3 0 0 1 1 

Row one is the block split: one contiguous chunk of three per thread. Row two deals iterations like cards, one at a time round-robin. Row three deals pairs. The assignment is fixed before the loop starts, which is why there is no scheduling overhead at run time. The OpenMP specification says that with schedule(static) and no chunk, the chunks are as equal as possible and at most one per thread; the second and third rows are the round-robin form with an explicit chunk size, and those assignments are mandated by the spec. This program assumes the runtime gives you the four threads you asked for.

When static is right

Use static when iteration cost is uniform or when it changes smoothly in a way a round-robin chunk can balance. A matrix scale, a vector add, a stencil update: every iteration does the same work, so equal blocks finish together and the cheapest scheduler wins. Static also has a property the dynamic schedules lack: the same iteration lands on the same thread every time you run the loop, so the data that thread touched last time is still warm in its cache. Programs that run the same loop repeatedly over the same array, such as an iterative solver, benefit from that.

Static fails when the cost is skewed. Consider a triangular workload where iteration i does i units of work.

triangular.cc
#include <stdio.h>
#include <omp.h>

#define N 8

int main(void) {
    long work[4] = {0, 0, 0, 0};

    #pragma omp parallel num_threads(4)
    {
        int t = omp_get_thread_num();
        long mine = 0;

        #pragma omp for schedule(static)
        for (int i = 0; i < N; i++) mine += i + 1;

        work[t] = mine;
    }

    for (int t = 0; t < 4; t++) printf("thread %d did %ld units\n", t, work[t]);
    return 0;
}
Text
thread 0 did 3 units
thread 1 did 7 units
thread 2 did 11 units
thread 3 did 15 units

The total is 36 units, so a perfect split is 9 each. Static blocks give thread 3 five times the work of thread 0, and the loop takes as long as thread 3 does. The counts are deterministic here because they are sums over a fixed assignment: thread 0 gets iterations 0 and 1 (weights 1 and 2), thread 1 gets 2 and 3 (3 and 4), and so on.

Dynamic: hand out work as threads become free

schedule(dynamic, chunk) keeps a queue of chunks. A thread takes the next chunk when it finishes its current one. Fast threads take more chunks, so the load balances itself.

schedule-dynamic.cc
#include <stdio.h>
#include <omp.h>

#define N 8

int main(void) {
    long work[4] = {0, 0, 0, 0};
    long total = 0;

    #pragma omp parallel num_threads(4) reduction(+:total)
    {
        int t = omp_get_thread_num();
        long mine = 0;

        #pragma omp for schedule(dynamic, 1)
        for (int i = 0; i < N; i++) mine += i + 1;

        work[t] = mine;
        total += mine;
    }

    printf("total units: %ld\n", total);
    return 0;
}
Text
total units: 36

The per-thread split is deliberately not printed, because under dynamic it changes from run to run: it depends on which thread asks for work first. What is stable is the total. The price is a synchronized operation, usually an atomic increment on a shared counter, every time a thread claims a chunk. With a chunk size of 1 and a loop body that takes nanoseconds, that overhead dominates. The rule of thumb is that each chunk of work should take much longer than a claim does, and you raise the chunk size to make that true.

dynamic is the right choice when iteration cost is unpredictable: ray tracing where some pixels hit nothing and some bounce a dozen times, graph traversal, anything that branches on data. The default chunk size for dynamic is 1, which is the most balanced and the most expensive.

Guided: big chunks first, small chunks last

schedule(guided, min) starts with large chunks and shrinks them as the loop drains. The runtime sizes each chunk roughly as the remaining iterations divided by the number of threads, but never smaller than min. Early on, chunks are big, so the claim overhead is low. At the end, chunks are small, so no thread is stuck with a big leftover. It is a compromise that works well when cost is somewhat predictable and the loop has many iterations.

schedule-guided.cc
#include <stdio.h>
#include <omp.h>

int main(void) {
    long hits = 0;

    #pragma omp parallel for schedule(guided, 4) reduction(+:hits)
    for (int i = 0; i < 1000; i++) {
        if (i % 7 == 0) hits++;
    }

    printf("multiples of 7 below 1000: %ld\n", hits);
    return 0;
}
Text
multiples of 7 below 1000: 143

Zero through 999 contains 143 multiples of 7 (0, 7, up to 994). The result is the same under every schedule, and that is the safety property: a schedule changes who does which iteration, never what the loop computes. If a schedule changes your answer, the loop had a dependence to begin with.

Let the environment decide: runtime and auto

schedule(runtime) defers the choice to the OMP_SCHEDULE environment variable, which makes it ideal for experiments: change the schedule without recompiling.

schedule-runtime.cc
#include <stdio.h>
#include <omp.h>

int main(void) {
    omp_sched_t kind;
    int chunk;
    long sum = 0;

    #pragma omp parallel for schedule(runtime) reduction(+:sum)
    for (int i = 1; i <= 100; i++) sum += i;

    omp_get_schedule(&kind, &chunk);
    printf("sum = %ld, kind = %d, chunk = %d\n", sum, (int)kind, chunk);
    return 0;
}
Shell
OMP_SCHEDULE="dynamic,8" ./schedule-runtime
Text
sum = 5050, kind = 2, chunk = 8

omp_get_schedule reports the runtime schedule, where kind values are 1 static, 2 dynamic, 3 guided and 4 auto in the enumeration omp_sched_t from omp.h. schedule(auto) hands the decision to the compiler and runtime, which in practice often means static, and tells you nothing portable. Use runtime while measuring, then bake the winner in.

A decision table

Cost of iteration iScheduleChunk advice
Equal for every istaticnone
Equal, but loop re-runs over the same datastaticnone, for cache reuse
Smoothly rising or falling (triangular)static, 1 or small chunkround-robin balances the slope
Random, data-dependentdynamicchunk large enough to amortize the claim
Many iterations, mildly unevenguidedsmall min, such as 4 to 16
Not known yet, experimentingruntimeset via OMP_SCHEDULE

The triangular case is worth knowing: static, 1 on the earlier loop gives the four threads 6, 8, 10 and 12 units, because each thread now takes one iteration from each end of the weight range. The spread between the busiest and idlest thread drops from 12 units to 6 with no run-time claiming at all. Cheap and partly balanced beats dynamic when the pattern is regular.

When not to tune the schedule

If the loop is memory-bound, no schedule fixes it: the threads are waiting on the memory bus, not on each other. If the imbalance comes from the data layout, such as one thread owning a hot region, changing the schedule hides the symptom. Profile first; profiling parallel scalability with perf shows how to see time spent in the barrier, which is the signature of imbalance. And if the loop is recursive or irregular rather than an index range, tasks fit better than any schedule: see OpenMP tasks for recursive parallelism.

For the authoritative option and behavior details, see the OpenMP specifications.

Share

Keep reading

Flat illustration of two options weighed side by side, with the C, C++ and Rust logos.
Performance

Spinlocks vs Mutexes: When Spinning Wins

A spinlock burns a core while it waits and a mutex sleeps. Compare their costs, write a correct C11 spinlock, and see when user-space spinning beats a mutex.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.