From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Received: by 2002:a05:620a:20d9:b0:706:6dd9:b7d9 with SMTP id f25-20020a05620a20d900b007066dd9b7d9mr741732qka.111.1674429061153; Sun, 22 Jan 2023 15:11:01 -0800 (PST) X-Received: by 2002:a05:6870:2b02:b0:148:a6a:e616 with SMTP id ld2-20020a0568702b0200b001480a6ae616mr1781949oab.63.1674429060913; Sun, 22 Jan 2023 15:11:00 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 22 Jan 2023 15:11:00 -0800 (PST) In-Reply-To: <6dbff95d-8e30-4a60-ad03-d0aa4cff1583n@googlegroups.com> Injection-Info: google-groups.googlegroups.com; posting-host=188.150.24.77; posting-account=HFCrOQoAAABZD_f-UUbYHm3lJDIrh-UX NNTP-Posting-Host: 188.150.24.77 References: <9c7cccd9-733f-49a8-b482-087ccb14b58dn@googlegroups.com> <3db6b678-5a45-4b4d-9f6c-e4933b04d6c2n@googlegroups.com> <6dbff95d-8e30-4a60-ad03-d0aa4cff1583n@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7dbadce1-0a82-4527-90c6-8fbb80bc3744n@googlegroups.com> Subject: Re: Real_Arrays on heap with overloaded operators and clean syntax From: Joakim Strandberg Injection-Date: Sun, 22 Jan 2023 23:11:01 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 6327 Xref: reader01.eternal-september.org comp.lang.ada:64854 List-Id: s=C3=B6ndag 22 januari 2023 kl. 23:49:11 UTC+1 skrev dhmos.a...@gmail.com: > > > > > Dear ADA lovers,=20 > > > > > with stack allocation of Real_Vector ( 1 .. N ) when N >=3D 100,0= 00 I get STACK_OVERFLOW ERROR while trying to check how fast operator overl= oading is working for an expression=20 > > > > >=20 > > > > > X :=3D A + B + C + C + A + B, where=20 > > > > > A,B,C,X are all Real_Vector ( 1 .. N ).=20 > > > > >=20 > > > > > So my only option was to allocate on the heap using new. But then= I lost the clean syntax=20 > > > > >=20 > > > > > X :=3D A + B + C + C + A + B=20 > > > > >=20 > > > > > and I had to write instead:=20 > > > > >=20 > > > > > X.all :=3D A.all + B.all + C.all + C.all + A.all + B.all.=20 > > > > >=20 > > > > > This is really ugly and annoying because when you are using Real_= Arrays for implementing some linear algebra method who relies heavilly on m= atrix vector products and vector updates, you do need to allocate on the he= ap (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 d= eclaring A,B,C,X as access Real_Vector?=20 > > > > > Thanks for your time!=20 > > > > 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 y= ou 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.=20 > > > >=20 > > > > Best regards,=20 > > > > Joakim=20 > > > Thank you for your reply,=20 > > > since I am a newbie I was under the impression that tasks are used on= ly when you want to write a parallel code that takes advantage of multicore= architectures. You suggest I have a single task and single thread somethin= g 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] similar= ly to the C++ std::vector to make things simpler and overloaded operators s= imilarly 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?=20 > > 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 rea= sons for example performance, the application needs to ask the operating sy= stem 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 sta= ck sizes when declaring tasks. It is not part of the Ada standard to specif= y stack size of the environment task. The Ada way is to declare new tasks a= nd do work on them. Prefer the bounded containers over the unbounded contai= ners. If you really need to allocate objects I recommend using storage pool= s 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 solv= ers 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 a= re 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 in= formation online about so important things such as allocation? For what you described above being able to write X :=3D A + B + C + C + A + B; working with a bigger stack is the easiest solution. Don't understand why y= ou think it would restrict all scientific modelling? Maybe I haven't descri= bed it well enough. >From the top of my head: task T with Storage_Size =3D> 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 :=3D False) is record case Exists is when True =3D> Value : Integer; when False =3D> null; end Exists; end record;