C function in CUDA


I’m brand new to CUDA C.

I am wondering if it is possible to run separate functions in different gpu threads (for example, first would do Matrix Multiplication, second would transpose matrix, third would add up 2 arrays,etc…)

Just simple if else statements won’t work for me, so this:

[codebox]global void MagicFunct (/some args here/)


if (threadIdx.x == 1){ /* do matrix mult*/}

else if (threadIdx.x == 2){ /* do transpose*/ }

else if (threadIdx.x == 3){ /do addition/}


is not what I desire. Is there any way different threads could run different kernel functions? I did some googling but didn’t find an answer.

Second question is: can I call functions from a kernel function?

kinda like this way

[codebox]global void AnotherFunct (/args here/)


//some data

MagicFunct(/* pass args */);



thank you for your help,


No and no.

When a kernel launches, all the threads have to run the same kernel. Now, you can have your kernel start with a huge if statement, so that different threads perform different tasks. However, that should be done at the warp level. One of the tricks a GPU uses to cram more execution units onto a chip is to only have one program counter per warp. Individual threads can take different branches, but it gets inefficient very quickly.

As for calling other kernels… can’t be done. However, global functions can call device functions.