1

I have a c# windows service that starts a couple of threads and then each thread executes the same fortran function from a static dll library compiled with intel fortran 9.

When that happen, the first function keeps running just fine, while the other one triggers a c# exception.

Is there any compiler option to fix that? I googled a lot and only find recursive and save. I have to test them in the office tomorrow, but I am not optimistic.

PS: The fortran code has modules

Thanks!

BCartolo
  • 720
  • 4
  • 21
  • It would help if you showed the code that you are using. Are you sure that the problem is in the fortran library? What is the exception message? Would it make sense to wrap the code in critical section so it is NOT executed simultaneously? – Piotr Rodak Jun 22 '11 at 12:17

1 Answers1

3

Your problem is most likely the Fortran code. It's not uncommon for Fortran code to use shared global state variables, especially for older code. That's just one possibility, but there are plenty of other reasons why the Fortran code may not be thread-safe for your usage.

If this is indeed the problem then you have a few options that may help:

  • Serialise calls to the Fortran code with a mutex/lock.
  • Re-factor the Fortran code to remove global shared state, e.g. moving it onto the stack.
  • Arrange that each thread uses a separate instance of the DLL.

The last option is a rather gross hack, but it may be the most effective short term solution. To arrange that you have separate instances you just need to copy and rename the DLL so that each thread loads a DLL with a different name. Even if they are identical this is enough to persuade Windows to load separate instances of the DLL module and therefore separate instances of all global data.

One final thought: make sure that you are linking the Fortran to the multi-threaded version of the Fortran runtime.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks david for your quick response!! Serialise calls to the Fortran code with a mutex/lock. I dont know how to do that Re-factor the Fortran code to remove global shared state, e.g. moving it onto the stack. Very long code! I dont want to do that. If I want to re program the whole thing then I would do it in C# directly :D Arrange that each thread uses a separate instance of the DLL. THIS SOLUTION ROCKS!!! Really simple! Thanks again One final thought: make sure that you are linking the Fortran to the multi-threaded version of the Fortran runtime.Dont know how to do that – BCartolo Jun 22 '11 at 12:55
  • You use a lock with the `lock` statement in C#. Have a google around for it but it's really pretty simple. – David Heffernan Jun 22 '11 at 12:59
  • re multi-threaded runtime, it's always compiler specific and I know nothing about compiling and linking Fortran. In fact if truth be told I know nothing worth speaking of about Fortran and always deal with it using my favourite program: [f2c](http://en.wikipedia.org/wiki/F2c). – David Heffernan Jun 22 '11 at 13:02
  • Your answers have being very useful. Thanks again! – BCartolo Jun 22 '11 at 15:27