Overview of Ada 2022
Jeff Cousins
Contents   Index   Search   Previous   Next 

2.7 An example of the features working together

Not directly related to parallelism, Index parameters in array aggregates (AI12-0061) is also used in the example below as it is just such a useful new feature.
-- AI12-0241 Specifying Nonblocking for Language-Defined Units
-- AI12-0079-3 Global-in and global-out annotations – 
--   default Global => null (i.e. no read or write of any 
--   global variable) for Pure packages
-- package Ada.Numerics.Generic_Elementary_Functions
--     with Pure, Nonblocking is
--     function Sqrt (X : Float_Type'Base) return Float_Type'Base;
--    ...
with Ada.Numerics.Elementary_Functions;
-- ...
declare
   Max_CPUs_To_Use : constant := 10;
   Max                           : constant := 100;
   subtype Range_Type is Positive range 1 .. Max;
   type Float_Array_Type is array (Range_Type) of Float;
   type Positive_Array_Type is array (Range_Type) of Positive;
   -- AI12-0061 Index parameters in array aggregates
   -- modified by Container aggregates; generalized array
   -- aggregates (AI12-0212) to use [ ]
   Numbers : constant Positive_Array_Type :=
                  [ for I in Range_Type => I ];
   Squares                 : Positive_Array_Type;
   Square_Roots       : Float_Array_Type;
   Sum_Of_Squares : Integer;
   -- AI12-0064-2 - Nonblocking subprograms
   -- AI12-0079-3 Global-in and global-out annotations
   function Square (P : Positive) return Positive is (P**2)
      with Nonblocking, Global => null;
begin
   -- Ignoring any risk of overflows...
   -- AI12-0119 Parallel operations
   -- Iteration over the elements of an array
   -- AI12-0251-1 Explicit chunk definition for parallel loops
   parallel (Max_CPUs_To_Use)
   for I in Range_Type loop
      Squares           (I) := Square (I);
      Square_Roots (I) := Ada.Numerics.Elementary_Functions.Sqrt (Float (I));
   end loop;
   -- AI12-0242 Shorthand Reduction Expressions for Objects
   -- (dependent on AI12-0262 Map-Reduce attribute)
   Sum_Of_Squares := Squares'Reduce ("+", 0);
end;

Contents   Index   Search   Previous   Next 
© 2021, 2022 Jeff Cousins