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.
34 #include <LPISupport/InfoItems.hpp>
43 #include <chrono> // Para medir tiempos de ejecución
45 using namespace std::chrono;
46 #include <iomanip> // Para std::setprecision
47 #include <OpenCLIPER/OpenCLIPERDataModel.hpp>
48 #include <../performanceTests/PerformanceTestArrayOpParallel.hpp>
51 using namespace OpenCLIPER;
53 int main (int argc, char* argv[]) {
55 unsigned long numberOfIterations = 10;
56 std::shared_ptr<LPISupport::SampleCollection> pSamples = std::make_shared<LPISupport::SampleCollection>("execution time");
58 // Step 0: get a new OpenCLIPER app
59 std::shared_ptr<CLapp> pCLapp = std::make_shared<CLapp>();
62 PerformanceTestArrayOpParallel* pPerfTest = new PerformanceTestArrayOpParallel(argc, argv);
63 auto pConfigTraits = std::dynamic_pointer_cast<PerformanceTestArrayOpParallel::ConfigTraits>(pPerfTest->getConfigTraits());
64 numberOfIterations = pConfigTraits->repetitions;
67 // Step 1: initialize computing device
68 CLapp::PlatformTraits platformTraits;
69 CLapp::DeviceTraits deviceTraits;
71 deviceTraits.type=CLapp::DEVICE_TYPE_GPU;
72 if (!(pConfigTraits->deviceName.empty())) {
73 deviceTraits.name = pConfigTraits->deviceName;
76 deviceTraits.type=CLapp::DEVICE_TYPE_CPU;
78 deviceTraits.queueProperties = cl::QueueProperties(CL_QUEUE_PROFILING_ENABLE);
79 pCLapp->init(platformTraits,deviceTraits);
81 // Step 2: load OpenCL kernel(s)
82 pCLapp->loadKernels("rss.cl");
84 const unsigned int SHOW_SIZE = 10;
85 const unsigned int PRECISION_DIGITS = 10;
87 cerr << argv[0] << " performance measurement" << std::endl;
88 cout << "Starting program... " << flush;
90 // Step 3: load input data from Matlab file
91 vector<string> matlabVars = {"KData", "SensitivityMaps"};
92 std::shared_ptr<Data> pInputKData(new KData("MRIdataSOS.mat", matlabVars));
94 // Step 4: create output with suitable size
95 std::shared_ptr<Data> pOutputXData(new XData(dynamic_pointer_cast<KData>(pInputKData)));
98 // Set 5: register input and output in our CL app
99 DataHandle inHandle = pCLapp->addData(pInputKData);
100 DataHandle outHandle = pCLapp->addData(pOutputXData);
102 cout << "Done." << endl;
104 // Step 6: create new process bound to our CL app
105 // and set its input/output data sets
106 std::unique_ptr<Process> pProcess(new SimpleMRIReconSOS(pCLapp));
107 pProcess->setInHandle(inHandle);
108 pProcess->setOutHandle(outHandle);
110 // Step 7: initialize process
113 cerr << "Executing " << numberOfIterations << " iteration(s)\n";
114 LPISupport::InfoItems infoItems;
115 cerr << pCLapp->getHWSWInfo().to_string(pConfigTraits->outputFormat);
116 TIME_DIFF_TYPE diffT2T1 = 0;
117 std::stringstream strstr;
118 strstr << setprecision(PRECISION_DIGITS);
119 cout << "Starting product... " << endl;
120 for (unsigned long iteration = 0; iteration < numberOfIterations; iteration++) {
121 cout << "Iteration #" << iteration << std::endl;
123 // Step 7.2 launch process
124 Process::ProfileParameters profileParameters;
125 // Enable gpu profiling
126 profileParameters.profilingEnabled = true;
127 profileParameters.numOfRepetitions = 1;
129 pProcess->launch(profileParameters); // Enable gpu profiling
130 pCLapp->getCommandQueue().finish();
133 TIME_DIFF(diffT2T1, t1, t2);
134 pSamples->appendSample(diffT2T1);
138 // Step 8: get data back from computing device
139 pCLapp->device2Host(outHandle, SyncSource::BUFFER_ONLY);
141 // Step 9: store output data into matlab file
142 auto outputData=dynamic_pointer_cast<XData>(pCLapp->getData(outHandle));
143 outputData->matlabSave("outputFramesSOS.mat", "XData", SyncSource::BUFFER_ONLY);
146 LPISupport::InfoItems infoItemsProfilingGPU;
147 infoItemsProfilingGPU.append(pProcess->getSamplesGPUExecTime()->to_infoItems(PRECISION_DIGITS));
149 cerr << infoItemsProfilingGPU.to_string(LPISupport::InfoItems::OutputFormat::HUMAN);
151 LPISupport::InfoItems infoItemsProfilingGPUAndCPU;
152 infoItemsProfilingGPUAndCPU.append(pProcess->getSamplesGPU_CPUExecTime()->to_infoItems(PRECISION_DIGITS));
153 cerr << infoItemsProfilingGPUAndCPU.to_string(LPISupport::InfoItems::OutputFormat::HUMAN);
155 pConfigTraits->deviceType = pCLapp->getDeviceTypeAsString();
156 pConfigTraits->deviceName = pCLapp->getDeviceVendor() + " " + pCLapp->getDeviceName();
158 pPerfTest->buildTestInfo(pProcess->getSamplesGPU_CPUExecTime());
160 pPerfTest->buildTestInfo(pProcess->getSamplesGPU_CPUExecTime());
162 pPerfTest->saveOrPrint();
165 pProcess.reset(nullptr);
166 pCLapp->delData(inHandle);
167 pCLapp->delData(outHandle);
172 } catch(string msg) {
173 cerr << "Exception caught in main(): " << msg << endl;