Passing Fortran allocated arrays back to C#

Passing Fortran allocated arrays back to C#

Our application has a C# front-end and a Fortran DLL backend for numerical computations.

I'd like to pass arrays allocated in Fortran back to C#, while avoiding copying of the arrays.

Is this possible? If so, can someone provide a simple example.



2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Kevin,

We use the same approach you describe in our engineering software: a C# GUI calling routines in Fortran DLLs.  We have had good success declaring arrays in the C# method before calling the Fortran subroutine, and returning results or data from a file in the passed array.  We pass the arrays by reference rather than by value, which I think avoids the issue of copying the array you mentioned.  If we use a multi-dimensional array, we transpose the array in the C# code before calling the Fortran subroutine.  We also pass integer variables with the array row and column dimensions so that we can fully declare the array in the Fortran subroutine.

Depending on the data returned from the Fortran routine in the array, such as values from a file or from a calculation, we either determine the required array size or get an estimate of the needed array size to declare the array in the C# method.  There is error checking in the Fortran subroutine to avoid going out of bounds in the array.  Could you use this approach?


Leave a Comment

Please sign in to add a comment. Not a member? Join today