Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modern Fortran Interface #437

Open
Nicholaswogan opened this issue Jan 28, 2022 · 3 comments
Open

Modern Fortran Interface #437

Nicholaswogan opened this issue Jan 28, 2022 · 3 comments

Comments

@Nicholaswogan
Copy link

Looks like the fortran interfaces uses Fortran 77. From the fortran side, the compiler doesn't know the function and subroutine interfaces. So, it is really easy to call a function incorrectly, receive no compiler warning, and get a buggy result.

But a much more reliable fortran-c interface could be made with iso_c_binding, and interfaces.

@awvwgk
Copy link
Contributor

awvwgk commented Jan 28, 2022

There are Fortran bindings based on iso_c_binding in grimme-lab/nlopt-f exposing the full object-oriented API in a Fortran-friendly way.

However, the nlopt include file for Fortran could still be improved by including explicit interface definitions to provide at least type-safety, see #401 (comment). One would have to do some more elaborate transformation from the nlopt.h header, but this could be challenging to implement in pure CMake.

@ivan-pi
Copy link

ivan-pi commented Jan 29, 2022

Just to elaborate more, the existing Fortran API does not provide any interface definitions for procedures. It does provides access to the return values for successful termination and error codes. Also the algorithm identifier codes are available. The consumer can include these in his program, using the line

   include 'nlopt.f'

The file nlopt.f is generated automatically by the script nlopt/cmake/generate-fortran.cmake.

As @awvwgk has mentioned, it would be difficult to do the same thing for procedures. It is sad that C compilers don't have an option to export Fortran interfaces...

@ivan-pi
Copy link

ivan-pi commented Jan 29, 2022

Here's a related 2017 thread from the Intel Community Forum: https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-solve-using-different-types-of-name-mangling/m-p/1081325

A small fix would also be needed in the NLopt documentation (NLopt_Fortran_Reference.md). As Steve Lionel commented towards the end of that thread:

Neither "integer4" nor "integer8" is correct. What should be used is INTEGER(C_INTPTR_T) (USE ISO_C_BINDING to get that constant.)

I also would strongly advise against using /names to handle name-mangling. Best is to declare an interface with BIND(C) and use NAME= if you need mixed-case.

@xecej4 also commented with:

It would have been nice if they provided an interface module for using the NLopt C routines in Fortran.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants