From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.fcku.it!peer02.fr7!futter-mich.highwinds-media.com!news.highwinds-media.com!fx26.fr7.POSTED!not-for-mail Subject: Re: How to make tasks to run in parallel Newsgroups: comp.lang.ada References: From: Per Sandberg User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Message-ID: X-Complaints-To: abuse@usenet.se NNTP-Posting-Date: Tue, 17 Oct 2017 06:17:53 UTC Organization: usenet.se Date: Tue, 17 Oct 2017 08:17:53 +0200 X-Received-Bytes: 6019 X-Received-Body-CRC: 449475094 Xref: news.eternal-september.org comp.lang.ada:48483 Date: 2017-10-17T08:17:53+02:00 List-Id: 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; >