@@ -35,7 +35,7 @@ static void clDumpBuffer(const char * fileNameFormat, cl_command_queue opencl_cm
3535 char fileName[1024 ]; sprintf (fileName, fileNameFormat, dumpBufferCount);
3636 cl_mem opencl_buffer = data->opencl_buffer ;
3737 cl_uint opencl_buffer_offset = data->opencl_buffer_offset ;
38- cl_uint size = (cl_uint)data->size ;
38+ cl_uint size = (cl_uint)( data->u . img . stride_in_bytes *data-> u . img . height ) ;
3939 FILE * fp = fopen (fileName, " wb" ); if (!fp) { printf (" ERROR: unable to create: %s\n " , fileName); exit (1 ); }
4040 clFinish (opencl_cmdq);
4141 void * p = clEnqueueMapBuffer (opencl_cmdq, opencl_buffer, CL_TRUE, CL_MAP_READ, 0 , opencl_buffer_offset + size, 0 , NULL , NULL , NULL );
@@ -52,6 +52,26 @@ static void clDumpBuffer(const char * fileNameFormat, cl_command_queue opencl_cm
5252#endif
5353
5454#if ENABLE_OPENCL
55+ static cl_mem agoGpuOclCreateBuffer (AgoContext * context, cl_mem_flags flags, size_t size, void * host_ptr, cl_int * errcode_ret)
56+ {
57+ cl_mem mem = clCreateBuffer (context->opencl_context , flags, size, host_ptr, errcode_ret);
58+ if (mem) {
59+ context->opencl_mem_alloc_count ++;
60+ context->opencl_mem_alloc_size += size;
61+ }
62+ return mem;
63+ }
64+
65+ static cl_mem agoGpuOclCreateImage (AgoContext * context, cl_mem_flags flags, const cl_image_format * image_format, const cl_image_desc * image_desc, void * host_ptr, cl_int * errcode_ret)
66+ {
67+ cl_mem mem = clCreateImage (context->opencl_context , flags, image_format, image_desc, host_ptr, errcode_ret);
68+ if (mem) {
69+ context->opencl_mem_alloc_count ++;
70+ context->opencl_mem_alloc_size += image_desc->image_width ; // TBD: currently assumes 8-bit 1D image
71+ }
72+ return mem;
73+ }
74+
5575int agoGpuOclReleaseContext (AgoContext * context)
5676{
5777 if (context->opencl_cmdq ) {
@@ -114,6 +134,7 @@ int agoGpuOclReleaseData(AgoData * data)
114134 if (data->opencl_buffer_allocated ) {
115135 clReleaseMemObject (data->opencl_buffer_allocated );
116136 data->opencl_buffer_allocated = NULL ;
137+ data->ref .context ->opencl_mem_release_count ++;
117138 }
118139#if defined(CL_VERSION_2_0)
119140 if (data->opencl_svm_buffer_allocated ) {
@@ -342,17 +363,17 @@ int agoGpuOclAllocBuffer(AgoData * data)
342363 dataMaster->buffer = dataMaster->opencl_svm_buffer_allocated + dataMaster->opencl_buffer_offset ;
343364 if (context->opencl_config_flags & CONFIG_OPENCL_SVM_AS_CLMEM) {
344365 // use svm buffer as opencl_buffer(GPU)
345- dataMaster->opencl_buffer = dataMaster->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, dataMaster->size + dataMaster->opencl_buffer_offset , dataMaster->opencl_svm_buffer_allocated , &err);
366+ dataMaster->opencl_buffer = dataMaster->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, dataMaster->size + dataMaster->opencl_buffer_offset , dataMaster->opencl_svm_buffer_allocated , &err);
346367 }
347368 }
348369 else
349370#endif
350371 {
351372 // allocate normal opencl_buffer
352- dataMaster->opencl_buffer = dataMaster->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE, dataMaster->size + dataMaster->opencl_buffer_offset , NULL , &err);
373+ dataMaster->opencl_buffer = dataMaster->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE, dataMaster->size + dataMaster->opencl_buffer_offset , NULL , &err);
353374 }
354375 if (err) {
355- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d\n " , context->opencl_context , (int )dataMaster->size + dataMaster->opencl_buffer_offset , err);
376+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d\n " , context->opencl_context , (int )dataMaster->size + dataMaster->opencl_buffer_offset , err);
356377 return -1 ;
357378 }
358379 if (dataMaster->u .img .isUniform ) {
@@ -412,14 +433,14 @@ int agoGpuOclAllocBuffer(AgoData * data)
412433 data->buffer = data->opencl_svm_buffer_allocated + data->opencl_buffer_offset ;
413434 if (context->opencl_config_flags & CONFIG_OPENCL_SVM_AS_CLMEM) {
414435 // use svm buffer as opencl_buffer(GPU)
415- data->opencl_buffer = data->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, data->size + data->opencl_buffer_offset , data->opencl_svm_buffer_allocated , &err);
436+ data->opencl_buffer = data->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, data->size + data->opencl_buffer_offset , data->opencl_svm_buffer_allocated , &err);
416437 }
417438 }
418439 else
419440#endif
420441 {
421442 // normal opencl_buffer allocation
422- data->opencl_buffer = data->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE, data->size + data->opencl_buffer_offset , NULL , &err);
443+ data->opencl_buffer = data->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE, data->size + data->opencl_buffer_offset , NULL , &err);
423444 if (data->opencl_buffer ) {
424445 // initialize array header which containts numitems
425446 vx_uint32 zero = 0 ;
@@ -429,7 +450,7 @@ int agoGpuOclAllocBuffer(AgoData * data)
429450 }
430451 }
431452 if (err) {
432- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (array/cannystack)\n " , context->opencl_context , (int )data->size , err);
453+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (array/cannystack)\n " , context->opencl_context , (int )data->size , err);
433454 return -1 ;
434455 }
435456 }
@@ -444,19 +465,19 @@ int agoGpuOclAllocBuffer(AgoData * data)
444465 cl_int err = -1 ;
445466 cl_image_format format = { CL_INTENSITY, CL_UNORM_INT8 };
446467 cl_image_desc desc = { CL_MEM_OBJECT_IMAGE1D, 256 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , NULL };
447- data->opencl_buffer = data->opencl_buffer_allocated = clCreateImage (context-> opencl_context , CL_MEM_READ_WRITE, &format, &desc, NULL , &err);
468+ data->opencl_buffer = data->opencl_buffer_allocated = agoGpuOclCreateImage (context, CL_MEM_READ_WRITE, &format, &desc, NULL , &err);
448469 if (err) {
449- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateImage (%p,CL_MEM_READ_WRITE,1D/U8,256,0,*) => %d (for LUT)\n " , context->opencl_context , err);
470+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateImage (%p,CL_MEM_READ_WRITE,1D/U8,256,0,*) => %d (for LUT)\n " , context->opencl_context , err);
450471 return -1 ;
451472 }
452473 data->opencl_buffer_offset = 0 ;
453474 }
454475 else {
455476 // normal opencl_buffer allocation
456477 cl_int err = -1 ;
457- data->opencl_buffer = data->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE, data->size + data->opencl_buffer_offset , NULL , &err);
478+ data->opencl_buffer = data->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE, data->size + data->opencl_buffer_offset , NULL , &err);
458479 if (err) {
459- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateBuffer (%p,CL_MEM_READ_WRITE,%d,*) => %d (for LUT)\n " , context->opencl_context , (int )(data->size + data->opencl_buffer_offset ), err);
480+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateBuffer (%p,CL_MEM_READ_WRITE,%d,*) => %d (for LUT)\n " , context->opencl_context , (int )(data->size + data->opencl_buffer_offset ), err);
460481 return -1 ;
461482 }
462483 }
@@ -465,9 +486,9 @@ int agoGpuOclAllocBuffer(AgoData * data)
465486 else if (data->ref .type == VX_TYPE_REMAP) {
466487 if (!data->opencl_buffer ) {
467488 cl_int err = -1 ;
468- data->opencl_buffer = data->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE, data->size , NULL , &err);
489+ data->opencl_buffer = data->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE, data->size , NULL , &err);
469490 if (err) {
470- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (for Remap)\n " , context->opencl_context , (int )data->size , err);
491+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (for Remap)\n " , context->opencl_context , (int )data->size , err);
471492 return -1 ;
472493 }
473494 data->opencl_buffer_offset = 0 ;
@@ -476,9 +497,9 @@ int agoGpuOclAllocBuffer(AgoData * data)
476497 else if (data->ref .type == VX_TYPE_MATRIX) {
477498 if (!data->opencl_buffer ) {
478499 cl_int err = -1 ;
479- data->opencl_buffer = data->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE, data->size , NULL , &err);
500+ data->opencl_buffer = data->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE, data->size , NULL , &err);
480501 if (err) {
481- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (for Matrix)\n " , context->opencl_context , (int )data->size , err);
502+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (for Matrix)\n " , context->opencl_context , (int )data->size , err);
482503 return -1 ;
483504 }
484505 data->opencl_buffer_offset = 0 ;
@@ -488,9 +509,9 @@ int agoGpuOclAllocBuffer(AgoData * data)
488509 AgoData * dataMaster = data->u .tensor .roiMaster ? data->u .tensor .roiMaster : data; // to handle tensor ROI
489510 if (!dataMaster->opencl_buffer ) {
490511 cl_int err = -1 ;
491- dataMaster->opencl_buffer = dataMaster->opencl_buffer_allocated = clCreateBuffer (context-> opencl_context , CL_MEM_READ_WRITE, dataMaster->size , NULL , &err);
512+ dataMaster->opencl_buffer = dataMaster->opencl_buffer_allocated = agoGpuOclCreateBuffer (context, CL_MEM_READ_WRITE, dataMaster->size , NULL , &err);
492513 if (err) {
493- agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: clCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (for Tensor)\n " , context->opencl_context , (int )dataMaster->size , err);
514+ agoAddLogEntry (&context->ref , VX_FAILURE, " ERROR: agoGpuOclCreateBuffer (%p,CL_MEM_READ_WRITE,%d,0,*) => %d (for Tensor)\n " , context->opencl_context , (int )dataMaster->size , err);
494515 return -1 ;
495516 }
496517 dataMaster->opencl_buffer_offset = 0 ;
0 commit comments