1 /* Copyright (C) 2018 Federico Simmross Wattenberg,
2 * Manuel Rodríguez Cayetano,
3 * Javier Royuela del Val,
4 * Elena Martín González,
6 * Marcos Martín Fernández and
7 * Carlos Alberola López
9 * This file is part of OpenCLIPER.
11 * OpenCLIPER is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 3 of the License.
15 * OpenCLIPER is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with OpenCLIPER; If not, see <http://www.gnu.org/licenses/>.
26 * Federico Simmross Wattenberg
27 * E.T.S.I. Telecomunicación
28 * Universidad de Valladolid
30 * 47011 Valladolid, Spain.
33 // Avoiding compiler errors due to multiple include of header files
37 #include <OpenCLIPER/OpenCLIPERDataModelCommonDefs.hpp>
38 #include <OpenCLIPER/Data.hpp>
39 #include <LPISupport/SampleCollection.hpp>
40 #include <LPISupport/InfoItems.hpp>
42 #include <iostream> // std::cout, std::fixed
43 #include <iomanip> // std::setprecision
45 // Needed for kernels to find includes outside "kernels" subdirectory
46 #define KERNELCOMPILEOPTS "-I../include/"
48 namespace OpenCLIPER {
54 * @brief Class representing common data and behaviour for all the processes in charge of operations on data.
59 Process(std::shared_ptr<CLapp> pCLapp);
63 /// Structure with initialization parameters (can be redefined in the subclass, if needed)
64 struct InitParameters {
66 virtual ~InitParameters() {}
69 /// Structure with launch parameters (can be redefined in the subclass, if needed)
70 struct LaunchParameters {
72 virtual ~LaunchParameters() {}
75 /// Structure with profiling parameters (can be redefined in the subclass, if needed)
76 struct ProfileParameters {
77 /// If profiling is enabled (true)
78 bool profilingEnabled = false;
79 /// Number of kernel execution repetitions
80 unsigned long numOfRepetitions = 1;
82 * @brief Default constructor for struct fields initialization.
84 * @param profEnabled boolean true if profiling is enabled
85 * @param numReps number of repetitions of kernel execution
87 ProfileParameters(bool profEnabled = false, unsigned long numReps = 1):
88 profilingEnabled(profEnabled), numOfRepetitions(numReps) {}
92 * @brief Returns a SampleCollection object containing values of host+device execution times
93 * @returns object of SampleCollection class
95 std::shared_ptr<LPISupport::SampleCollection> getSamplesGPU_CPUExecTime() {
96 return pSamplesGPU_CPUExecutionTime;
100 * @brief Returns a SampleCollection object containing values of device execution times
101 * @returns object of SampleCollection class
103 std::shared_ptr<LPISupport::SampleCollection> getSamplesGPUExecTime() {
104 return pSamplesGPUExecutionTime;
108 * @brief Returns pointer to init parameters
109 * @returns smart shared pointer to InitParameters object
111 virtual const shared_ptr<InitParameters> getInitParameters() const {
112 return pInitParameters;
116 * @brief Returns pointer to launch parameters
117 * @returns smart shared pointer to LaunchParameters object
119 virtual const shared_ptr<LaunchParameters> getLaunchParameters() const {
120 return pLaunchParameters;
123 void setApp(std::shared_ptr<CLapp> pCLapp);
125 void setInHandle(DataHandle pInputDataHandle);
126 void setOutHandle(DataHandle pOutputDataHandle);
129 * @brief Sets initialization parameters for a Process object (this method can be redefined in a Process subclass).
130 * @param[in] p structure with initialization parameters for a Process object
132 virtual void setInitParameters(const shared_ptr<InitParameters>& p) {
137 * @brief Sets launch parameters for a Process object (this method can be redefined in a Process subclass).
138 * @param[in] p structure with launch parameters of a Process object
140 virtual void setLaunchParameters(const shared_ptr<LaunchParameters>& p) {
145 * @brief Method for Process object initialization (intialization is specific of Process subclasses).
147 virtual void init() {checkValidCLapp(this->pCLapp, "Process::init");}
150 * @brief Method that sets OpenCL kernel parameters and executes it (method specific of Process object subclasses)
151 * @param[in] profileParameters parameters related to profiling of kernel execution
153 virtual void launch(ProfileParameters profileParameters = {false, 1}) = 0;
156 * @brief Gets infoItems class variable value
158 * @return the value of infoItems class variable
160 LPISupport::InfoItems getInfoItems() {
166 // Visible for all subclasses
168 void checkValidCLapp(std::shared_ptr<OpenCLIPER::CLapp> pCLapp, string errorMsgPrefix);
171 * @brief Get value of shared pointer to CLapp process assigned to this object
172 * @return shared pointer to CLapp object
174 const std::shared_ptr<CLapp> getApp() const {
179 * @brief Returns a shared pointer to input data
180 * @return shared pointer to input data
182 std::shared_ptr<Data> getInput() {
187 * @brief Returns handle associated to input data
188 * @return handle to input data
190 DataHandle getInHandle() {
195 * @brief Returns a shared pointer to output data
196 * @return shared pointer to output data
198 std::shared_ptr<Data> getOutput() {
203 * @brief Returns handle associated to output data
204 * @return handle to output data
206 DataHandle getOutHandle() {
210 void checkCommonLaunchParameters();
211 void checkXDataLaunchParameters(SyncSource syncSource = SYNCSOURCEDEFAULT);
212 void startProfiling(bool profilingEnabled);
213 void stopProfiling(bool profilingEnabled);
214 void startHostCodeProfiling(bool profilingEnabled);
215 void stopHostCodeProfiling(bool profilingEnabled);
216 void startKernelProfiling(bool profilingEnabled);
217 void stopKernelProfiling(bool profilingEnabled);
218 void buildKernelProfilingInfo(bool profilingEnabled);
219 void getKernelGroupExecutionTimes(std::vector<cl::Event> eventList, std::string itemTitle, std::string totalsTitle);
220 void addGlobalAndLocalWorkItemSizeInfo(cl::NDRange globalSizes, cl::NDRange localSizes, bool profilingEnabled);
222 /// Smart shared pointer to init parameters
223 std::shared_ptr<InitParameters> pInitParameters;
224 /// Smart shared pointer to launch parameters
225 std::shared_ptr<LaunchParameters> pLaunchParameters;
226 /// OpenCL Command queue
227 cl::CommandQueue queue;
228 /// Boolean that shows if selected device supports kernel profiling
229 bool profilingSupported = false;
232 /// Name of the kernel function to be executed
233 std::string kernelName;
234 /// Storage for error strings
236 /// Vector with InfoItems data (list of pairs title, value storing profiling information).
237 LPISupport::InfoItems infoItems;
238 /// Vector with values of GPU execution times
239 std::shared_ptr<LPISupport::SampleCollection> pSamplesGPUExecutionTime;
240 /// Vector with values of GPU+CPU execution times
241 std::shared_ptr<LPISupport::SampleCollection> pSamplesGPU_CPUExecutionTime;
242 /// Vector with events for profiling kernel execuion times
243 std::vector<cl::Event> eventsVector;
245 /// Smart shared pointer to Data object with input data
246 std::shared_ptr<Data> pInputData = nullptr;
247 /// Handle associated to input data
248 DataHandle inHandle = INVALIDDATAHANDLE;
249 /// Smart shared pointer to Data object with output data
250 std::shared_ptr<Data> pOutputData = nullptr;
251 /// Handle associated to output data
252 DataHandle outHandle = INVALIDDATAHANDLE;
253 /// Smart shared pointer to object with OpenCL context
254 std::shared_ptr<CLapp> pCLapp = nullptr;
255 /// Event for kernel execution start (used for profiling kernel execution times).
257 /// Event for kernel execution stop (used for profiling kernel execution times).
259 /// clock at CPU starting execution of host Process code
260 std::chrono::high_resolution_clock::time_point beginCPUExecTime;
261 /// clock at CPU ending execution of host Process code
262 std::chrono::high_resolution_clock::time_point endCPUExecTime;
264 } /* namespace OpenCLIPER */