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 Path: eternal-september.org!reader01.eternal-september.org!aioe.org!QJLXApsvkYYOaKx3c4LRTg.user.46.165.242.91.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Real_Arrays on heap with overloaded operators and clean syntax Date: Sun, 22 Jan 2023 23:13:14 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <9c7cccd9-733f-49a8-b482-087ccb14b58dn@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: gioia.aioe.org; logging-data="63539"; posting-host="QJLXApsvkYYOaKx3c4LRTg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.9.2 Xref: reader01.eternal-september.org comp.lang.ada:64850 List-Id: On 2023-01-22 22:34, 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? You can define "+" on the access type, which should probably be an arena pointer for performance reasons: Arena : Mark_And_Release_Pool; type Real_Vector_Ptr is access Real_Vector; for Real_Vector_Ptr'Storage_Pool use Arena; function "+" (Left, Right : Real_Vector_Ptr) return Real_Vector_Ptr is begin if Left'Length /= Right'Length then raise Constraint_Error; end if; return Result : Real_Vector_Ptr := new Real_Vector (Left'Range) do for I in Result'Range loop Result (I) := Left (I) + Right (I - Left'First + Right'First); end loop; end return; end "+"; You can overload that with function "+" (Left : Real_Vector_Ptr; Right : Real_Vector) return Real_Vector_Ptr is begin if Left'Length /= Right'Length then raise Constraint_Error; end if; return Result : Real_Vector_Ptr := new Real_Vector (Left'Range) do for I in Result'Range loop Result (I) := Left (I) + Right (I - Left'First + Right'First); end loop; end return; end "+"; and with function "+" (Left : Real_Vector; Right : Real_Vector_Ptr) return Real_Vector_Ptr; Then you will be able to write: X := A + B + C + C + A + B; -- Use X Free (X); -- Pop all arena garbage But of course, the optimal way to work large linear algebra problems is by using in-place operations, e.g. procedure Add (Left : in out Real_Vector; Right : Real_Vector); etc. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de