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 using half = cl_half;
21
22 template<typename Tv, typename Ts>
test_vector_times_scalar(cl_device_id deviceID,cl_context context,cl_command_queue queue,const char * Tname,std::vector<Tv> & h_lhs,std::vector<Ts> & h_rhs)23 int test_vector_times_scalar(cl_device_id deviceID,
24 cl_context context,
25 cl_command_queue queue,
26 const char *Tname,
27 std::vector<Tv> &h_lhs,
28 std::vector<Ts> &h_rhs)
29 {
30 if(std::string(Tname).find("double") != std::string::npos) {
31 if(!is_extension_available(deviceID, "cl_khr_fp64")) {
32 log_info("Extension cl_khr_fp64 not supported; skipping double tests.\n");
33 return 0;
34 }
35 }
36
37 if (std::string(Tname).find("half") != std::string::npos)
38 {
39 if (!is_extension_available(deviceID, "cl_khr_fp16"))
40 {
41 log_info("Extension cl_khr_fp16 not supported; skipping half "
42 "tests.\n");
43 return 0;
44 }
45 }
46
47 cl_int err = CL_SUCCESS;
48 int num = (int)h_lhs.size();
49 size_t lhs_bytes = num * sizeof(Tv);
50 size_t rhs_bytes = num * sizeof(Ts);
51 size_t res_bytes = lhs_bytes;
52 int vec_size = sizeof(Tv) / sizeof(Ts);
53
54 clMemWrapper lhs = clCreateBuffer(context, CL_MEM_READ_ONLY, lhs_bytes, NULL, &err);
55 SPIRV_CHECK_ERROR(err, "Failed to create lhs buffer");
56
57 err = clEnqueueWriteBuffer(queue, lhs, CL_TRUE, 0, lhs_bytes, &h_lhs[0], 0, NULL, NULL);
58 SPIRV_CHECK_ERROR(err, "Failed to copy to lhs buffer");
59
60 clMemWrapper rhs = clCreateBuffer(context, CL_MEM_READ_ONLY, rhs_bytes, NULL, &err);
61 SPIRV_CHECK_ERROR(err, "Failed to create rhs buffer");
62
63 err = clEnqueueWriteBuffer(queue, rhs, CL_TRUE, 0, rhs_bytes, &h_rhs[0], 0, NULL, NULL);
64 SPIRV_CHECK_ERROR(err, "Failed to copy to rhs buffer");
65
66 std::string kernelStr;
67
68 {
69 std::stringstream kernelStream;
70
71 if (is_double<Ts>::value) {
72 kernelStream << "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
73 } else if (sizeof(Ts) == sizeof(cl_half)) {
74 kernelStream << "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
75 }
76
77 kernelStream << "#define Ts " << Tname << "\n";
78 kernelStream << "#define Tv " << Tname << vec_size << "\n";
79 kernelStream << "__kernel void vector_times_scalar( \n";
80 kernelStream << " __global Tv *out, \n";
81 kernelStream << " const __global Tv *lhs,\n";
82 kernelStream << " const __global Ts *rhs)\n";
83 kernelStream << "{ \n";
84 kernelStream << " int id = get_global_id(0); \n";
85 kernelStream << " out[id] = lhs[id] * rhs[id]; \n";
86 kernelStream << "} \n";
87 kernelStr = kernelStream.str();
88 }
89
90 const char *kernelBuf = kernelStr.c_str();
91
92 std::vector<Tv> h_ref(num);
93 {
94 // Run the cl kernel for reference results
95 clProgramWrapper prog;
96 clKernelWrapper kernel;
97 err = create_single_kernel_helper(context, &prog, &kernel, 1,
98 &kernelBuf, "vector_times_scalar");
99 SPIRV_CHECK_ERROR(err, "Failed to create cl program");
100
101 clMemWrapper ref = clCreateBuffer(context, CL_MEM_READ_WRITE, res_bytes, NULL, &err);
102 SPIRV_CHECK_ERROR(err, "Failed to create ref buffer");
103
104 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &ref);
105 SPIRV_CHECK_ERROR(err, "Failed to set arg 0");
106
107 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &lhs);
108 SPIRV_CHECK_ERROR(err, "Failed to set arg 1");
109
110 err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &rhs);
111 SPIRV_CHECK_ERROR(err, "Failed to set arg 2");
112
113 size_t global = num;
114 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
115 SPIRV_CHECK_ERROR(err, "Failed to enqueue cl kernel");
116
117 err = clEnqueueReadBuffer(queue, ref, CL_TRUE, 0, res_bytes, &h_ref[0], 0, NULL, NULL);
118 SPIRV_CHECK_ERROR(err, "Failed to read from ref");
119 }
120
121 std::string ref = "vector_times_scalar_";
122 ref += Tname;
123 const char *spvName = ref.c_str();
124
125 clProgramWrapper prog;
126 err = get_program_with_il(prog, deviceID, context, spvName);
127 SPIRV_CHECK_ERROR(err, "Failed to build program");
128
129 clKernelWrapper kernel = clCreateKernel(prog, "vector_times_scalar", &err);
130 SPIRV_CHECK_ERROR(err, "Failed to create spv kernel");
131
132 clMemWrapper res = clCreateBuffer(context, CL_MEM_READ_WRITE, res_bytes, NULL, &err);
133 SPIRV_CHECK_ERROR(err, "Failed to create res buffer");
134
135 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &res);
136 SPIRV_CHECK_ERROR(err, "Failed to set arg 0");
137
138 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &lhs);
139 SPIRV_CHECK_ERROR(err, "Failed to set arg 1");
140
141 err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &rhs);
142 SPIRV_CHECK_ERROR(err, "Failed to set arg 2");
143
144 size_t global = num;
145 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
146 SPIRV_CHECK_ERROR(err, "Failed to enqueue cl kernel");
147
148 std::vector<Tv> h_res(num);
149 err = clEnqueueReadBuffer(queue, res, CL_TRUE, 0, res_bytes, &h_res[0], 0, NULL, NULL);
150 SPIRV_CHECK_ERROR(err, "Failed to read from ref");
151
152 for (int i = 0; i < num; i++) {
153 if (h_res[i] != h_ref[i]) {
154 log_error("Values do not match at location %d\n", i);
155 return -1;
156 }
157 }
158 return 0;
159 }
160
161 #define TEST_VECTOR_TIMES_SCALAR(TYPE, N) \
162 TEST_SPIRV_FUNC(op_vector_times_scalar_##TYPE) \
163 { \
164 if (sizeof(cl_##TYPE) == 2) { \
165 PASSIVE_REQUIRE_FP16_SUPPORT(deviceID); \
166 } \
167 typedef cl_##TYPE##N Tv; \
168 typedef cl_##TYPE Ts; \
169 const int num = 1 << 20; \
170 std::vector<Tv> lhs(num); \
171 std::vector<Ts> rhs(num); \
172 \
173 RandomSeed seed(gRandomSeed); \
174 \
175 for (int i = 0; i < num; i++) { \
176 lhs[i] = genrandReal<cl_##TYPE##N>(seed); \
177 rhs[i] = genrandReal<cl_##TYPE>(seed); \
178 } \
179 \
180 return test_vector_times_scalar<Tv, Ts>(deviceID, \
181 context, queue, \
182 #TYPE, \
183 lhs, rhs); \
184 }
185
186
187 TEST_VECTOR_TIMES_SCALAR(float, 4)
188 TEST_VECTOR_TIMES_SCALAR(double, 4)
189 TEST_VECTOR_TIMES_SCALAR(half, 4)
190