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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,edda2b296e2577cf X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!b19g2000prj.googlegroups.com!not-for-mail From: Anh Vo Newsgroups: comp.lang.ada Subject: Re: GNAT's Protected Objects Date: Mon, 8 Nov 2010 17:50:24 -0800 (PST) Organization: http://groups.google.com Message-ID: <6ede403f-7c92-4edc-82c3-1bc85cbc9cb7@b19g2000prj.googlegroups.com> References: <4af1a5f4-7bf3-47ee-af67-db50e589e7a8@n32g2000pre.googlegroups.com> NNTP-Posting-Host: 149.32.224.33 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1289267424 2345 127.0.0.1 (9 Nov 2010 01:50:24 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 9 Nov 2010 01:50:24 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b19g2000prj.googlegroups.com; posting-host=149.32.224.33; posting-account=Qh2kiQoAAADpCLlhT_KTYoGO8dU3n4I6 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6.6; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; MS-RTC LM 8),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:15393 Date: 2010-11-08T17:50:24-08:00 List-Id: On Nov 8, 2:32=A0pm, Jeffrey Carter wrote: > On 11/08/2010 02:38 PM, Anh Vo wrote: > > > > > How may tasks used, two or four, when slowness was observed when > > compared to simple task? I will be supprised if the answer is two. It > > is logically expected that two tasks should perform better than single > > task. However, when it comes to four or greater task, the result may > > not be true due to task switching cost. > > That's what I expected. However, any number of tasks > 1 took longer than= a > single task. > > > I would be glad to test it on my two core CPU mahine if the your > > little program is posted. > > I have appended the code to this message. Watch for line wrapping. > > -- > Jeff Carter > "Sir Robin the not-quite-so-brave-as-Sir-Lancelot, > who had nearly fought the Dragon of Angnor, > who nearly stood up to the vicious Chicken of Bristol, > and who had personally wet himself at the > Battle of Badon Hill." > Monty Python & the Holy Grail > 68 > > with Ada.Exceptions; > with Ada.Numerics.Float_Random; > with Ada.Real_Time; > with Ada.Text_IO; > > with System.Task_Info; > > procedure MP_Mult_PO is > =A0 =A0 Num_Processors : constant Positive :=3D System.Task_Info.Number_O= f_Processors; > > =A0 =A0 subtype Index_Value is Integer range 1 .. 500; > > =A0 =A0 type Matrix is array (Index_Value, Index_Value) of Float; > > =A0 =A0 function Mult (Left : in Matrix; Right : in Matrix; Num_Tasks : i= n Positive) > return Matrix; > =A0 =A0 -- Perform a concurrent multiplication of Left * Right using Num_= Tasks tasks > > =A0 =A0 function Mult (Left : in Matrix; Right : in Matrix; Num_Tasks : i= n Positive) > return Matrix is > =A0 =A0 =A0 =A0task type Calc_One; > > =A0 =A0 =A0 =A0protected Control is > =A0 =A0 =A0 =A0 =A0 procedure Get (Row : out Natural; Col : out Natural); > =A0 =A0 =A0 =A0 =A0 -- Returns the row and column of a result to calculat= e. > =A0 =A0 =A0 =A0 =A0 -- Returns zero for both when there are no more resul= ts to calculate. > =A0 =A0 =A0 =A0private -- Control > =A0 =A0 =A0 =A0 =A0 Next_Row : Positive :=3D 1; > =A0 =A0 =A0 =A0 =A0 Next_Col : Positive :=3D 1; > =A0 =A0 =A0 =A0 =A0 Done =A0 =A0 : Boolean =A0:=3D False; > =A0 =A0 =A0 =A0end Control; > > =A0 =A0 =A0 =A0Result : Matrix; > > =A0 =A0 =A0 =A0task body Calc_One is > =A0 =A0 =A0 =A0 =A0 Row : Natural; > =A0 =A0 =A0 =A0 =A0 Col : Natural; > =A0 =A0 =A0 =A0begin -- Calc_One > =A0 =A0 =A0 =A0 =A0 All_Results : loop > =A0 =A0 =A0 =A0 =A0 =A0 =A0Control.Get (Row =3D> Row, Col =3D> Col); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0exit All_Results when Row =3D 0; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0Result (Row, Col) :=3D 0.0; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0Sum : for K in Index_Value loop > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Result (Row, Col) :=3D Result (Row, Col) = + Left (Row, K) * Right > (K, Col); > =A0 =A0 =A0 =A0 =A0 =A0 =A0end loop Sum; > =A0 =A0 =A0 =A0 =A0 end loop All_Results; > =A0 =A0 =A0 =A0exception -- Calc_One > =A0 =A0 =A0 =A0when E : others =3D> > =A0 =A0 =A0 =A0 =A0 Ada.Text_IO.Put_Line (Item =3D> "Calc_One " & > Ada.Exceptions.Exception_Information (E) ); > =A0 =A0 =A0 =A0end Calc_One; > > =A0 =A0 =A0 =A0protected body Control is > =A0 =A0 =A0 =A0 =A0 procedure Get (Row : out Natural; Col : out Natural) = is > =A0 =A0 =A0 =A0 =A0 begin -- Get > =A0 =A0 =A0 =A0 =A0 =A0 =A0if Done then > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Row :=3D 0; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Col :=3D 0; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return; > =A0 =A0 =A0 =A0 =A0 =A0 =A0end if; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0Row :=3D Next_Row; > =A0 =A0 =A0 =A0 =A0 =A0 =A0Col :=3D Next_Col; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0if Next_Col < Index_Value'Last then > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Next_Col :=3D Next_Col + 1; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return; > =A0 =A0 =A0 =A0 =A0 =A0 =A0end if; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0Next_Col :=3D 1; > =A0 =A0 =A0 =A0 =A0 =A0 =A0Next_Row :=3D Next_Row + 1; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0Done :=3D Next_Row > Index_Value'Last; > =A0 =A0 =A0 =A0 =A0 end Get; > =A0 =A0 =A0 =A0end Control; > =A0 =A0 begin -- Mult > =A0 =A0 =A0 =A0Create_Tasks : declare > =A0 =A0 =A0 =A0 =A0 type Task_List is array (1 .. Num_Tasks) of Calc_One; > > =A0 =A0 =A0 =A0 =A0 Tasks : Task_List; > =A0 =A0 =A0 =A0begin -- Create_Tasks > =A0 =A0 =A0 =A0 =A0 null; -- Wait for all tasks to complete > =A0 =A0 =A0 =A0end Create_Tasks; > > =A0 =A0 =A0 =A0return Result; > =A0 =A0 exception -- Mult > =A0 =A0 when E : others =3D> > =A0 =A0 =A0 =A0Ada.Text_IO.Put_Line (Item =3D> "Mult " & > Ada.Exceptions.Exception_Information (E) ); > > =A0 =A0 =A0 =A0raise; > =A0 =A0 end Mult; > > =A0 =A0 function Random return Float; > > =A0 =A0 Gen : Ada.Numerics.Float_Random.Generator; > > =A0 =A0 function Random return Float is > =A0 =A0 begin -- Random > =A0 =A0 =A0 =A0return 200.0 * Ada.Numerics.Float_Random.Random (Gen) - 10= 0.0; -- -100 .. > 100. > =A0 =A0 end Random; > > =A0 =A0 A : constant Matrix :=3D Matrix'(others =3D> (others =3D> Random)= ); > =A0 =A0 B : constant Matrix :=3D Matrix'(others =3D> (others =3D> Random)= ); > > =A0 =A0 C : Matrix; > > =A0 =A0 Elapsed =A0 : Duration; > =A0 =A0 Prev =A0 =A0 =A0: Duration :=3D Duration'Last; > =A0 =A0 Start =A0 =A0 : Ada.Real_Time.Time; > =A0 =A0 Num_Tasks : Positive :=3D 1; > > =A0 =A0 use type Ada.Real_Time.Time; > begin -- MP_Mult_PO > =A0 =A0 Ada.Text_IO.Put_Line (Item =3D> "Num processors" & Integer'Image > (Num_Processors) ); > > =A0 =A0 All_Calls : loop > =A0 =A0 =A0 =A0Start :=3D Ada.Real_Time.Clock; > =A0 =A0 =A0 =A0C :=3D Mult (A, B, Num_Tasks); > =A0 =A0 =A0 =A0Elapsed :=3D Ada.Real_Time.To_Duration (Ada.Real_Time.Cloc= k - Start); > =A0 =A0 =A0 =A0Ada.Text_IO.Put_Line (Item =3D> Integer'Image (Num_Tasks) = & ' ' & > Duration'Image (Elapsed) ); > > =A0 =A0 =A0 =A0exit All_Calls when Num_Tasks > 2 * Num_Processors and Ela= psed > Prev; > > =A0 =A0 =A0 =A0Prev :=3D Elapsed; > =A0 =A0 =A0 =A0Num_Tasks :=3D Num_Tasks + 1; > =A0 =A0 end loop All_Calls; > exception -- MP_Mult_PO > when E : others =3D> > =A0 =A0 Ada.Text_IO.Put_Line (Item =3D> "MP_Mult_PO " & > Ada.Exceptions.Exception_Information (E) ); > end MP_Mult_PO; Below are the test results conducted on GNAT 2010 running on Windows XP. Num processors 2 1 0.077000009 2 0.055627741 3 0.023719495 4 0.018366580 5 0.018512129 The output looks reasonable. The speed is improved up to 4 tasks. It slows down with 5 tasks due to tasking switch. However, it is still better than single task. As Robert suggested, I would divide work among tasks by passing paramter into each task. For example, in case of two tasks, one task handles from rows 1 .. 250 while the other task handle from rows 251 .. 500. By the way, the protected singleton type Control is no longer needed. Anh Vo