1*6467f958SSadaf Ebrahimi // 2*6467f958SSadaf Ebrahimi // Copyright (c) 2017-2019 The Khronos Group Inc. 3*6467f958SSadaf Ebrahimi // 4*6467f958SSadaf Ebrahimi // Licensed under the Apache License, Version 2.0 (the "License"); 5*6467f958SSadaf Ebrahimi // you may not use this file except in compliance with the License. 6*6467f958SSadaf Ebrahimi // You may obtain a copy of the License at 7*6467f958SSadaf Ebrahimi // 8*6467f958SSadaf Ebrahimi // http://www.apache.org/licenses/LICENSE-2.0 9*6467f958SSadaf Ebrahimi // 10*6467f958SSadaf Ebrahimi // Unless required by applicable law or agreed to in writing, software 11*6467f958SSadaf Ebrahimi // distributed under the License is distributed on an "AS IS" BASIS, 12*6467f958SSadaf Ebrahimi // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*6467f958SSadaf Ebrahimi // See the License for the specific language governing permissions and 14*6467f958SSadaf Ebrahimi // limitations under the License. 15*6467f958SSadaf Ebrahimi // 16*6467f958SSadaf Ebrahimi #ifndef _testHarness_h 17*6467f958SSadaf Ebrahimi #define _testHarness_h 18*6467f958SSadaf Ebrahimi 19*6467f958SSadaf Ebrahimi #include "clImageHelper.h" 20*6467f958SSadaf Ebrahimi #include <string> 21*6467f958SSadaf Ebrahimi #include <sstream> 22*6467f958SSadaf Ebrahimi 23*6467f958SSadaf Ebrahimi #include <string> 24*6467f958SSadaf Ebrahimi 25*6467f958SSadaf Ebrahimi class Version { 26*6467f958SSadaf Ebrahimi public: Version()27*6467f958SSadaf Ebrahimi Version(): m_major(0), m_minor(0) {} Version(int major,int minor)28*6467f958SSadaf Ebrahimi Version(int major, int minor): m_major(major), m_minor(minor) {} 29*6467f958SSadaf Ebrahimi bool operator>(const Version &rhs) const { return to_int() > rhs.to_int(); } 30*6467f958SSadaf Ebrahimi bool operator<(const Version &rhs) const { return to_int() < rhs.to_int(); } 31*6467f958SSadaf Ebrahimi bool operator<=(const Version &rhs) const 32*6467f958SSadaf Ebrahimi { 33*6467f958SSadaf Ebrahimi return to_int() <= rhs.to_int(); 34*6467f958SSadaf Ebrahimi } 35*6467f958SSadaf Ebrahimi bool operator>=(const Version &rhs) const 36*6467f958SSadaf Ebrahimi { 37*6467f958SSadaf Ebrahimi return to_int() >= rhs.to_int(); 38*6467f958SSadaf Ebrahimi } 39*6467f958SSadaf Ebrahimi bool operator==(const Version &rhs) const 40*6467f958SSadaf Ebrahimi { 41*6467f958SSadaf Ebrahimi return to_int() == rhs.to_int(); 42*6467f958SSadaf Ebrahimi } to_int()43*6467f958SSadaf Ebrahimi int to_int() const { return m_major * 10 + m_minor; } to_string()44*6467f958SSadaf Ebrahimi std::string to_string() const 45*6467f958SSadaf Ebrahimi { 46*6467f958SSadaf Ebrahimi std::stringstream ss; 47*6467f958SSadaf Ebrahimi ss << m_major << "." << m_minor; 48*6467f958SSadaf Ebrahimi return ss.str(); 49*6467f958SSadaf Ebrahimi } 50*6467f958SSadaf Ebrahimi 51*6467f958SSadaf Ebrahimi private: 52*6467f958SSadaf Ebrahimi int m_major; 53*6467f958SSadaf Ebrahimi int m_minor; 54*6467f958SSadaf Ebrahimi }; 55*6467f958SSadaf Ebrahimi 56*6467f958SSadaf Ebrahimi Version get_device_cl_version(cl_device_id device); 57*6467f958SSadaf Ebrahimi 58*6467f958SSadaf Ebrahimi #define ADD_TEST(fn) \ 59*6467f958SSadaf Ebrahimi { \ 60*6467f958SSadaf Ebrahimi test_##fn, #fn, Version(1, 0) \ 61*6467f958SSadaf Ebrahimi } 62*6467f958SSadaf Ebrahimi #define ADD_TEST_VERSION(fn, ver) \ 63*6467f958SSadaf Ebrahimi { \ 64*6467f958SSadaf Ebrahimi test_##fn, #fn, ver \ 65*6467f958SSadaf Ebrahimi } 66*6467f958SSadaf Ebrahimi 67*6467f958SSadaf Ebrahimi #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 68*6467f958SSadaf Ebrahimi 69*6467f958SSadaf Ebrahimi typedef int (*test_function_pointer)(cl_device_id deviceID, cl_context context, 70*6467f958SSadaf Ebrahimi cl_command_queue queue, int num_elements); 71*6467f958SSadaf Ebrahimi 72*6467f958SSadaf Ebrahimi typedef struct test_definition 73*6467f958SSadaf Ebrahimi { 74*6467f958SSadaf Ebrahimi test_function_pointer func; 75*6467f958SSadaf Ebrahimi const char *name; 76*6467f958SSadaf Ebrahimi Version min_version; 77*6467f958SSadaf Ebrahimi } test_definition; 78*6467f958SSadaf Ebrahimi 79*6467f958SSadaf Ebrahimi 80*6467f958SSadaf Ebrahimi typedef enum test_status 81*6467f958SSadaf Ebrahimi { 82*6467f958SSadaf Ebrahimi TEST_PASS = 0, 83*6467f958SSadaf Ebrahimi TEST_FAIL = 1, 84*6467f958SSadaf Ebrahimi TEST_SKIP = 2, 85*6467f958SSadaf Ebrahimi TEST_SKIPPED_ITSELF = -100, 86*6467f958SSadaf Ebrahimi } test_status; 87*6467f958SSadaf Ebrahimi 88*6467f958SSadaf Ebrahimi struct test_harness_config 89*6467f958SSadaf Ebrahimi { 90*6467f958SSadaf Ebrahimi int forceNoContextCreation; 91*6467f958SSadaf Ebrahimi int numElementsToUse; 92*6467f958SSadaf Ebrahimi cl_command_queue_properties queueProps; 93*6467f958SSadaf Ebrahimi unsigned numWorkerThreads; 94*6467f958SSadaf Ebrahimi }; 95*6467f958SSadaf Ebrahimi 96*6467f958SSadaf Ebrahimi extern int gFailCount; 97*6467f958SSadaf Ebrahimi extern int gTestCount; 98*6467f958SSadaf Ebrahimi extern cl_uint gReSeed; 99*6467f958SSadaf Ebrahimi extern cl_uint gRandomSeed; 100*6467f958SSadaf Ebrahimi 101*6467f958SSadaf Ebrahimi // Supply a list of functions to test here. This will allocate a CL device, 102*6467f958SSadaf Ebrahimi // create a context, all that setup work, and then call each function in turn as 103*6467f958SSadaf Ebrahimi // dictatated by the passed arguments. Returns EXIT_SUCCESS iff all tests 104*6467f958SSadaf Ebrahimi // succeeded or the tests were listed, otherwise return EXIT_FAILURE. 105*6467f958SSadaf Ebrahimi extern int runTestHarness(int argc, const char *argv[], int testNum, 106*6467f958SSadaf Ebrahimi test_definition testList[], 107*6467f958SSadaf Ebrahimi int forceNoContextCreation, 108*6467f958SSadaf Ebrahimi cl_command_queue_properties queueProps); 109*6467f958SSadaf Ebrahimi 110*6467f958SSadaf Ebrahimi // Device checking function. See runTestHarnessWithCheck. If this function 111*6467f958SSadaf Ebrahimi // returns anything other than TEST_PASS, the harness exits. 112*6467f958SSadaf Ebrahimi typedef test_status (*DeviceCheckFn)(cl_device_id device); 113*6467f958SSadaf Ebrahimi 114*6467f958SSadaf Ebrahimi // Same as runTestHarness, but also supplies a function that checks the created 115*6467f958SSadaf Ebrahimi // device for required functionality. Returns EXIT_SUCCESS iff all tests 116*6467f958SSadaf Ebrahimi // succeeded or the tests were listed, otherwise return EXIT_FAILURE. 117*6467f958SSadaf Ebrahimi extern int runTestHarnessWithCheck(int argc, const char *argv[], int testNum, 118*6467f958SSadaf Ebrahimi test_definition testList[], 119*6467f958SSadaf Ebrahimi int forceNoContextCreation, 120*6467f958SSadaf Ebrahimi cl_command_queue_properties queueProps, 121*6467f958SSadaf Ebrahimi DeviceCheckFn deviceCheckFn); 122*6467f958SSadaf Ebrahimi 123*6467f958SSadaf Ebrahimi // The command line parser used by runTestHarness to break up parameters into 124*6467f958SSadaf Ebrahimi // calls to callTestFunctions 125*6467f958SSadaf Ebrahimi extern int parseAndCallCommandLineTests(int argc, const char *argv[], 126*6467f958SSadaf Ebrahimi cl_device_id device, int testNum, 127*6467f958SSadaf Ebrahimi test_definition testList[], 128*6467f958SSadaf Ebrahimi const test_harness_config &config); 129*6467f958SSadaf Ebrahimi 130*6467f958SSadaf Ebrahimi // Call this function if you need to do all the setup work yourself, and just 131*6467f958SSadaf Ebrahimi // need the function list called/ managed. 132*6467f958SSadaf Ebrahimi // testList is the data structure that contains test functions and its names 133*6467f958SSadaf Ebrahimi // selectedTestList is an array of integers (treated as bools) which tell 134*6467f958SSadaf Ebrahimi // which function is to be called, 135*6467f958SSadaf Ebrahimi // each element at index i, corresponds to the element in testList at 136*6467f958SSadaf Ebrahimi // index i 137*6467f958SSadaf Ebrahimi // resultTestList is an array of statuses which contain the result of each 138*6467f958SSadaf Ebrahimi // selected test testNum is the number of tests in testList, selectedTestList 139*6467f958SSadaf Ebrahimi // and resultTestList contextProps are used to create a testing context for 140*6467f958SSadaf Ebrahimi // each test deviceToUse and config are all just passed to each 141*6467f958SSadaf Ebrahimi // test function 142*6467f958SSadaf Ebrahimi extern void callTestFunctions(test_definition testList[], 143*6467f958SSadaf Ebrahimi unsigned char selectedTestList[], 144*6467f958SSadaf Ebrahimi test_status resultTestList[], int testNum, 145*6467f958SSadaf Ebrahimi cl_device_id deviceToUse, 146*6467f958SSadaf Ebrahimi const test_harness_config &config); 147*6467f958SSadaf Ebrahimi 148*6467f958SSadaf Ebrahimi // This function is called by callTestFunctions, once per function, to do setup, 149*6467f958SSadaf Ebrahimi // call, logging and cleanup 150*6467f958SSadaf Ebrahimi extern test_status callSingleTestFunction(test_definition test, 151*6467f958SSadaf Ebrahimi cl_device_id deviceToUse, 152*6467f958SSadaf Ebrahimi const test_harness_config &config); 153*6467f958SSadaf Ebrahimi 154*6467f958SSadaf Ebrahimi ///// Miscellaneous steps 155*6467f958SSadaf Ebrahimi 156*6467f958SSadaf Ebrahimi // standard callback function for context pfn_notify 157*6467f958SSadaf Ebrahimi extern void CL_CALLBACK notify_callback(const char *errinfo, 158*6467f958SSadaf Ebrahimi const void *private_info, size_t cb, 159*6467f958SSadaf Ebrahimi void *user_data); 160*6467f958SSadaf Ebrahimi 161*6467f958SSadaf Ebrahimi extern cl_device_type GetDeviceType(cl_device_id); 162*6467f958SSadaf Ebrahimi 163*6467f958SSadaf Ebrahimi // Given a device (most likely passed in by the harness, but not required), will 164*6467f958SSadaf Ebrahimi // attempt to find a DIFFERENT device and return it. Useful for finding another 165*6467f958SSadaf Ebrahimi // device to run multi-device tests against. Note that returning NULL means an 166*6467f958SSadaf Ebrahimi // error was hit, but if no error was hit and the device passed in is the only 167*6467f958SSadaf Ebrahimi // device available, the SAME device is returned, so check! 168*6467f958SSadaf Ebrahimi extern cl_device_id GetOpposingDevice(cl_device_id device); 169*6467f958SSadaf Ebrahimi 170*6467f958SSadaf Ebrahimi Version get_device_spirv_il_version(cl_device_id device); 171*6467f958SSadaf Ebrahimi bool check_device_spirv_il_support(cl_device_id device); 172*6467f958SSadaf Ebrahimi void version_expected_info(const char *test_name, const char *api_name, 173*6467f958SSadaf Ebrahimi const char *expected_version, 174*6467f958SSadaf Ebrahimi const char *device_version); 175*6467f958SSadaf Ebrahimi test_status check_spirv_compilation_readiness(cl_device_id device); 176*6467f958SSadaf Ebrahimi 177*6467f958SSadaf Ebrahimi 178*6467f958SSadaf Ebrahimi extern int gFlushDenormsToZero; // This is set to 1 if the device does not 179*6467f958SSadaf Ebrahimi // support denorms (CL_FP_DENORM) 180*6467f958SSadaf Ebrahimi extern int gInfNanSupport; // This is set to 1 if the device supports infinities 181*6467f958SSadaf Ebrahimi // and NaNs 182*6467f958SSadaf Ebrahimi extern int gIsEmbedded; // This is set to 1 if the device is an embedded device 183*6467f958SSadaf Ebrahimi extern int gHasLong; // This is set to 1 if the device suppots long and ulong 184*6467f958SSadaf Ebrahimi // types in OpenCL C. 185*6467f958SSadaf Ebrahimi extern bool gCoreILProgram; 186*6467f958SSadaf Ebrahimi 187*6467f958SSadaf Ebrahimi extern cl_platform_id getPlatformFromDevice(cl_device_id deviceID); 188*6467f958SSadaf Ebrahimi 189*6467f958SSadaf Ebrahimi #if !defined(__APPLE__) 190*6467f958SSadaf Ebrahimi void memset_pattern4(void *, const void *, size_t); 191*6467f958SSadaf Ebrahimi #endif 192*6467f958SSadaf Ebrahimi 193*6467f958SSadaf Ebrahimi extern void PrintArch(void); 194*6467f958SSadaf Ebrahimi 195*6467f958SSadaf Ebrahimi 196*6467f958SSadaf Ebrahimi #endif // _testHarness_h 197