changeset 1812:df5fc3a6d318 draft

Release mem object in GPU
author YuhiTOMARI
date Wed, 11 Dec 2013 20:41:24 +0900
parents 8039c48763c4
children d7973604e81f
files TaskManager/Gpu/GpuScheduler.cc TaskManager/Gpu/GpuScheduler.h
diffstat 2 files changed, 43 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/TaskManager/Gpu/GpuScheduler.cc	Wed Dec 11 20:00:54 2013 +0900
+++ b/TaskManager/Gpu/GpuScheduler.cc	Wed Dec 11 20:41:24 2013 +0900
@@ -63,34 +63,34 @@
 }
 
 void
-GpuScheduler::initGpuBuffer(GpuBuffer m) {
-    m.size = 0;
-    m.allocate_size = 64;
-    m.buf  = (cl_mem*)malloc(m.allocate_size*sizeof(cl_mem*));
-    m.event  = (cl_event*)malloc(m.allocate_size*sizeof(cl_event*));
+GpuScheduler::initGpuBuffer(GpuBufferPtr m) {
+    m->size = 0;
+    m->allocate_size = 64;
+    m->buf  = (cl_mem*)malloc(m->allocate_size*sizeof(cl_mem*));
+    m->event  = (cl_event*)malloc(m->allocate_size*sizeof(cl_event*));
 }
 
 void
-GpuScheduler::destroyGpuBuffer(GpuBuffer m) {
-    // TODO
+GpuScheduler::destroyGpuBuffer(GpuBufferPtr m) {
+    free(m->buf);
+    free(m->event);
+    m->size = 0;
+    m->allocate_size = 0;
+    m->buf = 0;
+    m->event = 0;
 }
 
 cl_mem
-GpuScheduler::createBuffer(GpuBuffer m, int i,  cl_context context, cl_mem_flags flags, size_t size, cl_int *error) {
-    if (i > m.allocate_size) {
+GpuScheduler::createBuffer(GpuBufferPtr m, int i,  cl_context context, cl_mem_flags flags, size_t size, cl_int *error) {
+    if (i > m->allocate_size) {
         // reallocate buffer size 
-        m.allocate_size *= 2;
-        m.buf = (cl_mem*)realloc(m.buf, m.allocate_size*sizeof(cl_mem*));
-        m.event = (cl_event*)realloc(m.event, m.allocate_size*sizeof(cl_event*));
+        m->allocate_size *= 2;
+        m->buf = (cl_mem*)realloc(m->buf, m->allocate_size*sizeof(cl_mem*));
+        m->event = (cl_event*)realloc(m->event, m->allocate_size*sizeof(cl_event*));
     }
 
-    if (m.buf[i]) {
-        clReleaseMemObject(m.buf[i]);
-    }
-
-    flags |= CL_MEM_USE_HOST_PTR;
-    void *buf = m.buf[i];
-    return clCreateBuffer(context, flags, size, buf, error);
+    m->buf[i] = clCreateBuffer(context, flags, size, 0, error);
+    return m->buf[i];
 }
 
 #define NOP_REPLY NULL
@@ -124,8 +124,20 @@
         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;
         }
     }
+
+    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;
+        }
+    }
+
     if(reply[1-cur]) {
         connector->mail_write(reply[1-cur]);
         reply[1-cur]=0;
@@ -166,6 +178,8 @@
     int cur = 0;
     TaskListPtr tasklist[2];
     tasklist[0]=NULL;tasklist[1]=NULL;
+    initGpuBuffer(&memin[0]);initGpuBuffer(&memin[1]);
+    initGpuBuffer(&memout[0]);initGpuBuffer(&memout[1]);
     memset(&flag, 0, sizeof(HTask::htask_flag));
 
     for (;;) {
@@ -178,10 +192,10 @@
             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]);
-            destroyGpuBuffer(memin[cur-1]);
+            destroyGpuBuffer(&memout[cur-1]);
+            destroyGpuBuffer(&memout[cur]);
+            destroyGpuBuffer(&memin[cur]);
+            destroyGpuBuffer(&memin[cur-1]);
             return ;
         }
 
@@ -228,7 +242,7 @@
                 int param = 0;
 
                 // set arg count
-                cl_mem memparam = createBuffer(memin[cur], 0, context, CL_MEM_READ_ONLY,
+                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);
@@ -258,7 +272,7 @@
                     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], i+1, context, mem_flag, input_buf->size, &ret);
                         if (ret<0) {
                             const char *msg=convert_error_status(ret);
                             error(msg);
@@ -290,7 +304,7 @@
                 for(int i = 0; i<nextTask->outData_count;i++) { // set output data
                     ListElement *output_buf = flag.flip? nextTask->inData(i) : nextTask->outData(i);
                     if (output_buf->size==0) break;
-                    createBuffer(memout[cur], i, context, out_mem_flag, output_buf->size, &ret);
+                    createBuffer(&memout[cur], i, context, out_mem_flag, output_buf->size, &ret);
                     if (ret<0) {
                         const char *msg=convert_error_status(ret);
                         error(msg);
--- a/TaskManager/Gpu/GpuScheduler.h	Wed Dec 11 20:00:54 2013 +0900
+++ b/TaskManager/Gpu/GpuScheduler.h	Wed Dec 11 20:41:24 2013 +0900
@@ -58,9 +58,9 @@
 private:
     FifoDmaManager *fifoDmaManager;
     int load_kernel(int cmd);
-    cl_mem createBuffer(GpuBuffer m, int i, cl_context context, cl_mem_flags flags, size_t size, cl_int *error);
-    void initGpuBuffer(GpuBuffer m);
-    void destroyGpuBuffer(GpuBuffer m);
+    cl_mem createBuffer(GpuBufferPtr m, int i, cl_context context, cl_mem_flags flags, size_t size, cl_int *error);
+    void initGpuBuffer(GpuBufferPtr m);
+    void destroyGpuBuffer(GpuBufferPtr m);
     void gpuTaskError(int cur, TaskListPtr *tasklist, int ret);
 };