Topic: Translate FORTRAN to C++ for CUDA

Hello All,

Its been a couple of years since last post and I haven't seen this topic in the the forum and apologize if I missed it.

I have my OpenMP FORTRAN simulation that I am thinking about converting to use multi-processing capabilities of CUDA cores. I would like to continue to use Windows as my development and runtime platform. I have done a cursory look at this and this seems possible. However, I don't want to rewrite the entire code into C++. I think SF (never attempted this) supports C++ subroutines called from FORTRAN, would it be possible to create CUDA C++ subroutines called from in my SF FORTRAN compile and link?

Thanks in advance,
Rod

Re: Translate FORTRAN to C++ for CUDA

Rod,

You'll want to use the ISO_C_BINDING intrinsic module for calling C++ routines. You will need to create a C interface to your C++ routines, though, but that issue is common to many C++ libraries that expose their API.  Fortran has no concept of C++ classes, so they'll appear as opaque type(c_ptr) arguments in Fortran.

You'll want to look at the ISO_C_BINDING module and the section on interoperability with C as a starting point, at least.  If you plan on really getting into this, you might consider a book on modern Fortran that would explain using ISO_C_BINDING.

EDIT: You'll also likely run into linking issues.  What your purporting to do isn't particularly easy or straightforward as you'll be using an additional compiler for all the CUDA code.

Jeff Armstrong
Approximatrix, LLC

3 (edited by grogley 2024-11-22 16:03:11)

Re: Translate FORTRAN to C++ for CUDA

Thanks Jeff!

My starting point for this project is just get FORTRAN to call a C++ subroutine. It looks like I have to work outside the confines of SF compile and link stages... but if not how to do this since I think the C++ file needs to be compiled first to object code then linked to the FORTRAN compile link stage. Will SF compile a C++ subroutine (not the brightest bulb in the programing universe)?

In that event I can't use the SF environment, I have downloaded the latest GNU files for Windows but I think given the path for these new files, the compile stage is going to SF path. How can I be sure it is using the non-SF path for compile and link?

What I am doing specifically, a friend told me about Copilot (This a revelation to me, seems like cheating!) and it gave an example of a mixed FORTRAN calling a C++ subroutine. So I am trying to compile and link this example.

Apparently the forum editor will not allow me to cut and paste the code.

Thanks again in advance. Sorry to waste your time if this is not in the SF frameworks.
Rod

Re: Translate FORTRAN to C++ for CUDA

grogley wrote:

Will SF compile a C++ subroutine (not the brightest bulb in the programing universe)?

Yes, Simply Fortran for Windows includes g++, and the development environment should handle it.  The only issue you will have is linking.  You'll need to add a flag to Linker under Compiler Flags in Project Options:

-lstdc++

That additional flag is necessary purely because Simply Fortran will use the Fortran compiler frontend for linking, and it won't automatically include the C++ standard library.

grogley wrote:

In that event I can't use the SF environment, I have downloaded the latest GNU files for Windows

This step was probably unnecessary, but that's okay.  We include the C and C++ compilers as well as most of the Windows-specific libraries.

grogley wrote:

How can I be sure it is using the non-SF path for compile and link?

You can execute the commands:

gfortran --version
gcc --version
g++ --version

The Simply Fortran compilers should include the text GCC for Simply Fortran.

grogley wrote:

Apparently the forum editor will not allow me to cut and paste the code.

That should work; I do it regularly.

Jeff Armstrong
Approximatrix, LLC