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