2

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!

4
  • What's the significance of the first 4 indices? Does each one define a value of a different variable or something? In other words, why is each 2D matrix defined by 4 dimensions/variables/addresses? Commented Dec 3, 2020 at 15:02
  • @JoeTodd yes, each index refers to the value of a particular variable. The vars don't just linearly transform the matrices or anything like that, though, the matrices are all totally different (in general). Commented Dec 3, 2020 at 15:50
  • 1
    You do not want to have the small matrices in the last indexes. That makes them completely scattered in memory. You want them in the first indexes. Commented Dec 3, 2020 at 18:42
  • Arrays of arrays can only be done in Fortran using derived types. You may want to construct an equivalent class using ctypes or something. Commented Dec 3, 2020 at 18:43

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.