Compute Library
 22.05
CLTuningParams.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021 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 
29 #include "support/StringSupport.h"
30 
31 #include <ostream>
32 
33 namespace arm_compute
34 {
35 /**< OpenCL tuner parameters */
37 {
38 public:
39  CLTuningParams(const CLTuningParams &) = default;
40 
41  CLTuningParams(unsigned int lws_x = 0, unsigned int lws_y = 0, unsigned int lws_z = 0, int wbsm = 0)
42  : _lws(lws_x, lws_y, lws_z), _wbsm(wbsm)
43  {
44  }
45  CLTuningParams(cl::NDRange lws, cl_int wbsm = 0)
46  : _lws(lws), _wbsm(wbsm)
47  {
48  }
49 
50  CLTuningParams(cl_int wbsm)
51  : CLTuningParams(cl::NullRange, wbsm)
52  {
53  }
54 
55  void set_lws(cl::NDRange lws)
56  {
57  _lws = lws;
58  }
59 
60  cl::NDRange get_lws() const
61  {
62  return _lws;
63  }
64 
65  void set_wbsm(cl_int wbsm)
66  {
67  _wbsm = wbsm;
68  }
69 
70  cl_int get_wbsm() const
71  {
72  return _wbsm;
73  }
74 
75  std::string to_string(CLTuningInfo tuning_info)
76  {
77  std::string tuning_params_string = "";
78  tuning_params_string += ";" + support::cpp11::to_string(_lws[0]) + ";" + support::cpp11::to_string(_lws[1]) + ";" + support::cpp11::to_string(_lws[2]);
79  if(tuning_info.tune_wbsm)
80  {
81  tuning_params_string += ";" + support::cpp11::to_string(_wbsm);
82  }
83  return tuning_params_string;
84  }
85 
86  bool from_string(CLTuningInfo tuning_info, std::string tuning_params_string)
87  {
88  std::replace(tuning_params_string.begin(), tuning_params_string.end(), ';', ' ');
89  std::vector<std::string> array;
90  std::stringstream ss(tuning_params_string);
91  std::string temp;
92  while(ss >> temp)
93  {
94  array.push_back(temp);
95  }
96  // Read 3 values for lws
97  if(array.size() < 3)
98  {
99  return false;
100  }
101  const unsigned int lws_0 = support::cpp11::stoi(array[0]);
102  const unsigned int lws_1 = support::cpp11::stoi(array[1]);
103  const unsigned int lws_2 = support::cpp11::stoi(array[2]);
104  if(lws_0 == 0 && lws_1 == 0 && lws_2 == 0)
105  {
106  // If lws values are 0, cl::NullRange has to be used
107  // otherwise the lws object will be badly created
108  _lws = cl::NullRange;
109  }
110  else
111  {
112  _lws = cl::NDRange(lws_0, lws_1, lws_2);
113  }
114  array.erase(array.begin(), array.begin() + 3);
115  if(tuning_info.tune_wbsm)
116  {
117  if(array.size() < 1)
118  {
119  return false;
120  }
121  _wbsm = support::cpp11::stoi(array[0]);
122  array.erase(array.begin());
123  }
124  return true;
125  }
126 
127 private:
128  cl::NDRange _lws;
129  cl_int _wbsm;
130 };
131 } // namespace arm_compute
132 #endif /*ARM_COMPUTE_CLTUNING_PARAMS_H */
Definition: ICLTensor.h:33
cl::NDRange get_lws() const
bool tune_wbsm
Flag to tune the batches of work groups distributed to compute units.
Definition: CLTunerTypes.h:46
std::string to_string(T &&value)
Convert integer and float values to string.
std::stringstream ss(mlgo_str)
< OpenCL tuner parameters
Copyright (c) 2017-2022 Arm Limited.
void set_lws(cl::NDRange lws)
CLTuningParams(cl::NDRange lws, cl_int wbsm=0)
CLTuningParams(const CLTuningParams &)=default
void set_wbsm(cl_int wbsm)
int stoi(const std::string &str, std::size_t *pos=0, NumericBase base=NumericBase::BASE_10)
Convert string values to integer.
Definition: StringSupport.h:55
std::string to_string(CLTuningInfo tuning_info)
Wrapper to configure the Khronos OpenCL C++ header.
CLTuningParams(unsigned int lws_x=0, unsigned int lws_y=0, unsigned int lws_z=0, int wbsm=0)
bool from_string(CLTuningInfo tuning_info, std::string tuning_params_string)