1

Modules in a Fortran project are exchanging data by mean of saved variables. That works fine even if I don't like the design.

Now, I have to call a subroutine externally inside a C++ loop. The problem is that there is no way to reset the state of the program each call. The called Fortran subroutine retains information from the previous call.

Excluding total rewrite of the Fortran code, since there are too many saved variables and they seem quite organized, how can I destroy these variables each successive call from C++ so that each call is like a fresh new ?

  • 1
    What is the nature of the FORTRAN module? Is that a dll, a static library, or is it running in a different process? What OS is your application running under? – Michaël Roy Jun 16 '17 at 04:19
  • Are there initial values defines for the module variables? It would be possible to close and open again a dynamic .so library, but that is slow. – Vladimir F Героям слава Jun 16 '17 at 05:17
  • I am running under (GNU) Linux x86_64, the front end of the Fortran code is a single file inside my directory, the rest is inside a _shared_ library. Yes, there are initial values which I want to use each call instead of the updated ones. I am looking for something like a "_GLOBAL DESTROYER/INITIALIZER_" for all saved variables. – Navalona Ramanantoanina Jun 16 '17 at 13:36
  • 2
    In my experience, it's pretty standard to 'initialize' all local variables to a default value at the beginning of each subroutine. Is this all that you need? Also, note that a common Fortran "gotcha" is that declaring variables like `integer :: val = 0` only initializes it the first time, and then they have an implicit `save`. – Matt P Jun 16 '17 at 20:32
  • I still didn't find a solution for this problem. At least, is there any trick I can use to do a comparison of memory status between two calls? Making an inventory of non initialized saved variable would be a first step. – Navalona Ramanantoanina Jun 19 '17 at 17:44
  • why not write a Fortran subroutine that upon call, does the job of variable initialization in the module (i.e., the reset functionality that you are interested)? I think this is the standard way of doing it. – Scientist Jun 20 '17 at 23:43
  • Probably write some code to show the nature of your program, how the subroutine is called, etc. Even pseudocode would give the rest of us a better chance of helping. – Matt P Jun 22 '17 at 14:21

1 Answers1

0

You can reset a module by closing a shared library and opening it again. If you do it too often it will be slow. It is more complicated than normal shared library linking, because the library must be loaded and unloaded at run-time. See an example in https://stackoverflow.com/a/44309012/721644 which unloads and loads again a module from an .so shared library in Linux. This example calls dlopen and dlclose from C, you can also call it from C++ (with some interface even from Fortran).