From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f00ac8eb01f056e8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!u20g2000yqj.googlegroups.com!not-for-mail From: "vincent.diemunsch@gmail.com" Newsgroups: comp.lang.ada Subject: Re: Interfacing Ada to C for linear algebra Date: Mon, 5 Sep 2011 23:53:40 -0700 (PDT) Organization: http://groups.google.com Message-ID: <109a98c6-8f88-490f-9596-84656f1d6c76@u20g2000yqj.googlegroups.com> References: <2cc811c2-e805-4407-8f2e-d1b5804782c9@i21g2000yqd.googlegroups.com> NNTP-Posting-Host: 192.196.142.21 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1315292020 26285 127.0.0.1 (6 Sep 2011 06:53:40 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 6 Sep 2011 06:53:40 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u20g2000yqj.googlegroups.com; posting-host=192.196.142.21; posting-account=hya6vwoAAADTA0O27Aq3u6Su3lQKpSMz User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESRCNK X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20100101 Firefox/6.0.1,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:20854 Date: 2011-09-05T23:53:40-07:00 List-Id: On 2 sep, 16:53, Simon Wright wrote: > "vincent.diemun...@gmail.com" writes: > > Hello everybody, > > > I would like to use in Ada some linear algebra libraries written in C, > > especially the SuiteSparse : > > =A0http://www.cise.ufl.edu/research/sparse/SuiteSparse/ > > These libraries handle Sparse Matrices that are in the format used > > internally by MatLab : a column-compressed version of the standard > > matrices. > > > Ada defines Generic_Real_Arrays to handle vectors and matrices. So I > > would like to simply create a Generic_Real_Sparse_Arrays package to > > define MatLab compatible matrices. Since the internal format of MatLab > > matrices is in C, in need to do a conversion between my Sparse_Matrix > > and the C arrays that represent the Sparse Matrix in MatLab. > > > Looking at the implementation of Generic_Real_Arrays in GNAT, I see > > that they internally call the BLAS=A0and LAPACK=A0packages, written in > > Fortran, through the use of two packages : System.Generic_Real_BLAS > > and System.Generic_Real_LAPACK. These package do exactly the same as > > what I want to do : they convert Ada arrays of Ada floating point type > > in Fortran arrays of Fortran Float type and back, without using > > copying, so that the compiler produces no code for this conversion > > (using pragma Inline_Always and so on). This is important because if > > we use optimized versions of linear algebra packages it is because we > > manipulate huge arrays and we don't want to loose time in copying > > values from on representation to another ! > > The LAPACK interface used in Generic_Real_Arrays deals with matrices in > Ada (row-major) format and - usually, anyway - transposes in order to > call the Fortran implementations. Unlike BLAS, LAPACK itself doesn't > have options to deal with row-major matrices. > > We did some timings and found that it's quite a bit quicker to declare > the BLAS/LAPACK interfaces in terms of Fortran (column-major) matrices > (declared using pragma Convention (Fortran)) and have the compiler > implement the necessary moves on assignment, rather than using a library > Transpose subprogram. But make no mistake, the Generic_Real_Arrays code > definitely copies! (look at the body for confirmation). > > > I suppose that since C is very common, there must already exist a very > > good implementation of conversion of Ada arrays of floating point > > types in C arrays of C.Float, idealy using the GNAT > > implementation. This could save me a lot of time doing interfaces... > > GNAT and GCC share the same compiler backend, so the underlying numeric > types are the same. Just tell the compiler what to import: > > =A0 =A0procedure dgeev > =A0 =A0 =A0(Jobv_L : =A0 =A0 =A0 =A0Character; > =A0 =A0 =A0 Jobv_R : =A0 =A0 =A0 =A0Character; > =A0 =A0 =A0 N =A0 =A0 =A0: =A0 =A0 =A0 =A0Positive; > =A0 =A0 =A0 A =A0 =A0 =A0: in out Real_Arrays.Real_Matrix; > =A0 =A0 =A0 Ld_A =A0 : =A0 =A0 =A0 =A0Positive; > =A0 =A0 =A0 W_R =A0 =A0: =A0 =A0out Real_Arrays.Real_Vector; > =A0 =A0 =A0 W_I =A0 =A0: =A0 =A0out Real_Arrays.Real_Vector; > =A0 =A0 =A0 V_L =A0 =A0: =A0 =A0out Real_Arrays.Real_Matrix; > =A0 =A0 =A0 Ld_V_L : =A0 =A0 =A0 =A0Integer; > =A0 =A0 =A0 V_R =A0 =A0: =A0 =A0out Real_Arrays.Real_Matrix; > =A0 =A0 =A0 Ld_V_R : =A0 =A0 =A0 =A0Integer; > =A0 =A0 =A0 Work =A0 : =A0 =A0out Real_Arrays.Real_Vector; > =A0 =A0 =A0 L_Work : =A0 =A0 =A0 =A0Integer; > =A0 =A0 =A0 Info =A0 : =A0 =A0out Integer); > =A0 =A0pragma Import (Fortran, dgeev, "dgeev_"); > > (Real_Arrays.Real_Matrix etc from Generic_Real_Arrays). The Fortran > convention means that the Character and Integer arguments are passed by > reference without your needing to fake up accesses. > > More of this athttps://sourceforge.net/projects/gnat-math-extn/. Thank you Siomon for your answer. You are right I will take some inspiration from your packages ! Since Gnat is so close to C, I hope I won't need copying. Regards, Vincent