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