My question is about interworking of Fortran and C. The interface part of my program is written on C (using MFC), and it calls Fortran function, that performs some calculations. These calculations take some time (say, solution of system of linear equations). I want to see the progress of this work in C program (say, to display it in progress bar) and to have an ability to cancel it in any time (by pressing an appropriate button).
So, the question is how to implement all these in the best way? If the Fortran function will start as a new thread, with help of what techniques and functions can I implement inter-thread information interchange?
Any help/suggestions would be very welcome.
We have done a simpler form of this for many years, but the idea of status seems to indicate that you need the Fortran to indicate to the C caller what progress it has made. My only suggestion along these lines is running the Fortran as a callable DLL with a queryable status indicator. That is, start the Fortran routine (the one that takes time) but within that routine, say within a module, have some idea of status kept. For example, if the long-running Fortran routine has a large DO loop, the status might be the DO count. The DO count would be a module level variable which could be reported queried by an external routine.
Just a thought. Currently, the only way I can imagine doing this is through .NET architecture and their threading constructs. MFC is a few generations behind this.
Add/Include to your DLL a module that contains 3 integers: iBegin, iEnd, iPos. These can be in a struct (user definded type).
Add a DLL function that you can call from C/C++/other that obtains the address of this struct. Add to the C/C++/other code a thread that periodically polls the values in the struct, and then uses them to update a progress bar. The polling thread would also need to know when the program terminated.
You can repeat this should you require multiple progress bars.
Next, modify your code in the DLL by locating the major loop
ProgressBar%iBegin = 1 ProgressBar%iEnd = nObjs DO I = 1,nObjs ProgressBar%iPos = I ... ! code end do
This is relatively light weight computationally in the Fortran DLL and is not dependent upon the dialog box with the progress bar being present.
Note, the Windows Dialog system likely has a periodic timer feature which you can use to perform the polling. Note, the pointer to the ProgressBar should be =NULL'd at build and updated when you call the DLL to obtain the address. When NULL, the polling task should avoid updating the progress bar (or display 0). Likewise, on exit, you need to nullify the pointer to avoid a race condition that may (seldom) occur should your DLL get unloaded prior to termination of the Dialog box.
In a dynamic process simulator (www.xpsimworld.com) the windows interface is developed in C++ and the math engine in fortran (IVF 16) this kind of interaction is implemented using a very simple solution: two short sequential files.
The C++ fills a 'request file', the fortran reads the request and creates an 'answer file' which is finally read by the C++ monitor.