 # An example ' each thread access one or more elements '

Hello , I am a little confused right now and I wanted if someone can give me a code example where each thread processing one element and each thread processing more elements.

For example ,

``````int index = threadIdx + blockDimx * blockIdx;
result[index] = a[index] + b[index];
``````

``````while (index < NbElements) {

result[index] = a[index] + b[index]

index += gridDim.x * blockDim.x;
}
``````

here?What is going on?

Also, when we use the one or the other option ? ( each thread one element or more)?
When we are out of resources?
Can you give me an example on all these?

Thank you very much!

In the second case each thread computes NbElements/(gridDim.x * blockDim.x) elements. The loop will go through elements which are gridDim.x * blockDim.x apart from each other.

Lets say that we have 100 elements and 10 threads.

I can’t understand this:

result = a + b
index = 10

So, in the next loop :

result = a + b
index = 20

result = a + b
index = 100

How each thread computes 10 elements??And also ,we have 10 threads.The 90?

Thanks

The variable index is local to each thread. If you have 10 threads. Than the gridDim.x * blockDim.x=10 and you have:

First iteration
thread 0 result = a + b; index=10
thread 1 result = a + b; index=11
thread 2 result = a + b; index=12
thread 3 result = a + b
thread 4 result = a + b
thread 5 result = a + b
thread 6 result = a + b
thread 7 result = a + b
thread 8 result = a + b
thread 9 result = a + b; index =19
Second interation:
thread 0 result = a + b; index=10
thread 1 result = a + b; index=11
thread 2 result = a + b; index =12
thread 3 result = a + b
thread 4 result = a + b
thread 5 result = a + b
thread 6 result = a + b
thread 7 result = a + b
thread 8 result = a + b
thread 9 result = a + b; index =19

Late edit, fixed!

1. I think in the second loop the indices must be :

index = 20
index = 21
index = 22

index = 29

Also, we have 10 threads but I can see thread for example…I am confused.

1. Finally , I can’t see how each thread computes 10 elements.I can see each thread computes one element!

Perhaps you could modify this array-based example to help understand how the threads end up doing work:

The CUDA by Example book has an example that uses that approach in the context of calculating dot products in Chapter 5.

Edit: here are some examples of that modality: