Should this give an error, and if yes, how to check it? (OpenCL)

Viewed 281

I am learning to make robust code in OpenCL and facing the following kernel code:

string kernel_code =
" void kernel simple_add(global const int *A,    "
"                        global const int *B,    "
"                        global int *C, int n) { "
"                                                "
"   int index = get_global_id(0);                "
"   C[index]=A[index]+B[index];                  "
" }                                              ";

And using on purpose the following code for sending it to GPU:

Kernel ker(program, "simple_add");
ker.setArg(0, buffer_A);
ker.setArg(1, buffer_B);
ker.setArg(2, buffer_C);
ker.setArg(3, N);
q.enqueueNDRangeKernel(ker,NullRange,NDRange(32),NDRange(32));
q.finish();

The thing is, I am using more work items than is needed, so I think I should check if the index is out of bounds on the kernel code. But I am not using it and checking errors returned by enqueueNDRangeKernel or by finish is giving me CL_SUCCESS.. So, or this should not give me errors for some reason or I do not know how to get them.. What is the answer?

1 Answers

As in C and C++, going out of bounds in an array is undefined behaviour in OpenCL, so it won't necessarily raise an error condition, but can cause pretty much any kind of strange behaviour. So don't rely on your runtime to catch this sort of programming mistake. It's more likely that your program will crash, or even your whole system will crash (if it doesn't use an IOMMU) as a result of this sort of error, rather than an error code in the return value.

As an implementation detail, you will most likely find that buffers are mapped to the GPU's memory space in 4096-byte increments - one page of memory. So if your out-of-bounds access is still within a valid 4096-byte page, it'll more likely behave like a typical CPU-based buffer overflow.

Related