Passing .NET arrays to Fortran

Passing .NET arrays to Fortran

Hi !

I am trying to find an easy way to use some Fortran subroutines in Visual Studio (VB.NET) and take advantage of the speed difference a FORTRAN DLL provides.

My problem is that my FORTRAN code uses many non-zero based (multidimensional) arrays, which is something forbidden in Visual Basic.

A nice way to overcome this in Visual Basic is the use of .NET arrays (created with the Array.CreateInstance method). Thus, in Visual Basic instead of creating an array the standard way: 
           Dim x(np) as Single 
I use: 
                       x = Array.CreateInstance(GetType(Single), New Integer() {np}, New Integer() {1}).
Unfortunately, such an array when "sent" to the FORTRAN subroutine it crashes.

Am I wrong to expect that these arrays should also be handled by a compiler labeled with Visual Studio?

Also, concerning multi-dimensional arrays passes from VS to Fortran, wouldn't be nice if there was a compiler directive to "reshape" such arrays automatically, instead of taking care of it with a Reshape function, just before the subroutine returns?



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

Yes, you would be mistaken to assume that all forms of variables in one language are automatically interoperable with a different language, whether it be Fortran or C++ or whatever. However, it is generally possible to access Visual Basic multidimensional arrays in Intel Visual Fortran (note that "Visual Studio" is not in the name.) We provide an example program that does exactlty this called VB.Net-Safearrays. You'll find it in the archive under Samples in the installed product on your system. (Unzip to a writable folder not under Program Files.)

It isn't as simple as you'd like, but it isn't difficult either.

Retired 12/31/2016

Hello Steve !

You are right; I succeeded to transform the VB.NET-SafeArrays for REAL data types instead of CHARACTERs.
However, in the Fortran subroutine, retrieving one-by-one the values using the SafeArrayGetElement() isn't that elegant. What would you propose instead? 

Also, this example worked for me when sending to the Fortran subroutine, a VB array and not the .NET array (created with the Array.CreateInstance method which could have non-zero lower array bounds. Am I doing something wrong?



I am not familiar with this other array type.

The reason you need SafeArrayGetElement is that there's no simple memory correspondence for the array element locations.

Retired 12/31/2016

Leave a Comment

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