Hi all,
How to correctly manipulate pointers in kernel functions?
I have a link structure, which has a int value and a pointer point to another link structure.
I use the kernel to setup a linked list. ( link[0] —> link[1] —> link[2] )
Also, I set a value to of each link[i]. (e.g. Link[0].value = 0;)
Finally, I output the value of each node of this linked list.
However, the result is different between emuDebug/emuRelease and Debug/Release mode.
The result in Debug/Release is incorrect…
Which step is wrong?
Thanks in advance!!!
typedef struct link_struct {
link_struct* next;
int value;
} Link;
// [file] link_test.cpp
extern "C" void run_kernel(Link* link);
void main()
{
Link* link_h;
Link* link_d;
const int n = 3;
// alloc host memory
cudaHostAlloc((void**) &link_h, n*sizeof(Link), cudaHostAllocDefault);
// alloc device memory
cudaMalloc((void**) &link_d, n*sizeof(Link));
// run kernel
run_kernel(link_d);
// copy mem from device to host
cudaMemcpy(&(link_h[0]), link_d, n*sizeof(Link), cudaMemcpyDeviceToHost);
Link* cur = &link_h[0];
do {
printf("value: %d\n", cur->value);
cur = cur->next;
} while(cur != NULL);
}
// [file] link_kernel.cu
__global__ void link_kernel(Link* link)
{
link[0].value = 0;
link[1].value = 1;
link[2].value = 2;
link[0].next = &link[1];
link[1].next = &link[2];
link[2].next = NULL;
}
extern "C" void run_kernel(Link* link)
{
// execute the kernel
link_kernel<<< 1, 1>>>(link);
}