Blitz logo

Blitz Support :

From: Todd Veldhuizen (tveldhui_at_[hidden])
Date: 2003-10-01 17:21:52


Hi Sean,

> #include <vector>
> int Nx=10;
> int Ny=20;
> vector<float> V(Nx*Ny); // 2D represented by 1D vector<>
>
> Now if I want to use blitz++ instead,
>
> Array<float,2> A(Nx,Ny);
>
> then I have three questions:
>
> 1. Is the underlying data always contiguous in memory (assuming stride=1)?

Yes, although some views of the data (subarrays) can be noncontiguous
of course.

> 2. If so, is there a way of referencing the underlying 2D contiguous
> data as I would with a pointer to the 1D STL vector<>?
> e.g.
> legacy_C_app( &V[0], Nx*Ny ); // pass the whole "2D" array
> or
> legacy_F77_app( &V[0], Nx*Ny ); // pass the whole "2D" array
> (I find the above method especially convenient for calling F77 since I
> can sidestep the different index-ordering of C and F77.)

Yes, see the data(), dataFirst(), dataZero() methods of Array.
http://www.oonumerics.org/blitz/manual/blitz02.html#index00119

> 3. If I take a slice of a 2D Array in either dimension using the
> assignment operator, will the resulting 1D Arrays be contiguous in
> memory? (I know this won't work with the copy constructor)
> i.e., given
> Array<int,1> Xslice( Nx ), Yslice( Ny );
> Xslice = A( Range::all(), 3 );
> Yslice = A( 3, Range::all() );

Hmm, this is fine (although inefficient since you're copying), and
the Xslice and Yslice arrays will be contiguous.

> can I now pass Xslice and Yslice to legacy apps?
> e.g.
> legacy_C_app( &Xslice[0], Nx ); // pass a contiguous slice along the
> X axis
> legacy_C_app( &Yslice[0], Ny ); // pass a contiguous slice along the
> Y axis
>

Looks like it'll all work.

Cheers,
Todd