Coding Cuda in fortran specific coding example

hey guys,

so i’ve read a lot about using cuda with fortran and I’ve read through the nVidia manuals and everything but i’m learning on my own and am having trouble getting the hang of it. What i want to do is simply run some FORTRAN code and call a (eventually multiple) functions in it, but using cuda. So a simple example:

we’ll call it main.f90

PROGRAM runme
IMPLICIT NONE
INTEGER a,b
a=12
b=10
CALL addition(a,b)

END PROGRAM runme

SUBROUTINE addition(a,b)
INTEGER a,b,c
c = a+b
PRINT *, ‘FORTRAN computation:’
PRINT *, c

END SUBROUTINE addition

So what would be the easiest way to execute the main section (the addition) on the GPU? giving code examples would be helpful if you could, since i’m trying to learn the cuda code.
thanks so much.

also if there are errors in this little code segment please point them out since Fortran is new to me :) adios

It is actually very easy: just look for information on how to call C from Fortran on your system. This is OS dependent, but in general you only need to use capital letters for the function being called on Windows, and append underscore on Unix.

Note that in Fortran all parameters are passed by reference as well, this is something to remember.

At the moment you can’t write CUDA kernel code in Fortran, only C/C++. You can wrap a CUDA kernel call and supporting C/C++ code in a C subroutine with a Fortran compatible argument list and then call that from Fortran, but that is about all you can do at the moment.

You can also combine host CPU Fortran code with CUDA using Python as “glue” via the numpy F2py facility and pycuda, although F2py still has a number of limitations at the moment (it doesn’t support F90 user types and allocatable arrays, for example).

I saw a thread just a few days ago that a company is working on writing a FORTRAN compiler for CUDA (to actually write your kernels in FORTRAN, not just link to the C version). That doesn’t solve your problems now, but you might want to keep an ear out for it if you’d prefer doing things in pure FORTRAN…

That was PGI, and according to their press release, they are going to be rolling CUDA support into their standard FORTRAN compiler release in November. The FORTRAN-only version of their compiler costs $700 for non-academic users, so factor that into your plans.