summaryrefslogtreecommitdiff
path: root/utests/compiler_abs_diff.cpp
blob: 1df7d47da5962af920b653608614369ccb857a6c (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "utest_helper.hpp"
#include "string.h"

template <typename T, int N>
struct cl_vec {
    T ptr[((N+1)/2)*2]; //align to 2 elements.

    typedef cl_vec<T, N> vec_type;

    cl_vec(void) {
        memset(ptr, 0, sizeof(T) * ((N+1)/2)*2);
    }
    cl_vec(vec_type & other) {
        memset(ptr, 0, sizeof(T) * ((N+1)/2)*2);
        memcpy (this->ptr, other.ptr, sizeof(T) * N);
    }

    vec_type& operator= (vec_type & other) {
        memset(ptr, 0, sizeof(T) * ((N+1)/2)*2);
        memcpy (this->ptr, other.ptr, sizeof(T) * N);
        return *this;
    }

    template <typename U> vec_type& operator= (cl_vec<U, N> & other) {
        memset(ptr, 0, sizeof(T) * ((N+1)/2)*2);
        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_diff(vec_type & other) {
        int i = 0;
        for (; i < N; i++) {
            T a = ptr[i];
            T b = other.ptr[i];
            T f = a > b ? (a - b) : (b - a);
            ptr[i] = f;
        }
    }
};

template <typename T, typename U, int N> static void cpu (int global_id,
        cl_vec<T, N> *x, cl_vec<T, N> *y, cl_vec<U, N> *diff)
{
    cl_vec<T, N> v  = x[global_id];
    v.abs_diff(y[global_id]);
    diff[global_id] = v;
}

template <typename T, typename U> static void cpu(int global_id, T *x, T *y, U *diff)
{
    T a = x[global_id];
    T b = y[global_id];
    U f = a > b ? (a - b) : (b - a);
    diff[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() & 63) - 32);
    }
}

template <typename T> static void gen_rand_val (T & val)
{
    val = static_cast<T>((rand() & 63) - 32);
}

template <typename T>
inline static void print_data (T& val)
{
    if (std::is_unsigned<T>::value)
        printf(" %u", val);
    else
        printf(" %d", val);
}

template <typename T, typename U, int N> static void dump_data (cl_vec<T, N>* x,
        cl_vec<T, N>* y, cl_vec<U, N>* diff, int n)
{
    U* val = reinterpret_cast<U *>(diff);

    n = n*((N+1)/2)*2;

    printf("\nRaw x: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(((T *)buf_data[0])[i]);
    }
    printf("\nRaw y: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(((T *)buf_data[1])[i]);
    }

    printf("\nCPU diff: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(val[i]);
    }
    printf("\nGPU diff: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(((U *)buf_data[2])[i]);
    }
}

template <typename T, typename U> static void dump_data (T* x, T* y, U* diff, int n)
{
    printf("\nRaw x: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(((T *)buf_data[0])[i]);
    }
    printf("\nRaw y: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(((T *)buf_data[1])[i]);
    }

    printf("\nCPU diff: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(diff[i]);
    }
    printf("\nGPU diff: \n");
    for (int32_t i = 0; i < (int32_t) n; ++i) {
        print_data(((U *)buf_data[2])[i]);
    }
}

template <typename T>
static void check_result(T* actual, T* expected)
{
    OCL_ASSERT(*actual == *expected);
}

template <typename T, int N>
static void check_result(cl_vec<T, N>* actual, cl_vec<T, N>* expected)
{
    OCL_ASSERT(!memcmp(actual, expected, sizeof(T)*N));
}

template <typename T, typename U> static void compiler_abs_diff_with_type(void)
{
    const size_t n = 16;
    U cpu_diff[16];
    T cpu_x[16];
    T cpu_y[16];

    // Setup buffers
    OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(T), NULL);
    OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(T), NULL);
    OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(U), 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;

    // Run random tests
    for (uint32_t pass = 0; pass < 8; ++pass) {
        OCL_MAP_BUFFER(0);
        OCL_MAP_BUFFER(1);

        /* Clear the dst buffer to avoid random data. */
        OCL_MAP_BUFFER(2);
        memset(buf_data[2], 0, sizeof(U) * n);
        OCL_UNMAP_BUFFER(2);

        for (int32_t i = 0; i < (int32_t) n; ++i) {
            gen_rand_val(cpu_x[i]);
            gen_rand_val(cpu_y[i]);
        }

        memcpy(buf_data[0], cpu_x, sizeof(T) * n);
        memcpy(buf_data[1], cpu_y, 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_x, cpu_y, cpu_diff);

        // Compare
        OCL_MAP_BUFFER(2);

//      dump_data(cpu_x, cpu_y, cpu_diff, n);

        U* actual = (U*)buf_data[2];
        U* expected = cpu_diff;
        for (size_t i = 0; i < n; ++i)
            check_result(&actual[i], &expected[i]);

        OCL_UNMAP_BUFFER(0);
        OCL_UNMAP_BUFFER(1);
        OCL_UNMAP_BUFFER(2);
    }
}


