Overview of Ada 2022
Jeff Cousins
Contents   Index   Search   Previous   Next 

2.6 Control over the degree of parallelism

Explicit chunk definition for parallel loops (AI12-0251) gives the user control of the degree of parallelism, for example if processing 100 elements of an array on a 20 core machine one may wish to have 10 logical threads of control (potentially executing on one core each) each processing a group of 10 elements (leaving 10 cores free for other things). Such a group is referred to as a “chunk”.
The optional chunk specification (see RM 5.5) is placed, enclosed by round brackets, after the reserved word parallel.
In the more complicated form, an identifier is given that can be used within the parallel construct, for instance:
declare
   Partial_Sum : array (1 .. Max_CPUs_To_Use) of Integer := (others => 0);
begin
   parallel (Chunk in Partial_Sum'Range)
   for I in Arr'Range loop
      declare
         Z : Integer;
      begin
         Z := some_complicated_computation;
           ... -- Complex machinations
         Partial_Sum (Chunk) := @ + Arr (I)**2 * Z;
           ... -- Other stuff that also happens in this
           ... -- very complicated loop ...
      end;
   end loop;
   Sum := Partial_Sum'Reduce ("+", 0);
end;
This makes uses of a reduction expression, as described above, and @ is a shorthand for the left hand side (see 7.7 in the Others section).
In the simpler form, just the maximum number of logical threads of control to be created to execute the loop is given:
parallel (Max_CPUs_To_Use)
for I in Arr'Range loop
   A (I) := B (I) + C (I);
end loop;

Contents   Index   Search   Previous   Next 
© 2021, 2022 Jeff Cousins