blob: b50481336bc8a565efd7b72a2efd9f83b4724817 [file] [log] [blame]
Manuel Bottinib56c1752020-11-18 17:56:30 +00001/*
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
27#include "arm_compute/core/CL/OpenCL.h"
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000028#include "arm_compute/runtime/CL/CLTunerTypes.h"
29#include "support/StringSupport.h"
30
31#include <ostream>
Manuel Bottinib56c1752020-11-18 17:56:30 +000032
33namespace arm_compute
34{
35/**< OpenCL tuner parameters */
36class CLTuningParams
37{
38public:
39 CLTuningParams(const CLTuningParams &) = default;
40
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000041 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)
Manuel Bottinib56c1752020-11-18 17:56:30 +000043 {
44 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000045 CLTuningParams(cl::NDRange lws, cl_int wbsm = 0)
46 : _lws(lws), _wbsm(wbsm)
Manuel Bottinib56c1752020-11-18 17:56:30 +000047 {
48 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000049
50 CLTuningParams(cl_int wbsm)
51 : CLTuningParams(cl::NullRange, wbsm)
52 {
53 }
54
55 void set_lws(cl::NDRange lws)
Manuel Bottinib56c1752020-11-18 17:56:30 +000056 {
57 _lws = lws;
58 }
59
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000060 cl::NDRange get_lws() const
Manuel Bottinib56c1752020-11-18 17:56:30 +000061 {
62 return _lws;
63 }
64
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000065 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
Manuel Bottinib56c1752020-11-18 17:56:30 +0000127private:
128 cl::NDRange _lws;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000129 cl_int _wbsm;
Manuel Bottinib56c1752020-11-18 17:56:30 +0000130};
131} // namespace arm_compute
132#endif /*ARM_COMPUTE_CLTUNING_PARAMS_H */