Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28632 Discussions

Passing Fortran allocated arrays back to C#

Kevin_Vandersloot
410 Views

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.

Thanks,

Kevin

0 Kudos
1 Reply
Greg_T_
Valued Contributor I
410 Views

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?

Regards,
Greg

0 Kudos
Reply