• 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_selection_merge(cl_device_id deviceID,cl_context context,cl_command_queue queue,const char * name,const std::vector<T> & h_in,const std::vector<T> & h_ref,const int rep)22 int test_selection_merge(cl_device_id deviceID,
23                          cl_context context,
24                          cl_command_queue queue,
25                          const char *name,
26                          const std::vector<T> &h_in,
27                          const std::vector<T> &h_ref,
28                          const int rep)
29 {
30 
31     cl_int err = CL_SUCCESS;
32     int num = (int)h_ref.size();
33     size_t bytes = num * sizeof(T);
34     size_t in_bytes = rep * bytes;
35 
36     clMemWrapper in = clCreateBuffer(context, CL_MEM_READ_ONLY, in_bytes, NULL, &err);
37     SPIRV_CHECK_ERROR(err, "Failed to create in buffer");
38 
39     err = clEnqueueWriteBuffer(queue, in, CL_TRUE, 0, in_bytes, &h_in[0], 0, NULL, NULL);
40     SPIRV_CHECK_ERROR(err, "Failed to copy to in buffer");
41 
42     clProgramWrapper prog;
43     err = get_program_with_il(prog, deviceID, context, name);
44     SPIRV_CHECK_ERROR(err, "Failed to build program");
45 
46     clKernelWrapper kernel = clCreateKernel(prog, name, &err);
47     SPIRV_CHECK_ERROR(err, "Failed to create spv kernel");
48 
49     clMemWrapper out = clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, NULL, &err);
50     SPIRV_CHECK_ERROR(err, "Failed to create out buffer");
51 
52     err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &out);
53     SPIRV_CHECK_ERROR(err, "Failed to set arg 0");
54 
55     err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &in);
56     SPIRV_CHECK_ERROR(err, "Failed to set arg 1");
57 
58     err = clSetKernelArg(kernel, 2, sizeof(int), &rep);
59     SPIRV_CHECK_ERROR(err, "Failed to set arg 2");
60 
61     err = clSetKernelArg(kernel, 3, sizeof(int), &num);
62     SPIRV_CHECK_ERROR(err, "Failed to set arg 3");
63 
64     size_t global = num;
65     err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, NULL);
66     SPIRV_CHECK_ERROR(err, "Failed to enqueue cl kernel");
67 
68     std::vector<T> h_out(num);
69     err = clEnqueueReadBuffer(queue, out, CL_TRUE, 0, bytes, &h_out[0], 0, NULL, NULL);
70     SPIRV_CHECK_ERROR(err, "Failed to read from ref");
71 
72     for (int i = 0; i < num; i++) {
73         if (h_out[i] != h_ref[i]) {
74             log_error("Values do not match at location %d\n", i);
75             return -1;
76         }
77     }
78     return 0;
79 }
80 
81 #define TEST_LOOP_BRANCH(control)                                   \
82     TEST_SPIRV_FUNC(op_loop_merge_branch_##control)                 \
83     {                                                               \
84         const int num = 1 << 10;                                    \
85         RandomSeed seed(gRandomSeed);                               \
86                                                                     \
87         int rep = 4;                                                \
88         std::vector<cl_int> in(rep * num);                          \
89         std::vector<cl_int> out(num);                               \
90                                                                     \
91         for (int i = 0; i < num; i++) {                             \
92             int res = 0;                                            \
93             for (int j = 0; j < rep; j++) {                         \
94                 cl_int val = genrand<cl_int>(seed) % 1024;          \
95                 res += val;                                         \
96                 in[j * num + i] = val;                              \
97             }                                                       \
98             out[i] = res;                                           \
99         }                                                           \
100                                                                     \
101         return test_selection_merge(deviceID, context, queue,       \
102                                     "loop_merge_branch_" #control,  \
103                                     in, out, rep);                  \
104     }                                                               \
105 
106 TEST_LOOP_BRANCH(none)
107 TEST_LOOP_BRANCH(unroll)
108 TEST_LOOP_BRANCH(dont_unroll)
109 
110 
111 TEST_LOOP_BRANCH(conditional_none)
112 TEST_LOOP_BRANCH(conditional_unroll)
113 TEST_LOOP_BRANCH(conditional_dont_unroll)
114