[Beignet] [PATCH v5 2/3] Test new math built-in functions

Homer Hsing homer.xing at intel.com
Wed May 15 19:54:16 PDT 2013


Use random test data.
Test 1000 times.

Signed-off-by: Homer Hsing <homer.xing at intel.com>
---
 kernels/compiler_math.cl     | 46 ++++++++++++++++++-----
 kernels/compiler_math_2op.cl | 20 ++++++++++
 kernels/compiler_math_3op.cl | 10 +++++
 utests/compiler_math.cpp     | 88 ++++++++++++++++++++++++++++++--------------
 utests/compiler_math_2op.cpp | 80 ++++++++++++++++++++++++++++++++++++++++
 utests/compiler_math_3op.cpp | 64 ++++++++++++++++++++++++++++++++
 6 files changed, 271 insertions(+), 37 deletions(-)
 create mode 100644 kernels/compiler_math_2op.cl
 create mode 100644 kernels/compiler_math_3op.cl
 create mode 100644 utests/compiler_math_2op.cpp
 create mode 100644 utests/compiler_math_3op.cpp

diff --git a/kernels/compiler_math.cl b/kernels/compiler_math.cl
index 0659840..695fc2c 100644
--- a/kernels/compiler_math.cl
+++ b/kernels/compiler_math.cl
@@ -1,14 +1,40 @@
 __kernel void compiler_math(__global float *dst, __global float *src) {
-  const float x = src[get_global_id(0)];
-  switch (get_global_id(0)) {
-    case 0: dst[get_global_id(0)] = native_cos(x); break;
-    case 1: dst[get_global_id(0)] = native_sin(x); break;
-    case 2: dst[get_global_id(0)] = native_log2(x); break;
-    case 3: dst[get_global_id(0)] = native_sqrt(x); break;
-    case 4: dst[get_global_id(0)] = native_rsqrt(x); break;
-    case 5: dst[get_global_id(0)] = native_recip(x); break;
-    case 6: dst[get_global_id(0)] = native_tan(x); break;
-    default: dst[get_global_id(0)] = 1.f; break;
+  int i = get_global_id(0);
+  const float x = src[i];
+  switch (i) {
+    case 0: dst[i] = cos(x); break;
+    case 1: dst[i] = sin(x); break;
+    case 2: dst[i] = log2(x); break;
+    case 3: dst[i] = sqrt(x); break;
+    case 4: dst[i] = rsqrt(x); break;
+    case 5: dst[i] = native_recip(x); break;
+    case 6: dst[i] = tan(x); break;
+    case 7: dst[i] = cbrt(x); break;
+    case 8: dst[i] = ceil(x); break;
+    case 9: dst[i] = cospi(x); break;
+    case 10: dst[i] = exp2(x); break;
+    case 11: dst[i] = exp10(x); break;
+    case 12: dst[i] = expm1(x); break;
+    case 13: dst[i] = log1p(x); break;
+    case 14: dst[i] = logb(x); break;
+    case 15: dst[i] = sinpi(x); break;
+    case 16: dst[i] = tanpi(x); break;
+    case 17: dst[i] = rint(x); break;
+    case 18: dst[i] = sinh(x); break;
+    case 19: dst[i] = cosh(x); break;
+    case 20: dst[i] = tanh(x); break;
+    case 21: dst[i] = asinh(x); break;
+    case 22: dst[i] = acosh(x); break;
+    case 23: dst[i] = atanh(x); break;
+    case 24: dst[i] = asin(x); break;
+    case 25: dst[i] = acos(x); break;
+    case 26: dst[i] = atan(x); break;
+    case 27: dst[i] = asinpi(x); break;
+    case 28: dst[i] = acospi(x); break;
+    case 29: dst[i] = atanpi(x); break;
+    case 30: dst[i] = erf(x); break;
+    case 31: dst[i] = nan((uint)x); break;
+    default: dst[i] = 1.f; break;
   };
 }
 
diff --git a/kernels/compiler_math_2op.cl b/kernels/compiler_math_2op.cl
new file mode 100644
index 0000000..dd86dbf
--- /dev/null
+++ b/kernels/compiler_math_2op.cl
@@ -0,0 +1,20 @@
+kernel void compiler_math_2op(global float *dst, global float *src1, global float *src2) {
+  int i = get_global_id(0);
+  const float x = src1[i], y = src2[i];
+  float z;
+  switch (i) {
+    case 0: dst[i] = native_divide(x, y); break;
+    case 1: dst[i] = fdim(x, y); break;
+    case 2: dst[i] = fract(x, &z); break;
+    case 3: dst[i] = hypot(x, y); break;
+    case 4: dst[i] = ldexp(x, y); break;
+    case 5: dst[i] = pown(x, (int)y); break;
+    case 6: dst[i] = remainder(x, y); break;
+    case 7: dst[i] = rootn(x, (int)(y+1)); break;
+    case 8: dst[i] = copysign(x, y); break;
+    case 9: dst[i] = maxmag(x, y); break;
+    case 10: dst[i] = minmag(x, y); break;
+    default: dst[i] = 1.f; break;
+  };
+}
+
diff --git a/kernels/compiler_math_3op.cl b/kernels/compiler_math_3op.cl
new file mode 100644
index 0000000..e1d7c37
--- /dev/null
+++ b/kernels/compiler_math_3op.cl
@@ -0,0 +1,10 @@
+kernel void compiler_math_3op(global float *dst, global float *src1, global float *src2, global float *src3) {
+  int i = get_global_id(0);
+  const float x = src1[i], y = src2[i], z = src3[i];
+  switch (i) {
+    case 0: dst[i] = mad(x, y, z); break;
+    case 1: dst[i] = fma(x, y, z); break;
+    default: dst[i] = 1.f; break;
+  };
+}
+
diff --git a/utests/compiler_math.cpp b/utests/compiler_math.cpp
index 7303dd5..e0c4487 100644
--- a/utests/compiler_math.cpp
+++ b/utests/compiler_math.cpp
@@ -2,18 +2,44 @@
 #include <cmath>
 #include <algorithm>
 
-static void cpu_compiler_math(float *dst, float *src, int get_global_id0)
+static void cpu_compiler_math(float *dst, float *src, int i)
 {
-  const float x = src[get_global_id0];
-  switch (get_global_id0) {
-    case 0: dst[get_global_id0] = cosf(x); break;
-    case 1: dst[get_global_id0] = sinf(x); break;
-    case 2: dst[get_global_id0] = log2f(x); break;
-    case 3: dst[get_global_id0] = sqrtf(x); break;
-    case 4: dst[get_global_id0] = 1.f/ sqrtf(x); break;
-    case 5: dst[get_global_id0] = 1.f / x; break;
-    case 6: dst[get_global_id0] = tanf(x); break;
-    default: dst[get_global_id0] = 1.f; break;
+  const float x = src[i];
+  const float PI = 3.141592653589793f;
+  switch (i) {
+    case 0: dst[i] = cosf(x); break;
+    case 1: dst[i] = sinf(x); break;
+    case 2: dst[i] = log2f(x); break;
+    case 3: dst[i] = sqrtf(x); break;
+    case 4: dst[i] = 1.f/ sqrtf(x); break;
+    case 5: dst[i] = 1.f / x; break;
+    case 6: dst[i] = tanf(x); break;
+    case 7: dst[i] = powf(x, 0.3333333333333333333f); break;
+    case 8: dst[i] = ceilf(x); break;
+    case 9: dst[i] = cosf(PI * x); break;
+    case 10: dst[i] = powf(2, x); break;
+    case 11: dst[i] = powf(10, x); break;
+    case 12: dst[i] = expf(x) - 1; break;
+    case 13: dst[i] = logf(x + 1); break;
+    case 14: dst[i] = floorf(log2f(x)); break;
+    case 15: dst[i] = sinf(PI * x); break;
+    case 16: dst[i] = tanf(PI * x); break;
+    case 17: dst[i] = 2 * roundf(x / 2); break;
+    case 18: dst[i] = sinhf(x); break;
+    case 19: dst[i] = coshf(x); break;
+    case 20: dst[i] = tanhf(x); break;
+    case 21: dst[i] = asinhf(x); break;
+    case 22: dst[i] = acoshf(x); break;
+    case 23: dst[i] = atanhf(x); break;
+    case 24: dst[i] = asinf(x); break;
+    case 25: dst[i] = acosf(x); break;
+    case 26: dst[i] = atanf(x); break;
+    case 27: dst[i] = asinf(x) / PI; break;
+    case 28: dst[i] = acosf(x) / PI; break;
+    case 29: dst[i] = atanf(x) / PI; break;
+    case 30: dst[i] = erff(x); break;
+    case 31: dst[i] = nanf(""); break;
+    default: dst[i] = 1.f; break;
   };
 }
 
@@ -31,23 +57,31 @@ static void compiler_math(void)
   globals[0] = 16;
   locals[0] = 16;
 
-  OCL_MAP_BUFFER(1);
-  for (uint32_t i = 0; i < 32; ++i)
-    cpu_src[i] = ((float*)buf_data[1])[i] = float(i);
-  OCL_UNMAP_BUFFER(1);
-  OCL_NDRANGE(1);
-
-  OCL_MAP_BUFFER(0);
-  OCL_MAP_BUFFER(1);
-  for (int i = 0; i < 16; ++i)
-    cpu_compiler_math(cpu_dst, cpu_src, i);
-  for (int i = 0; i < 16; ++i) {
-    const float cpu = cpu_dst[i];
-    const float gpu = ((float*)buf_data[0])[i];
-    OCL_ASSERT(fabs(gpu-cpu)/std::max(fabs(cpu), fabs(gpu)) < 1e-4f);
+  int j;
+  for(j = 0; j < 1000; j ++) {
+    OCL_MAP_BUFFER(1);
+    for (uint32_t i = 0; i < 32; ++i)
+      cpu_src[i] = ((float*)buf_data[1])[i] = .1f * (rand() & 15);
+    OCL_UNMAP_BUFFER(1);
+    OCL_NDRANGE(1);
+
+    OCL_MAP_BUFFER(0);
+    OCL_MAP_BUFFER(1);
+    for (int i = 0; i < 16; ++i)
+      cpu_compiler_math(cpu_dst, cpu_src, i);
+    for (int i = 0; i < 16; ++i) {
+      const float cpu = cpu_dst[i];
+      const float gpu = ((float*)buf_data[0])[i];
+      if (isinf(cpu))
+        OCL_ASSERT(isinf(gpu));
+      else if (isnan(cpu))
+        OCL_ASSERT(isnan(gpu));
+      else
+        OCL_ASSERT(fabs(gpu-cpu) < 1e-3f);
+    }
+    OCL_UNMAP_BUFFER(0);
+    OCL_UNMAP_BUFFER(1);
   }
-  OCL_UNMAP_BUFFER(0);
-  OCL_UNMAP_BUFFER(1);
 }
 
 MAKE_UTEST_FROM_FUNCTION(compiler_math)
diff --git a/utests/compiler_math_2op.cpp b/utests/compiler_math_2op.cpp
new file mode 100644
index 0000000..454967d
--- /dev/null
+++ b/utests/compiler_math_2op.cpp
@@ -0,0 +1,80 @@
+#include "utest_helper.hpp"
+#include <cmath>
+#include <algorithm>
+
+static float rnde(float v) {
+  if(v - floorf(v) > 0.5f)
+    return floorf(v) + 1;
+  if(v - floorf(v) < 0.5f)
+    return floorf(v);
+  if((int)(floorf(v)) & 1)
+    return floorf(v) + 1;
+  return floorf(v);
+}
+
+static void cpu_compiler_math(float *dst, float *src1, float *src2, int i)
+{
+  const float x = src1[i], y = src2[i];
+  switch (i) {
+    case 0: dst[i] = x / y; break;
+    case 1: dst[i] = x > y ? x - y : 0; break;
+    case 2: dst[i] = fminf(x - floorf(x), 0x1.FFFFFep-1F); break;
+    case 3: dst[i] = sqrtf(x*x + y*y); break;
+    case 4: dst[i] = x * powf(2, (int)y); break;
+    case 5: dst[i] = powf(x, (int)y); break;
+    case 6: dst[i] = x - rnde(x/y)*y; break;
+    case 7: dst[i] = powf(x, 1.f/(int)(y+1)); break;
+    case 8: dst[i] = x * y < 0 ? -x : x; break;
+    case 9: dst[i] = fabsf(x) > fabsf(y) ? x : fabsf(y) > fabsf(x) ? y : fmaxf(x, y); break;
+    case 10: dst[i] = fabsf(x) < fabsf(y) ? x : fabsf(y) < fabsf(x) ? y : fminf(x, y); break;
+    default: dst[i] = 1.f; break;
+  };
+}
+
+static void compiler_math_2op(void)
+{
+  const size_t n = 32;
+  float cpu_dst[32], cpu_src1[32], cpu_src2[32];
+
+  // Setup kernel and buffers
+  OCL_CREATE_KERNEL("compiler_math_2op");
+  OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(float), NULL);
+  OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(float), NULL);
+  OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(float), 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]);
+  globals[0] = 16;
+  locals[0] = 16;
+
+  int j;
+  for(j = 0; j < 1000; j ++) {
+    OCL_MAP_BUFFER(1);
+    OCL_MAP_BUFFER(2);
+    for (uint32_t i = 0; i < 32; ++i) {
+      cpu_src1[i] = ((float*)buf_data[1])[i] = .1f * (rand() & 15);
+      cpu_src2[i] = ((float*)buf_data[2])[i] = .1f * (rand() & 15);
+    }
+    OCL_UNMAP_BUFFER(1);
+    OCL_UNMAP_BUFFER(2);
+    OCL_NDRANGE(1);
+
+    for (int i = 0; i < 16; ++i)
+      cpu_compiler_math(cpu_dst, cpu_src1, cpu_src2, i);
+    OCL_MAP_BUFFER(0);
+    for (int i = 0; i < 16; ++i) {
+      const float cpu = cpu_dst[i];
+      const float gpu = ((float*)buf_data[0])[i];
+      if (isinf(cpu))
+        OCL_ASSERT(isinf(gpu));
+      else if (isnan(cpu))
+        OCL_ASSERT(isnan(gpu));
+      else {
+        OCL_ASSERT(fabs(gpu-cpu) < 1e-3f);
+      }
+    }
+    OCL_UNMAP_BUFFER(0);
+  }
+}
+
+MAKE_UTEST_FROM_FUNCTION(compiler_math_2op)
diff --git a/utests/compiler_math_3op.cpp b/utests/compiler_math_3op.cpp
new file mode 100644
index 0000000..a382b0a
--- /dev/null
+++ b/utests/compiler_math_3op.cpp
@@ -0,0 +1,64 @@
+#include "utest_helper.hpp"
+#include <cmath>
+#include <algorithm>
+
+static void cpu_compiler_math(float *dst, float *src1, float *src2, float *src3, int i)
+{
+  const float x = src1[i], y = src2[i], z = src3[i];
+  switch (i) {
+    case 0: dst[i] = x * y + z; break;
+    case 1: dst[i] = x * y + z; break;
+    default: dst[i] = 1.f; break;
+  };
+}
+
+static void compiler_math_3op(void)
+{
+  const size_t n = 32;
+  float cpu_dst[32], cpu_src1[32], cpu_src2[32], cpu_src3[32];
+
+  // Setup kernel and buffers
+  OCL_CREATE_KERNEL("compiler_math_3op");
+  OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(float), NULL);
+  OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(float), NULL);
+  OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(float), NULL);
+  OCL_CREATE_BUFFER(buf[3], 0, n * sizeof(float), 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(cl_mem), &buf[3]);
+  globals[0] = 16;
+  locals[0] = 16;
+
+  for (int j = 0; j < 1000; j ++) {
+    OCL_MAP_BUFFER(1);
+    OCL_MAP_BUFFER(2);
+    OCL_MAP_BUFFER(3);
+    for (uint32_t i = 0; i < 32; ++i) {
+      cpu_src1[i] = ((float*)buf_data[1])[i] = .1f * (rand() & 15);
+      cpu_src2[i] = ((float*)buf_data[2])[i] = .1f * (rand() & 15);
+      cpu_src3[i] = ((float*)buf_data[3])[i] = .1f * (rand() & 15);
+    }
+    OCL_UNMAP_BUFFER(1);
+    OCL_UNMAP_BUFFER(2);
+    OCL_UNMAP_BUFFER(3);
+    OCL_NDRANGE(1);
+
+    for (int i = 0; i < 16; ++i)
+      cpu_compiler_math(cpu_dst, cpu_src1, cpu_src2, cpu_src3, i);
+    OCL_MAP_BUFFER(0);
+    for (int i = 0; i < 16; ++i) {
+      const float cpu = cpu_dst[i];
+      const float gpu = ((float*)buf_data[0])[i];
+      if (isinf(cpu))
+        OCL_ASSERT(isinf(gpu));
+      else if (isnan(cpu))
+        OCL_ASSERT(isnan(gpu));
+      else
+        OCL_ASSERT(fabs(gpu-cpu) < 1e-3f);
+    }
+    OCL_UNMAP_BUFFER(0);
+  }
+}
+
+MAKE_UTEST_FROM_FUNCTION(compiler_math_3op)
-- 
1.8.1.2



More information about the Beignet mailing list