How to make different threads execute different parts in CUDA?

I am working on CUDA and I have a problem related to thread synchronization. In my code I need threads to execute different parts of the code, like:

one thread -> all thread -> one thread ->

This is what I want. In the initial part of code only one thread will execute and then some part will be executed by all threads then again single thread. Also the threads are executing in a loop. Can anyone tell me how to do that? It's kinda urgent. I'll be grateful for any help.

Thanks

-------------Problems Reply------------

You can only synchronize threads within a single blocks. It is possible to synchronize between multiple blocks, but only under very specific circumstances. If you need global synchronization between all threads, the way to do that is to launch a new kernel.

Within a block, you can synchronize threads using __syncthreads(). For example:

__global__ void F(float *A, int N)
{
int idx = threadIdx.x + blockIdx.x * blockDim.x;

if (threadIdx.x == 0) // thread 0 of each block does this:
{
// Whatever
}
__syncthreads();

if (idx < N) // prevent buffer overruns
{
A[idx] = A[idx] * A[idx]; // "real work"
}

__syncthreads();

if (threadIdx.x == 0) // thread 0 of each block does this:
{
// Whatever
}
}

You need to use the thread ID to control what is executed, e.g.

if (thread_ID == 0)
{
// do single thread stuff
}

// do common stuff on all threads

if (thread_ID == 0)
{
// do single thread stuff
}

If your program contains multiple blocks, you need to use custom synchronization mechanism across blocks. If your kernel launches only one block, then __syncthreads() will work.

Category:multithreading Views:0 Time:2010-05-06

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.234 (s). 11 q(s)