>>>>> "William" == William F Mitchell <[log in to unmask]> writes:
>> Phil Austin <[log in to unmask]> wrote:
>>
>> I'd like to write some Matlab and Numeric Python extensions
>> using Fortran90, and therefore am looking for the best way to
>> pass a C pointer into an F90 function and have it represented
>> as a (say) 3 dimensional F90 array. I've taken a look at
>> William Mitchell's Opengl bindings
>> (http://math.nist.gov/f90gl), which declare F90 arrays as, for
>> instance:
>>
>> integer(kind=glint), dimension(*) INTENT_IN lists
William> AFAIK, to be able to do the things you want to do, you
William> will have to use explicit shape arrays and pass the three
William> dimensions from your C routine to Fortran.
Good, I can live with this--in C++/Blitz I would write (assuming
I didn't want to modify my input array):
C++: (let's say I want the Fortran indexing convention)
extern "C" void fortran_sub( const float* in, float* out,
const int& n1, const int& n2, const int& n3 );
...
dim1=2; dim2=5; dim3=10; /* you will have to pass the dimensions */
Array<float,3> in(dim1,dim2,dim3,FortranArray<3>());
Array<float,3> out(dim1,dim2,dim3,FortranArray<3>());
...
//note that C++ references eliminate need for pointers
fortran_sub( in.data(), out.data(),
in.extent(firstDim),in.extent(secondDim),in.extent(thirdDim));
Question: Is intent ok in the declaration? (I take it explicit shape
arrays are full f90 citizens?)
SUBROUTINE fortran_sub( in, out, n1, n2, n3 )
! Unchanged from Peter's example
IMPLICIT NONE
INTEGER n1, n2, n3
REAL, INTENT_IN in( n1, n2, n3 )
REAL, INTENT_OUT out( n1, n2, n3 )
>> I'll also need to go the other way (i.e. return a C pointer to
>> lists' data).
William> I'm not sure exactly what you're after here. I can think
William> of two or three interpretations of this statement. Can
William> you be a little more explicit?
I think I can live without it, but I could imagine wanting my Fortran
subroutine to control allocation/deallocation, and wanting to call
a wrapped C++ function that did something with the data. If Fortran was able to
provide a C-style pointer to the start of the allocated array,
I could construct a Blitz array referenced to it:
C++:
extern "C" void fortran_sub(const float* inArray, float* outPtr,int& dim1,
int& dim2, int& dim3);
...
//Make a tied Blitz array pointing at the same data:
Array<float,3> outArray(outPtr,shape(dim1,dim2,dim3),neverDeleteData,
FortranArray<3>(),);
Because Blitz arrays are referenced counted, when outArray goes out of
scope in fortran_sub only the reference will be deleted, not
the data pointed to by outPtr.
Regards, Phil
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|