# Fortran and cuFFT

**URL:** <https://forums.developer.nvidia.com/t/fortran-and-cufft/11552>\
**Category:** CUDA Programming and Performance\
**Created:** [August 13, 2009, 2:26am UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552 "2009-08-13T02:26:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [August 13, 2009, 2:26am UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/1 "2009-08-13T02:26:44Z")

</div>

What is the best way to call the cuFFT functions from an existing fortran program which uses the fftw3 library calls.

The last problem I am having is that the fortran compiler is case-insensitive for the generated function names. I can get rid of the underscore with a compiler option but all functions are lower-case only so they are not similar to the cuFFT library names. There is a fortran wrapper for the cublas library but not for the cuFFT library.  
Is there a fortran wrapper for the cuFFT library planed?

OOps!  
Forget about that, I was not thinking…  
nvcc is still needed

---

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [August 14, 2009, 2:53pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/2 "2009-08-14T14:53:56Z")

</div>

Ok. I did progress a bit on this problem.

However, I have an error message which is not in the latest cuFFT user guide. Maybe it is somewhere else.

In calling cufftPlan3D:

cufft: Failed to find applicable transform

Can someone show me what this error means?

---

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [August 14, 2009, 4:44pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/3 "2009-08-14T16:44:40Z")

</div>

And why does this piece compile properly?

From SDK2.3

from simpleCUFFT:

// CUFFT plan

```
cufftHandle plan;

cufftSafeCall(cufftPlan1d(&plan, new_size, CUFFT_C2C, 1));

```

this is too many arguments if I try in my code.

---

<div class="post-metadata">

**Author:** ![mfatica](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mfatica](https://forums.developer.nvidia.com/u/mfatica)\
**Post date:** [August 14, 2009, 4:55pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/4 "2009-08-14T16:55:56Z")

</div>

cufftPlan1d(&plan, new\_size, CUFFT\_C2C, 1)

That is the correct calling sequence for 1D transform.

---

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [August 14, 2009, 5:21pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/5 "2009-08-14T17:21:01Z")

</div>

> [@](#):
>
> cufftPlan1d(&plan, new\_size, CUFFT\_C2C, 1)
> 
> That is the correct calling sequence for 1D transform.

Silly me. I did not notice the 1d and I am working with 3d.

How about my previous question on the error message?

---

<div class="post-metadata">

**Author:** ![mfatica](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mfatica](https://forums.developer.nvidia.com/u/mfatica)\
**Post date:** [August 14, 2009, 5:28pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/6 "2009-08-14T17:28:59Z")

</div>

Are you taking in account that Fortran is passing arguments by reference and C by value?  
Take a look at the Fortran example that calls CUDA code in this presentation ([http://gpgpu.org/wp/wp-content/uploads/2009/06/03-Toolkit.pdf](http://gpgpu.org/wp/wp-content/uploads/2009/06/03-Toolkit.pdf) )

---

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [August 14, 2009, 6:03pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/7 "2009-08-14T18:03:07Z")

</div>

> [@](#):
>
> Are you taking in account that Fortran is passing arguments by reference and C by value?
> 
> Take a look at the Fortran example that calls CUDA code in this presentation ([http://gpgpu.org/wp/wp-content/uploads/2009/06/03-Toolkit.pdf](http://gpgpu.org/wp/wp-content/uploads/2009/06/03-Toolkit.pdf) )

The arguments passing should be OK. The presentation is very short on the F-\>CUDA. I would have to change the subroutine too much to follow this approach and I don’t know how much ripple effect it would have on the others subs. I think I already modify as much as I could at this stage.

---

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [August 19, 2009, 2:03pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/8 "2009-08-19T14:03:59Z")

</div>

Back to Fortran to Cuda calls

Which is the correct way?

1. Fortran  
real\*8 ::r(0:2,0:3,0:2)  
…call fun ( i,j,k , r)  
Cuda  
void fun\_ ( int \*i , int \*j, int \*k, cufftReal \*r);  
In this case, the array r is 1d and I have to use cufftPlan1d

Is the elements order correct? Will the fft be OK?

1. Fortran  
real\*8 ::r(0:2,0:2,0:2)  
…call fun ( i,j,k , r)  
Cuda

Do I have to modify the elements order to agree with fortran? will the fft be OK?

On the return form the fft, do I have to modify the array order before going back the the calling fortran program?

---

<div class="post-metadata">

**Author:** ![jam11](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jam11](https://forums.developer.nvidia.com/u/jam11)\
**Post date:** [September 19, 2009, 3:20pm UTC](https://forums.developer.nvidia.com/t/fortran-and-cufft/11552/9 "2009-09-19T15:20:04Z")

</div>

And the solution is :

1. do the host memory allocation on the fortran side using this:

[codebox]module cuda\_alloc

interface

! cudaMallocHost

integer (C\_INT) function cudaMallocHost(buffer, size) bind(C,name=“cudaMallocHost”)

use iso\_c\_binding

implicit none

type (C\_PTR) :: buffer

integer (C\_LONG), value :: size

end function cudaMallocHost

! cudaFreeHost

integer (C\_INT) function cudaFreeHost(buffer) bind(C,name=“cudaFreeHost”)

use iso\_c\_binding

implicit none

type (C\_PTR), value :: buffer

end function cudaFreeHost

end interface

end module cuda\_alloc

[/codebox]

1. call the cuFFT program using this subroutine :

[codebox]

subroutine r2c\_3d( Nx, Ny, Nz, r, c)

use iso\_c\_binding

use cuda\_alloc

implicit none

integer Nx, Ny, Nz,res

real\*8 :: r(0:Nx-1, 0:Ny-1, 0:Nz-1)

complex\*16 :: c(0:Nx/2, 0:Ny-1, 0:Nz-1)

complex\*16 , parameter :: size1=dcmplx(0.0,0.0)

real\*8 , parameter :: size2=dble(0.0)

type(C\_PTR) :: cptr\_cd, cptr\_rd

complex\*16 , dimension(:,:,:), pointer :: cd

real\*8 , dimension(:,:,:), pointer :: rd

res = cudaMallocHost ( cptr\_cd,Nx_Ny_Nz\*sizeof(size1)/2+1 )

call c\_f\_pointer ( cptr\_cd,cd , [Nx/2+1,Ny,Nz] )

res = cudaMallocHost ( cptr\_rd, Nx_Ny_Nz\*sizeof(size2) )

call c\_f\_pointer ( cptr\_rd,rd , [Nx,Ny,Nz] )

rd = r

call fft\_3d\_r2c( Nx, Ny, Nz, rd, cd)

c= cd

res = cudaFreeHost(cptr\_rd)

res = cudaFreeHost(cptr\_cd)

end subroutine r2c\_3d

subroutine c2r\_3d(Nx, Ny, Nz, c, r)

use iso\_c\_binding

use cuda\_alloc

implicit none

integer Nx, Ny, Nz,res

real\*8 :: r(0:Nx-1, 0:Ny-1, 0:Nz-1)

complex\*16 :: c(0:Nx/2, 0:Ny-1, 0:Nz-1)

complex\*16 , parameter :: size1=dcmplx(0.0,0.0)

real\*8 , parameter :: size2=dble(0.0)

type(C\_PTR) :: cptr\_cd, cptr\_rd

complex\*16 , dimension(:,:,:), pointer :: cd

real\*8 , dimension(:,:,:), pointer :: rd

res = cudaMallocHost ( cptr\_cd,Nx_Ny_Nz\*sizeof(size1) )

call c\_f\_pointer ( cptr\_cd,cd , [Nx/2+1,Ny,Nz] )

res = cudaMallocHost ( cptr\_rd, Nx_Ny_Nz\*sizeof(size2) )

call c\_f\_pointer ( cptr\_rd,rd , [Nx,Ny,Nz] )

cd = c

call fft\_3d\_c2r( Nx, Ny, Nz, cd ,rd)

r = rd

res = cudaFreeHost(cptr\_rd)

res = cudaFreeHost(cptr\_cd)

end subroutine c2r\_3d

[/codebox]

3 ) and finally this is the .cu code

[codebox]

#include \<stdio.h\>

#include \<stdlib.h\>

#include “cufft.h”

#include “cuda.h”

#include “cutil\_inline.h”

extern “C” void fft\_3d\_r2c\_( int \*nx ,int \*ny, int \*nz, cufftDoubleReal \*h\_a, cufftDoubleComplex \*h\_b)

{

unsigned long size1 = sizeof(cufftDoubleReal)\* (_nx)_(_ny)_(\*nz); // calulate memory size

```
    unsigned long size2 = sizeof(cufftDoubleComplex)* (*nx/2+1)*(*ny)*(*nz); // calulate memory size

```

cufftDoubleReal \*d\_ic=NULL; // define pointer to device mem input

```
    cufftDoubleComplex *d_oc=NULL; // define pointer to device mem output

    cufftHandle plan;

```

cutilSafeCall( cudaMalloc((void \*\*)&d\_ic, size1)); // allocate mem on device

cutilSafeCall( cudaMalloc((void \*\*)&d\_oc, size2)); // allocate mem on device

cutilSafeCall( cudaMemcpy(d\_ic,h\_a, size1 , cudaMemcpyHostToDevice)); //copy host array to device

cufftSafeCall( cufftPlan3d(&plan, (\*nz),(\*ny),(\*nx) ,CUFFT\_D2Z )); // perform 3d fft complex to complex

cufftSafeCall( cufftExecD2Z(plan, d\_ic, d\_oc )); // out of place

cutilSafeCall( cudaMemcpy(h\_b,d\_oc, size2 , cudaMemcpyDeviceToHost)); // copy back to host array

cudaFree(d\_ic);

```
    cudaFree(d_oc);

    cufftDestroy(plan);

```

}

extern “C” void fft\_3d\_c2r\_( int\* nx ,int \*ny, int \*nz, cufftDoubleComplex \*h\_a, cufftDoubleReal \*h\_b)

{

```
    unsigned long size1 = sizeof(cufftDoubleComplex)* (*nx)*(*ny)*(*nz); // calulate memory size

    unsigned long size2 = sizeof(cufftDoubleReal)* (*nx)*(*ny)*(*nz); // calulate memory size

    cufftDoubleComplex *d_ic=NULL; // define pointer to device mem input

    cufftDoubleReal *d_oc=NULL; // define pointer to device mem output

    cufftHandle plan;

```

cutilSafeCall( cudaMalloc((void \*\*)&d\_ic, size1)); // allocate mem on device

cutilSafeCall( cudaMalloc((void \*\*)&d\_oc, size2)); // allocate mem on device

cutilSafeCall( cudaMemcpy(d\_ic,h\_a, size1 , cudaMemcpyHostToDevice)); //copy host array to device

cufftSafeCall( cufftPlan3d(&plan, \*nz,\*ny,\*nx ,CUFFT\_Z2D )); // perform 3d fft complex to complex

cufftSafeCall( cufftExecZ2D(plan, d\_ic, d\_oc )); // out of place

cutilSafeCall( cudaMemcpy(h\_b,d\_oc, size2 , cudaMemcpyDeviceToHost)); // copy back to host array

cudaFree(d\_ic);

```
    cudaFree(d_oc);

    cufftDestroy(plan);

```

}

[/codebox]

Note that only the latest gnufortran ( after aug , 2009) would work properly.

It is critical to swap the indices nx and nz in cufftPlan3d.
