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_copy(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_copy(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_COPY(NAME, type, value) \
65 TEST_SPIRV_FUNC(op_copy_##NAME##_simple) \
66 { \
67 std::vector<type> results(1024, (type)value); \
68 return test_copy(deviceID, context, queue, \
69 "copy_" #NAME "_simple", \
70 results); \
71 } \
72
73 // Integer tests
74 TEST_COPY(int , cl_int , 123 )
75 TEST_COPY(uint , cl_uint , 54321 )
76 TEST_COPY(char , cl_char , 20 )
77 TEST_COPY(uchar , cl_uchar , 19 )
78 TEST_COPY(ushort, cl_ushort, 65000 )
79 TEST_COPY(long , cl_long , 34359738368L )
80 TEST_COPY(ulong , cl_ulong , 9223372036854775810UL)
81
82 #ifdef __GNUC__
83 // std::vector<cl_short> is causing compilation errors on GCC 5.3 (works on gcc 4.8)
84 // Needs further investigation
85 TEST_COPY(short , int16_t , 32000 )
86 #else
87 TEST_COPY(short , cl_short , 32000 )
88 #endif
89
90 // Float tests
91 TEST_COPY(float , cl_float , 3.1415927 )
92 TEST_COPY(double , cl_double , 3.141592653589793)
93
TEST_SPIRV_FUNC(op_copy_int4_simple)94 TEST_SPIRV_FUNC(op_copy_int4_simple)
95 {
96 cl_int4 value = {123, 122, 121, 119};
97 std::vector<cl_int4> results(256, value);
98 return test_copy(deviceID, context, queue, "copy_int4_simple", results);
99 }
100
TEST_SPIRV_FUNC(op_copy_int3_simple)101 TEST_SPIRV_FUNC(op_copy_int3_simple)
102 {
103 cl_int3 value = {123, 122, 121, 0};
104 std::vector<cl_int3> results(256, value);
105 return test_copy(deviceID, context, queue, "copy_int3_simple",
106 results, isVectorNotEqual<cl_int3, 3>);
107 }
108
TEST_SPIRV_FUNC(op_copy_struct_int_float_simple)109 TEST_SPIRV_FUNC(op_copy_struct_int_float_simple)
110 {
111 AbstractStruct2<int, float> value = {1024, 3.1415};
112 std::vector<AbstractStruct2<int, float> > results(256, value);
113 return test_copy(deviceID, context, queue, "copy_struct_int_float_simple", results);
114 }
115
TEST_SPIRV_FUNC(op_copy_struct_int_char_simple)116 TEST_SPIRV_FUNC(op_copy_struct_int_char_simple)
117 {
118 AbstractStruct2<int, char> value = {2100483600, 128};
119 std::vector<AbstractStruct2<int, char> > results(256, value);
120 return test_copy(deviceID, context, queue, "copy_struct_int_char_simple", results);
121 }
122
TEST_SPIRV_FUNC(op_copy_struct_struct_simple)123 TEST_SPIRV_FUNC(op_copy_struct_struct_simple)
124 {
125 typedef AbstractStruct2<int, char> CustomType1;
126 typedef AbstractStruct2<cl_int2, CustomType1> CustomType2;
127
128 CustomType1 value1 = {2100483600, 128};
129 cl_int2 intvals = {2100480000, 2100480000};
130 CustomType2 value2 = {intvals, value1};
131
132 std::vector<CustomType2> results(256, value2);
133 return test_copy(deviceID, context, queue, "copy_struct_struct_simple", results);
134 }
135
TEST_SPIRV_FUNC(op_copy_half_simple)136 TEST_SPIRV_FUNC(op_copy_half_simple)
137 {
138 PASSIVE_REQUIRE_FP16_SUPPORT(deviceID);
139 std::vector<cl_float> results(1024, 3.25);
140 return test_copy(deviceID, context, queue,
141 "copy_half_simple",
142 results);
143 }
144