comp.lang.ada
 help / color / mirror / Atom feed
From: Joakim Strandberg <joakimds@kth.se>
Subject: Re: Real_Arrays on heap with overloaded operators and clean syntax
Date: Sun, 22 Jan 2023 15:11:00 -0800 (PST)	[thread overview]
Message-ID: <7dbadce1-0a82-4527-90c6-8fbb80bc3744n@googlegroups.com> (raw)
In-Reply-To: <6dbff95d-8e30-4a60-ad03-d0aa4cff1583n@googlegroups.com>

söndag 22 januari 2023 kl. 23:49:11 UTC+1 skrev dhmos.a...@gmail.com:
> > > > > Dear ADA lovers, 
> > > > > with stack allocation of Real_Vector ( 1 .. N ) when N >= 100,000 I get STACK_OVERFLOW ERROR while trying to check how fast operator overloading is working for an expression 
> > > > > 
> > > > > X := A + B + C + C + A + B, where 
> > > > > A,B,C,X are all Real_Vector ( 1 .. N ). 
> > > > > 
> > > > > So my only option was to allocate on the heap using new. But then I lost the clean syntax 
> > > > > 
> > > > > X := A + B + C + C + A + B 
> > > > > 
> > > > > and I had to write instead: 
> > > > > 
> > > > > X.all := A.all + B.all + C.all + C.all + A.all + B.all. 
> > > > > 
> > > > > This is really ugly and annoying because when you are using Real_Arrays for implementing some linear algebra method who relies heavilly on matrix vector products and vector updates, you do need to allocate on the heap (sizes are determined in runtime) and you do need a clean syntax. So, is there any way to simplify my life without using the .all or even without declaring A,B,C,X as access Real_Vector? 
> > > > > Thanks for your time! 
> > > > Easiest solution is probably to declare a new task and specify the stack size using the Storage_Size aspect. Allocate as much stack space as you need to be able to do the calculations and do all the allocations on the declared task, not on the environment task. You will avoid the unnecessary heap allocations and have nice clean syntax. 
> > > > 
> > > > Best regards, 
> > > > Joakim 
> > > Thank you for your reply, 
> > > since I am a newbie I was under the impression that tasks are used only when you want to write a parallel code that takes advantage of multicore architectures. You suggest I have a single task and single thread something like this? I see, but there should be a way to do this also for the main program. But thanks anyway. Are you aware of any libraries similar to Real_Arrays, but who allocated memory internally using heap? This is the natural way to do such things. Similarly to the Containers.Vector. But Vector has such an awful syntax. There should be something like an indexer [i] similarly to the C++ std::vector to make things simpler and overloaded operators similarly to Real_Arrays. It is a no brainer. Most programs need to allocate on the heap, why did they restrict Real_Arrays on the stack? 
> > It my impression that in the Ada community the preferred way of working is in general stack only. Heap allocations are avoided for a number of reasons for example performance, the application needs to ask the operating system for memory which one doesn't know how much time that will take nor if it always will succeed. In addition, applications that use the heap may be susceptible to heap memory fragmentation. In Ada, it is easy to specify stack sizes when declaring tasks. It is not part of the Ada standard to specify stack size of the environment task. The Ada way is to declare new tasks and do work on them. Prefer the bounded containers over the unbounded containers. If you really need to allocate objects I recommend using storage pools with pre-allocated memory also known as arena pools.
> With great depression I realized that the preferred way is of stack only. This is very restrictive excluding all scientific modelling involving solvers for partial differential equations, linear algebra kernels, etc. It is insane. Completely insane. 3D simulations of physical phenomena may involve billions of grid-cells and at each grid-cell several unknowns are defined (velocity, pressure, temperature, energy, density, etc). That is why they are using Fortran or C++, but ADA has really cool stuff for so many things, why not vectors and matrices and heap allocation? Would you please give me an example, I googled and I cannot find a single example demonstrating how to use a task with the declaration of stack size. Why is there so little information online about so important things such as allocation?

For what you described above being able to write
X := A + B + C + C + A + B;
working with a bigger stack is the easiest solution. Don't understand why you think it would restrict all scientific modelling? Maybe I haven't described it well enough.

From the top of my head:
task T with Storage_Size => 10_000_000 is

end T;

task body T is
begin
    null;
end T;

Tasks need to be defined inside Ada packages, they cannot be stand-alone.

An optional type is for example in Ada:
type Optional_Integer (Exists : Boolean := False) is record
   case Exists is
      when True => Value : Integer;
      when False => null;
   end Exists;
end record;

  reply	other threads:[~2023-01-22 23:11 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-22 21:34 Real_Arrays on heap with overloaded operators and clean syntax Jim Paloander
2023-01-22 21:56 ` Joakim Strandberg
2023-01-22 22:07   ` Jim Paloander
2023-01-22 22:42     ` Joakim Strandberg
2023-01-22 22:49       ` Jim Paloander
2023-01-22 23:11         ` Joakim Strandberg [this message]
2023-01-22 23:14         ` Gautier write-only address
2023-01-23  1:14           ` Leo Brewin
2023-01-23  6:01             ` Jim Paloander
2023-01-23  8:39     ` G.B.
2023-01-22 22:13 ` Dmitry A. Kazakov
2023-01-22 22:36   ` Jim Paloander
2023-01-23  8:28     ` Dmitry A. Kazakov
2023-01-24  1:04       ` Jim Paloander
2023-01-24 10:42         ` J-P. Rosen
2023-01-25  9:52           ` Jim Paloander
2023-01-25 12:21             ` J-P. Rosen
2023-01-25 22:41               ` Gautier write-only address
2023-01-26 19:08                 ` Jim Paloander
2023-01-22 23:18 ` Rod Kay
2023-01-22 23:20   ` Jim Paloander
2023-01-22 23:34     ` Rod Kay
2023-01-22 23:53       ` Joakim Strandberg
2023-01-23  7:50         ` Egil H H
2023-01-23  8:51           ` J-P. Rosen
2023-01-23  6:34       ` Rod Kay
2023-01-23  6:56         ` Jim Paloander
2023-01-23  7:31           ` Rod Kay
2023-01-24 19:47 ` Gautier write-only address
2023-01-24 23:02   ` Gautier write-only address
2023-01-25  9:50     ` Jim Paloander
2023-01-26 20:39 ` Jerry
2023-01-26 21:52   ` Jim Paloander
2023-02-02 21:59     ` Jerry
replies disabled

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