• 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 
21 template<typename T>
test_phi(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_phi(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 
TEST_SPIRV_FUNC(op_phi_2_blocks)83 TEST_SPIRV_FUNC(op_phi_2_blocks)
84 {
85     const int num = 1 << 10;
86     RandomSeed seed(gRandomSeed);
87 
88     std::vector<cl_int> lhs(num);
89     std::vector<cl_int> rhs(num);
90     std::vector<cl_int> out(num);
91 
92     for (int i = 0; i < num; i++) {
93         lhs[i] = genrand<cl_int>(seed);
94         rhs[i] = genrand<cl_int>(seed);
95         out[i] = lhs[i] < rhs[i] ? (rhs[i] - lhs[i]) : (lhs[i] - rhs[i]);
96     }
97 
98     return test_phi(deviceID, context, queue, "phi_2", lhs, rhs, out);
99 }
100 
TEST_SPIRV_FUNC(op_phi_3_blocks)101 TEST_SPIRV_FUNC(op_phi_3_blocks)
102 {
103     const int num = 1 << 10;
104     RandomSeed seed(gRandomSeed);
105 
106     std::vector<cl_int> lhs(num);
107     std::vector<cl_int> rhs(num);
108     std::vector<cl_int> out(num);
109 
110     for (int i = 0; i < num; i++) {
111         lhs[i] = genrand<cl_int>(seed);
112         rhs[i] = genrand<cl_int>(seed);
113         if (lhs[i] < rhs[i]) {
114             out[i] = lhs[i] < 0 ? -lhs[i] : lhs[i];
115         } else {
116             out[i] = lhs[i] - rhs[i];
117         }
118     }
119 
120     return test_phi(deviceID, context, queue, "phi_3", lhs, rhs, out);
121 }
122 
TEST_SPIRV_FUNC(op_phi_4_blocks)123 TEST_SPIRV_FUNC(op_phi_4_blocks)
124 {
125     const int num = 1 << 10;
126     RandomSeed seed(gRandomSeed);
127 
128     std::vector<cl_int> lhs(num);
129     std::vector<cl_int> rhs(num);
130     std::vector<cl_int> out(num);
131 
132     for (int i = 0; i < num; i++) {
133         lhs[i] = genrand<cl_int>(seed);
134         rhs[i] = genrand<cl_int>(seed);
135         if (lhs[i] < rhs[i]) {
136             out[i] = lhs[i] < 0 ? -lhs[i] : lhs[i];
137         } else {
138             out[i] = rhs[i] < 0 ? -rhs[i] : rhs[i];
139         }
140     }
141 
142     return test_phi(deviceID, context, queue, "phi_4", lhs, rhs, out);
143 }
144