comp.lang.ada
 help / color / mirror / Atom feed
From: Per Sandberg <per.s.sandberg@bahnhof.se>
Subject: Re: How to make tasks to run in parallel
Date: Tue, 17 Oct 2017 08:17:53 +0200
Date: 2017-10-17T08:17:53+02:00	[thread overview]
Message-ID: <lghFB.28855$I81.24241@fx26.fr7> (raw)
In-Reply-To: <d60e5e8f-e06a-410c-bcce-bb8fba358d52@googlegroups.com>

It won't run in parallel since all the computations are done within the 
roundevouz where both tasks are are running "in the same thread".
And also why a private Real type when there is a general Float.
Have a look on the updated code.
/P
----------------------------
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
with Ada.Numerics.Elementary_Functions;

procedure Main is

    use Ada.Text_IO;
    use Ada.Integer_Text_IO;
    use Ada.Float_Text_IO;
    use Ada.Numerics.Elementary_Functions;

    N : constant Integer := 10_000_000;

    -- 
---------------------------------------------------------------------------
    task type Test1_T (A : access Float);

    task body Test1_T is
       X : Float := A.all;
    begin
       -- stupid time consuming loop:
       for I in 1 .. N loop
          X := X + Float (I) / Float (N);
          X := 0.5 * X + 1.0 + Sin (X) * Cos (X) + Sin (X) + Cos (X) +
            Sin (X) * Sin (X) + Cos (X) * Cos (X);
       end loop;
       New_Line; Put ("test1 x:"); Put (X, 4, 16, 0); New_Line (1);
    end Test1_T;
    -- 
---------------------------------------------------------------------------
    task type Test2_T is
       entry Run1 (A : in Float);
    end Test2_T;

    task body Test2_T is
       X : Float;
    begin
       accept Run1 (A : in Float) do
          X := A;
       end Run1;
       -- stupid time consuming loop:
       for I in 1 .. N loop
          X := X + Float (I) / Float (N);
          X := 0.5 * X + 1.0 + Sin (X) * Cos (X) + Sin (X) + Cos (X) +
            Sin (X) * Sin (X) + Cos (X) * Cos (X);
       end loop;
       New_Line; Put ("test2 x:"); Put (X, 4, 16, 0); New_Line (1);
    end Test2_T;

    X     : aliased Float;
    Y     : Float;
    Dx, Dy : constant := 0.5;

    Test1 : array (1 .. 4) of access Test1_T;
    Test2 : array (1 .. 4) of access Test2_T;
    -- 
---------------------------------------------------------------------------

begin

    -- This goes in parallel:
    X := 1.0;
    for E of Test1 loop
       X := X + Dx;
       Put_Line (" Test1: ");
       E := new Test1_T (A => X'Access);
    end loop;

    Y := 1.0;
    for E of Test2 loop
       Y := Y + Dy;
       Put_Line (" Test2: ");
       E := new Test2_T;
       E.Run1 (Y);
    end loop;

end Main;
---------------------------------------------------------------------





Den 2017-10-17 kl. 06:58, skrev reinert:
> Hi,
> 
> The test program below includes two tasks. The first task gets an "argument" (input data) via a discriminant. The other task gets data via entry/accept. The first task runs easily in parallel. The other not.
> 
> I would prefer to enter data via entry/accept. But is it possible without giving up parallel processing? I feel it is something I should understand better here :-)
> 
> reinert
> 
> 
> with Text_IO;       use Text_IO;
> with Ada.Numerics.Generic_Elementary_Functions;
> with Ada.Text_IO;
> procedure ttest1 is
>    type Real is new Float;
>    package Flt_Io is new Text_IO.Float_Io   (Real);
>    package Int_Io is new Text_IO.Integer_Io (Integer);
>    package E_F is new Ada.Numerics.Generic_Elementary_Functions (Real);
>    use E_F,Flt_Io,Int_Io;
> 
>    N : constant Integer := 10_000_000;
> 
> -- ---------------------------------------------------------------------------
>    task type test1_t(a : access real);
> 
>    task body test1_t is
>        x : real := a.all;
>    begin
> -- stupid time consuming loop:
>        for I in 1 .. N loop
>            x := x + Real(I)/Real(N);
>            x := 0.5*x + 1.0 + sin(x)*cos(x) + sin(x) + cos(x) +
>                               sin(x)*sin(x) + cos(x)*cos(x);
>        end loop;
>        new_line;Put("test1 x:");Put (x,4,16,0); New_Line (1);
>    end test1_t;
> -- ---------------------------------------------------------------------------
>    task type test2_t is
>       entry run1(a : in real);
>    end test2_t;
> 
>    task body test2_t is
>      x : real;
>    begin
>        accept run1(a : in real) do
>          x := a;
> -- stupid time consuming loop:
>          for I in 1 .. N loop
>              x := x + Real(I)/Real(N);
>              x := 0.5*x + 1.0 + sin(x)*cos(x) + sin(x) + cos(x) +
>                               sin(x)*sin(x) + cos(x)*cos(x);
>          end loop;
>          new_line;Put("test2 x:");Put (x,4,16,0); New_Line (1);
>          return;
>        end run1;
>    end test2_t;
> 
>    x : aliased real;
>    y : real;
>    dx,dy : constant := 0.5;
> 
>    test1 : array(1..4) of access test1_t;
>    test2 : array(1..4) of access test2_t;
> -- ---------------------------------------------------------------------------
> 
> begin
> 
> -- This goes in parallel:
>       x := 1.0;
>       for e of test1 loop
>           x := x + dx;
>           Put_Line(" Test1: ");
>           e := new test1_t(a => x'access);
>       end loop;
> 
> -- This goes *not* in parallel (why?):
>       y := 1.0;
>       for e of test2 loop
>           y := y + dy;
>           Put_Line(" Test2: ");
>           e := new test2_t;
>           e.run1(y);
>       end loop;
> 
> end ttest1;
> 


  parent reply	other threads:[~2017-10-17  6:17 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-17  4:58 How to make tasks to run in parallel reinert
2017-10-17  6:08 ` gautier_niouzes
2017-10-17  7:25   ` reinert
2017-10-17 15:36   ` Jeffrey R. Carter
2017-10-21 12:32   ` Jacob Sparre Andersen
2017-10-17  6:17 ` Per Sandberg [this message]
2017-10-17  6:30   ` gautier_niouzes
2017-10-17  7:32     ` reinert
2017-10-17  7:47       ` Dmitry A. Kazakov
2017-10-17  8:06         ` Niklas Holsti
2017-10-17  8:48           ` Dmitry A. Kazakov
2017-10-17 11:13             ` Simon Wright
2017-10-17 12:11               ` Dmitry A. Kazakov
2017-10-17 14:23                 ` AdaMagica
2017-10-17  9:06         ` reinert
2017-10-17 10:04           ` Dmitry A. Kazakov
2017-10-17  7:29 ` Dmitry A. Kazakov
2017-10-17  7:36   ` reinert
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox