comp.lang.ada
 help / color / mirror / Atom feed
From: Stefan Bellon <sbellon@sbellon.de>
Subject: Re: Ada tasking question
Date: Thu, 19 Apr 2007 00:57:38 +0200
Date: 2007-04-19T00:57:38+02:00	[thread overview]
Message-ID: <20070419005738.692eeeb6@cube.tz.axivion.com> (raw)
In-Reply-To: 462688E3.6050105@nowhere.com

Leif Holmgren wrote:

> If my memory does not fail me the advice to use an array here is
> double good.
> 
> First of all you don't need to bother with the nitty gritty details
> of dynamic allocation yourself.
> 
> Secondly and perhaps most important Ada will handle the
> synchronization of task termination for you automatically. It will
> not allow the array to go out of scope before all the tasks are
> completed.

Yes, I realized this after I read Randy's posting. This idea is just
excellent.

> Years ago I implemented such a system (using dynamically allocated 
> tasks) and it worked very well. By doing as suggested you will gain 
> maximum performance even if the buckets take different time to
> process.

Well, here I seem to have still problems. "In the beginning", it seems
to work very well. Debug output shows that the tasks do work and fetch
new jobs and so on. But as time goes by, some tasks get "lazy" and I
have no idea why. They do not terminate, but do not fetch new jobs
although there are still open buckets to process.

The task body looks (very shortened) like this:

   type Buckets_Array is array (Natural range <>) of Bucket_Item;

   type Buckets_Access is access all Buckets_Array;

   task type Bucket_Worker (Buckets : Buckets_Access) is
      entry Set_Task_No (No : in Integer);  --  Just for debugging
   end Bucket_Worker;

   task body Bucket_Worker
   is
      Task_No         : Integer;
      Current         : Integer;
   begin
      accept Set_Task_No (No : in Integer) do
         Task_No := No;
      end Set_Task_No;
      Debug ("Task" & Task_No'Img & ": starting");
      Bucket_Jobs.Next (Current);
      while Current in Buckets.all'Range loop
         Debug ("Task" & Task_No'Img & ": bucket" & Current'Img)
         --  Do the work.
         Bucket_Jobs.Next (Current);
      end loop;
      Debug ("Task" & Task_No'Img & ": finished");
   end Bucket_Worker;

The initialization and starting of the tasks looks like this:

      Bucket_Jobs.Init (My_Buckets'First);
      declare
         Compare_Tasks : array (1 .. Num_CPUs) of Bucket_Worker
           (Buckets  => My_Buckets'Unrestricted_Access);
      begin
         for I in Compare_Tasks'Range loop
            Compare_Tasks (I).Set_Task_No (I);
         end loop;
      end;

And Bucket_Jobs is the protected object as posted in the followup to
Jeffrey Carter.

The observation of the tasks that get lazy over the time can be made by
two ways: First, the debug output shows a lot of activity for _all_
four tasks in the beginning, but at the end, only one task fetches new
jobs although the others have not finished and there are buckets left
to process. The second way of observing is by looking at the processor
usage with something like top. In the beginning it is quite over 100 %
and all CPUs are used. Later one it drops and only one CPU is used.

I have no explanation for this behaviour, but perhaps you can spot
something which is fundamentally wrong in my code?

-- 
Stefan Bellon



  reply	other threads:[~2007-04-18 22:57 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-18 18:13 Ada tasking question Stefan Bellon
2007-04-18 18:40 ` Randy Brukardt
2007-04-18 20:12   ` Jeffrey R. Carter
2007-04-18 22:43     ` Stefan Bellon
2007-04-19  2:02       ` Steve
2007-04-18 21:08   ` Leif Holmgren
2007-04-18 22:57     ` Stefan Bellon [this message]
2007-04-18 23:41       ` Brian May
2007-04-19  0:25       ` Randy Brukardt
2007-04-19  8:02         ` Stefan Bellon
2007-04-19 12:47         ` Jacob Sparre Andersen
2007-04-19 16:11           ` Anh Vo
2007-04-20  4:32           ` Jeffrey R. Carter
2007-04-19  0:50       ` Jeffrey R. Carter
2007-04-18 19:50 ` Alex R. Mosteo
2007-04-18 23:00   ` Stefan Bellon
2007-04-19 20:37   ` Pascal Obry
2007-04-19  2:13 ` jimmaureenrogers
2007-04-19  7:49 ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
1996-10-18  0:00 whiting_ms@corning.com (Matt Whiting)
replies disabled

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