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 enum ClKhrs 117 { 118 no_extensions = KhrValue<0>::Mask, 119 has_cl_khr_int64_base_atomics = KhrValue<1>::Mask, 120 has_cl_khr_int64_extended_atomics = KhrValue<2>::Mask, 121 has_cl_khr_3d_image_writes = KhrValue<3>::Mask, 122 has_cl_khr_fp16 = KhrValue<4>::Mask, 123 has_cl_khr_gl_sharing = KhrValue<5>::Mask, 124 has_cl_khr_gl_event = KhrValue<6>::Mask, 125 has_cl_khr_d3d10_sharing = KhrValue<7>::Mask, 126 has_cl_khr_dx9_media_sharing = KhrValue<8>::Mask, 127 has_cl_khr_d3d11_sharing = KhrValue<9>::Mask, 128 has_cl_khr_depth_images = KhrValue<10>::Mask, 129 has_cl_khr_gl_depth_images = KhrValue<11>::Mask, 130 has_cl_khr_gl_msaa_sharing = KhrValue<12>::Mask, 131 has_cl_khr_image2d_from_buffer = KhrValue<13>::Mask, 132 has_cl_khr_initialize_memory = KhrValue<14>::Mask, 133 has_cl_khr_context_abort = KhrValue<15>::Mask, 134 has_cl_khr_spir = KhrValue<16>::Mask, 135 has_cl_khr_fp64 = KhrValue<17>::Mask, 136 has_cl_khr_global_int32_base_atomics = KhrValue<18>::Mask, 137 has_cl_khr_global_int32_extended_atomics = KhrValue<19>::Mask, 138 has_cl_khr_local_int32_base_atomics = KhrValue<20>::Mask, 139 has_cl_khr_local_int32_extended_atomics = KhrValue<21>::Mask, 140 has_cl_khr_byte_addressable_store = KhrValue<22>::Mask, 141 has_cles_khr_int64 = KhrValue<23>::Mask, 142 has_cles_khr_2d_image_array_writes = KhrValue<24>::Mask, 143 }; 144 145 size_t m_extVector; 146 }; 147 148 std::ostream& operator<<(std::ostream& os, OclExtensions ext); 149 150 /* 151 * Indicates whether a given test needs KHR extension. 152 */ 153 154 class DataRow; 155 156 class DataTable 157 { 158 std::vector<DataRow*> m_rows; 159 public: 160 size_t getNumRows() const; 161 void addTableRow(DataRow*); 162 const DataRow& operator[](int index)const; 163 DataRow& operator[](int index); 164 }; 165 166 class KhrSupport 167 { 168 public: 169 static const KhrSupport* get(const std::string& csvFile); 170 DataRow* parseLine(const std::string&); 171 OclExtensions getRequiredExtensions(const char* suite, const char* test) const; 172 cl_bool isImagesRequired(const char* suite, const char* test) const; 173 cl_bool isImages3DRequired(const char* suite, const char* test) const; 174 175 private: 176 static const int SUITE_INDEX = 0; 177 static const int TEST_INDEX = 1; 178 static const int EXT_INDEX = 2; 179 static const int IMAGES_INDEX = 3; 180 static const int IMAGES_3D_INDEX = 4; 181 182 void parseCSV(std::fstream&); 183 184 DataTable m_dt; 185 static KhrSupport* m_instance; 186 }; 187 188 class DataRow 189 { 190 std::vector<std::string> m_row; 191 DataRow() {} 192 public: 193 const std::string& operator[](int)const; 194 std::string& operator[](int); 195 196 friend DataRow* KhrSupport::parseLine(const std::string&); 197 }; 198 199 /* 200 * Generates data for the given kernel. 201 * Parameters: 202 * context - The context of the kernel. 203 * kernel - The kernel to which arguments will be generated 204 * ws(OUT) - generated work size info. 205 * res(OUT)- generated test results. 206 */ 207 void generate_kernel_data(cl_context context, cl_kernel kernel, 208 WorkSizeInfo &ws, TestResult& res); 209 210 void run_kernel(cl_kernel kernel, cl_command_queue queue, WorkSizeInfo &ws, TestResult& result); 211 bool compare_results(const TestResult& lhs, const TestResult& rhs, float ulps); 212 213 #endif 214