[Beignet] [PATCH OCL2.0 2/2] Utest: Add test for get_global/local_linear_id
Pan Xiuli
xiuli.pan at intel.com
Wed Dec 2 00:57:39 PST 2015
Add two utest case test for OCL2.0 new work-item built-in
functions.
Signed-off-by: Pan Xiuli <xiuli.pan at intel.com>
---
kernels/builtin_global_linear_id.cl | 4 ++
kernels/builtin_local_linear_id.cl | 6 +++
utests/CMakeLists.txt | 4 +-
utests/builtin_global_linear_id.cpp | 89 +++++++++++++++++++++++++++++++++++++
utests/builtin_local_linear_id.cpp | 81 +++++++++++++++++++++++++++++++++
5 files changed, 183 insertions(+), 1 deletion(-)
create mode 100644 kernels/builtin_global_linear_id.cl
create mode 100644 kernels/builtin_local_linear_id.cl
create mode 100644 utests/builtin_global_linear_id.cpp
create mode 100644 utests/builtin_local_linear_id.cpp
diff --git a/kernels/builtin_global_linear_id.cl b/kernels/builtin_global_linear_id.cl
new file mode 100644
index 0000000..6810ffd
--- /dev/null
+++ b/kernels/builtin_global_linear_id.cl
@@ -0,0 +1,4 @@
+kernel void builtin_global_linear_id( __global int *ret) {
+ int id = get_global_linear_id();
+ ret[id] = id;
+}
diff --git a/kernels/builtin_local_linear_id.cl b/kernels/builtin_local_linear_id.cl
new file mode 100644
index 0000000..0ddcc73
--- /dev/null
+++ b/kernels/builtin_local_linear_id.cl
@@ -0,0 +1,6 @@
+kernel void builtin_local_linear_id( __global int *ret) {
+ int id = get_local_linear_id() + (get_group_id(0) + \
+ get_group_id(1) * 2 + get_group_id(2) * 6) * \
+ get_local_size(0) * get_local_size(1) * get_local_size(2);
+ ret[id] = id;
+}
diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt
index 6c945d2..95e0276 100644
--- a/utests/CMakeLists.txt
+++ b/utests/CMakeLists.txt
@@ -219,7 +219,9 @@ set (utests_sources
runtime_use_host_ptr_image.cpp
compiler_get_sub_group_size.cpp
compiler_get_sub_group_id.cpp
- compiler_sub_group_shuffle.cpp)
+ compiler_sub_group_shuffle.cpp
+ builtin_global_linear_id.cpp
+ builtin_local_linear_id.cpp)
if (LLVM_VERSION_NODOT VERSION_GREATER 34)
SET(utests_sources
diff --git a/utests/builtin_global_linear_id.cpp b/utests/builtin_global_linear_id.cpp
new file mode 100644
index 0000000..457092f
--- /dev/null
+++ b/utests/builtin_global_linear_id.cpp
@@ -0,0 +1,89 @@
+/*
+According to the OpenCL v2.0 chapter 6.13.1
+Now define global size as following:
+ globals[0] = 3;
+ globals[1] = 4;
+ globals[2] = 5;
+ offsets[0] = 1;
+ offsets[1] = 2;
+ offsets[2] = 3;
+
+Kernel:
+id = get_global_linear_id(0)
+
+dimension:1
+ 0 1 2
+dimension:2
+ 0 1 2
+ 3 4 5
+ 6 7 8
+ 9 10 11
+dimension:3
+ 0 1 2 12 13 14 24 25 26 36 37 38 48 49 50
+ 3 4 5 15 16 17 27 28 29 39 40 41 51 52 53
+ 6 7 8 18 19 20 30 31 32 42 43 44 54 55 56
+ 9 10 11 21 22 23 33 34 35 45 46 47 57 58 59
+*/
+
+#define udebug 0
+#include "utest_helper.hpp"
+static void builtin_global_linear_id(void)
+{
+
+ // Setup kernel and buffers
+ int dim, global_id[80], err, i, buf_len=1;
+ size_t offsets[3] = {0,0,0};
+ OCL_CREATE_KERNEL("builtin_global_linear_id");
+
+ OCL_CREATE_BUFFER(buf[0], CL_MEM_READ_WRITE, sizeof(int)*80, NULL);
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+
+ for( dim=1; dim <= 3; dim++ )
+ {
+ buf_len = 1;
+ for(i=1; i <= dim; i++)
+ {
+ globals[i - 1] = 2 + i;
+ locals[i - 1] = 2 + i;
+ offsets[i - 1] = i;
+ buf_len *= 2 + i;
+ }
+ for(i=dim+1; i <= 3; i++)
+ {
+ globals[i - 1] = 0;
+ locals[i - 1] = 0;
+ offsets[i - 1] = 0;
+ }
+
+ // Run the kernel
+ err = clEnqueueNDRangeKernel(queue, kernel, dim, offsets, globals, locals, 0, NULL, NULL);
+ if (err != CL_SUCCESS)
+ {
+ printf("Error: Failed to excute kernel! %d\n", err);
+ exit(1);
+ }
+
+ clFinish(queue);
+
+ err = clEnqueueReadBuffer( queue, buf[0], CL_TRUE, 0, sizeof(int) * buf_len, &global_id, 0, NULL, NULL);
+
+ if (err != CL_SUCCESS)
+ {
+ printf("Error: Failed to read output array! %d\n", err);
+ exit(1);
+ }
+
+#if udebug
+ for(i = 0; i < buf_len; i++)
+ {
+ printf("%2d ", global_id[i]);
+ if ((i + 1) % 3 == 0) printf("\n");
+ }
+#endif
+
+ for( i = 0; i < buf_len; i++)
+ OCL_ASSERT( global_id[i] == i);
+ }
+}
+
+MAKE_UTEST_FROM_FUNCTION(builtin_global_linear_id);
diff --git a/utests/builtin_local_linear_id.cpp b/utests/builtin_local_linear_id.cpp
new file mode 100644
index 0000000..c2df7be
--- /dev/null
+++ b/utests/builtin_local_linear_id.cpp
@@ -0,0 +1,81 @@
+/*
+According to the OpenCL v2.0 chapter 6.13.1
+Now define local and global size as following:
+ globals[0] = 4;
+ globals[1] = 9;
+ globals[2] = 16;
+ locals[0] = 2;
+ locals[1] = 3;
+ locals[2] = 4;
+
+Kernel:
+ int id = get_local_linear_id() + (get_group_id(0) + \
+ get_group_id(1) * 2 + get_group_id(2) * 2 * 3) * \
+ get_local_size(0) * get_local_size(1) * get_local_size(2);
+
+dimension:1
+ 0 1 2 3
+dimension:2
+ 0 1 2 3 4 5 6 7 8 9 10 11
+12 13 14 15 16 17 18 19 20 21 22 23
+24 25 26 27 28 29 30 31 32 33 34 35
+dimension:3
+ 0 1 2 3 4 5 6 7 ... 139 140 141 142 143
+...
+...
+429 430 431 432 433 434 ... 571 572 573 574 575
+*/
+
+#define udebug 0
+#include "utest_helper.hpp"
+static void builtin_local_linear_id(void)
+{
+
+ // Setup kernel and buffers
+ int dim, local_id[576], err, i, buf_len=1;
+ OCL_CREATE_KERNEL("builtin_local_linear_id");
+
+ OCL_CREATE_BUFFER(buf[0], CL_MEM_READ_WRITE, sizeof(int)*576, NULL);
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+
+ for( dim=1; dim <= 3; dim++ )
+ {
+ buf_len = 1;
+ for(i=1; i <= dim; i++)
+ {
+ locals[i - 1] = i + 1;
+ globals[i - 1] = (i + 1) * (i + 1);
+ buf_len *= ((i + 1) * (i + 1));
+ }
+ for(i = dim+1; i <= 3; i++)
+ {
+ globals[i - 1] = 0;
+ locals[i - 1] = 0;
+ }
+
+ // Run the kernel
+ OCL_NDRANGE( dim );
+ clFinish(queue);
+
+ err = clEnqueueReadBuffer( queue, buf[0], CL_TRUE, 0, sizeof(int) * buf_len, &local_id, 0, NULL, NULL);
+
+ if (err != CL_SUCCESS)
+ {
+ printf("Error: Failed to read output array! %d\n", err);
+ exit(1);
+ }
+
+#if udebug
+ for(i = 0; i < buf_len; i++)
+ {
+ printf("%2d ", local_id[i]);
+ if ((i + 1) % 4 == 0) printf("\n");
+ }
+#endif
+
+ for( i = 0; i < buf_len; i++)
+ OCL_ASSERT( local_id[i] == i);
+ }
+}
+
+MAKE_UTEST_FROM_FUNCTION(builtin_local_linear_id);
--
2.1.4
More information about the Beignet
mailing list