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

Passing .NET arrays to Fortran

dfousekcres_gr
Beginner
534 Views

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}).
U
nfortunately, 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?

Regards,
Dimitri


 

0 Kudos
1 Solution
Steven_L_Intel1
Employee
534 Views

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 MixedLanguage.zip 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.

View solution in original post

0 Kudos
3 Replies
Steven_L_Intel1
Employee
535 Views

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 MixedLanguage.zip 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.

0 Kudos
dfousekcres_gr
Beginner
534 Views

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?

Regards,

Dimitri 

0 Kudos
Steven_L_Intel1
Employee
534 Views

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.

0 Kudos
Reply