From 1db364d9471c8574eb41aed6e6903284687dc2ea Mon Sep 17 00:00:00 2001 From: Yi Sun Date: Mon, 12 Aug 2013 10:35:39 +0800 Subject: [PATCH] utest: Add test case for function acos/acosh/asin/asinh. Case contains illegal, boundary and legal values. Signed-off-by: Yi Sun Reviewed-by: Zhigang Gong --- kernels/builtin_acos_asin.cl | 10 +++++ utests/CMakeLists.txt | 1 + utests/builtin_acos_asin.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 kernels/builtin_acos_asin.cl create mode 100644 utests/builtin_acos_asin.cpp diff --git a/kernels/builtin_acos_asin.cl b/kernels/builtin_acos_asin.cl new file mode 100644 index 0000000..bba2d21 --- /dev/null +++ b/kernels/builtin_acos_asin.cl @@ -0,0 +1,10 @@ +__kernel void builtin_acos_asin(__global float *dst, __global float *src, __global int *max_func) { + int i = get_global_id(0); + float x = src[i]; + + dst[i * (*max_func) + 0] = acos(x); + dst[i * (*max_func) + 1] = acosh(x); + dst[i * (*max_func) + 2] = asin(x); + dst[i * (*max_func) + 3] = asinh(x); + dst[i * (*max_func) + 4] = x; +}; diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt index b205c67..c30e10e 100644 --- a/utests/CMakeLists.txt +++ b/utests/CMakeLists.txt @@ -115,6 +115,7 @@ set (utests_sources builtin_global_id.cpp builtin_num_groups.cpp builtin_local_id.cpp + builtin_acos_asin.cpp runtime_createcontext.cpp runtime_null_kernel_arg.cpp compiler_double.cpp diff --git a/utests/builtin_acos_asin.cpp b/utests/builtin_acos_asin.cpp new file mode 100644 index 0000000..0187226 --- /dev/null +++ b/utests/builtin_acos_asin.cpp @@ -0,0 +1,87 @@ +#include "utest_helper.hpp" +#include +#include + +#define udebug 0 +#define printf_c(...) \ +{\ + printf("\033[1m\033[40;31m");\ + printf( __VA_ARGS__ );\ + printf("\033[0m");\ +} + +const float input_data[] = {-30, -1, -0.92, -0.5, -0.09, 0, 0.09, 0.5, 0.92, 1, 30}; +const int count_input = sizeof(input_data) / sizeof(input_data[0]); +const int max_function = 5; + +static void cpu_compiler_math(float *dst, const float *src) +{ + const float x = *src; + + dst[0] = acos(x); + dst[1] = acosh(x); + dst[2] = asin(x); + dst[3] = asinh(x); + dst[4] = x; +} + +static void builtin_acos_asin(void) +{ + // Setup kernel and buffers + int k, i, index_cur; + float gpu_data[max_function * count_input] = {0}, cpu_data[max_function * count_input] = {0}; + + OCL_CREATE_KERNEL("builtin_acos_asin"); + + OCL_CREATE_BUFFER(buf[0], CL_MEM_READ_WRITE, count_input * max_function * sizeof(float), NULL); + OCL_CREATE_BUFFER(buf[1], CL_MEM_READ_WRITE, count_input * sizeof(float), NULL); + OCL_CREATE_BUFFER(buf[2], CL_MEM_READ_WRITE, sizeof(int), 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] = count_input; + locals[0] = 1; + + clEnqueueWriteBuffer( queue, buf[1], CL_TRUE, 0, count_input * sizeof(float), input_data, 0, NULL, NULL); + clEnqueueWriteBuffer( queue, buf[2], CL_TRUE, 0, sizeof(int), &max_function , 0, NULL, NULL); + + // Run the kernel + OCL_NDRANGE( 1 ); + + clEnqueueReadBuffer( queue, buf[0], CL_TRUE, 0, sizeof(float) * max_function * count_input, gpu_data, 0, NULL, NULL); + + for (k = 0; (uint)k < count_input; k++) + { + cpu_compiler_math( cpu_data + k * max_function, input_data + k); + + for (i = 0; i < max_function; i++) + { + index_cur = k * max_function + i; +#if udebug + if (isinf(cpu_data[index_cur]) && !isinf(gpu_data[index_cur])){ + printf_c("%d/%d: %f -> gpu:%f cpu:%f\n", k, i, input_data[k], gpu_data[index_cur], cpu_data[index_cur]); + } + else if (isnan(cpu_data[index_cur]) && !isnan(gpu_data[index_cur])){ + printf_c("%d/%d: %f -> gpu:%f cpu:%f\n", k, i, input_data[k], gpu_data[index_cur], cpu_data[index_cur]); + } + else if(fabs(gpu_data[index_cur] - cpu_data[index_cur]) > 1e-3f){ + printf_c("%d/%d: %f -> gpu:%f cpu:%f\n", k, i, input_data[k], gpu_data[index_cur], cpu_data[index_cur]); + } + else + printf("%d/%d: %f -> gpu:%f cpu:%f\n", k, i, input_data[k], gpu_data[index_cur], cpu_data[index_cur]); +#else + if (isinf(cpu_data[index_cur])) + OCL_ASSERT(isinf(gpu_data[index_cur])); + else if (isnan(cpu_data[index_cur])) + OCL_ASSERT(isnan(gpu_data[index_cur])); + else + { + OCL_ASSERT(fabs(gpu_data[index_cur] - cpu_data[index_cur]) < 1e-3f); + } +#endif + } + } +} + +MAKE_UTEST_FROM_FUNCTION(builtin_acos_asin) -- 2.7.4