safe deleting cpu data

I have some code like this:

buffer = clCreateBuffer( ... );
float* temp = new float[ size ];
clEnqueueWriteBuffer( ..., temp, ... );

clEnqueueBarrier( cq );

kernel = clCreateKernel( prog, "mykernel", ... );
clSetKernelArg( kernel, ..., &buffer );
clEnqueueNDRangeKernel( kernel, .... );

delete[] temp;  <--------- safe?

My question is: Without using events and clWaitForEvents(), where can I do delete my cpu buffer being sure that the cpu->gpu copy (clEnqueuWriteBuffer) has finished?

In my code I delete the cpu buffer after the execution of a kernel that must wait for the previous enqueued command (that copies “buffer” from cpu to gpu). Both clEnqueueWriteBuffer and clEnqueueNDRangeKernel are asynchronous, so my doubt is ¿does clEnqueueNDRangeKernel stall until clEnqueueWriteBuffer has finished?

You have to use events, or some other mechanism which synchronises the calls (e.g. job completion).

All the ‘enqueue’ commands - not surprisingly - only enqueue requests, they do not wait for anything. The only guarantee is that the copy will be finished before the kernel is executed: but neither has any synchronisation relation to the host code (enqueue calls).

I tend to allocate all buffers necessary for a task and keep them around until i’m well and truly done. Or use events to arbitrate.

You could use event callbacks or a cleanup thread (which synchronisly waits on the events) if you want explicit control.