1 /******************************************************************
2 Copyright (c) 2016 The Khronos Group Inc. All Rights Reserved.
3
4 This code is protected by copyright laws and contains material proprietary to the Khronos Group, Inc.
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE that may not be disclosed in whole or in part to
6 third parties, and may not be reproduced, republished, distributed, transmitted, displayed,
7 broadcast or otherwise exploited in any manner without the express prior written permission
8 of Khronos Group. The receipt or possession of this code does not convey any rights to reproduce,
9 disclose, or distribute its contents, or to manufacture, use, or sell anything that it may describe,
10 in whole or in part other than under the terms of the Khronos Adopters Agreement
11 or Khronos Conformance Test Source License Agreement as executed between Khronos and the recipient.
12 ******************************************************************/
13
14 #include "testBase.h"
15 #include "types.hpp"
16
17 #include <sstream>
18 #include <string>
19
20
21 template<typename T>
test_selection_merge(cl_device_id deviceID,cl_context context,cl_command_queue queue,const char * name,const std::vector<T> & h_lhs,const std::vector<T> & h_rhs,const std::vector<T> & h_ref)22 int test_selection_merge(cl_device_id deviceID,
23 cl_context context,
24 cl_command_queue queue,
25 const char *name,
26 const std::vector<T> &h_lhs,
27 const std::vector<T> &h_rhs,
28 const std::vector<T> &h_ref)
29 {
30
31 cl_int err = CL_SUCCESS;
32 int num = (int)h_lhs.size();
33 size_t bytes = num * sizeof(T);
34
35 clMemWrapper lhs = clCreateBuffer(context, CL_MEM_READ_ONLY, bytes, NULL, &err);
36 SPIRV_CHECK_ERROR(err, "Failed to create lhs buffer");
37
38 err = clEnqueueWriteBuffer(queue, lhs, CL_TRUE, 0, bytes, &h_lhs[0], 0, NULL, NULL);
39 SPIRV_CHECK_ERROR(err, "Failed to copy to lhs buffer");
40
41 clMemWrapper rhs = clCreateBuffer(context, CL_MEM_READ_ONLY, bytes, NULL, &err);
42 SPIRV_CHECK_ERROR(err, "Failed to create rhs buffer");
43
44 err = clEnqueueWriteBuffer(queue, rhs, CL_TRUE, 0, bytes, &h_rhs[0], 0, NULL, NULL);
45 SPIRV_CHECK_ERROR(err, "Failed to copy to rhs buffer");
46
47 clProgramWrapper prog;
48 err = get_program_with_il(prog, deviceID, context, name);
49 SPIRV_CHECK_ERROR(err, "Failed to build program");
50
51 clKernelWrapper kernel = clCreateKernel(prog, name, &err);
52 SPIRV_CHECK_ERROR(err, "Failed to create spv kernel");
53
54 clMemWrapper res = clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, NULL, &err);
55 SPIRV_CHECK_ERROR(err, "Failed to create res buffer");
56
57 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &res);
58 SPIRV_CHECK_ERROR(err, "Failed to set arg 0");
59
60 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &lhs);
61 SPIRV_CHECK_ERROR(err, "Failed to set arg 1");
62
63 err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &rhs);
64 SPIRV_CHECK_ERROR(err, "Failed to set arg 2");
65
66 size_t global = num;
67 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
68 SPIRV_CHECK_ERROR(err, "Failed to enqueue cl kernel");
69
70 std::vector<T> h_res(num);
71 err = clEnqueueReadBuffer(queue, res, CL_TRUE, 0, bytes, &h_res[0], 0, NULL, NULL);
72 SPIRV_CHECK_ERROR(err, "Failed to read from ref");
73
74 for (int i = 0; i < num; i++) {
75 if (h_res[i] != h_ref[i]) {
76 log_error("Values do not match at location %d\n", i);
77 return -1;
78 }
79 }
80 return 0;
81 }
82
83 #define TEST_SELECT_IF(control) \
84 TEST_SPIRV_FUNC(op_selection_merge_if_##control) \
85 { \
86 const int num = 1 << 10; \
87 RandomSeed seed(gRandomSeed); \
88 \
89 std::vector<cl_int> lhs(num); \
90 std::vector<cl_int> rhs(num); \
91 std::vector<cl_int> out(num); \
92 \
93 for (int i = 0; i < num; i++) { \
94 lhs[i] = genrand<cl_int>(seed); \
95 rhs[i] = genrand<cl_int>(seed); \
96 out[i] = lhs[i] < rhs[i] ? \
97 (rhs[i] - lhs[i]) : (lhs[i] - rhs[i]); \
98 } \
99 \
100 return test_selection_merge(deviceID, context, queue, \
101 "select_if_" #control, \
102 lhs, rhs, out); \
103 } \
104
105 TEST_SELECT_IF(none)
106 TEST_SELECT_IF(flatten)
107 TEST_SELECT_IF(dont_flatten)
108
109 #define TEST_SELECT_SWITCH(control) \
110 TEST_SPIRV_FUNC(op_selection_merge_swith_##control) \
111 { \
112 const int num = 1 << 10; \
113 RandomSeed seed(gRandomSeed); \
114 \
115 std::vector<cl_uint> lhs(num); \
116 std::vector<cl_uint> rhs(num); \
117 std::vector<cl_uint> out(num); \
118 \
119 for (int i = 0; i < num; i++) { \
120 lhs[i] = genrand<cl_uint>(seed); \
121 rhs[i] = genrand<cl_uint>(seed); \
122 out[i] = (lhs[i] + rhs[i]) % 4; \
123 } \
124 \
125 return test_selection_merge(deviceID, context, queue, \
126 "select_switch_" #control, \
127 lhs, rhs, out); \
128 } \
129
130 TEST_SELECT_SWITCH(none)
131 TEST_SELECT_SWITCH(flatten)
132 TEST_SELECT_SWITCH(dont_flatten)
133