Simple question for beginner.. Indexing

Guys,

When it is needed to refer the previous or next element of array in kernel function,

such as,

C code,

[codebox]float A[1000] = {…};

void func(float *a)

for(int i = 1; i < 999; i++)

{

float c = a[i-1]+a[i+1];

}[/codebox]

CUDA,

[codebox]

global

void _cudafunc(float *pa, int n)

{

const int idx = blockDim.x * blockIdx.x + threadIdx.x;

float prev, next;

if(idx - 1 < 0)

prev = &pa[n - 1];

else prev = &pa[idx - 1];

if(idx + 1 >= n)

next = &pa[0];

else next = &pa[idx + 1];

float c = prev + next;

}

[/codebox]

Is this conversion possible?

Its seems that the result depends on the numbers of block and threads…

Otherwise, how I can write the function?

You can convert this one-to-one to cuda:

[codebox]

global

void _cudafunc(float *a, int n)

{

const int i = blockDim.x * blockIdx.x + threadIdx.x;

if ((i>0) && (i<n-1)) {

float c = a[i-1]+a[i+1];

...

}

}

[/codebox]

However it seems that you want to implement a wrap-around in your CUDA version that is not in the plain C code?