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

Interrupting a long calculation

eos_pengwern
Beginner
622 Views
Hello,

Some of the calculations in my DLL can get quite long, and it would be helpful to have a way (other than running in debug mode)to interrupt them gracefully, check some variables, and then either stop or resume.

There doesn't seem to be a standard mechanism in Fortran, though, to allow for this. What I'd like to do is to have the calling application set a flag when it wants to stop, so that the Fortran calculation could check this from time to time and make its exit if it sees the flag set. In practice, though, I can't see a way of doing this without risking data race conditions if the calling program tries to set the flag at the same time the Fortran routine is checking it. As Fortran lacks a 'mutex' capability (other than that provided in OpenMP, which isn't really applicable here), or even a 'try-catch' structure, I can't see a way of making this work.

The only idea I have so far is to make the application write the flag into a file, since a file can only be open by one thread at a time and Fortran does at least provide the ERR specifier to intercept the case of trying to open an already-open file. This seems like a painfully clunky way to proceed, however.

I'm sure this is a fairly common requirement, so is there a better way of doing it that I haven't thought of?

Thanks,
Stephen.
0 Kudos
1 Solution
Steven_L_Intel1
Employee
622 Views
It's not hard at all. There's a sample provided that uses mutexes - DLL_SHARED_DATA. The coding is straightforward. You could look at MSDN C samples and translate if you felt the need. Probably the hardest part is to understand the behavior of each of the various synchronization objects.

View solution in original post

0 Kudos
4 Replies
Steven_L_Intel1
Employee
622 Views
The Win32 API has mutexes, events, barriers and more. Signaling an event could be detected by your Fortran code. You could also pass the address in of a variable that is changed by the caller when a pause is wanted. Your Fortran code would still have to test for this periodically.


0 Kudos
eos_pengwern
Beginner
622 Views
The Win32 API has mutexes, events, barriers and more. Signaling an event could be detected by your Fortran code. You could also pass the address in of a variable that is changed by the caller when a pause is wanted. Your Fortran code would still have to test for this periodically.



This had occurred to me, but I recoiled from the prospect as I haven't delved into the Win32 API much before! Presumbly the necessary API calls are available in the IFWIN module. Is there any documentation (or tutorial information) available for doing this specifically in Fortran, or do I just need to get a C++ textbook and translate?

Thanks,
Stephen.
0 Kudos
Steven_L_Intel1
Employee
623 Views
It's not hard at all. There's a sample provided that uses mutexes - DLL_SHARED_DATA. The coding is straightforward. You could look at MSDN C samples and translate if you felt the need. Probably the hardest part is to understand the behavior of each of the various synchronization objects.
0 Kudos
eos_pengwern
Beginner
622 Views
It's not hard at all. There's a sample provided that uses mutexes - DLL_SHARED_DATA. The coding is straightforward. You could look at MSDN C samples and translate if you felt the need. Probably the hardest part is to understand the behavior of each of the various synchronization objects.

OK, thank you; I'll look into it.

Stephen.
0 Kudos
Reply