blob: a876fad112dbb0d213abede760421a2b09886c6c [file] [log] [blame]
Manuel Bottinib56c1752020-11-18 17:56:30 +00001/*
Pablo Marquez Tellodc732462022-10-24 14:43:08 +01002 * Copyright (c) 2020-2022 Arm Limited.
Manuel Bottinib56c1752020-11-18 17:56:30 +00003 *
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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000030#include "support/StringSupport.h"
31
32#include <ostream>
Manuel Bottinib56c1752020-11-18 17:56:30 +000033
34namespace arm_compute
35{
36/**< OpenCL tuner parameters */
37class CLTuningParams
38{
39public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 CLTuningParams(const CLTuningParams &tuning_params) : _lws(tuning_params._lws), _wbsm(tuning_params._wbsm)
Pablo Marquez Tellodc732462022-10-24 14:43:08 +010041 {
42 }
Manuel Bottinib56c1752020-11-18 17:56:30 +000043
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000044 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)
Manuel Bottinib56c1752020-11-18 17:56:30 +000046 {
47 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 CLTuningParams(cl::NDRange lws, cl_int wbsm = 0) : _lws(lws), _wbsm(wbsm)
Manuel Bottinib56c1752020-11-18 17:56:30 +000049 {
50 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000051
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 CLTuningParams(cl_int wbsm) : CLTuningParams(cl::NullRange, wbsm)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000053 {
54 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 CLTuningParams &operator=(const CLTuningParams &other)
Pablo Marquez Tellodc732462022-10-24 14:43:08 +010056 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 _lws = other._lws;
Pablo Marquez Tellodc732462022-10-24 14:43:08 +010058 _wbsm = other._wbsm;
59 return *this;
60 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000061
62 void set_lws(cl::NDRange lws)
Manuel Bottinib56c1752020-11-18 17:56:30 +000063 {
64 _lws = lws;
65 }
66
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000067 cl::NDRange get_lws() const
Manuel Bottinib56c1752020-11-18 17:56:30 +000068 {
69 return _lws;
70 }
71
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000072 void set_wbsm(cl_int wbsm)
73 {
74 _wbsm = wbsm;
75 }
76
77 cl_int get_wbsm() const
78 {
79 return _wbsm;
80 }
81
82 std::string to_string(CLTuningInfo tuning_info)
83 {
84 std::string tuning_params_string = "";
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 tuning_params_string += ";" + support::cpp11::to_string(_lws[0]) + ";" + support::cpp11::to_string(_lws[1]) +
86 ";" + support::cpp11::to_string(_lws[2]);
87 if (tuning_info.tune_wbsm)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000088 {
89 tuning_params_string += ";" + support::cpp11::to_string(_wbsm);
90 }
91 return tuning_params_string;
92 }
93
94 bool from_string(CLTuningInfo tuning_info, std::string tuning_params_string)
95 {
96 std::replace(tuning_params_string.begin(), tuning_params_string.end(), ';', ' ');
97 std::vector<std::string> array;
98 std::stringstream ss(tuning_params_string);
99 std::string temp;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 while (ss >> temp)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000101 {
102 array.push_back(temp);
103 }
104 // Read 3 values for lws
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 if (array.size() < 3)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000106 {
107 return false;
108 }
109 const unsigned int lws_0 = support::cpp11::stoi(array[0]);
110 const unsigned int lws_1 = support::cpp11::stoi(array[1]);
111 const unsigned int lws_2 = support::cpp11::stoi(array[2]);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 if (lws_0 == 0 && lws_1 == 0 && lws_2 == 0)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000113 {
114 // If lws values are 0, cl::NullRange has to be used
115 // otherwise the lws object will be badly created
116 _lws = cl::NullRange;
117 }
118 else
119 {
120 _lws = cl::NDRange(lws_0, lws_1, lws_2);
121 }
122 array.erase(array.begin(), array.begin() + 3);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (tuning_info.tune_wbsm)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000124 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (array.size() < 1)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000126 {
127 return false;
128 }
129 _wbsm = support::cpp11::stoi(array[0]);
130 array.erase(array.begin());
131 }
132 return true;
133 }
134
Manuel Bottinib56c1752020-11-18 17:56:30 +0000135private:
136 cl::NDRange _lws;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000137 cl_int _wbsm;
Manuel Bottinib56c1752020-11-18 17:56:30 +0000138};
139} // namespace arm_compute
140#endif /*ARM_COMPUTE_CLTUNING_PARAMS_H */