#define ABS_TEST_DIFF_TYPE_2(TYPE, CLTYPE, UTYPE, KEEP_PROGRAM) \
	static void compiler_abs_diff_##CLTYPE (void) \
        { \
           OCL_CALL (cl_kernel_init, "compiler_abs_diff.cl", "compiler_abs_diff_"#CLTYPE, SOURCE, NULL);  \
           compiler_abs_diff_with_type<TYPE, UTYPE>(); \
        } \
	MAKE_UTEST_FROM_FUNCTION_KEEP_PROGRAM(compiler_abs_diff_##CLTYPE, KEEP_PROGRAM);

#define ABS_TEST_DIFF_TYPE(TYPE, UTYPE) ABS_TEST_DIFF_TYPE_2(TYPE, TYPE, UTYPE, true)

#define ABS_TEST_DIFF_TYPE_END(TYPE, UTYPE) ABS_TEST_DIFF_TYPE_2(TYPE, TYPE, UTYPE, false)


typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef uint64_t ulong64;
ABS_TEST_DIFF_TYPE(int, uint)
ABS_TEST_DIFF_TYPE_2(int64_t, long, ulong64, true)
ABS_TEST_DIFF_TYPE(short, ushort)
ABS_TEST_DIFF_TYPE(char, uchar)
ABS_TEST_DIFF_TYPE(uint, uint)
ABS_TEST_DIFF_TYPE_2(ulong64, ulong, ulong64, true)
ABS_TEST_DIFF_TYPE(ushort, ushort)
ABS_TEST_DIFF_TYPE(uchar, uchar)

typedef cl_vec<int, 2> int2;
typedef cl_vec<int, 3> int3;
typedef cl_vec<int, 4> int4;
typedef cl_vec<int, 8> int8;
typedef cl_vec<int, 16> int16;
typedef cl_vec<unsigned int, 2> uint2;
typedef cl_vec<unsigned int, 3> uint3;
typedef cl_vec<unsigned int, 4> uint4;
typedef cl_vec<unsigned int, 8> uint8;
typedef cl_vec<unsigned int, 16> uint16;
ABS_TEST_DIFF_TYPE(int2, uint2)
ABS_TEST_DIFF_TYPE(int3, uint3)
ABS_TEST_DIFF_TYPE(int4, uint4)
ABS_TEST_DIFF_TYPE(int8, uint8)
ABS_TEST_DIFF_TYPE(int16, uint16)
ABS_TEST_DIFF_TYPE(uint2, uint2)
ABS_TEST_DIFF_TYPE(uint3, uint3)
ABS_TEST_DIFF_TYPE(uint4, uint4)
ABS_TEST_DIFF_TYPE(uint8, uint8)
ABS_TEST_DIFF_TYPE(uint16, uint16)

typedef cl_vec<int64_t, 2> long2;
typedef cl_vec<int64_t, 3> long3;
typedef cl_vec<int64_t, 4> long4;
typedef cl_vec<int64_t, 8> long8;
typedef cl_vec<int64_t, 16> long16;
typedef cl_vec<uint64_t, 2> ulong2;
typedef cl_vec<uint64_t, 3> ulong3;
typedef cl_vec<uint64_t, 4> ulong4;
typedef cl_vec<uint64_t, 8> ulong8;
typedef cl_vec<uint64_t, 16> ulong16;
ABS_TEST_DIFF_TYPE(long2, ulong2)
ABS_TEST_DIFF_TYPE(long3, ulong3)
ABS_TEST_DIFF_TYPE(long4, ulong4)
ABS_TEST_DIFF_TYPE(long8, ulong8)
ABS_TEST_DIFF_TYPE(long16, ulong16)
ABS_TEST_DIFF_TYPE(ulong2, ulong2)
ABS_TEST_DIFF_TYPE(ulong3, ulong3)
ABS_TEST_DIFF_TYPE(ulong4, ulong4)
ABS_TEST_DIFF_TYPE(ulong8, ulong8)
ABS_TEST_DIFF_TYPE(ulong16, ulong16)

typedef cl_vec<char, 2> char2;
typedef cl_vec<char, 3> char3;
typedef cl_vec<char, 4> char4;
typedef cl_vec<char, 8> char8;
typedef cl_vec<char, 16> char16;
typedef cl_vec<unsigned char, 2> uchar2;
typedef cl_vec<unsigned char, 3> uchar3;
typedef cl_vec<unsigned char, 4> uchar4;
typedef cl_vec<unsigned char, 8> uchar8;
typedef cl_vec<unsigned char, 16> uchar16;
ABS_TEST_DIFF_TYPE(char2, uchar2)
ABS_TEST_DIFF_TYPE(char3, uchar3)
ABS_TEST_DIFF_TYPE(char4, uchar4)
ABS_TEST_DIFF_TYPE(char8, uchar8)
ABS_TEST_DIFF_TYPE(char16, uchar16)
ABS_TEST_DIFF_TYPE(uchar2, uchar2)
ABS_TEST_DIFF_TYPE(uchar3, uchar3)
ABS_TEST_DIFF_TYPE(uchar4, uchar4)
ABS_TEST_DIFF_TYPE(uchar8, uchar8)
ABS_TEST_DIFF_TYPE(uchar16, uchar16)


typedef cl_vec<short, 2> short2;
typedef cl_vec<short, 3> short3;
typedef cl_vec<short, 4> short4;
typedef cl_vec<short, 8> short8;
typedef cl_vec<short, 16> short16;
typedef cl_vec<unsigned short, 2> ushort2;
typedef cl_vec<unsigned short, 3> ushort3;
typedef cl_vec<unsigned short, 4> ushort4;
typedef cl_vec<unsigned short, 8> ushort8;
typedef cl_vec<unsigned short, 16> ushort16;
ABS_TEST_DIFF_TYPE(short2, ushort2)
ABS_TEST_DIFF_TYPE(short3, ushort3)
ABS_TEST_DIFF_TYPE(short4, ushort4)
ABS_TEST_DIFF_TYPE(short8, ushort8)
ABS_TEST_DIFF_TYPE(short16, ushort16)
ABS_TEST_DIFF_TYPE(ushort2, ushort2)
ABS_TEST_DIFF_TYPE(ushort3, ushort3)
ABS_TEST_DIFF_TYPE(ushort4, ushort4)
ABS_TEST_DIFF_TYPE(ushort8, ushort8)
ABS_TEST_DIFF_TYPE_END(ushort16, ushort16)