comp.lang.ada
 help / color / mirror / Atom feed
From: reinert <reinkor@gmail.com>
Subject: How to make tasks to run in parallel
Date: Mon, 16 Oct 2017 21:58:56 -0700 (PDT)
Date: 2017-10-16T21:58:56-07:00	[thread overview]
Message-ID: <d60e5e8f-e06a-410c-bcce-bb8fba358d52@googlegroups.com> (raw)

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;


             reply	other threads:[~2017-10-17  4:58 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-17  4:58 reinert [this message]
2017-10-17  6:08 ` How to make tasks to run in parallel 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
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