Different Tasks on Same Kernel Launch

Hello guys,

I have a short question. Hope to make myself understood. Is it possible to perform different algorithms on the same kernel launch. Here is a quick example:

// Kernel code

int algorithmId = someFunc(threadIdx.x, blockIdx.x)

switch (algorithmId)
{
case 1:
Do algorithm 1
break;
case 2:
Do algorithm 2
break;
default:
Do something;
break;
}

Mainly, each algorithm should use data stored inside the global device memory, according to the thread identifiers it resides in.

Thank you,
Valentin

Do you mean:

// Kernel code
device int somFunc(threadIdx.x, blockIdx.x)
{
//…
}

global void kernel(…)
{
//…
int algorithmId = someFunc(threadIdx.x, blockIdx.x)
switch (algorithmId)
{
case 1:
Do algorithm 1
break;
case 2:
Do algorithm 2
break;
default:
Do something;
break;
}
}

As far as I know, you can do it. But you need to take care with divergent branch.

Do you mean:

// Kernel code
device int somFunc(threadIdx.x, blockIdx.x)
{
//…
}

global void kernel(…)
{
//…
int algorithmId = someFunc(threadIdx.x, blockIdx.x)
switch (algorithmId)
{
case 1:
Do algorithm 1
break;
case 2:
Do algorithm 2
break;
default:
Do something;
break;
}
}

As far as I know, you can do it. But you need to take care with divergent branch.

Yeah, that’s actually a well known pattern in CUDA programming.

Yeah, that’s actually a well known pattern in CUDA programming.

Hello,

Thanks for the reply.

So I understand that each core has it’s separate instruction FIFO. Is that right ?

However I did not understand what “divergent branch” means.

Cheeers
Valentin

Hello,

Thanks for the reply.

So I understand that each core has it’s separate instruction FIFO. Is that right ?

However I did not understand what “divergent branch” means.

Cheeers
Valentin