Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

VS2005 C# call 64bit Fortran DLL (cannot use Fortran Array)

umchenz
Beginner
929 Views

Hi, all, I have a really weird problem. I am trying to use Dllimport in C# (VS2005) to call a 64 bit fortran dll, the follows is my fortran program. This works fine. But if I change the statement "a=b" to "a(1)=b(1)" or "a(:)=b(:)"and try to load this dll in C#, it always throw me an exception tells me that unable to load the module. What I mean is that as long as I did some operation on some elements of an array in Fortran dll, the C# program cannot even load it though that dll compiles successfully. If I just do operation on the whole array in dll, like "a=b"C# works fine. That is really weird. I hope anyone can be helpful, thanks.

subroutine test(a,b,size)

!DEC$ ATTRIBUTES DLLEXPORT :: test

!DEC$ ATTRIBUTES ALIAS:'test' :: test

!DEC$ ATTRIBUTES REFERENCE :: a,b

!DEC$ ATTRIBUTESVALUE:: size

integer size

doubleprecision a(1:size)

doubleprecision b(1:size)

a = b

end subroutine test

Sam

0 Kudos
4 Replies
Steven_L_Intel1
Employee
929 Views
When it fails to load, you have ended up with calls to the Fortran run-time libraries and thus the Fortran run-time DLLs. If those are not in PATH, you'll get this behavior.

As an alternative, you can build your Fortran DLL linked to static libraries. In your application's environment, this will work fine. I don't recommend static linking of DLLs when native C/++ or Fortran code is also in the application.
0 Kudos
umchenz
Beginner
929 Views

Hi, Steve, thanks a lot for your explanation, it makes sense to me. As I tried other functions like
some math functions in my Fortran dll, then the C# project won't even load it. I guess your solution should work though I have not tried it.

I am new to Intel Fortran though I have been on C# for a while. The reason I use Fortran is we think if we
put heavy math operation in the Fortran Dll instead of C++/C# dll, the speed will be enhanced, do you think it will make huge difference?

I have another naive question. I am not sure in the Fortran dll, I can put some function (eg. FunctionA) to be EXPORTED, and some other
function (eg. FunctionB) is only called internally in Fortran dll by FunctionA. I tried it, but it did not work to me, after I write "call functionB" in
functionA, my C# project won't load that Fortran Dll, I guess that is maybe the same reason because I did not specify my Fortran Runtime DLL path in my
PATH enviorment or copied it to my C# working directory. I am wondering in Fortran, there is a big "system.dll" for "array operation, math functions, and call
another subroutine" or they are separated runtime DLLs in Fortran? Any idea under which directory I may find them? Thanks.

Sam

0 Kudos
Steven_L_Intel1
Employee
929 Views
I can't tell you that using Fortran code for some of the computations will be faster. It may well be. You'll have to see for yourself. For best results, the Fortran routine needs to do a lot of work - say, process a whole array not compute a single value. If the routine is short any advantage will be outweighed by the overhead of loading and calling the DLL from managed code.

Your Fortran DLL will rely on one or more DLLs installed with Intel Visual Fortran. The folder containing these DLLs needs to be in the definition of the PATH environment variable, otherwise any DLL dependent on those DLLs won't load. Your own DLL needs to be wherever you say it is in the C# specification.
0 Kudos
umchenz
Beginner
929 Views
Thanks a lot.
0 Kudos
Reply