[Beignet] [PATCH V3 2/2] Add the test case for builtin abs() function

Zhigang Gong zhigang.gong at linux.intel.com
Sun Jun 30 19:09:25 PDT 2013


I met the following compilation errors for the cl kernel.

compiler_abs_int:
/tmp/file5TK38w.cl:5579:33: error: variadic macros not supported in OpenCL
#define COMPILER_ABS_FUNC(TYPE, ...) \
                                ^
/tmp/file5TK38w.cl:5593:1: error: expected function body after function declarator
COMPILER_ABS(int)
^
/tmp/file5TK38w.cl:5588:5: note: expanded from macro 'COMPILER_ABS'
    COMPILER_ABS_FUNC(TYPE, 2) \
    ^
2 errors generated.
  compiler_abs_int()    [FAILED]
    Error: error calling clBuildProgram with errorCL_INVALID_PROGRAM

You can check the opencl specification  section 6.9 Restrictions:
"e. Variadic macros and functions are not supported. ".

You need to modify the test case accordingtly.

On Fri, Jun 28, 2013 at 02:50:41PM +0800, junyan.he at inbox.com wrote:
> From: Junyan He <junyan.he at linux.intel.com>
> 
> All the integer value types check are supported.
> Please use the case named compiler_abs_xxxx,
> where xxxx means the data type such as int2, char4
> 
> Signed-off-by: Junyan He <junyan.he at linux.intel.com>
> ---
>  kernels/compiler_abs.cl |   20 +++++
>  utests/CMakeLists.txt   |    1 +
>  utests/compiler_abs.cpp |  214 +++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 235 insertions(+)
>  create mode 100644 kernels/compiler_abs.cl
>  create mode 100644 utests/compiler_abs.cpp
> 
> diff --git a/kernels/compiler_abs.cl b/kernels/compiler_abs.cl
> new file mode 100644
> index 0000000..cf06431
> --- /dev/null
> +++ b/kernels/compiler_abs.cl
> @@ -0,0 +1,20 @@
> +#define COMPILER_ABS_FUNC(TYPE, ...) \
> +    kernel void compiler_abs_##TYPE##__VA_ARGS__( \
> +           global TYPE##__VA_ARGS__* src, global TYPE##__VA_ARGS__* dst) { \
> +        int i = get_global_id(0); \
> +        dst[i] = abs(src[i]);     \
> +    }
> +
> +#define COMPILER_ABS(TYPE)  \
> +    COMPILER_ABS_FUNC(TYPE) \
> +    COMPILER_ABS_FUNC(TYPE, 2) \
> +    COMPILER_ABS_FUNC(TYPE, 4) \
> +    COMPILER_ABS_FUNC(TYPE, 8) \
> +    COMPILER_ABS_FUNC(TYPE, 16)
> +
> +COMPILER_ABS(int)
> +COMPILER_ABS(uint)
> +COMPILER_ABS(char)
> +COMPILER_ABS(uchar)
> +COMPILER_ABS(short)
> +COMPILER_ABS(ushort)
> diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt
> index c115de3..9c3ef8c 100644
> --- a/utests/CMakeLists.txt
> +++ b/utests/CMakeLists.txt
> @@ -34,6 +34,7 @@ set (utests_sources
>    compiler_double_2.cpp
>    compiler_double_3.cpp
>    compiler_fabs.cpp
> +  compiler_abs.cpp
>    compiler_fill_image.cpp
>    compiler_fill_image0.cpp
>    compiler_fill_image_3d.cpp
> diff --git a/utests/compiler_abs.cpp b/utests/compiler_abs.cpp
> new file mode 100644
> index 0000000..78134ad
> --- /dev/null
> +++ b/utests/compiler_abs.cpp
> @@ -0,0 +1,214 @@
> +#include "utest_helper.hpp"
> +#include "string.h"
> +
> +template <typename T, int N>
> +struct cl_vec {
> +    T ptr[N];
> +
> +    typedef cl_vec<T, N> vec_type;
> +
> +    cl_vec(void) {
> +        memset(ptr, 0, sizeof(T) * N);
> +    }
> +    cl_vec(vec_type & other) {
> +        memcpy (this->ptr, other.ptr, sizeof(T) * N);
> +    }
> +
> +    vec_type& operator= (vec_type & other) {
> +        memcpy (this->ptr, other.ptr, sizeof(T) * N);
> +        return *this;
> +    }
> +
> +    bool operator== (vec_type & other) {
> +        return !memcmp (this->ptr, other.ptr, sizeof(T) * N);
> +    }
> +
> +    void abs(void) {
> +        int i = 0;
> +        for (; i < N; i++) {
> +            T f = ptr[i];
> +            f = f < 0 ? -f : f;
> +            ptr[i] = f;
> +        }
> +    }
> +};
> +
> +template <typename T, int N> static void cpu (int global_id,
> +        cl_vec<T, N> *src, cl_vec<T, N> *dst)
> +{
> +    cl_vec<T, N> v  = src[global_id];
> +    v.abs();
> +    dst[global_id] = v;
> +}
> +
> +template <typename T> static void cpu(int global_id, T *src, T *dst)
> +{
> +    T f = src[global_id];
> +    f = f < 0 ? -f : f;
> +    dst[global_id] = f;
> +}
> +
> +template <typename T, int N> static void gen_rand_val (cl_vec<T, N>& vect)
> +{
> +    int i = 0;
> +    for (; i < N; i++) {
> +        vect.ptr[i] = static_cast<T>((rand() & 15) - 7);
> +    }
> +}
> +
> +template <typename T> static void gen_rand_val (T & val)
> +{
> +    val = static_cast<T>((rand() & 15) - 7);
> +}
> +
> +template <typename T, int N> static void dump_data (cl_vec<T, N>* vect, int n)
> +{
> +    T* val = reinterpret_cast<T *>(vect);
> +
> +    n = n*N;
> +
> +    printf("\nRaw: \n");
> +    for (int32_t i = 0; i < (int32_t) n; ++i) {
> +        printf(" %d", ((T *)buf_data[0])[i]);
> +    }
> +
> +    printf("\nCPU: \n");
> +    for (int32_t i = 0; i < (int32_t) n; ++i) {
> +        printf(" %d", val[i]);
> +    }
> +    printf("\nGPU: \n");
> +    for (int32_t i = 0; i < (int32_t) n; ++i) {
> +        printf(" %d", ((T *)buf_data[1])[i]);
> +    }
> +}
> +
> +template <typename T> static void dump_data (T* val, int n)
> +{
> +    printf("\nRaw: \n");
> +    for (int32_t i = 0; i < (int32_t) n; ++i) {
> +        printf(" %d", ((T *)buf_data[0])[i]);
> +    }
> +
> +    printf("\nCPU: \n");
> +    for (int32_t i = 0; i < (int32_t) n; ++i) {
> +        printf(" %d", val[i]);
> +    }
> +    printf("\nGPU: \n");
> +    for (int32_t i = 0; i < (int32_t) n; ++i) {
> +        printf(" %d", ((T *)buf_data[1])[i]);
> +    }
> +}
> +
> +template <typename T> static void compiler_abs_with_type(void)
> +{
> +    const size_t n = 16;
> +    T cpu_dst[16], cpu_src[16];
> +
> +    // Setup buffers
> +    OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(T), NULL);
> +    OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(T), NULL);
> +    OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
> +    OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
> +    globals[0] = 16;
> +    locals[0] = 16;
> +
> +    // Run random tests
> +    for (uint32_t pass = 0; pass < 8; ++pass) {
> +        OCL_MAP_BUFFER(0);
> +        for (int32_t i = 0; i < (int32_t) n; ++i) {
> +            gen_rand_val(cpu_src[i]);
> +        }
> +
> +        memcpy(buf_data[0], cpu_src, sizeof(T) * n);
> +
> +        // Run the kernel on GPU
> +        OCL_NDRANGE(1);
> +
> +        // Run on CPU
> +        for (int32_t i = 0; i < (int32_t) n; ++i)
> +            cpu(i, cpu_src, cpu_dst);
> +
> +        // Compare
> +        OCL_MAP_BUFFER(1);
> +
> +//      dump_data(cpu_dst, n);
> +
> +        OCL_ASSERT(!memcmp(buf_data[1], cpu_dst, sizeof(T) * n));
> +        OCL_UNMAP_BUFFER(1);
> +        OCL_UNMAP_BUFFER(0);
> +    }
> +}
> +
> +#define ABS_TEST_TYPE(TYPE) \
> +	static void compiler_abs_##TYPE (void) \
> +        { \
> +           OCL_CALL (cl_kernel_init, "compiler_abs.cl", "compiler_abs_"#TYPE, SOURCE, NULL);  \
> +           compiler_abs_with_type<TYPE>(); \
> +        } \
> +	MAKE_UTEST_FROM_FUNCTION(compiler_abs_##TYPE);
> +
> +ABS_TEST_TYPE(int)
> +ABS_TEST_TYPE(short)
> +ABS_TEST_TYPE(char)
> +
> +typedef unsigned char uchar;
> +typedef unsigned short ushort;
> +typedef unsigned int uint;
> +ABS_TEST_TYPE(uint)
> +ABS_TEST_TYPE(ushort)
> +ABS_TEST_TYPE(uchar)
> +
> +typedef cl_vec<int, 2> int2;
> +typedef cl_vec<int, 4> int4;
> +typedef cl_vec<int, 8> int8;
> +typedef cl_vec<int, 16> int16;
> +ABS_TEST_TYPE(int2)
> +ABS_TEST_TYPE(int4)
> +ABS_TEST_TYPE(int8)
> +ABS_TEST_TYPE(int16)
> +
> +typedef cl_vec<char, 2> char2;
> +typedef cl_vec<char, 4> char4;
> +typedef cl_vec<char, 8> char8;
> +typedef cl_vec<char, 16> char16;
> +ABS_TEST_TYPE(char2)
> +ABS_TEST_TYPE(char4)
> +ABS_TEST_TYPE(char8)
> +ABS_TEST_TYPE(char16)
> +
> +typedef cl_vec<short, 2> short2;
> +typedef cl_vec<short, 4> short4;
> +typedef cl_vec<short, 8> short8;
> +typedef cl_vec<short, 16> short16;
> +ABS_TEST_TYPE(short2)
> +ABS_TEST_TYPE(short4)
> +ABS_TEST_TYPE(short8)
> +ABS_TEST_TYPE(short16)
> +
> +
> +typedef cl_vec<unsigned int, 2> uint2;
> +typedef cl_vec<unsigned int, 4> uint4;
> +typedef cl_vec<unsigned int, 8> uint8;
> +typedef cl_vec<unsigned int, 16> uint16;
> +ABS_TEST_TYPE(uint2)
> +ABS_TEST_TYPE(uint4)
> +ABS_TEST_TYPE(uint8)
> +ABS_TEST_TYPE(uint16)
> +
> +typedef cl_vec<unsigned char, 2> uchar2;
> +typedef cl_vec<unsigned char, 4> uchar4;
> +typedef cl_vec<unsigned char, 8> uchar8;
> +typedef cl_vec<unsigned char, 16> uchar16;
> +ABS_TEST_TYPE(uchar2)
> +ABS_TEST_TYPE(uchar4)
> +ABS_TEST_TYPE(uchar8)
> +ABS_TEST_TYPE(uchar16)
> +
> +typedef cl_vec<unsigned short, 2> ushort2;
> +typedef cl_vec<unsigned short, 4> ushort4;
> +typedef cl_vec<unsigned short, 8> ushort8;
> +typedef cl_vec<unsigned short, 16> ushort16;
> +ABS_TEST_TYPE(ushort2)
> +ABS_TEST_TYPE(ushort4)
> +ABS_TEST_TYPE(ushort8)
> +ABS_TEST_TYPE(ushort16)
> -- 
> 1.7.9.5
> 
> _______________________________________________
> Beignet mailing list
> Beignet at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/beignet


More information about the Beignet mailing list