From: Simon Wright <simon@pushface.org>
Subject: Re: Need some light on using Ada or not
Date: Mon, 21 Feb 2011 13:44:04 +0000
Date: 2011-02-21T13:44:04+00:00 [thread overview]
Message-ID: <m2sjvh1p0b.fsf@pushface.org> (raw)
In-Reply-To: 6nm4m6h6il8j02l9b11qkjsg3k2cjhjul8@4ax.com
[-- Attachment #1: Type: text/plain, Size: 367 bytes --]
Brian Drummond <brian_drummond@btconnect.com> writes:
> As this is my first experiment with tasking, comments are welcome (and
> I'd be interested to see your version).
See end.
> If people think this is worth submitting to the shootout, I'll go
> ahead.
I think it definitely is: the only Ada code for binary-trees is
single-threaded, so looks needlessly poor.
[-- Attachment #2: simple multi-thread version of binary trees benchmark --]
[-- Type: text/plain, Size: 5283 bytes --]
----------------------------------------------------------------
-- BinaryTrees
--
-- Ada 95 (GNAT)
--
-- Contributed by Jim Rogers
-- Modified by Simon Wright
----------------------------------------------------------------
with Tree_Nodes; use Tree_Nodes;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with System;
procedure Binarytrees is
Min_Depth : constant Positive := 4;
N : Natural := 1;
Stretch_Tree : Tree_Node;
Long_Lived_Tree : Tree_Node;
Max_Depth : Positive;
Stretch_Depth : Positive;
task type Check_Tree is
pragma Priority (System.Default_Priority - 1);
entry Start (Iterations : Positive; Depth : Positive);
entry Sum (Result : out Integer);
end Check_Tree;
task body Check_Tree is
Iterations : Positive;
Depth : Positive;
Tree : Tree_Node;
Check : Integer := 0;
begin
accept Start (Iterations : Positive; Depth : Positive) do
Check_Tree.Iterations := Iterations;
Check_Tree.Depth := Depth;
end Start;
for J in 1 .. Iterations loop
Tree := Bottom_Up_Tree (J, Depth);
Check := Check + Item_Check (Tree);
Delete_Tree (Tree);
Tree := Bottom_Up_Tree (-J, Depth);
Check := Check + Item_Check (Tree);
Delete_Tree (Tree);
end loop;
accept Sum (Result : out Integer) do
Result := Check;
end Sum;
end Check_Tree;
begin
if Argument_Count > 0 then
N := Positive'Value (Argument (1));
end if;
Max_Depth := Positive'Max (Min_Depth + 2, N);
Stretch_Depth := Max_Depth + 1;
Stretch_Tree := Bottom_Up_Tree (0, Stretch_Depth);
Put ("stretch tree of depth ");
Put (Item => Stretch_Depth, Width => 1);
Put (Ht & " check: ");
Put (Item => Item_Check (Stretch_Tree), Width => 1);
New_Line;
Delete_Tree (Stretch_Tree);
Long_Lived_Tree := Bottom_Up_Tree (0, Max_Depth);
declare
subtype Check_Trees_Array_Range
is Natural range 0 .. (Max_Depth - Min_Depth) / 2;
Check_Trees : array (Check_Trees_Array_Range) of Check_Tree;
function Depth (For_Entry : Check_Trees_Array_Range) return Natural
is
begin
return For_Entry * 2 + Min_Depth;
end Depth;
function Iterations (For_Entry : Check_Trees_Array_Range) return Positive
is
begin
return 2 ** (Max_Depth - Depth (For_Entry) + Min_Depth);
end Iterations;
begin
for D in Check_Trees'Range loop
Check_Trees (D).Start (Iterations => Iterations (D),
Depth => Depth (D));
end loop;
for D in Check_Trees'Range loop
Put (Item => Iterations (D) * 2, Width => 0);
Put (Ht & " trees of depth ");
Put (Item => Depth (D), Width => 0);
declare
Check : Integer;
begin
Check_Trees (D).Sum (Result => Check);
Put (Ht & " check: ");
Put (Item => Check, Width => 0);
end;
New_Line;
end loop;
end;
Put ("long lived tree of depth ");
Put (Item => Max_Depth, Width => 0);
Put (Ht & " check: ");
Put (Item => Item_Check (Long_Lived_Tree), Width => 0);
New_Line;
Delete_Tree (Long_Lived_Tree);
end BinaryTrees;
----------------------------------------------------------------
-- BinaryTrees
--
-- Ada 95 (GNAT)
--
-- Contributed by Jim Rogers
-- Modified by Simon Wright
----------------------------------------------------------------
with Ada.Unchecked_Deallocation;
package body Tree_Nodes is
function Bottom_Up_Tree (Item : Integer; Depth : Natural) return Tree_Node
is
begin
if Depth > 0 then
return new Node'(Bottom_Up_Tree (2 * Item - 1, Depth - 1),
Bottom_Up_Tree (2 * Item, Depth - 1),
Item);
else
return new Node'(null, null, Item);
end if;
end Bottom_Up_Tree;
function Item_Check (This : Tree_Node) return Integer
is
begin
if This.Left = null then
return This.Item;
else
return This.Item + Item_Check (This.Left) - Item_Check (This.Right);
end if;
end Item_Check;
procedure Delete_Tree (This : in out Tree_Node)
is
procedure Free is new Ada.Unchecked_Deallocation (Node, Tree_Node);
begin
if This /= null then
Delete_Tree (This.Left);
Delete_Tree (This.Right);
Free (This);
end if;
end Delete_Tree;
end Tree_Nodes;
----------------------------------------------------------------
-- BinaryTrees
--
-- Ada 95 (GNAT)
--
-- Contributed by Jim Rogers
-- Modified by Simon Wright
----------------------------------------------------------------
package Tree_Nodes is
type Tree_Node is private;
function Bottom_Up_Tree (Item : Integer; Depth : Natural) return Tree_Node;
function Item_Check (This : Tree_Node) return Integer;
procedure Delete_Tree (This : in out Tree_Node);
private
type Node;
type Tree_Node is access Node;
type Node is record
Left : Tree_Node;
Right : Tree_Node;
Item : Integer := 0;
end record;
end Tree_Nodes;
next prev parent reply other threads:[~2011-02-21 13:44 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-18 22:52 Need some light on using Ada or not Luis P. Mendes
2011-02-18 23:58 ` Georg Bauhaus
2011-02-19 14:25 ` Simon Wright
2011-02-19 0:20 ` Edward Fish
2011-02-20 0:13 ` Luis P. Mendes
2011-02-20 1:36 ` Marc A. Criley
2011-02-20 9:59 ` mockturtle
2011-02-20 10:37 ` Brian Drummond
2011-02-20 11:08 ` Ludovic Brenta
2011-03-01 8:10 ` Adrian Hoe
2011-03-01 8:29 ` Thomas Løcke
2011-03-04 13:34 ` Adrian Hoe
2011-02-19 8:43 ` Vadim Godunko
2011-02-19 13:07 ` Brian Drummond
2011-02-19 14:17 ` Simon Wright
2011-02-19 18:02 ` Brian Drummond
2011-02-19 18:07 ` Bill Findlay
2011-02-20 10:42 ` Brian Drummond
2011-02-19 14:36 ` Georg Bauhaus
2011-02-19 18:25 ` Brian Drummond
2011-02-20 14:34 ` Brian Drummond
2011-02-20 15:45 ` jonathan
2011-02-20 16:18 ` Brian Drummond
2011-02-20 19:49 ` Pascal Obry
2011-02-20 19:57 ` Brian Drummond
2011-02-20 20:10 ` jonathan
2011-02-20 21:15 ` Pascal Obry
2011-02-20 21:26 ` Vinzent Hoefler
2011-02-20 21:33 ` Vinzent Hoefler
2011-02-20 21:36 ` Pascal Obry
2011-02-20 21:50 ` Vinzent Hoefler
2011-02-20 22:18 ` jonathan
2011-02-20 22:47 ` Simon Wright
2011-02-21 12:52 ` Brian Drummond
2011-02-21 13:44 ` Simon Wright [this message]
2011-02-24 0:19 ` Brian Drummond
2011-02-24 7:41 ` Jacob Sparre Andersen
2011-02-22 2:15 ` Shark8
2011-02-20 16:42 ` jonathan
2011-02-20 20:02 ` Brian Drummond
2011-02-20 0:20 ` Luis P. Mendes
2011-02-20 10:50 ` Brian Drummond
2011-02-20 19:54 ` Brian Drummond
2011-02-23 22:19 ` Luis P. Mendes
2011-02-24 17:06 ` Brian Drummond
2011-02-27 17:51 ` Luis P. Mendes
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox