Silverfrost Forums

Welcome to our forums

Passing C# arrays to fortran

28 Feb 2007 2:26 #1762

Hi,

I got the following C# function

float[,] c = {{1.0f,2.0f},{3.0f,4.0f},{5.0f,6.0f}}; // Reshuffling to ftn indexing float[,] buf = FtnUtilities.ToFortran(c); int i=3, j=2; FromDotNet(buf, i, j);

The FromDotNet is a fortran assembly interface as declared below Subroutine FromDotNet(Buf2D, IN, JN) IMPLICIT NONE ASSEMBLY_INTERFACE(Name='FromDotNet') INTEGER IN, JN REAL(Kind=1) Buf2D(:,:)

   Call FtnSub(Buf2D, IN, JN)
   End Subroutine FromDotNet
   
   Subroutine FtnSub (Buf2D, IN, JN)
   IMPLICIT NONE
   INTEGER IN, JN
   REAL(Kind=1) Buf2D(IN,JN)

   End Subroutine FtnSub

When the buffer received by the fortran assembly interface function is passed to the fortran function, the following exception is thrown: ''Salford.Fortran.RuntimeException' occurred in ftn95lib.mdl Additional information: 19: Argument one is too small for its declared size'. Is it possible to pass a C# array to a fortran assembly interface and then passing the same array further on to a fortran subroutine? If so, how should the 2 dimensional array be declared in the ftn assembly interface and in the fortran subroutine to make this possible?

28 Feb 2007 8:46 #1766

The relevant information can be found in the help file under .NET Platform->.NET Programming->.NET Arrays.

FTN95 does not convert .NET arrays into Fortran arrays. You can pass a .NET array (or indeed a Fortran array) but you must treat it as the same object at both ends or do your own copying from one form to the other.

You need also to be aware of the garbage collection problem, namely that (in .NET) objects can be moved by the garbage collector and this can be a problem if they are treated as fixed in memory whilst in Fortran code. Thus, for example, you can have problems if a .NET array element is passed as an INTENT(OUT) argument to a subroutine (if garbage collection occurs whilst in the Fortran routine then the element may have been moved to a different address when the information is passed back to the caller). The latest version of FTN95 prevents you from doing this.

Please login to reply.