• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef __RUN_SERVICES_H
17 #define __RUN_SERVICES_H
18 
19 #include <string>
20 #include "kernelargs.h"
21 #include "datagen.h"
22 #include <list>
23 
24 void get_cl_file_path(const char *folder, const char *str, std::string &cl_file_path);
25 void get_bc_file_path(const char *folder, const char *str, std::string &bc_file_path, cl_uint size_t_width);
26 void get_h_file_path(const char *folder, const char *str, std::string &h_file_path);
27 void get_kernel_name(const char *test_name, std::string &kernel_name);
28 
29 cl_device_id get_context_device(cl_context context);
30 
31 void create_context_and_queue(cl_device_id device, cl_context *out_context, cl_command_queue *out_queue);
32 cl_program create_program_from_cl(cl_context context, const std::string& file_name);
33 cl_program create_program_from_bc(cl_context context, const std::string& file_name);
34 /**
35  Retrieves the kernel with the given name from the program
36  */
37 cl_kernel create_kernel_helper(cl_program program, const std::string& kernel_name);
38 
39 cl_device_id get_program_device (cl_program program);
40 
41 void generate_kernel_ws( cl_device_id device, cl_kernel kernel, WorkSizeInfo& ws);
42 
43 /**
44  Responsible for holding the result of a single test
45  */
46 class TestResult
47 {
48 public:
TestResult()49     TestResult(){};
50 
kernelArgs()51     KernelArgs& kernelArgs() { return m_kernelArgs; }
52 
kernelArgs()53     const KernelArgs& kernelArgs() const { return m_kernelArgs; }
54 
readToHost(cl_command_queue queue)55     void readToHost(cl_command_queue queue) { m_kernelArgs.readToHost(queue); }
56 
57     /*
58      * Clones this object to a newly heap-allocated (deeply copied) object.
59      */
60     TestResult* clone(cl_context ctx, const WorkSizeInfo& ws, const cl_kernel kernel, const cl_device_id device) const;
61 
62 private:
63     KernelArgs m_kernelArgs;
64 };
65 
66 template <int i>
67 struct KhrValue
68 {
69   enum {Mask = (1 << i)};
70 };
71 
72 template <>
73 struct KhrValue<0>
74 {
75   enum {Mask = 1};
76 };
77 
78 /*
79  * Represents a set of OpenCL extension.
80  */
81 class OclExtensions
82 {
83 public:
84     static OclExtensions getDeviceCapabilities(cl_device_id);
85 
86     static OclExtensions empty();
87 
88     #define STRINIGFY(X) #X
89 
90     #define RETURN_IF_ENUM(S, E) if(S == STRINIGFY(E)) return E
91 
92 
93     static OclExtensions fromString(const std::string&);
94 
95     std::string toString();
96 
97     // Operators
98 
99     // Merges the given extension and this one together, and returns the merged
100     // value.
101     OclExtensions operator|(const OclExtensions&) const;
102 
103 
104     // Indicates whether each extension in this objects also resides in b.
105     bool supports(const OclExtensions& b) const;
106 
107     // Return list of missing extensions
108     OclExtensions get_missing(const OclExtensions& b) const;
109 
110 
111     size_t get() const { return m_extVector; }
112 private:
113 
114     OclExtensions(size_t ext) : m_extVector(ext) {}
115 
116 // Fix a compilation error, since cl_khr_gl_sharing is defined as a macro.
117 #ifdef cl_khr_gl_sharing
118 #undef cl_khr_gl_sharing
119 #endif//cl_khr_gl_sharing
120 
121 #ifdef cl_khr_icd
122 #undef cl_khr_icd
123 #endif//cl_khr_icd
124 
125     enum ClKhrs
126     {
127         no_extensions                 = KhrValue<0>::Mask,
128         cl_khr_int64_base_atomics     = KhrValue<1>::Mask,
129         cl_khr_int64_extended_atomics = KhrValue<2>::Mask,
130         cl_khr_3d_image_writes        = KhrValue<3>::Mask,
131         cl_khr_fp16                   = KhrValue<4>::Mask,
132         cl_khr_gl_sharing             = KhrValue<5>::Mask,
133         cl_khr_gl_event               = KhrValue<6>::Mask,
134         cl_khr_d3d10_sharing          = KhrValue<7>::Mask,
135         cl_khr_dx9_media_sharing      = KhrValue<8>::Mask,
136         cl_khr_d3d11_sharing          = KhrValue<9>::Mask,
137         cl_khr_depth_images           = KhrValue<10>::Mask,
138         cl_khr_gl_depth_images        = KhrValue<11>::Mask,
139         cl_khr_gl_msaa_sharing        = KhrValue<12>::Mask,
140         cl_khr_image2d_from_buffer    = KhrValue<13>::Mask,
141         cl_khr_initialize_memory      = KhrValue<14>::Mask,
142         cl_khr_context_abort          = KhrValue<15>::Mask,
143         cl_khr_spir                   = KhrValue<16>::Mask,
144         cl_khr_fp64                   = KhrValue<17>::Mask,
145         cl_khr_global_int32_base_atomics     = KhrValue<18>::Mask,
146         cl_khr_global_int32_extended_atomics = KhrValue<19>::Mask,
147         cl_khr_local_int32_base_atomics      = KhrValue<20>::Mask,
148         cl_khr_local_int32_extended_atomics  = KhrValue<21>::Mask,
149         cl_khr_byte_addressable_store        = KhrValue<22>::Mask,
150         cles_khr_int64                         = KhrValue<23>::Mask,
151         cles_khr_2d_image_array_writes         = KhrValue<24>::Mask,
152     };
153 
154     size_t m_extVector;
155 };
156 
157 std::ostream& operator<<(std::ostream& os, OclExtensions ext);
158 
159 /*
160  * Indicates whether a given test needs KHR extension.
161  */
162 
163 class DataRow;
164 
165 class DataTable
166 {
167   std::vector<DataRow*> m_rows;
168 public:
169     size_t getNumRows() const;
170     void addTableRow(DataRow*);
171     const DataRow& operator[](int index)const;
172     DataRow& operator[](int index);
173 };
174 
175 class KhrSupport
176 {
177 public:
178   static const KhrSupport* get(const std::string& csvFile);
179   DataRow* parseLine(const std::string&);
180   OclExtensions getRequiredExtensions(const char* suite, const char* test) const;
181   cl_bool isImagesRequired(const char* suite, const char* test) const;
182   cl_bool isImages3DRequired(const char* suite, const char* test) const;
183 
184 private:
185   static const int SUITE_INDEX     = 0;
186   static const int TEST_INDEX      = 1;
187   static const int EXT_INDEX       = 2;
188   static const int IMAGES_INDEX    = 3;
189   static const int IMAGES_3D_INDEX = 4;
190 
191   void parseCSV(std::fstream&);
192 
193   DataTable m_dt;
194   static KhrSupport* m_instance;
195 };
196 
197 class DataRow
198 {
199     std::vector<std::string> m_row;
200     DataRow() {}
201 public:
202     const std::string& operator[](int)const;
203     std::string&      operator[](int);
204 
205     friend DataRow* KhrSupport::parseLine(const std::string&);
206 };
207 
208 /*
209  * Generates data for the given kernel.
210  * Parameters:
211  *   context - The context of the kernel.
212  *   kernel  - The kernel to which arguments will be generated
213  *   ws(OUT) - generated work size info.
214  *   res(OUT)- generated test results.
215  */
216 void generate_kernel_data(cl_context context, cl_kernel kernel,
217                           WorkSizeInfo &ws, TestResult& res);
218 
219 void run_kernel(cl_kernel kernel, cl_command_queue queue, WorkSizeInfo &ws, TestResult& result);
220 bool compare_results(const TestResult& lhs, const TestResult& rhs, float ulps);
221 
222 #endif
223