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
18
19 template<typename T>
test_constant(cl_device_id deviceID,cl_context context,cl_command_queue queue,const char * name,std::vector<T> & results,bool (* notEqual)(const T &,const T &)=isNotEqual<T>)20 int test_constant(cl_device_id deviceID, cl_context context,
21 cl_command_queue queue, const char *name,
22 std::vector<T> &results,
23 bool (*notEqual)(const T&, const T&) = isNotEqual<T>)
24 {
25 if(std::string(name).find("double") != std::string::npos) {
26 if(!is_extension_available(deviceID, "cl_khr_fp64")) {
27 log_info("Extension cl_khr_fp64 not supported; skipping double tests.\n");
28 return 0;
29 }
30 }
31 clProgramWrapper prog;
32 cl_int err = get_program_with_il(prog, deviceID, context, name);
33 SPIRV_CHECK_ERROR(err, "Failed to build program");
34
35 clKernelWrapper kernel = clCreateKernel(prog, name, &err);
36 SPIRV_CHECK_ERROR(err, "Failed to create kernel");
37
38 int num = (int)results.size();
39
40 size_t bytes = num * sizeof(T);
41 clMemWrapper mem = clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, NULL, &err);
42 SPIRV_CHECK_ERROR(err, "Failed to create buffer");
43
44 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &mem);
45 SPIRV_CHECK_ERROR(err, "Failed to set kernel argument");
46
47 size_t global = num;
48 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
49 SPIRV_CHECK_ERROR(err, "Failed to enqueue kernel");
50
51 std::vector<T> host(num);
52 err = clEnqueueReadBuffer(queue, mem, CL_TRUE, 0, bytes, &host[0], 0, NULL, NULL);
53 SPIRV_CHECK_ERROR(err, "Failed to copy from cl_buffer");
54
55 for (int i = 0; i < num; i++) {
56 if (notEqual(host[i], results[i])) {
57 log_error("Values do not match at location %d\n", i);
58 return -1;
59 }
60 }
61 return 0;
62 }
63
64 #define TEST_CONSTANT(NAME, type, value) \
65 TEST_SPIRV_FUNC(op_constant_##NAME##_simple) \
66 { \
67 std::vector<type> results(1024, (type)value); \
68 return test_constant(deviceID, context, queue, \
69 "constant_" #NAME "_simple", \
70 results); \
71 } \
72
73 // Boolean tests
74 TEST_CONSTANT(true , cl_int , 1 )
75 TEST_CONSTANT(false , cl_int , 0 )
76
77 // Integer tests
78 TEST_CONSTANT(int , cl_int , 123 )
79 TEST_CONSTANT(uint , cl_uint , 54321 )
80 TEST_CONSTANT(char , cl_char , 20 )
81 TEST_CONSTANT(uchar , cl_uchar , 19 )
82 TEST_CONSTANT(ushort, cl_ushort, 65000 )
83 TEST_CONSTANT(long , cl_long , 34359738368L )
84 TEST_CONSTANT(ulong , cl_ulong , 9223372036854775810UL)
85
86 #ifdef __GNUC__
87 // std::vector<cl_short> is causing compilation errors on GCC 5.3 (works on gcc 4.8)
88 // Needs further investigation
89 TEST_CONSTANT(short , int16_t , 32000 )
90 #else
91 TEST_CONSTANT(short , cl_short , 32000 )
92 #endif
93
94 // Float tests
95 TEST_CONSTANT(float , cl_float , 3.1415927 )
96 TEST_CONSTANT(double , cl_double , 3.141592653589793)
97
TEST_SPIRV_FUNC(op_constant_int4_simple)98 TEST_SPIRV_FUNC(op_constant_int4_simple)
99 {
100 cl_int4 value = {123, 122, 121, 119};
101 std::vector<cl_int4> results(256, value);
102 return test_constant(deviceID, context, queue, "constant_int4_simple", results);
103 }
104
TEST_SPIRV_FUNC(op_constant_int3_simple)105 TEST_SPIRV_FUNC(op_constant_int3_simple)
106 {
107 cl_int3 value = {123, 122, 121, 0};
108 std::vector<cl_int3> results(256, value);
109 return test_constant(deviceID, context, queue, "constant_int3_simple",
110 results, isVectorNotEqual<cl_int3, 3>);
111 }
112
TEST_SPIRV_FUNC(op_constant_struct_int_float_simple)113 TEST_SPIRV_FUNC(op_constant_struct_int_float_simple)
114 {
115 AbstractStruct2<int, float> value = {1024, 3.1415};
116 std::vector<AbstractStruct2<int, float> > results(256, value);
117 return test_constant(deviceID, context, queue, "constant_struct_int_float_simple", results);
118 }
119
TEST_SPIRV_FUNC(op_constant_struct_int_char_simple)120 TEST_SPIRV_FUNC(op_constant_struct_int_char_simple)
121 {
122 AbstractStruct2<int, char> value = {2100483600, 128};
123 std::vector<AbstractStruct2<int, char> > results(256, value);
124 return test_constant(deviceID, context, queue, "constant_struct_int_char_simple", results);
125 }
126
TEST_SPIRV_FUNC(op_constant_struct_struct_simple)127 TEST_SPIRV_FUNC(op_constant_struct_struct_simple)
128 {
129 typedef AbstractStruct2<int, char> CustomType1;
130 typedef AbstractStruct2<cl_int2, CustomType1> CustomType2;
131
132 CustomType1 value1 = {2100483600, 128};
133 cl_int2 intvals = {2100480000, 2100480000};
134 CustomType2 value2 = {intvals, value1};
135
136 std::vector<CustomType2> results(256, value2);
137 return test_constant(deviceID, context, queue, "constant_struct_struct_simple", results);
138 }
139
TEST_SPIRV_FUNC(op_constant_half_simple)140 TEST_SPIRV_FUNC(op_constant_half_simple)
141 {
142 PASSIVE_REQUIRE_FP16_SUPPORT(deviceID);
143 std::vector<cl_float> results(1024, 3.25);
144 return test_constant(deviceID, context, queue,
145 "constant_half_simple",
146 results);
147 }
148