Alex H
Alex H

Reputation: 41

Passing jagged arrays to Fortran using f2py

I currently have a 6D numpy array in Python, where the first 4 indices specify which 2D matrix is required. I can pass that into Fortran90 using f2py, then access the 2D matrices using:

subroutine mySubroutine(myArray)

real(8), dimension(:,:,:,:,:,:) :: myArray

matrix = myArray(a,b,c,d,:,:)

However, the matrices can in general be different sizes. The only method I've found that allows for this is to make the final two dimensions equal to the largest possible size, store smaller matrices in the top left, and get the smaller matrix using:

matrix = myArray(a,b,c,d,1:matrixSize,1:matrixSize)

The problem with this is that it's very inefficient memory-wise, as I'm storing huge empty sections of the array that are never used. In Python I can address this by making lists of lists of lists of lists of 2D numpy arrays of the correct size, but I'm not sure how to pass them to Fortran using f2py, or if that's even possible. Any ideas would be appreciated. Thanks!

Upvotes: 2

Views: 128

Answers (1)

Joe Todd
Joe Todd

Reputation: 897

Definitely think you're right to avoid leaving lots of empty space - that definitely wouldn't scale well.

What about passing a completely flat array, and the shapes of each of the matrices? Then you can rebuild it at the other end? Because you have 2D matrices, the shape of each matrix can be defined by two integers. Then, so long as you have a standardised ordering of the flattened array, you can easily reconstruct them at the other side.

So if you had, for example, two matrices, one of size 10x10, and one of size 100x100, you could pass in:

FlatArr(10100) = [your_data]
SizeArr(4) = [10, 10, 100, 100]

Upvotes: 2

Related Questions