Hi, I’m new to OpenCL and I’m trying to process an array with my GPU.
So, I wrote a simple program to do some tests.
First, I defined an array in the host program as
int *a = new int[81];
I just gave arbitrary values to each array elements like a[i]=i;
Then, I created buffer for this array and passed it to the kernel as an argument
[i]cl_mem memIn=clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, sizeof(a), a, &status);
status=clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memIn);[/i]
And here is my kernel program (I have set global size to 1)
[i]_kernel void process(__global int* a, __global int* b){ //b is not used
unsigned int id = get_global_id(0);
int c[81];
for(int i=0; i<81; i++){
c[i]=*(a+i);
printf("%d ", c); //just to figure out whether the array is successfully passed in
}
return;
}
The result is that all elements of c[] are 0 (not the same with the a[])
However, if I define the array in the host program as
int a[81]; //not using “new”
It all worked. Since I may need to build a really big array in the real program, I think I’ll have to use the first method to avoid the stack overflow problem.
So, if someone can tell me where I’m doing wrong, or is it impossible to pass an array defined in that way to a kernel program?
Thanks!