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.
29280 Discussions

Bizarre Problem: call to EXP intrinsic function not working

Bryant_Jurgens
Beginner
1,633 Views

Pulling my hair out...hoping some can help.

I've created a dll in fortran which uses the EXP intrinsic function and am calling the dll in excel.The exp function is called from a large fortran program I wrote, so I created a test dll to demonstrate the problem:


! Dll1.f90
!
! FUNCTIONS/SUBROUTINES exported from Dll1.dll:
! Dll1 - function
!
Real(8) function TestExp(a)
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL::TestExp
!DEC$ ATTRIBUTES ALIAS:'TestExp' :: TestExp
!DEC$ ATTRIBUTES REFERENCE :: a

IMPLICIT NONE

Real(8), intent(in) :: a

TestExp = dexp(a)
end function TestExp


It is then called from Excel using the declare statment in a VB module:

Declare Function TestExp Lib "D:\Default User\My Documents\Visual Studio 2005\Projects\Excel\Dll1\Dll1\Release\Dll1.dll" (ByRef a As Double) As Double

Unfortunately, the dllbombs when trying to calculate the exp for no ryhme or reason that I can think. Interestingly, I can change the EXP(a) to a SQRT(a) and the function works properly. This is obviously extremely aggravating and I can't figure out why it will work with some intrinsic functions and not the exponential. Any help would be greatly appreciated.

Bryant
0 Kudos
8 Replies
DavidWhite
Valued Contributor II
1,633 Views
Quoting - Bryant Jurgens

Pulling my hair out...hoping some can help.

I've created a dll in fortran which uses the EXP intrinsic function and am calling the dll in excel.The exp function is called from a large fortran program I wrote, so I created a test dll to demonstrate the problem:


! Dll1.f90
!
! FUNCTIONS/SUBROUTINES exported from Dll1.dll:
! Dll1 - function
!
Real(8) function TestExp(a)
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL::TestExp
!DEC$ ATTRIBUTES ALIAS:'TestExp' :: TestExp
!DEC$ ATTRIBUTES REFERENCE :: a

IMPLICIT NONE

Real(8), intent(in) :: a

TestExp = dexp(a)
end function TestExp


It is then called from Excel using the declare statment in a VB module:

Declare Function TestExp Lib "D:Default UserMy DocumentsVisual Studio 2005ProjectsExcelDll1Dll1ReleaseDll1.dll" (ByRef a As Double) As Double

Unfortunately, the dllbombs when trying to calculate the exp for no ryhme or reason that I can think. Interestingly, I can change the EXP(a) to a SQRT(a) and the function works properly. This is obviously extremely aggravating and I can't figure out why it will work with some intrinsic functions and not the exponential. Any help would be greatly appreciated.

Bryant

Bryant,

Have you checked that that value of a is being passed correctly? If this is being corrupted by the interface, then I could imagine values where SQRT(a) is still valid, while EXP(a) would cause an overflow. On error, the DLL will attempt to write an error to the console, and this will cause Excel to fail.

David
0 Kudos
Bryant_Jurgens
Beginner
1,633 Views
Thanks for the reply David. I just checked the function for values of a by replacing TestExp = dexp(a) with just TestExp =a:
afort func TextExp
-1.00E-10 -1E-10
-1.00E-09 -0.000000001
-1.00E-08 -0.00000001
-1.00E-07-0.0000001
-1.00E-06 -0.000001
-1.00E-05 -0.00001
-1.00E-04 -0.0001
-1.00E-03 -0.001
-1.00E-02 -0.01
-1.00E-01 -0.1
-1.00E+00 -1
-1.00E+01 -10
-1.00E+02 -100
I checked this for positive values as well....I'm truly dumbfounded

Bryant
0 Kudos
DavidWhite
Valued Contributor II
1,633 Views
Quoting - David White

Bryant,

Have you checked that that value of a is being passed correctly? If this is being corrupted by the interface, then I could imagine values where SQRT(a) is still valid, while EXP(a) would cause an overflow. On error, the DLL will attempt to write an error to the console, and this will cause Excel to fail.

David
Bryant,

I assume these values are in your calling routine. What is important, is the values are inside the DLL. If the data types and convention for passing data across the interface are not correct, then the values inside the routine may be meaningless. Copying the values back to the calling routine as you have done will give meaningful values in the calling routine, but not in the DLL.

In the DLL add some code like
OPEN(1, FILE="C:Test.out", POSITION="APPEND")
WRITE(1,*) A
CLOSE(1)

Then look at the file after calling with the examples you gave. If the data is being corrupted across the interface, you will see it.

David
0 Kudos
Steven_L_Intel1
Employee
1,633 Views
What does "DLL bombs" mean?
0 Kudos
Bryant_Jurgens
Beginner
1,633 Views
I added the code you provided to the dll but the program fails to write the file and the functionwon't return a value to Excel. Can you explain a little more why some instrinsic functions might handle the data being passed and other not work? For a little more background, I use the declare function statement in VB to link to the dll. Then, in any excel worksheet I can type in various values of a in cells and use the fortran function in an adjacent cell using the formula "=TestExp(A2)." For operations involving the exponential function, i.e. code in dll: TestExp = exp(a) or dexp(a) , the dll fails to return a value. However, if I change the code in the dll to: TestExp = sqrt(abs(a)**3), I get the correct value returned to excel. I really don't understand how the dll could return resultsfor someintrinsic functions and not others? Is there some fortran setting I should be checking?

0 Kudos
TimP
Honored Contributor III
1,633 Views
Quoting - Bryant Jurgens
For operations involving the exponential function, i.e. code in dll: TestExp = exp(a) or dexp(a) , the dll fails to return a value. However, if I change the code in the dll to: TestExp = sqrt(abs(a)**3), I get the correct value returned to excel. I really don't understand how the dll could return resultsfor someintrinsic functions and not others? Is there some fortran setting I should be checking?

The difference here would be that sqrt() and abs() are compiled in line and don't require run-time library support. This makes it look like a library interface or link problem.
0 Kudos
Bryant_Jurgens
Beginner
1,633 Views
Quoting - tim18
The difference here would be that sqrt() and abs() are compiled in line and don't require run-time library support. This makes it look like a library interface or link problem.

So is there any way to diagnose where the error might be? Are there certain settings that I should be checking/changing at when compiling the dll.
0 Kudos
Bryant_Jurgens
Beginner
1,633 Views
I just updated Intel visual fortran from version 10 to 11. Then, I recompiled the dll with the exponential function, i.e. TestExp = dexp(a), and the function now returns the correct values!.....These little programming quirks can sure shorten a life....

0 Kudos
Reply