Is it possible for the host to read/write a buffer written/read by the GPU kernel while the kernel is running?

Discussion created by maelteir on Aug 29, 2011
Latest reply on Sep 2, 2011 by MicahVillmow

Hi all,

I am trying to write a code that needs collaboration between the host and device. Specifically:

1- The device sets a variable X to a non-zero value and waits for Y to be non-zero.

2- The host reads X, when it becomes non-zero, it sets Y to non-zero value.

Below is the code running on the GPU and CPU.

This code hangs, so I assume that reading and writing by the host to variables manipulated by the GPU kernel is not possible while the kernel is running. Is that true?


Thank you.

-------------------- GPU kernel code -------------------- int tid = get_global_id(0); Y[tid]=0; X[tid]=tid; while (1) { if ( Y[tid] > 0 ) break; } ----------- CPU code ----------- for (int i=0; i< numThreads; i++) Y[i]=0; count=0; while (1) { clEnqueueReadBuffer(...,X,....) for (int i=0; i< numThreads; i++) { if ( Y[i] == 0) { if (X[i] > 0) { Y[i]=1; count++; } } if (count > 0 ) clEnqueueWriteBuffer(...,Y,....) if (count == numThreads) break; //All threads are done }