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

Use of legacy fortran code in a multithreaded C++ application

Sam_C_
Beginner
325 Views

 

I have a legacy code written in fortran 90 and would like to run that in a separate thread in a c++ application.  I looked to see if I can use Threads library for c++11 to run the fortran subroutine in separate threads but I have compilation errors.

I have Intel Parallel Studio XE 2017, I'd like a starting point. Should I pursue C++11 thread library to run fortran code, or is there an OpenMp documentation on how to do something similar with OpenMp API in Windows with the help of Intel Parallel Studio

hanks

 

0 Kudos
3 Replies
Steven_L_Intel1
Employee
325 Views

If the thread is to be started by C++ and there is only one thread, then the only thing you need to do is to make sure that both the C++ and Fortran code are built to link with the DLL run-time libraries. It doesn't sound to me as if you need to start threads in the Fortran code.

If there will be more than one thread running the same Fortran code, then add the /recursive option to the Fortran compiles.

It would also be a good idea to call for_rtl_init_ at the start of the C++ program and for_rtl_finish_ at the end. These are documented in the Intel Fortran docs.

If you somehow want the Fortran code to start its own new thread, that's a bit more complicated but you don't need to use OpenMP for it.

0 Kudos
Sam_C_
Beginner
325 Views

Great Steve,

You were right, the fortran code doesn't need to run a thread, but only the C++. There will be in this C++ application two threads , one C++ thread delegating some configurations and logging while the other when it starts it launches the fortran legacy code that does periodic computations.

Note that the fortran legacy code is a .lib library.

Can you direct me to the documentation to implement this interoperability between C++ and fortran using Intel compilers.

Thanks

0 Kudos
Steven_L_Intel1
Employee
325 Views

In the Intel Fortran documentation, Compiler Reference > Mixed Language Programming is where this topic is discussed. The link to the 17.0 documentation is https://software.intel.com/en-us/node/678420 - this link may not be valid when future versions are released, so go to the Documentation link in the FAQ. Keep in mind that Fortran has features to interoperate with C, not C++. C++ will work if you treat it as C. Any Fortran routines you declare in C++ code need to have 'extern "C"' in their prototype.

That your Fortran code is in a .lib is no problem - just make sure that the Libraries > Use Run-Time Library setting matches what your C++ project has under Code generation. Also see https://software.intel.com/en-us/articles/configuring-visual-studio-for-mixed-language-applications for tips on how to configure Visual C++ to link to Fortran code.

0 Kudos
Reply