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

Fortran DLL in C#

K_R_2
Beginner
968 Views

Hi. I am new to this forum. I need some help/advice on how to declare and use multi-dimension arrays in Fortran. I am trying to create a Fortran DLL using Intel XE 2013 and use this DLL in a C# code (VS 2010).

The Fortran code looks like:

 module mCritspdData
 !dec$ pack:4
      type st_TestDLL
             integer *2 :: Points
             real *8 :: SK1(10,20)  
             real *8 :: SK2(10,20)
      end type st_TestDLL
 end module myTestModule

What is the equivalent "struct" in C# code?

0 Kudos
4 Replies
Greg_T_
Valued Contributor I
968 Views

Hi K. R.,

Would you want to start with basic data arrays before trying to pass a more complicated case such as a derived type? 

I have had good success passing 1-D and multi-dimensional integer and double arrays from C# (the GUI of our program) to Fortran DLLs (our engineering modules).  One place to look is the C interoperability information in the user manual. 

One important detail when passing multi-dimensional arrays from C# to Fortran is to take care with the transpose.  Since Fortran arrays are stored by column order and C# arrays are stored by row order, I generally transpose the array definition in the C# code (reverse the order of the indexes).  Then the arrays are passed by reference to Fortran and the transpose is taken care of there so that you can access the arrays in Fortran. 

Have you added the "using System.Runtime.InteropServices;" syntax at the top of your C# class?  And use the "[DllImport("yourDllName")]" to declare a static extern void declaration you can call from within the C# code.   

I also found useful information at this web page: http://www.nag.co.uk/numeric/csharpinfo.asp

Is this the type of getting started information you're looking for?

Regards,
Greg

0 Kudos
K_R_2
Beginner
968 Views

Greg,

Thanks for the response. I do have the "....InteropServices" and the "DLLImport..." statements in the code. As you said, I started with 1-D arrays both as variable and in a "struct" and was able to pass the struct from C# to Fortran and get it back. I even got a 2D array to work when used as a parameter in the function call from C# to Fortran. However, the problem is to get the 2D array to work when it is inside a TYPE in Fortran (corresponding to STRUCT in the C# code).

K.R. 

0 Kudos
FortranFan
Honored Contributor II
968 Views

@K.R.,

See this thread, especially the attachments with Message #2:

https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-fo...

Of course, the case in the above thread is with Visual Basic, but as you know, in the .NET world, you can easily apply the same concepts to C# as shown with Visual Basic; the Fortran code remains the same.

As I indicate in the above thread, I would go with standard C interoperability feature in Fortran, use P/Invoke on the C# side, and perhaps work with 1-D arrays on the C# side unless there is a real need to work in 2-D mode:

   use, intrinsic :: iso_fortran_env, only : c_int, c_double
   
   integer(c_int), parameter :: N = 10
   integer(c_int), parameter :: M = 20
   type, bind(C) :: st_TestDLL
      integer(c_int) :: Points
      real(c_double) :: SK1(N,M)  
      real(c_double) :: SK2(N,M)
   end type st_TestDLL
    public const int N = 10;
    public const int M = 20;

    [StructLayout(LayoutKind.Sequential)]
    public struct st_TestDLL
    {
        public int Points;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = N*M)]
        public double[] SK1;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = N*M)]
        public double[] SK2;
    }

 

0 Kudos
ver__jomy
Beginner
968 Views
Difference between normal DLL and .Net Dll
0 Kudos
Reply