1 /* 2 * Copyright (c) 2020-2022 Arm Limited. 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #ifndef ARM_COMPUTE_CLTUNING_PARAMS_H 25 #define ARM_COMPUTE_CLTUNING_PARAMS_H 26 27 #include "arm_compute/core/CL/OpenCL.h" 28 #include "arm_compute/runtime/CL/CLTunerTypes.h" 29 #include "support/StringSupport.h" 30 31 #include <ostream> 32 33 namespace arm_compute 34 { 35 /**< OpenCL tuner parameters */ 36 class CLTuningParams 37 { 38 public: CLTuningParams(const CLTuningParams & tuning_params)39 CLTuningParams(const CLTuningParams &tuning_params) 40 : _lws(tuning_params._lws), _wbsm(tuning_params._wbsm) 41 { 42 } 43 44 CLTuningParams(unsigned int lws_x = 0, unsigned int lws_y = 0, unsigned int lws_z = 0, int wbsm = 0) _lws(lws_x,lws_y,lws_z)45 : _lws(lws_x, lws_y, lws_z), _wbsm(wbsm) 46 { 47 } 48 CLTuningParams(cl::NDRange lws, cl_int wbsm = 0) _lws(lws)49 : _lws(lws), _wbsm(wbsm) 50 { 51 } 52 CLTuningParams(cl_int wbsm)53 CLTuningParams(cl_int wbsm) 54 : CLTuningParams(cl::NullRange, wbsm) 55 { 56 } 57 CLTuningParams& operator=(const CLTuningParams &other) 58 { 59 _lws = other._lws; 60 _wbsm = other._wbsm; 61 return *this; 62 } 63 set_lws(cl::NDRange lws)64 void set_lws(cl::NDRange lws) 65 { 66 _lws = lws; 67 } 68 get_lws()69 cl::NDRange get_lws() const 70 { 71 return _lws; 72 } 73 set_wbsm(cl_int wbsm)74 void set_wbsm(cl_int wbsm) 75 { 76 _wbsm = wbsm; 77 } 78 get_wbsm()79 cl_int get_wbsm() const 80 { 81 return _wbsm; 82 } 83 to_string(CLTuningInfo tuning_info)84 std::string to_string(CLTuningInfo tuning_info) 85 { 86 std::string tuning_params_string = ""; 87 tuning_params_string += ";" + support::cpp11::to_string(_lws[0]) + ";" + support::cpp11::to_string(_lws[1]) + ";" + support::cpp11::to_string(_lws[2]); 88 if(tuning_info.tune_wbsm) 89 { 90 tuning_params_string += ";" + support::cpp11::to_string(_wbsm); 91 } 92 return tuning_params_string; 93 } 94 from_string(CLTuningInfo tuning_info,std::string tuning_params_string)95 bool from_string(CLTuningInfo tuning_info, std::string tuning_params_string) 96 { 97 std::replace(tuning_params_string.begin(), tuning_params_string.end(), ';', ' '); 98 std::vector<std::string> array; 99 std::stringstream ss(tuning_params_string); 100 std::string temp; 101 while(ss >> temp) 102 { 103 array.push_back(temp); 104 } 105 // Read 3 values for lws 106 if(array.size() < 3) 107 { 108 return false; 109 } 110 const unsigned int lws_0 = support::cpp11::stoi(array[0]); 111 const unsigned int lws_1 = support::cpp11::stoi(array[1]); 112 const unsigned int lws_2 = support::cpp11::stoi(array[2]); 113 if(lws_0 == 0 && lws_1 == 0 && lws_2 == 0) 114 { 115 // If lws values are 0, cl::NullRange has to be used 116 // otherwise the lws object will be badly created 117 _lws = cl::NullRange; 118 } 119 else 120 { 121 _lws = cl::NDRange(lws_0, lws_1, lws_2); 122 } 123 array.erase(array.begin(), array.begin() + 3); 124 if(tuning_info.tune_wbsm) 125 { 126 if(array.size() < 1) 127 { 128 return false; 129 } 130 _wbsm = support::cpp11::stoi(array[0]); 131 array.erase(array.begin()); 132 } 133 return true; 134 } 135 136 private: 137 cl::NDRange _lws; 138 cl_int _wbsm; 139 }; 140 } // namespace arm_compute 141 #endif /*ARM_COMPUTE_CLTUNING_PARAMS_H */ 142