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?