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 template<typename Ts, typename Tv>
test_insert(cl_device_id deviceID,cl_context context,cl_command_queue queue,const char * name,const std::vector<Ts> & h_in,const int n)18 int test_insert(cl_device_id deviceID, cl_context context,
19 cl_command_queue queue, const char *name,
20 const std::vector<Ts> &h_in, const int n)
21 {
22 if(std::string(name).find("double") != std::string::npos) {
23 if(!is_extension_available(deviceID, "cl_khr_fp64")) {
24 log_info("Extension cl_khr_fp64 not supported; skipping double tests.\n");
25 return 0;
26 }
27 }
28
29 if (std::string(name).find("half") != std::string::npos)
30 {
31 if (!is_extension_available(deviceID, "cl_khr_fp16"))
32 {
33 log_info(
34 "Extension cl_khr_fp16 not supported; skipping half tests.\n");
35 return 0;
36 }
37 }
38
39 cl_int err = CL_SUCCESS;
40 clProgramWrapper prog;
41 err = get_program_with_il(prog, deviceID, context, name);
42 SPIRV_CHECK_ERROR(err, "Failed to build program");
43
44 clKernelWrapper kernel = clCreateKernel(prog, name, &err);
45 SPIRV_CHECK_ERROR(err, "Failed to create kernel");
46
47 int num = (int)h_in.size();
48 std::vector<Tv> h_ref(num);
49 std::vector<Tv> h_out(num);
50
51 RandomSeed seed(gRandomSeed);
52 for (int i = 0; i < num; i++) {
53 for (int j = 0; j < n; j++) {
54 h_ref[i].s[j] = h_in[i] + genrand<Ts>(seed);
55 }
56 }
57
58 size_t in_bytes = num * sizeof(Ts);
59 clMemWrapper in = clCreateBuffer(context, CL_MEM_READ_WRITE, in_bytes, NULL, &err);
60 SPIRV_CHECK_ERROR(err, "Failed to create buffer");
61
62 err = clEnqueueWriteBuffer(queue, in, CL_TRUE, 0, in_bytes, &h_in[0], 0, NULL, NULL);
63 SPIRV_CHECK_ERROR(err, "Failed to copy to rhs buffer");
64
65 size_t out_bytes = num * sizeof(Tv);
66 clMemWrapper out = clCreateBuffer(context, CL_MEM_READ_WRITE, out_bytes, NULL, &err);
67 SPIRV_CHECK_ERROR(err, "Failed to create buffer");
68
69 err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &in);
70 SPIRV_CHECK_ERROR(err, "Failed to set kernel argument");
71
72 err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &out);
73 SPIRV_CHECK_ERROR(err, "Failed to set kernel argument");
74
75 for (int k = 0; k < n; k++) {
76
77 // Reset the values in h_out
78 err = clEnqueueWriteBuffer(queue, out, CL_TRUE, 0, out_bytes, &h_ref[0], 0, NULL, NULL);
79 SPIRV_CHECK_ERROR(err, "Failed to copy to cl_buffer");
80
81 err = clSetKernelArg(kernel, 2, sizeof(int), &k);
82 SPIRV_CHECK_ERROR(err, "Failed to set kernel argument");
83
84 size_t global = num;
85 // Kernel should the k'th value of the vector in h_out with h_in
86 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
87 SPIRV_CHECK_ERROR(err, "Failed to enqueue kernel");
88
89 err = clEnqueueReadBuffer(queue, out, CL_TRUE, 0, out_bytes, &h_out[0], 0, NULL, NULL);
90 SPIRV_CHECK_ERROR(err, "Failed to copy from cl_buffer");
91
92 for (int i = 0; i < num; i++) {
93 // Kernel should have replaced k'th value of the vector in h_out with h_in
94 // The remaining values should be unchanged.
95 Tv refVec = h_ref[i];
96 refVec.s[k] = h_in[i];
97 for (int j = 0; j < n; j++) {
98 if (h_out[i].s[j] != refVec.s[j]) {
99 log_error("Values do not match at location %d for vector position %d\n", i, k);
100 return -1;
101 }
102 }
103 }
104 }
105 return 0;
106 }
107
108 #define TEST_VECTOR_INSERT(TYPE, N) \
109 TEST_SPIRV_FUNC(op_vector_##TYPE##N##_insert) \
110 { \
111 if (sizeof(cl_##TYPE) == 2) \
112 { \
113 PASSIVE_REQUIRE_FP16_SUPPORT(deviceID); \
114 } \
115 typedef cl_##TYPE##N Tv; \
116 typedef cl_##TYPE Ts; \
117 const int num = 1 << 20; \
118 std::vector<Ts> in(num); \
119 const char *name = "vector_" #TYPE #N "_insert"; \
120 \
121 RandomSeed seed(gRandomSeed); \
122 \
123 for (int i = 0; i < num; i++) \
124 { \
125 in[i] = genrand<Ts>(seed); \
126 } \
127 \
128 return test_insert<Ts, Tv>(deviceID, context, queue, name, in, N); \
129 }
130
131 TEST_VECTOR_INSERT(half, 8)
132 TEST_VECTOR_INSERT(int, 4)
133 TEST_VECTOR_INSERT(float, 4)
134 TEST_VECTOR_INSERT(long, 2)
135 TEST_VECTOR_INSERT(double, 2)
136 TEST_VECTOR_INSERT(char, 16)
137