- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I am using VS 2013 and Intel Composer XE2017 for Fortran. I needed help in the process to pass a 2-d array from C# code to a Fortran DLL. The declarations are as given below:
In C# code: (the intent is that the array is 4x4)
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Maybe C# is different, but I always understood that in C a "2-D" array was an array of arrays of pointers.
What I would do is stop in the C# code just before the call and examine memory at the start of the array and make sure that the four elements are what I expect, also noting the address. Then step into the Fortran code and look at the same thing.
You have not shown us the Fortran procedure declaration nor the declarations of its arguments. These matter. I don't know how C# is passing the values. I will note that a rank-2 array is not interoperable with C, as far as the standard and BIND(C) are concerned. If you were using BIND(C), you'd have to declare the Fortran array as DIMENSION(4). Also keep in mind row-major vs. column-major.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve, Thanks for the quick reply. Here are the C# and Fortran code for the above problem that I am having.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well - that's different! See what I keep harping about snippets and paraphrases not being useful?
In your C# code, yVar and yVarDims are pointers to arrays, but your Fortran code has them as being arrays in the structure. You want the Fortran components to be of type C_PTR from ISO_C_BINDING. You can then use C_F_POINTER to map them onto a Fortran pointer of the desired shape. I would also STRONGLY recommend that you use the kind constants in ISO_C_BINDING rather than REAL*8 and INTEGER*2. (You would instead have REAL(C_DOUBLE) and INTEGER(C_SHORT).)
At this point I would recommend declaring the Fortran routine with BIND(C), and the derived type definition as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve, Thanks again. I have a favor to ask. Would you have an example of a Fortran code that shows the different pointers and Bindings that you mention in your email. I admit that I am not an "in-depth" Fortran programmer. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Google "Fortran example with bind(C)" and go to the link at the top of the list. There is a wealth of info online.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
R, K wrote:
Hi Steve, Thanks again. I have a favor to ask. Would you have an example of a Fortran code that shows the different pointers and Bindings that you mention in your email. I admit that I am not an "in-depth" Fortran programmer. Thanks.
@R. K.,
See Quote #25 in this thread for a fully-functional example: https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/740561
And also Quote #4 in this thread for some additional info: https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/697972
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page