-
Notifications
You must be signed in to change notification settings - Fork 556
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
[WIP] A Modern Fortran Interface #233
Conversation
The tests will need some updating, see #234. |
Thank you Julien for preparing the CMake. I have added a procedural test case where I access the C API directly and a second object-oriented example where I make use of derived-type extension in order to package the callback functions and required data together. I followed this discussion to get a minimal working Fortran CMake. To my dismay, I just noticed that the adaptor pattern introduces a memory leak; this happens after a call to A few more design choices I would like to discuss are:
use nlopt
implicit none
type(opt_type) :: opt
! ... do stuff with opt ...
end
|
The tests are compiled and run succesfully on osx, but the linux build fails due to an old version of gfortran (4.8.4) that does not support finalizers. I have built the Fortran interface successfully with gfortran-5 (5.4.0, there are some harmless compiler messages due to a bug in the compiler concerning finalization), or even better with gfortran-8 (8.1.0). I don't have much experience with Travis, but according to these guidelines I just need to include |
Would it be a lot of trouble to have a |
Excuse me for butting in, but one way to accomplish that would be to explicitly test whether a dummy program using the least frequently supported feature can be compiled: include(CheckFortranSourceCompiles)
CHECK_Fortran_SOURCE_COMPILES(
"module mod_test_finalizers
type test_finalizers
contains
final :: finalizer
end type
contains
subroutine finalizer(x)
type(test_finalizers) :: x
end subroutine
end module
program program_test_finalizer
use mod_test_finalizers
type(test_finalizers) :: x
end program"
HAVE_F03_FINALIZERS
SRC_EXT "f90"
) |
This would be very very helpful! |
Hi @gabrielgggg, you might want to check out the new Fortran bindings: https://github.com/grimme-lab/nlopt-f I will close this issue in favor of that project. |
Hello Steven,
I have been working on a modern Fortran interface for NLopt that is based upon the Fortran/C interoperability features defined in the newer Fortran language standards (> 2003). The new interface is a large improvement over the old interface that uses an
integer*8
as an opaque pointer to the "opt" class. Instead a derived-typeopt
is created that encapsulates the "opt" class as a C pointer and directly calls the C routines for access to the internals. It is quite similar to the C++ interface actually. The only thing missing at this point is the mechanism for returning error codes and the tutorial and API description are not finished completely.An adaptor strategy is used in order to hide implementation details of the C interface, particularly passing data to the callback functions (the objectives and constraints). The new Fortran way of doing this is based upon a "functor" pattern and expects the users to extend an abstract type. Alternatively the data can be safely passed as a
c_ptr
and must be unpacked "manually" by the user.I would be happy to get some input from you and other developers on what changes should I make that both preserve the philosophy of NLopt but are familiar to the modern Fortran programmer, before you would consider merging.
Looking forward to your response.
Sincerely,
Ivan