A scheduler can move an iteration to an idle worker; it cannot supply a value that has not yet been computed. The next step after load balancing is therefore to identify which orderings belong to the mathematics and which arise only from reuse of storage.
1. Draw an edge whenever order matters
A flow dependence (read after write) carries a value from a producer to a consumer. An anti-dependence (write after read) protects an old value from being overwritten too soon. An output dependence (write after write) determines which write survives. Conflicting accesses within a single iteration can be ordered normally; the challenge is an edge crossing iterations.
Move the consumer next to its producer
For and nonoverlapping arrays with at least elements, consider the source example:
for (int i = 1; i < n; ++i) {
b[i] += a[i-1];
a[i] += c[i];
}
Iteration reads the a[i] updated by iteration . Directly parallelising the loop changes which value is read. Reindex the consumer instead:
b[1] += a[0];
#pragma omp parallel for
for (int i = 1; i < n-1; ++i) {
a[i] += c[i];
b[i+1] += a[i];
}
a[n-1] += c[n-1];
Now each producer and consumer are in the same iteration. The boundary statements supply the unmatched first consumer and final producer. This is a dependence-preserving loop transformation, sometimes described here as shifting/skewing the iteration alignment. It works because different iterations otherwise touch disjoint elements; it is not a recipe for eliminating arbitrary recurrences.
Follow one produced value while the work-unit boundaries change. The transformation must move the consumer without reversing the value dependency.
The consumer must receive the updated value. Move the work-unit boundaries and see whether that requirement disappears.
Play, pause, or scrub to inspect a moment.
Paused. Each producer supplies a consumer in the next work unit, creating a cross-iteration ordering.
Preserve an old value by copying it
/* a has n+1 elements; b and c have n, without aliasing. */
for (int i = 0; i < n; ++i) {
double x = (b[i] + c[i]) / 2.0;
a[i] = a[i+1] + x;
}
Sequentially, a[i+1] is read before the next iteration overwrites it. A snapshot removes that anti-dependence:
for (int i = 0; i < n; ++i) a2[i] = a[i+1];
#pragma omp parallel for
for (int i = 0; i < n; ++i)
a[i] = a2[i] + (b[i] + c[i]) / 2.0;
Assume a2 is a distinct allocated array of length n; copying completes before the parallel loop. The extra memory and traffic may outweigh any speedup. This is the same principle as using separate old/new fields in a stencil.
Preserve the final writer explicitly
/* n > 0, arrays valid and nonoverlapping. */
double tmp;
#pragma omp parallel for lastprivate(tmp)
for (int i = 0; i < n; ++i) {
double x = (b[i] + c[i]) / 2.0;
a[i] += x;
tmp = 2.0*x;
}
d[0] = tmp;
The sequential version wrote d[0] on every iteration. Only its final value matters if there are no intervening readers or observable side effects. lastprivate preserves that value without concurrent writes to d[0]. For n=0, skip the assignment so the original d[0] remains unchanged. Do not list tmp in both private and lastprivate: the latter already provides private storage.
2. Expansion exposes a recurrence; it does not abolish it
The source combines a running sum with an independent element update:
for (int i = 0; i < n; ++i) {
y += a[i];
b[i] = (b[i] + c[i]) * y;
}
The required value is a prefix sum,
Store those values, then use them independently:
if (n > 0) {
double *y2 = malloc((size_t)n * sizeof *y2);
if (y2 == NULL) abort();
y2[0] = y + a[0];
for (int i = 1; i < n; ++i) y2[i] = y2[i-1] + a[i];
y = y2[n-1];
#pragma omp parallel for
for (int i = 0; i < n; ++i) b[i] = (b[i] + c[i]) * y2[i];
free(y2);
}
Include stdlib.h and check allocation-size representability in production. Assume distinct arrays and n>=0. This scalar expansion and loop fission parallelises the second phase. The first phase still has a serial recurrence; prefix-scan algorithms can reduce its span by changing association. Their floating-point outputs may differ from the sequential scan. The original summation order is retained in the code above.
3. A pointer is a value, its allocation is another object
private(ptr) normally gives an uninitialised private pointer, not a copy of the original address. firstprivate(ptr) copies the address, leaving the allocation shared. Neither creates independent scratch data.
#pragma omp parallel
{
double *scratch = malloc((size_t)n * sizeof *scratch);
if (scratch == NULL && n > 0) abort();
/* Initialise scratch before reading it. Use only within its lifetime. */
use_scratch(scratch, n);
free(scratch);
}
This fragment assumes a valid allocation size and a thread-safe use_scratch. Each allocation is distinct and is released after its final use. Allocate once per thread, rather than once per element, when scratch buffers are repeatedly reused.
4. Relaxed numerical order still requires legal memory access
The source called multiple writes of found=1 a “benign race”. Equal values do not remove the race. If no thread needs to observe the flag early, a reduction expresses the required existence test:
int found = 0;
#pragma omp parallel for reduction(|:found)
for (int i = 0; i < n; ++i)
found |= (a[i] > threshold);
The final integer is one if any test succeeds. An atomic write is another option if all accesses obey the required synchronisation, but a reduction avoids concurrent shared updates here.
The source's in-place two-dimensional relaxation also has dependencies:
A sequential sweep consumes a mixture of new and old values. Naively adding a parallel directive creates conflicting accesses, not a justified asynchronous solver. A two-buffer Jacobi-style update instead reads only an immutable old field:
#pragma omp parallel for
for (int i = 1; i < n-1; ++i)
for (int j = 1; j < n-1; ++j)
next[i][j] = (old[i][j] + old[i][j-1] + old[i][j+1]
+ old[i-1][j] + old[i+1][j]) / 5.0;
Keep boundary values defined in both buffers and swap only after the update completes. This changes the iteration scheme. Its fixed-point equation in the interior is , but matching fixed points does not establish matching convergence rates or convergence under arbitrary boundary conditions. In the linear form , convergence for every initial guess follows from . Legal asynchronous methods require additional convergence assumptions and synchronised/atomic accesses appropriate to their implementation. The OpenMP memory model supplies no exception for a race thought harmless by the programmer.
What makes this particular two-buffer method converge?
Suppose there are interior points, , surrounded by fixed Dirichlet boundary values. Subtract a fixed-point field from an iterate. The error is zero on the boundary and obeys the same five-point averaging rule in the interior. Hence
Nonincrease alone is not convergence to zero. Start with error one at every interior point. After one step, a point whose four neighbours are interior still has error one. The boundary must influence it before its error shrinks. With periodic boundaries, a constant error has no boundary to encounter and persists indefinitely.
For fixed Dirichlet values we can prove more than nonincrease. Expand an error after steps backwards through the averaging operations. Each step assigns weight to the current point and each neighbour. From every interior point there is a path of at most neighbour steps to the zero-error boundary. That path carries at least of the weight and contributes zero. All remaining weights are nonnegative, with sum at most . Therefore
Equivalently, the affine update applied times is a contraction on the finite interior field. It has a unique fixed point; that point is also fixed by one update, since applying one update maps an -step fixed point to another. Repeated blocks of steps contract by a factor strictly below one, and intervening steps cannot enlarge the error. Thus the two-buffer method converges to the unique fixed point for these boundary conditions in exact arithmetic. The bound is deliberately loose; it proves convergence without predicting a useful iteration count. Floating-point iteration still needs a residual tolerance and a stagnation check.
This proof depends on the immutable old field and the boundary condition. It proves neither the rate of an in-place sweep nor the validity of unsynchronised concurrent reads and writes. The distinction will matter again when MPI exchanges copies of boundary data.
The convergence proof explains why boundary influence eventually reaches every point. Watch that influence propagate over several discrete two-buffer steps, then remove the fixed boundary.
Begin with interior error one. Where must the first change appear, and can a constant error survive forever?
Play, pause, or scrub to inspect a moment.
Paused. Iteration 0; maximum displayed error 1. Intermediate geometry blends consecutive exact discrete states.
5. Nested teams add another resource constraint
#pragma omp parallel num_threads(2)
{
work_A();
#pragma omp parallel num_threads(4)
{ work_B(); }
}
Each outer thread encounters its own inner parallel region. If both inner teams are active with four members, there are eight participating inner threads in total; the outer threads also participate as their inner teams' primary threads. Calls are replicated according to this structure: work_A runs twice and work_B eight times in that case. This is not automatically a partition of either function's work.
export OMP_MAX_ACTIVE_LEVELS=2
omp_set_max_active_levels(2) sets the corresponding control programmatically; omp_get_level() reports nesting and omp_get_active_level() distinguishes active parallel levels. Legacy OMP_NESTED/omp_set_nested are deprecated. Resource limits can serialise inner regions or reduce team sizes; inspect actual sizes. Oversubscription can make nested execution slower. A single team with tasks or collapsed loops is sometimes a better design.
Exercises and continuation
- Verify the shifted producer/consumer loop for
n=2,3before timing larger inputs. Which aliases would invalidate the proof? - Explain why the scan expansion exposes parallel work but leaves a serial bottleneck.
- Compare two-buffer and sequential in-place relaxation using the same boundary data and residual stopping criterion. Distinguish a fixed point from the path to it.
- Inspect actual outer/inner team sizes and active levels; do not infer them from requested values.
OpenMP let us change execution while retaining shared storage. MPI changes the premise: another process's value is unavailable until we explicitly communicate it.