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.7 required=3.0 tests=BAYES_00,NICE_REPLY_A, REPLYTO_WITHOUT_TO_CC,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: "G.B." Newsgroups: comp.lang.ada Subject: Re: Real_Arrays on heap with overloaded operators and clean syntax Date: Mon, 23 Jan 2023 09:39:39 +0100 Organization: A noiseless patient Spider Message-ID: References: <9c7cccd9-733f-49a8-b482-087ccb14b58dn@googlegroups.com> <3db6b678-5a45-4b4d-9f6c-e4933b04d6c2n@googlegroups.com> Reply-To: nonlegitur@notmyhomepage.de MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 23 Jan 2023 08:39:39 -0000 (UTC) Injection-Info: reader01.eternal-september.org; posting-host="3b5089f498b4e76daf2f406ae4fa6394"; logging-data="3736643"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mxGYchzDPXez3XEwbpUrDHJJ9gteryMA=" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Cancel-Lock: sha1:9vOOf302k7dgD/RiUgmPDZ1VjJM= In-Reply-To: Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:64869 List-Id: On 22.01.23 23:07, Jim Paloander wrote: > >>> 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. Tasks---their name is an indication that they just perform something that is specified---are not restricted to parallelism, or to multiple cores. The can do the job on a single processor, or move to some other, they are more abstract than contemporary hardware, or hardware for that matter. > 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. It is unfortunate that GCC had to introduce a tiny scale stack(frame) model some time ago. > 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. The most natural way to work with an array of FPT numbers is for the programmer to declare an array indexed by some index type. Done. If GNAT gets in the way there, it might be worth a note sent to its maintainers. Whenever a programmer is tasked with considering memory allocation, then depending on one's propensity towards working on memory allocation it is inconvenient and distracting. Math programs don't make you do this, I think. Also, std::vector an its relatives shield the programmer from the absurdly clever, yet unreadable memory allocation that needs to be stuffed behind the scenes. More importantly, though, C++ introduced std::move semantics after a few decades of its existence, to address copying when using chains of +. It might be interesting to see Ada's in-situ construction of return values in comparison. > 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 .at() does some of what Ada does. Is v.at(k) = 4; less awful than v(k) := 4; ? Another thing: Mathematical notation has ellipsis, thus A + B + ... + Y + Z; Most general purpose languages don't have ellipsis for this kind of expression. However, even mathematical formulas use what programmers can usually achieve, too. The usual \sum_k A_k. No "+" at all, and an array of vectors, not single ones. Going further, some like to write reduce("+", A); In Ada, you could have a generic function for this, or use a function pointer. The .all thing vanishes automatically whenever you want to refer to a particular component of the pointed-at object, as opposed to all of them. So, A.all(K) is the same as A(K). Likewise, .all can be dropped if want to invoke the pointed-at subprogram if it has parameters.