[Beignet] [PATCH] utests: add utest to convert_double2int8

rander rander.wang at intel.com
Thu Mar 16 01:47:24 UTC 2017


Signed-off-by: rander <rander.wang at intel.com>
---
 kernels/builtin_convert_double2int8.cl |  73 ++++++++++++++++++
 utests/CMakeLists.txt                  |   3 +-
 utests/builtin_convert_double2int8.cpp | 133 +++++++++++++++++++++++++++++++++
 3 files changed, 208 insertions(+), 1 deletion(-)
 create mode 100644 kernels/builtin_convert_double2int8.cl
 create mode 100644 utests/builtin_convert_double2int8.cpp

diff --git a/kernels/builtin_convert_double2int8.cl b/kernels/builtin_convert_double2int8.cl
new file mode 100644
index 0000000..7454fb6
--- /dev/null
+++ b/kernels/builtin_convert_double2int8.cl
@@ -0,0 +1,73 @@
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+__kernel void builtin_convert_double2int8(__global double *X,
+												__global char *Z,
+												__global uchar *uZ,
+												int max_input)
+{
+	int i = get_global_id(0);
+	int j;
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_sat(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_rtz(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_rtn(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_rte(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_rtp(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_sat_rtz(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_sat_rtn(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_sat_rte(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		Z[i++] = convert_char_sat_rtp(X[j]);
+
+	i = 0;
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_sat(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_rtz(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_rtn(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_rte(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_rtp(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_sat_rtz(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_sat_rtn(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_sat_rte(X[j]);
+
+	for(j = 0; j < max_input; j++)
+		uZ[i++] = convert_uchar_sat_rtp(X[j]);
+
+}
+
diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt
index a4ab0ab..f12643f 100644
--- a/utests/CMakeLists.txt
+++ b/utests/CMakeLists.txt
@@ -299,7 +299,8 @@ set (utests_sources
   compiler_pipe_builtin.cpp
   compiler_device_enqueue.cpp
   builtin_relation_fp64.cpp
-  builtin_commonFunc_fp64.cpp)
+  builtin_commonFunc_fp64.cpp
+  builtin_convert_double2int8.cpp)
 
 if (LLVM_VERSION_NODOT VERSION_GREATER 34)
   SET(utests_sources
diff --git a/utests/builtin_convert_double2int8.cpp b/utests/builtin_convert_double2int8.cpp
new file mode 100644
index 0000000..44ca8f2
--- /dev/null
+++ b/utests/builtin_convert_double2int8.cpp
@@ -0,0 +1,133 @@
+#include "utest_helper.hpp"
+#include <cmath>
+#include <algorithm>
+
+namespace{
+    double doubleX[] = {
+        0x1.0000000001p0,
+        -0x1.0000000001p2,
+        0x1.1ffp4,
+        -0x1.1fffp4,
+        0x1.0p2,
+        -0x1.0p7,
+        0x1.fffp5,
+        -0x1.ffffp5,
+        0x1.ffffp7,
+        -0x1.ffffp6,
+        0x1.00001p2,
+        -0x1.00001p7,
+        0x1.00000001p3,
+        -0x1.00000001p3,
+        0x1.0p-32,
+        -0x1.0p-32,
+    };
+
+
+const char*  testFunc[] =
+{
+    " char convert_char(double x)",
+    " char convert_char_sat(double x)",
+    " char convert_char_rtz(double x)",
+    " char convert_char_rtn(double x)",
+    " char convert_char_rte(double x)",
+    " char convert_char_rtp(double x)",
+    " char convert_char_sat_rtz(double x)",
+    " char convert_char_sat_rtn(double x)",
+    " char convert_char_sat_rte(double x)",
+    " char convert_char_sat_rtp(double x)",
+
+    " uchar convert_uchar(double x)",
+    " uchar convert_uchar_sat(double x)",
+    " uchar convert_uchar_rtz(double x)",
+    " uchar convert_uchar_rtn(double x)",
+    " uchar convert_uchar_rte(double x)",
+    " uchar convert_uchar_rtp(double x)",
+    " uchar convert_uchar_sat_rtz(double x)",
+    " uchar convert_uchar_sat_rtn(double x)",
+    " uchar convert_uchar_sat_rte(double x)",
+    " uchar convert_uchar_sat_rtp(double x)",
+};
+
+char expectResultChar[] = {
+    1, -4, 17, -17, 4, -128, 63, -63, -1, -127, 4, -128, 8, -8, 0, 0,
+    1, -4, 17, -17, 4, -128, 63, -63, 127, -127, 4, -128, 8, -8, 0, 0,
+    1, -4, 17, -17, 4, -128, 63, -63, -1, -127, 4, -128, 8, -8, 0, 0,
+    1, -5, 17, -18, 4, -128, 63, -64, -1, -128, 4, 127, 8, -9, 0, -1,
+    1, -4, 18, -18, 4, -128, 64, -64, 0, -128, 4, -128, 8, -8, 0, 0,
+    2, -4, 18, -17, 4, -128, 64, -63, 0, -127, 5, -128, 9, -8, 1, 0,
+    1, -4, 17, -17, 4, -128, 63, -63, 127, -127, 4, -128, 8, -8, 0, 0,
+    1, -5, 17, -18, 4, -128, 63, -64, 127, -128, 4, -128, 8, -9, 0, -1,
+    1, -4, 18, -18, 4, -128, 64, -64, 127, -128, 4, -128, 8, -8, 0, 0,
+    2, -4, 18, -17, 4, -128, 64, -63, 127, -127, 5, -128, 9, -8, 1, 0,
+};
+
+unsigned char expectResultUChar[] = {
+    1, 0, 17, 0, 4, 0, 63, 0, 255, 0, 4, 0, 8, 0, 0, 0,
+    1, 0, 17, 0, 4, 0, 63, 0, 255, 0, 4, 0, 8, 0, 0, 0,
+    1, 0, 17, 0, 4, 0, 63, 0, 255, 0, 4, 0, 8, 0, 0, 0,
+    1, 4, 17, 17, 4, 128, 63, 63, 255, 127, 4, 128, 8, 8, 0, 0,
+    1, 252, 18, 238, 4, 128, 64, 192, 0, 128, 4, 128, 8, 248, 0, 0,
+    2, 0, 18, 0, 4, 0, 64, 0, 0, 0, 5, 0, 9, 0, 1, 0,
+    1, 0, 17, 0, 4, 0, 63, 0, 255, 0, 4, 0, 8, 0, 0, 0,
+    1, 0, 17, 0, 4, 0, 63, 0, 255, 0, 4, 0, 8, 0, 0, 0,
+    1, 0, 18, 0, 4, 0, 64, 0, 255, 0, 4, 0, 8, 0, 0, 0,
+    2, 0, 18, 0, 4, 0, 64, 0, 255, 0, 5, 0, 9, 0, 1, 0
+};
+
+double *input_data;
+const int count_input = 16;
+const int max_function = 20;
+
+static void builtin_convert_double2int8(void)
+{
+  // Setup kernel and buffers
+  int k, i, index_cur;
+  char gpu_data[max_function * count_input] = {0};
+  float diff;
+  char log[256] = {0};
+
+  OCL_CREATE_KERNEL("builtin_convert_double2int8");
+
+  OCL_CREATE_BUFFER(buf[0], CL_MEM_READ_WRITE, count_input * max_function * sizeof(double), NULL);
+  OCL_CREATE_BUFFER(buf[1], CL_MEM_READ_WRITE, count_input * max_function * sizeof(char), NULL);
+  OCL_CREATE_BUFFER(buf[2], CL_MEM_READ_WRITE, count_input * max_function * sizeof(char), NULL);
+
+  OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+  OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
+  OCL_SET_ARG(2, sizeof(cl_mem), &buf[2]);
+  OCL_SET_ARG(3, sizeof(int), &count_input);
+
+  globals[0] = 1;
+  locals[0] = 1;
+
+  input_data = (double *)doubleX;
+  clEnqueueWriteBuffer( queue, buf[0], CL_TRUE, 0, count_input * max_function * sizeof(double), input_data, 0, NULL, NULL);
+
+   // Run the kernel
+  OCL_NDRANGE( 1 );
+
+    clEnqueueReadBuffer( queue, buf[1], CL_TRUE, 0, sizeof(char) * max_function/2 * count_input, gpu_data, 0, NULL, NULL);
+    for (k = 0; (uint)k < count_input*max_function/2; k++)
+    {
+        OCL_ASSERT(gpu_data[k] == expectResultChar[k]);
+        if(gpu_data[k] != expectResultChar[k])
+        {
+            printf("failed at function:%s, index:%d  expect value: %d, but get :%d \n", testFunc[k/count_input], k%count_input, expectResultChar[k], gpu_data[k]);
+        }
+    }
+
+    clEnqueueReadBuffer( queue, buf[2], CL_TRUE, 0, sizeof(char) * max_function/2 * count_input, gpu_data, 0, NULL, NULL);
+    unsigned char *ugpu_data = (unsigned char *)gpu_data;
+      for (k = 0; (uint)k < count_input*max_function/2; k++)
+      {
+            OCL_ASSERT(ugpu_data[k] == expectResultUChar[k]);
+            if(ugpu_data[k] != expectResultUChar[k])
+            {
+                printf("failed at function:%s, index:%d expect value: %d, but get :%d \n", testFunc[k/count_input + max_function/2], k%count_input, expectResultUChar[k], ugpu_data[k]);
+            }
+      }
+
+}
+
+MAKE_UTEST_FROM_FUNCTION(builtin_convert_double2int8)
+}
-- 
2.7.4



More information about the Beignet mailing list