Skip to content

CPU 1. Parallel Regions

Phillip Allen Lane edited this page Nov 27, 2023 · 1 revision

DotMP is focused on a fork-join paradigm of parallelism. This means that your program runs sequentially until a parallel region is hit. Execution then "forks" for the region, and is executed on numerous threads. Then, before execution can progress past the parallel region, execution must conclude. The threads are then "joined," and execution resumes sequentially.

To create a parallel region, you can use the DotMP.Parallel.ParallelRegion function.

DotMP.Parallel.ParallelRegion(() =>
{
    System.Console.WriteLine("Hello from thread {0}!", DotMP.Parallel.GetThreadNum());
});

This code introduces two new functions: ParallelRegion and GetThreadNum. ParallelRegion forks execution and executes the body in parallel across numerous threads. GetThreadNum gets the ID of the current executing thread. This is the output on my machine:

Hello from thread 2!
Hello from thread 4!
Hello from thread 0!
Hello from thread 1!
Hello from thread 5!
Hello from thread 3!

You may notice that it is non-deterministic in the order in which these messages are printed. For instance, if I run the code again, I see:

Hello from thread 3!
Hello from thread 2!
Hello from thread 0!
Hello from thread 4!
Hello from thread 1!
Hello from thread 5!

If you don't understand why this is, it may be worth going and learning more about parallel programming. This is not a parallel programming tutorial, but a DotMP tutorial.

ParallelRegion also supports an optional parameter: num_threads. Specifying this will tell the runtime how many threads to use. By default, DotMP tries to use as many threads as there are logical processors on your system. On my system, I have 6 logical processors, so the DotMP runtime detects this and automatically spawns 6 threads. If I want to specify more or less than that, I can use this optional parameter. Note that if I use this optional parameter out-of-place (e.g., as the first argument), I need to specify the name of the delegate, which is action. An example is shown here:

DotMP.Parallel.ParallelRegion(num_threads: 64, action: () =>
{
    System.Console.WriteLine("Hello from thread {0}!", DotMP.Parallel.GetThreadNum());
});

This will spawn 64 threads, and as expected, I get messages from thread numbers 0 through 63.