tags:

views:

175

answers:

5

Does CUDA support recursion?

+1  A: 

Any recursive algorithm can be implemented with a stack and a loop. It's way more of a pain, but if you really need recursion, this can work.

dicroce
+2  A: 

Yes, see the NVIDIA CUDA Programming Guide:

device functions only support recursion in device code compiled for devices of compute capability 2.0.

You need a Fermi card to use them.

Matias Valdenegro
+3  A: 

It does on NVIDIA hardware supporting compute capability 2.0 and CUDA 3.1:

New language features added to CUDA C / C++ include:

Support for function pointers and recursion make it easier to port many existing algorithms to Fermi GPUs

http://developer.nvidia.com/object/cuda_3_1_downloads.html

Function pointers: http://developer.download.nvidia.com/compute/cuda/sdk/website/CUDA_Advanced_Topics.html#FunctionPointers

Recursion: I can't find a code sample on NVIDIA's website, but on the forum someone post this:

__device__ int fact(int f)
{
  if (f == 0)
    return 1;
  else
    return f * fact(f - 1);
}
Stringer Bell
Having "recent" hardware is not enough. Not all recent cards are Fermi (aka compute capability 2.0). At present, there are no Fermi mobile GPUs.
Mark Borgerding
You're right, I updated my anwser. But what about Geforce GTX 480M? There's a Fermi chip inside.
Stringer Bell
There are laptops with Fermi; Geforce 480M and Quadro FX5000M have been out for a little while.
Tom
Indeed. And new GeForce GT 415M, 420M, 425M, 435M, 445M and GTX 460M, 470M are coming! All have compute capability 2.0.
Stringer Bell
+1  A: 

CUDA 3.1 supports recursion

Jan C
you have a example?
JuanPablo