changeset 1831:1b4d4b3c0546 draft

release bufs in gpuTaskError
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Wed, 18 Dec 2013 18:55:23 +0900
parents 812b14ffe5dc
children be9076dc8d49
files TaskManager/Gpu/GpuScheduler.cc
diffstat 1 files changed, 73 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/TaskManager/Gpu/GpuScheduler.cc	Tue Dec 17 17:21:14 2013 +0900
+++ b/TaskManager/Gpu/GpuScheduler.cc	Wed Dec 18 18:55:23 2013 +0900
@@ -96,6 +96,19 @@
 
 #define NOP_REPLY NULL
 
+static void
+release_buf_event(int cur,GpuBufferPtr memout) {
+    for (int i=0; i < memout[1-cur].size; i++) {
+        if (memout[cur-1].event[i] != 0)
+            clReleaseEvent(memout[1-cur].event[i]);
+        memout[1-cur].event[i] = 0;
+        if (memout[cur-1].buf[i] != 0)
+            clReleaseMemObject(memout[1-cur].buf[i]);
+        memout[1-cur].buf[i]   = 0;
+    }
+    memout[1-cur].size = 0;
+}
+
 /**
  * wait for previous pipeline termination
  * kernel_event, memout_event
@@ -109,9 +122,19 @@
         }
     } else if (kernel_event[1-cur] != NULL) {
         int ret=clWaitForEvents(1,&kernel_event[1-cur]);
+
         if (ret<0) {
             error(convert_error_status(ret));
         }
+        if (taskList!=NULL){
+            cl_ulong start = 0;
+            cl_ulong end   = 0;
+            clGetEventProfilingInfo(kernel_event[1-cur],CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
+            clGetEventProfilingInfo(kernel_event[1-cur],CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
+            if (taskList->task_start_time == 0)
+                taskList->task_start_time = start;
+            taskList->task_end_time   = end;
+        }
         clReleaseEvent(kernel_event[1-cur]);
         kernel_event[1-cur] = 0;
     }
@@ -121,35 +144,13 @@
         if (ret<0) {
             error(convert_error_status(ret));
         }
-
-        for (int i=0; i < memout[1-cur].size; i++) {
-            clReleaseEvent(memout[1-cur].event[i]);
-            memout[1-cur].event[i] = 0;
-            clReleaseMemObject(memout[1-cur].buf[i]);
-            memout[1-cur].buf[i]   = 0;
-        }
-        memout[1-cur].size = 0;
+        release_buf_event(cur,memout);
     }
 
     if (memin[1-cur].size > 0) {
-        for (int i=0; i < memin[1-cur].size; i++) {
-            clReleaseEvent(memin[1-cur].event[i]);
-            memin[1-cur].event[i] = 0;
-            clReleaseMemObject(memin[1-cur].buf[i]);
-            memin[1-cur].buf[i]   = 0;
-        }
-        memin[1-cur].size = 0;
+        release_buf_event(cur,memin);
     }
 
-    if (taskList!=NULL){
-        cl_ulong start = 0;
-        cl_ulong end   = 0;
-        clGetEventProfilingInfo(kernel_event[1-cur],CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
-        clGetEventProfilingInfo(kernel_event[1-cur],CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
-        if (taskList->task_start_time == 0)
-            taskList->task_start_time = start;
-        taskList->task_end_time   = end;
-    }
 
 }
 
@@ -157,10 +158,14 @@
 GpuScheduler::gpuTaskError(int cur, TaskListPtr tasklist, int ret)
 {
     error(convert_error_status(ret));
+    if (kernel_event[cur] != 0)
+        clReleaseEvent(kernel_event[cur]);
     kernel_event[cur] = NOP_REPLY;
+    if (kernel[cur] != 0)
+        clReleaseKernel(kernel[cur]);
     kernel[cur] = 0;
-    memout[cur].buf = 0;
-    memin[cur].buf = 0;
+    release_buf_event(1-cur,memout);
+    release_buf_event(1-cur,memin);
     reply = (memaddr)tasklist->waiter;
 
     // wait kernel[1-cur] and write[1-cur]
@@ -187,11 +192,8 @@
         // read task list mail from DmaManager
 
         if ((memaddr)params_addr == (memaddr)MY_SPE_COMMAND_EXIT) {
+            // wait_for_envet was called, so all kernel,buf,event have been released.
             clFinish(command_queue);
-            if (kernel[0]) clReleaseKernel(kernel[0]);
-            if (kernel[1]) clReleaseKernel(kernel[1]);
-            if (kernel_event[0] && kernel_event[0]!=NOP_REPLY) clReleaseEvent(kernel_event[0]);
-            if (kernel_event[1] && kernel_event[1]!=NOP_REPLY) clReleaseEvent(kernel_event[1]);
             destroyGpuBuffer(&memout[cur-1]);
             destroyGpuBuffer(&memout[cur]);
             destroyGpuBuffer(&memin[cur]);
@@ -219,15 +221,18 @@
                 if(nextTask->command==ShowTime) {
                     connector->show_profile();
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
                 if(nextTask->command==StartProfile) {
                     connector->start_profile();
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
                 if (load_kernel(nextTask->command) == 0) {
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
                 cl_program& program = *gpu_task_list[nextTask->command].gputask->program;
@@ -238,6 +243,7 @@
                 kernel[cur] = clCreateKernel(program, function, &ret);
                 if (ret<0) {
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
 
@@ -247,49 +253,55 @@
                 cl_mem memparam = createBuffer(&memin[cur], 0, context, CL_MEM_READ_ONLY,
                                                sizeof(memaddr)*nextTask->param_count, &ret);
                 if (ret<0) {
-                    const char *msg=convert_error_status(ret);
-                    error(msg);
+                    gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
+                    continue;
                 }
+                // parameter is passed as first kernel arg 
+                param=0;
 
                 ret = clEnqueueWriteBuffer(command_queue, memparam, CL_TRUE, 0,sizeof(memaddr)*nextTask->param_count,
                                            nextTask->param(param), 0, NULL, &memin[cur].event[0]);
-                // parameter is passed as first kernel arg 
-                param=0;
                 if (ret<0) {
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
 
                 ret = clSetKernelArg(kernel[cur], param, sizeof(memaddr),(void *)&memin[cur].buf[0]);
                 if (ret<0) {
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
 
                 param++;
 
                 cl_mem_flags mem_flag = CL_MEM_READ_ONLY;
-
+                
                 if (!flag.flip) { // set input data when not flip
                     for(int i=0;i<nextTask->inData_count;i++) {
                         ListElement *input_buf = nextTask->inData(i);
                         if (input_buf->size==0) break;
-                        createBuffer(&memin[cur], i+1, context, mem_flag, input_buf->size, &ret);
+                        createBuffer(&memin[cur], param, context, mem_flag, input_buf->size, &ret);
                         if (ret<0) {
-                            const char *msg=convert_error_status(ret);
-                            error(msg);
+                            gpuTaskError(cur,tasklist,ret);
+                            nextTask = nextTask->next();
+                            continue;
                         }
-                        ret = clEnqueueWriteBuffer(command_queue, memin[cur].buf[i+1], CL_TRUE, 0,
+                        ret = clEnqueueWriteBuffer(command_queue, memin[cur].buf[param], CL_TRUE, 0,
                                                    input_buf->size, input_buf->addr, 0, 
-                                                   NULL, &memin[cur].event[i+1]);
+                                                   NULL, &memin[cur].event[param]);
                         if (ret<0) {
-                            const char *msg=convert_error_status(ret);
-                            error(msg);
+                            gpuTaskError(cur,tasklist,ret);
+                            nextTask = nextTask->next();
+                            continue;
                         }
-                        ret = clSetKernelArg(kernel[cur],  param, sizeof(memaddr), (void *)&memin[cur].buf[i+1]);
+                        ret = clSetKernelArg(kernel[cur],  param, sizeof(memaddr), (void *)&memin[cur].buf[param]);
                         if (ret<0) {
-                            const char *msg=convert_error_status(ret);
-                            error(msg);
+                            gpuTaskError(cur,tasklist,ret);
+                            nextTask = nextTask->next();
+                            continue;
                         }
                         
                         param++;
@@ -308,25 +320,28 @@
                     if (output_buf->size==0) break;
                     createBuffer(&memout[cur], i, context, out_mem_flag, output_buf->size, &ret);
                     if (ret<0) {
-                        const char *msg=convert_error_status(ret);
-                        error(msg);
+                        gpuTaskError(cur,tasklist,ret);
+                        nextTask = nextTask->next();
+                        continue;
                     }
 
                     if (flag.flip) { // use output buffer as input buffer
                         ListElement *input_buf = nextTask->inData(i);
                         
-                        ret = clEnqueueWriteBuffer(command_queue, memout[cur].buf[i+1], CL_TRUE, 0,
+                        ret = clEnqueueWriteBuffer(command_queue, memout[cur].buf[param], CL_TRUE, 0,
                                                    input_buf->size, input_buf->addr, 
-                                                   0, NULL, &memout[cur].event[i+1]);
+                                                   0, NULL, &memout[cur].event[param]);
                         if (ret<0) {
-                            const char *msg=convert_error_status(ret);
-                            error(msg);
+                            gpuTaskError(cur,tasklist,ret);
+                            nextTask = nextTask->next();
+                            continue;
                         }
                     }
                     ret = clSetKernelArg(kernel[cur],  param, sizeof(memaddr), (void *)&memout[cur].buf[i]);
                     if (ret<0) {
-                        const char *msg=convert_error_status(ret);
-                        error(msg);
+                        gpuTaskError(cur,tasklist,ret);
+                        nextTask = nextTask->next();
+                        continue;
                     }
                     param++;
                 }
@@ -342,6 +357,8 @@
                 }
                 if (ret<0) {
                     gpuTaskError(cur, tasklist, ret);
+                    nextTask = nextTask->next();
+                    continue;
                 }
 
                 for(int i=0;i<nextTask->outData_count;i++) { // read output data
@@ -352,18 +369,18 @@
 
                     if (ret<0) {
                         gpuTaskError(cur,tasklist,ret);
+                        nextTask = nextTask->next();
                         continue;
                     }
                 }
-                tasklist->task_end_time = gettime();
                 
                 if (ret<0) {
                     gpuTaskError(cur,tasklist,ret);
+                    nextTask = nextTask->next();
                     continue;
                 }
+                printf("GPU %d %s\t%lld\n",tasklist->self->cpu_type,(char*)(gpu_task_list[tasklist->tasks[0].command].name),tasklist->task_end_time-tasklist->task_start_time);
                 
-                printf("GPU %d %s\t%lld\n",tasklist->self->cpu_type,(char*)(gpu_task_list[tasklist->tasks[0].command].name),tasklist->task_end_time-tasklist->task_start_time);
-
                 // wait kernel[1-cur] and write[1-cur]
                 wait_for_event(kernel_event, memout, 0, tasklist, cur);
                 // pipeline    : 1-cur