Prev: Dhrystone
Next: Learning Ada
From: Dmitry A. Kazakov on
On Sat, 24 Jul 2010 04:57:53 -0700 (PDT), Ada novice wrote:

> So in essence I'm looking for examples which illustrate how to pass
> some data (e.g a matrix) from Ada to C,

Since C does not have arrays, especially 2D ones, you will have some custom
representation on the C side. It means that examples of bindings (there are
plenty of) won't help you unless you understand how you describe a
C-compatible object in Ada.

> do some computations on the
> matrix and return the results to the Ada environment.

Even more so, because C cannot return a compound object. Different C
libraries use different tricks to work around this.

I would propose reading RM B.3 and B.3.2. Another thing you have to
understand is the issue of flat arrays vs. arrays with a dope. The latter
cannot be used with C. Provided you know C well, the rest is simple.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Ada novice on
On Jul 24, 6:38 pm, Simon Wright <si...(a)pushface.org> wrote:

> but on a Windows machine it'll be something li
> <install-dir>\<gnat-release>\lib\gcc\<architecture>\<gcc-release>\adainclude\a-ngrear.ad[sb]
>
> This will lead you to System.Generic_Real_BLAS (s-gerebl.ad[sb]) and
> System.Generic_Real_LAPACK (s-gerela.ad[sb]), which are interfaces to
> the corresponding external libraries.
>
> Not sure that these will fit your 'simple' criterion, though!
>
> --S

Thanks. I've been able to locate these files under GNAT on my Windows
machine and read them. For eigensystems operations, only symmetric and
hermitian (complex-symmetric) matrices are allowed. I would like
however to be able to deal with a general non-symmetric matrix and
this I believe is not possible right now with Ada.

YC
From: Ada novice on
On Jul 24, 6:44 pm, "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de>
wrote:

> Since C does not have arrays, especially 2D ones, you will have some custom
> representation on the C side. It means that examples of bindings (there are
> plenty of) won't help you unless you understand how you describe a
> C-compatible object in Ada.


Thanks for the "warnings". One way that I'm thinking is to write the
elements of the matrix sequentially into a file in Ada, read them into
C, put them into a matrix in C, do the appropriate computations in C
(with the IMSL C library), write the output in C in a file, and
finally importing them back into Ada again for further processing. Of
course, having several read-write operations is not computationally
time-efficient but this is a modest way to get starting. I know C at a
basic level and I'm not not an extensive user.

YC
From: Dmitry A. Kazakov on
On Sat, 24 Jul 2010 11:04:18 -0700 (PDT), Ada novice wrote:

> On Jul 24, 6:44�pm, "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de>
> wrote:
>
>> Since C does not have arrays, especially 2D ones, you will have some custom
>> representation on the C side. It means that examples of bindings (there are
>> plenty of) won't help you unless you understand how you describe a
>> C-compatible object in Ada.
>
> Thanks for the "warnings". One way that I'm thinking is to write the
> elements of the matrix sequentially into a file in Ada, read them into
> C, put them into a matrix in C, do the appropriate computations in C
> (with the IMSL C library), write the output in C in a file, and
> finally importing them back into Ada again for further processing. Of
> course, having several read-write operations is not computationally
> time-efficient but this is a modest way to get starting. I know C at a
> basic level and I'm not not an extensive user.

Why not to call IMSL directly from Ada? I looked shortly here

http://www.vni.com/products/imsl/c/TechSpecs/CNLspecs3MatrixStorage.php

it looks well documented and fairly designed. There should be no problem to
call it from Ada.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: tmoran on
> On Jul 24, 6:44=A0pm, "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de>
> wrote:
>
> > Since C does not have arrays, especially 2D ones, you will have some cust=
> om
> > representation on the C side. It means that examples of bindings (there a=
> re
> > plenty of) won't help you unless you understand how you describe a
> > C-compatible object in Ada.
>
>
> Thanks for the "warnings". One way that I'm thinking is to write the
> elements of the matrix sequentially into a file in Ada, read them into
> C, put them into a matrix in C, do the appropriate computations in C
> (with the IMSL C library), write the output in C in a file, and
> finally importing them back into Ada again for further processing.

Why do somersaults when you can just walk a few steps. As Dmitry
says, it really does depend on just what you are supposed to pass to
the C routine. For instance, there's a very fast C routine to
calculate an FFT of a 256 element complex vector. Using

type Complex_Vectors is array(Integer range <>)
of Ada.Numerics.Complex_Types.Complex;

subtype Complex_256s is Complex_Vectors(1 .. 256);

procedure fftc4_256(V : in out Complex_256s);
pragma import(c, fftc4_256,"fftc4_256");

it can be called with

X : Complex_256s;
...
fftc4(X);

Hard to get any simpler. If the C routine wants a pointer to an array of
pointers to rows, plus a couple of dimensions, that's a little harder.
But if the array is stored simply, and C expects the elements laid out the
same way as Ada, you can probably just pass in a pointer to the first
element in the array:

type Matrix_Type is array(Integer Range <>, Integer range <>)
of aliased Ada.Numerics.Complex_Types.Complex;

type Pointer_To_Element is access all Ada.Numerics.Complex_Types.Complex;

procedure C_Routine(M : in Pointer_To_Element;
Height, Width : in Interfaces.C.Unsigned);
pragma import(C, C_Routine, "c_routine");

M : Matrix_Type(1 .. 10, 0 .. 100);
...
C_Routine(M(M'first(1), M'first(2))'access,
Height => M'length(1),
Width => M'length(2));

For a whole lot of examples of Ada calling C, download the source for
CLAW (Class Library for Ada Windows) from www.rrsoftware.com
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12
Prev: Dhrystone
Next: Learning Ada