Does CUDA support recursion?
Contents. A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input.
To my knowledge, all modern imperative programming languages support recursion in the sense that a procedure can call itself.
Yes, recursion can be used in production code with some defensive coding practices.
TLDR; My Arduino Nano (ATmega328 mcu) can do 211 recursive function calls (for the code given below) before it has a stack overflow and crashes.
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); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With