[Beignet] [PATCH 3/3] Add the test case for sub buffer check

junyan.he at inbox.com junyan.he at inbox.com
Mon Nov 4 21:51:23 PST 2013


From: Junyan He <junyan.he at linux.intel.com>

Signed-off-by: Junyan He <junyan.he at linux.intel.com>
---
 utests/CMakeLists.txt |    1 +
 utests/sub_buffer.cpp |  135 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+)
 create mode 100644 utests/sub_buffer.cpp

diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt
index d25621f..a63f323 100644
--- a/utests/CMakeLists.txt
+++ b/utests/CMakeLists.txt
@@ -132,6 +132,7 @@ set (utests_sources
   builtin_pow.cpp
   builtin_exp.cpp
   builtin_convert_sat.cpp
+  sub_buffer.cpp
   runtime_createcontext.cpp
   runtime_null_kernel_arg.cpp
   runtime_event.cpp
diff --git a/utests/sub_buffer.cpp b/utests/sub_buffer.cpp
new file mode 100644
index 0000000..f65e8ff
--- /dev/null
+++ b/utests/sub_buffer.cpp
@@ -0,0 +1,135 @@
+#include "utest_helper.hpp"
+
+void sub_bufffer_check(void)
+{
+    cl_int error;
+    cl_ulong max_alloc_size;
+    cl_uint address_align;
+    cl_mem main_buf;
+    cl_mem sub_buf;
+    char *main_buf_content;
+    char sub_buf_content[32];
+
+    error = clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(max_alloc_size), &max_alloc_size, NULL);
+    OCL_ASSERT(error == CL_SUCCESS);
+    error = clGetDeviceInfo(device, CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(address_align ), &address_align, NULL );
+    OCL_ASSERT(error == CL_SUCCESS);
+
+    main_buf_content = (char *)malloc(sizeof(char) * max_alloc_size);
+
+    for (cl_ulong i = 0; i < max_alloc_size; i++) {
+        main_buf_content[i] = rand() & 63;
+    }
+
+    main_buf = clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, max_alloc_size, main_buf_content, &error);
+    OCL_ASSERT(error == CL_SUCCESS);
+
+    /* Test read sub buffer. */
+    for (cl_ulong sz = 64; sz < max_alloc_size; sz*=4) {
+        for (cl_ulong off = 0; off < max_alloc_size; off += 1234) {
+            cl_buffer_region region;
+            region.origin = off;
+            region.size = sz;
+
+            sub_buf = clCreateSubBuffer(main_buf, 0, CL_BUFFER_CREATE_TYPE_REGION, &region, &error );
+
+            /* invalid size, should be failed. */
+            if(off + sz > max_alloc_size) {
+                OCL_ASSERT(error != CL_SUCCESS);
+                continue;
+            }
+            /* invalid align, should be failed. */
+            if(off & (address_align-1)) {
+                OCL_ASSERT(error != CL_SUCCESS);
+                continue;
+            }
+
+            OCL_ASSERT(error == CL_SUCCESS);
+
+            error = clEnqueueReadBuffer(queue, sub_buf, CL_TRUE, 0, 32, (void *)sub_buf_content, 0, NULL, NULL);
+            OCL_ASSERT(error == CL_SUCCESS);
+
+#if 0
+            printf("\nRead ########### Src buffer: \n");
+            for (int i = 0; i < 32; ++i)
+                printf(" %2.2u", main_buf_content[off + i]);
+
+            printf("\nRead ########### dst buffer: \n");
+            for (int i = 0; i < 32; ++i)
+                printf(" %2.2u", sub_buf_content[i]);
+            printf("\n");
+#endif
+            for (int i = 0; i < 32; ++i) {
+
+                if (main_buf_content[off + i] != sub_buf_content[i]) {
+                    printf ("different index is %d\n", i);
+                    OCL_ASSERT(0);
+                }
+            }
+
+        }
+    }
+
+
+    for (cl_ulong sz = 64; sz < max_alloc_size; sz*=4) {
+        for (cl_ulong off = 0; off < max_alloc_size; off += 1234) {
+            cl_buffer_region region;
+            region.origin = off;
+            region.size = sz;
+
+            sub_buf = clCreateSubBuffer(main_buf, 0, CL_BUFFER_CREATE_TYPE_REGION, &region, &error );
+
+            /* invalid size, should be failed. */
+            if(off + sz > max_alloc_size) {
+                OCL_ASSERT(error != CL_SUCCESS);
+                continue;
+            }
+            /* invalid align, should be failed. */
+            if(off & (address_align-1)) {
+                OCL_ASSERT(error != CL_SUCCESS);
+                continue;
+            }
+
+            OCL_ASSERT(error == CL_SUCCESS);
+
+            for (int i = 0; i < 32; i++) {
+                sub_buf_content[i] = rand() & 63;
+            }
+
+            error = clEnqueueWriteBuffer(queue, main_buf, CL_TRUE, off, 32, sub_buf_content, 0, NULL, NULL);
+            OCL_ASSERT(error == CL_SUCCESS);
+
+            void * mapped_ptr = clEnqueueMapBuffer(queue, sub_buf, CL_TRUE, (cl_map_flags)( CL_MAP_READ | CL_MAP_WRITE ),
+                    0, 32, 0, NULL, NULL, &error );
+            OCL_ASSERT(error == CL_SUCCESS);
+
+#if 0
+            printf("\nMap ########### Src buffer: \n");
+            for (int i = 0; i < 32; ++i)
+                printf(" %2.2u", sub_buf_content[i]);
+
+            printf("\nMap ########### dst buffer: \n");
+            for (int i = 0; i < 32; ++i)
+                printf(" %2.2u", ((char *)mapped_ptr)[i]);
+            printf("\n");
+#endif
+            for (int i = 0; i < 32; i++) {
+
+                if (((char *)mapped_ptr)[i] != sub_buf_content[i]) {
+                    printf ("different index is %d\n", i);
+                    OCL_ASSERT(0);
+                }
+            }
+
+            error = clEnqueueUnmapMemObject(queue, sub_buf, mapped_ptr, 0, NULL, NULL );
+            OCL_ASSERT(error == CL_SUCCESS);
+
+            clReleaseMemObject(sub_buf);
+        }
+    }
+
+
+    free(main_buf_content);
+}
+
+MAKE_UTEST_FROM_FUNCTION(sub_bufffer_check);
-- 
1.7.9.5



More information about the Beignet mailing list