From 5364cf1093c53f04f22f650bedbca1a5b27fe423 Mon Sep 17 00:00:00 2001 From: Junyan He Date: Tue, 12 Apr 2016 15:37:50 +0800 Subject: add finish --- backend/src/driver/cl_gen_command_queue.cpp | 79 +++++++++++++++++++++++++++-- backend/src/driver/cl_gen_driver.c | 2 + backend/src/driver/cl_gen_driver.h | 2 + backend/src/driver/cl_gen_driver.hpp | 2 + 4 files changed, 82 insertions(+), 3 deletions(-) (limited to 'backend/src/driver') diff --git a/backend/src/driver/cl_gen_command_queue.cpp b/backend/src/driver/cl_gen_command_queue.cpp index 5ee7730e..1616487e 100644 --- a/backend/src/driver/cl_gen_command_queue.cpp +++ b/backend/src/driver/cl_gen_command_queue.cpp @@ -178,15 +178,63 @@ void GenGPUCommandQueue::waitOnEvent(GenGPUWorkItem* item) } while (this->quit == false) { - if ((item && item->state == CL_COMPLETE) || - (!item && (this->workItems.empty() || this->inExec == true))) { + if (item && item->state != CL_COMPLETE) { pthread_cond_wait(&this->cond, &this->mutex); + } else { + break; } } pthread_mutex_unlock(&this->mutex); } +void GenGPUCommandQueue::waitForFlush(void) +{ + pthread_mutex_lock(&this->mutex); + if (this->quit) { // already destroy the queue? + pthread_mutex_unlock(&this->mutex); + return; + } + + bool needWait = false; + while (this->quit == false) { + needWait = false; + for (list::iterator it = this->workItems.begin(); + it != this->workItems.end(); it++) { + if ((*it)->state > CL_SUBMITTED) { + needWait = true; + break; + } + } + + if (needWait) { + pthread_cond_wait(&this->cond, &this->mutex); + } else { + break; + } + } + + pthread_mutex_unlock(&this->mutex); +} + +void GenGPUCommandQueue::waitForFinish(void) +{ + pthread_mutex_lock(&this->mutex); + if (this->quit) { // already destroy the queue? + pthread_mutex_unlock(&this->mutex); + return; + } + + while (this->quit == false) { + if (this->workItems.empty() && this->inExec == false) + break; + + pthread_cond_wait(&this->cond, &this->mutex); + } + + pthread_mutex_unlock(&this->mutex); +} + void GenGPUCommandQueue::userEventChange(void) { pthread_mutex_lock(&this->mutex); @@ -263,10 +311,35 @@ cl_int GenReleaseCommandQueue(cl_command_queue queue) { GenGPUCommandQueue* gpuQueue = (GenGPUCommandQueue*)getGenCommandQueuePrivate(queue); if (gpuQueue == NULL) { - return CL_INVALID_VALUE; + return CL_INVALID_COMMAND_QUEUE; } GBE_DELETE(gpuQueue); setGenCommandQueuePrivate(queue, NULL); return CL_SUCCESS; } + +extern "C" +cl_int GenFlushCommandQueue(cl_command_queue queue) +{ + GenGPUCommandQueue* gpuQueue = (GenGPUCommandQueue*)getGenCommandQueuePrivate(queue); + if (gpuQueue == NULL) { + return CL_INVALID_COMMAND_QUEUE; + } + + gpuQueue->waitForFlush(); + return CL_SUCCESS; +} + +extern "C" +cl_int GenFinishCommandQueue(cl_command_queue queue) +{ + GenGPUCommandQueue* gpuQueue = (GenGPUCommandQueue*)getGenCommandQueuePrivate(queue); + if (gpuQueue == NULL) { + return CL_INVALID_COMMAND_QUEUE; + } + + gpuQueue->waitForFinish(); + return CL_SUCCESS; +} + diff --git a/backend/src/driver/cl_gen_driver.c b/backend/src/driver/cl_gen_driver.c index 00cb8070..c8f962f7 100644 --- a/backend/src/driver/cl_gen_driver.c +++ b/backend/src/driver/cl_gen_driver.c @@ -25,6 +25,8 @@ _cl_driver clgenDriver = { .release_context = GenReleaseContext, .create_command_queue = GenCreateCommandQueue, .release_command_queue = GenReleaseCommandQueue, + .flush_command_queue = GenFlushCommandQueue, + .finish_command_queue = GenFinishCommandQueue, .build_program = GenBuildProgram, .get_program_kernel_names = GenGetProgramKernelNames, .release_program = GenReleaseProgram, diff --git a/backend/src/driver/cl_gen_driver.h b/backend/src/driver/cl_gen_driver.h index 7d66103b..ba572a42 100644 --- a/backend/src/driver/cl_gen_driver.h +++ b/backend/src/driver/cl_gen_driver.h @@ -34,6 +34,8 @@ cl_int GenCreateContext(cl_context context, const cl_device_id device); cl_int GenReleaseContext(cl_context context, const cl_device_id device); cl_int GenCreateCommandQueue(cl_command_queue queue); cl_int GenReleaseCommandQueue(cl_command_queue queue); +cl_int GenFlushCommandQueue(cl_command_queue queue); +cl_int GenFinishCommandQueue(cl_command_queue queue); cl_int GenBuildProgram(cl_program program, const cl_device_id device); cl_int GenGetProgramKernelNames(cl_program program, const cl_device_id device, char *names, cl_uint name_sz, cl_uint* ret_sz, cl_uint* ker_num); diff --git a/backend/src/driver/cl_gen_driver.hpp b/backend/src/driver/cl_gen_driver.hpp index a671f38b..ff58c7d3 100644 --- a/backend/src/driver/cl_gen_driver.hpp +++ b/backend/src/driver/cl_gen_driver.hpp @@ -256,6 +256,8 @@ struct GenGPUCommandQueue { bool enqueueWorkItem(GenGPUWorkItem* item); void userEventChange(void); void waitOnEvent(GenGPUWorkItem* item); + void waitForFlush(void); + void waitForFinish(void); GenGPUCommandQueue(dri_bufmgr *bufmgr, drm_intel_context *ctx); ~GenGPUCommandQueue(void); }; -- cgit v1.2.3