Compute Library
 23.08
CLTuningParams.h
Go to the documentation of this file.
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 
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 &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)
45  : _lws(lws_x, lws_y, lws_z), _wbsm(wbsm)
46  {
47  }
48  CLTuningParams(cl::NDRange lws, cl_int wbsm = 0)
49  : _lws(lws), _wbsm(wbsm)
50  {
51  }
52 
53  CLTuningParams(cl_int wbsm)
54  : CLTuningParams(cl::NullRange, wbsm)
55  {
56  }
58  {
59  _lws = other._lws;
60  _wbsm = other._wbsm;
61  return *this;
62  }
63 
64  void set_lws(cl::NDRange lws)
65  {
66  _lws = lws;
67  }
68 
69  cl::NDRange get_lws() const
70  {
71  return _lws;
72  }
73 
74  void set_wbsm(cl_int wbsm)
75  {
76  _wbsm = wbsm;
77  }
78 
79  cl_int get_wbsm() const
80  {
81  return _wbsm;
82  }
83 
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 
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 */
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
StringSupport.h
arm_compute::CLTuningParams::get_lws
cl::NDRange get_lws() const
Definition: CLTuningParams.h:69
arm_compute::CLTuningParams::CLTuningParams
CLTuningParams(const CLTuningParams &tuning_params)
Definition: CLTuningParams.h:39
arm_compute::CLTuningParams::CLTuningParams
CLTuningParams(cl_int wbsm)
Definition: CLTuningParams.h:53
arm_compute::CLTuningParams::get_wbsm
cl_int get_wbsm() const
Definition: CLTuningParams.h:79
arm_compute::CLTuningParams::set_lws
void set_lws(cl::NDRange lws)
Definition: CLTuningParams.h:64
arm_compute::test::validation::ss
std::stringstream ss(mlgo_str)
arm_compute::CLTuningParams::CLTuningParams
CLTuningParams(cl::NDRange lws, cl_int wbsm=0)
Definition: CLTuningParams.h:48
arm_compute::CLTuningInfo
Definition: CLTunerTypes.h:43
arm_compute::CLTuningParams
< OpenCL tuner parameters
Definition: CLTuningParams.h:36
OpenCL.h
Wrapper to configure the Khronos OpenCL C++ header.
arm_compute::CLTuningParams::to_string
std::string to_string(CLTuningInfo tuning_info)
Definition: CLTuningParams.h:84
arm_compute::CLTuningParams::set_wbsm
void set_wbsm(cl_int wbsm)
Definition: CLTuningParams.h:74
CLTunerTypes.h
arm_compute::CLTuningParams::operator=
CLTuningParams & operator=(const CLTuningParams &other)
Definition: CLTuningParams.h:57
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::support::cpp11::stoi
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
arm_compute::CLTuningInfo::tune_wbsm
bool tune_wbsm
Flag to tune the batches of work groups distributed to compute units.
Definition: CLTunerTypes.h:46
cl
Definition: ICLTensor.h:33
arm_compute::CLTuningParams::CLTuningParams
CLTuningParams(unsigned int lws_x=0, unsigned int lws_y=0, unsigned int lws_z=0, int wbsm=0)
Definition: CLTuningParams.h:44
arm_compute::CLTuningParams::from_string
bool from_string(CLTuningInfo tuning_info, std::string tuning_params_string)
Definition: CLTuningParams.h:95