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,FREEMAIL_FROM, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Received: by 2002:a05:622a:17cd:b0:3ad:3d6e:937 with SMTP id u13-20020a05622a17cd00b003ad3d6e0937mr612165qtk.459.1674426989343; Sun, 22 Jan 2023 14:36:29 -0800 (PST) X-Received: by 2002:a05:6808:616:b0:36c:6148:42a9 with SMTP id y22-20020a056808061600b0036c614842a9mr1016938oih.129.1674426988999; Sun, 22 Jan 2023 14:36:28 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!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 14:36:28 -0800 (PST) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=157.143.56.236; posting-account=Wbe3fAoAAAALa8UT9MWTy6mw2ahlRJms NNTP-Posting-Host: 157.143.56.236 References: <9c7cccd9-733f-49a8-b482-087ccb14b58dn@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <2801afb2-045f-4fec-b41e-f83b2c80e672n@googlegroups.com> Subject: Re: Real_Arrays on heap with overloaded operators and clean syntax From: Jim Paloander Injection-Date: Sun, 22 Jan 2023 22:36:29 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:64851 List-Id: > > Dear ADA lovers,=20 > > with stack allocation of Real_Vector ( 1 .. N ) when N >=3D 100,000 I g= et STACK_OVERFLOW ERROR while trying to check how fast operator overloading= 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 los= t 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 matrix = vector products and vector updates, you do need to allocate on the heap (si= zes 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 declari= ng A,B,C,X as access Real_Vector? > You can define "+" on the access type, which should probably be an arena= =20 > pointer for performance reasons:=20 >=20 > Arena : Mark_And_Release_Pool;=20 > type Real_Vector_Ptr is access Real_Vector;=20 > for Real_Vector_Ptr'Storage_Pool use Arena;=20 >=20 > function "+" (Left, Right : Real_Vector_Ptr)=20 > return Real_Vector_Ptr is=20 > begin=20 > if Left'Length /=3D Right'Length then=20 > raise Constraint_Error;=20 > end if;=20 > return Result : Real_Vector_Ptr :=3D new Real_Vector (Left'Range) do=20 > for I in Result'Range loop=20 > Result (I) :=3D=20 > Left (I) + Right (I - Left'First + Right'First);=20 > end loop;=20 > end return;=20 > end "+";=20 >=20 > You can overload that with=20 >=20 > function "+" (Left : Real_Vector_Ptr; Right : Real_Vector)=20 > return Real_Vector_Ptr is=20 > begin=20 > if Left'Length /=3D Right'Length then=20 > raise Constraint_Error;=20 > end if;=20 > return Result : Real_Vector_Ptr :=3D new Real_Vector (Left'Range) do=20 > for I in Result'Range loop=20 > Result (I) :=3D=20 > Left (I) + Right (I - Left'First + Right'First);=20 > end loop;=20 > end return;=20 > end "+";=20 >=20 > and with=20 >=20 > function "+" (Left : Real_Vector; Right : Real_Vector_Ptr)=20 > return Real_Vector_Ptr;=20 >=20 > Then you will be able to write:=20 >=20 > X :=3D A + B + C + C + A + B;=20 > -- Use X=20 > Free (X); -- Pop all arena garbage=20 >=20 > But of course, the optimal way to work large linear algebra problems is= =20 > by using in-place operations, e.g.=20 >=20 > procedure Add (Left : in out Real_Vector; Right : Real_Vector);=20 >=20 > etc.=20 >=20 > --=20 > Regards,=20 > Dmitry A. Kazakov=20 > http://www.dmitry-kazakov.de Privet Dmitry, thank you for your answer, I was thinking about that, but I was not sure wh= ether or not it can be avoided with Implicit_Dereference,=20 type Accessor (Data: not null access Element) is limited private with Implicit_Dereference =3D> Data; somehow... Otherwise what you described for operator+ one has to do for eve= ry operator overloaded inside Real_Arrays package. The optimal way to work = large linear algebra problem is what you describe because unfortunately ADA= does not allow what Fortran does since 30 years ago or more. But in C++ yo= u can reproduce the same functionality as Fortran using Expression Template= s and Template Metaprogramming. Perhaps ADA should allow something like tha= t. Because for maintainability reasons the best would be to write the mathe= matical expressions as close as possible to the mathematical formulas. Spacebo Bolshoe, Jim.