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

Issue with FileNotFoundError when calling Fortran DLL in Python due to dcos function

411390
Beginner
388 Views

Dear Intel Fortran Community,

After compiling a Fortran code using the command "ifx /dll Sample.f90", when calling it via "ctypes.CDLL" in Python, I encountered the error: "FileNotFoundError : Could not find module '{folderpath}Sample.dll' (or one of its dependencies). Try using the full path with constructor syntax." The file path to Sample.dll is correct, and it exists on the system.

 

Upon investigation, I found that the cause of the issue is the dcos function in line 18 of Sample.f90, which uses the input variable ("sweep" in my code). For example, if I replace "dcos(0d0 * pi / 180d0)" with a real number such as 0d0, the FileNotFoundError is resolved. However, I would like to be able to use sweep in the dcos function and still call the DLL from Python.

 

module Sample
    use ISO_C_binding
    implicit none
    contains
!*****************************************************************************************************************
    subroutine Samplecode(imax, xc, zc, sweep, node_pos) bind(C, name = "Samplecode")
        !DEC$ ATTRIBUTES DLLEXPORT :: Samplecode
        integer(c_int), intent(in) :: imax
        real(c_double), intent(in) :: xc(imax)
        real(c_double), intent(in) :: zc(imax)
        real(c_double), intent(in) :: sweep
        real(c_double), intent(out) :: node_pos(imax, 2)

        real(c_double) :: pi

        pi = dasin(1d0) * 2d0
        node_pos(:, 1) = xc(:)
        node_pos(:, 2) = zc(:) / dcos(sweep * pi / 180d0)

    end subroutine Samplecode
!*****************************************************************************************************************
end module Sample

 

To resolve the issue, I have tried the following steps, but none of them were effective:

 

1.  Added the folder containing the DLL to the environment variables using "os.environ['Path']" in Python.

 

2. Replaced the dcos function with cos in the Fortran code.

 

3. Used an intermediate variable to store the result of "dcos(sweep * pi / 180d0)" in Fortran.

 

4. Verified ucrtbase.dll using the "sfc /scannow" command.

 

5. Tried various compilation commands:

・ifx /dll /Od Sample.f90

・ifx /dll /Od /fp:precise Sample.f90

・ifx /dll /fp:source Sample.f90

・ifx /dll /Qimf-arch-consistency:true Sample.f90

 

6. Verified the architecture of the DLL:

・Sample.dll architecture: 8664 machine (x64)

・Python architecture: 64-bit

 

7. Checked the runtime libraries by executing "ifx /?", which displayed the Intel Fortran Compiler Help as expected.

 

8. Used the "dumpbin /exports Sample.dll" command to verify the correct export of functions from "Sample.dll".

 

Despite these efforts, the issue persists. I would appreciate any insights or suggestions on how to resolve this issue.

Labels (2)
0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
304 Views

"one of its dependencies" - this is the key. the DCOS function is supported by libmmd.dll - this needs to be in PATH or in one of the other places Windows looks for DLLs. However, if you use this DLL only from Python, and not other Fortran code, the simple solution is to add /libs:static to your ifx command.

View solution in original post

2 Replies
Steve_Lionel
Honored Contributor III
305 Views

"one of its dependencies" - this is the key. the DCOS function is supported by libmmd.dll - this needs to be in PATH or in one of the other places Windows looks for DLLs. However, if you use this DLL only from Python, and not other Fortran code, the simple solution is to add /libs:static to your ifx command.

411390
Beginner
280 Views

Thank you for your reply.

Among the solutions you suggested,  I added "/libs:static" to the ifx command to resolve the issue.

0 Kudos
Reply