Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท Concurrency and Memory

Parallelism with std.parallelism in D

Learn data parallelism in D with std.parallelism: parallel foreach, taskPool.reduce, tasks and work units, plus races to avoid; C pthread stand-ins split work across threads.

D offers several concurrency models. The simplest to adopt is data parallelism: you have a loop whose iterations are independent, and you want them spread across CPU cores. The std.parallelism module does that with a one-word change to a foreach. Later lessons cover message passing between threads and the garbage collector.

D code is read-only on this site. The runnable examples are C stand-ins that use POSIX threads (pthread) to split work the way the D thread pool does. The sandbox has a single CPU, so you will see correct results but not a speed-up.

parallel foreach

D
import std.parallelism;
import std.range : iota;

void main()
{
    auto data = new double[1_000_000];

    foreach (i, ref x; parallel(data))
    {
        x = i * 0.5;           // each element is independent
    }
}

parallel(data) returns a range wrapper whose foreach hands out work units (chunks of consecutive elements) to threads of the default taskPool. The loop body must not depend on other iterations and must not touch shared mutable state without protection. You can set the chunk size as a second argument: parallel(data, 1000). Use a bigger chunk for cheap bodies (less scheduling overhead) and a small one for expensive and uneven bodies (better balance).

parallel also works with iota, with arrays of arrays, and with any range that supports the needed operations. The loop blocks until every iteration has finished, so after it you can use the results normally.

Reductions: taskPool.reduce

A sum, product or maximum combines results from all elements. Doing it with a shared variable would be a data race, so use reduce:

D
auto total = taskPool.reduce!"a + b"(0.0, data);
auto biggest = taskPool.reduce!max(data);

reduce gives each thread its own partial result and combines the partials at the end. The operation must be associative (grouping does not change the answer); addition of integers and max are, but floating-point addition is only approximately so, so parallel sums can differ in the last digits from a serial sum.

Building a split-and-combine in C

Each thread sums its own slice, writes the result to its own slot, and the main thread combines them. There is no shared counter, so no race.

C Stand-in: parallel sum with one result slot per thread

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The slices are fixed here. D's pool makes them dynamic, so a thread that finishes early takes another chunk.

Tasks

For irregular work, create tasks explicitly:

D
auto t = task!expensive(arg1, arg2);
taskPool.put(t);            // starts on another thread
doOtherWork();
auto result = t.yieldForce; // waits for the answer

yieldForce runs other pool work while waiting if it can, whereas workForce and spinForce use different waiting strategies. Use task when you have a handful of heavy, unrelated jobs, and parallel when you have a large loop.

asyncBuf and map come in parallel flavors as well: taskPool.map!f(range) computes the results ahead of use in a buffer, which is useful for a pipeline stage.

The danger: data races

Two threads writing the same variable without synchronization is undefined behavior. The next program shows the fix with a mutex. Without the lock, counter++ is a read, an add and a write, and two threads can interleave them and lose updates; on a multi-core machine the total is then usually too low, and on the single-CPU sandbox it may happen to come out right, which is exactly why races are dangerous. Try deleting the lock and unlock lines to experiment.

C Stand-in: protecting a shared counter with a mutex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In D the equivalent tools are synchronized blocks, core.atomic (atomicOp!"+="(counter, 1)) and shared variables. D makes sharing explicit: ordinary variables are thread-local by default, and only shared or __gshared data is visible to every thread. That design removes whole classes of accidental sharing.

Guidelines

  • Parallelize only loops with enough work per iteration; tiny bodies get slower.
  • Never let iterations write the same element or variable; reduce instead.
  • Keep I/O and the garbage collector in mind: heavy allocation inside the loop can serialize threads on the GC (see the GC lesson).
  • Measure with and without parallel. Set a pool size with new TaskPool(n) if the default (CPU count minus one worker) is not right.
flowchart LR
    M["Main thread"] --> P["taskPool"]
    P --> W1["Worker 1: chunk A"]
    P --> W2["Worker 2: chunk B"]
    P --> W3["Worker 3: chunk C"]
    W1 --> J["Join: results ready"]
    W2 --> J
    W3 --> J

A parallel foreach splits the range into work units and waits for all of them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must be true of the iterations of a parallel foreach?

    The pool runs chunks on different threads in any order, so iterations cannot depend on each other.
  2. 2. Why use taskPool.reduce for a total instead of total += x in the loop?

    Unsynchronized writes to shared state are a race. reduce combines per-thread partials.
  3. 3. What does ordinary (unmarked) D data default to?

    D variables are thread-local unless marked shared or __gshared.
  4. 4. When is a bigger work-unit size better?

    Cheap iterations need bigger chunks to amortize scheduling overhead.

Summary

  • parallel(range) turns a foreach into a data-parallel loop on the taskPool.
  • Use reduce for sums and maxima; operations must be associative.
  • task runs heavy independent jobs; yieldForce collects the result.
  • Share data only deliberately: shared, synchronized and core.atomic protect it.

Next: Message Passing with std.concurrency