Initial public release.
[OpenCLIPER] / tests / MRIReconMatlabTest.cpp
1 /* Copyright (C) 2018 Federico Simmross Wattenberg,
2  *                    Manuel Rodríguez Cayetano,
3  *                    Javier Royuela del Val,
4  *                    Elena Martín González,
5  *                    Elisa Moya Sáez,
6  *                    Marcos Martín Fernández and
7  *                    Carlos Alberola López
8  *
9  * This file is part of OpenCLIPER.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  *
24  *  Contact:
25  *
26  *  Federico Simmross Wattenberg
27  *  E.T.S.I. Telecomunicación
28  *  Universidad de Valladolid
29  *  Paseo de Belén 15
30  *  47011 Valladolid, Spain.
31  *  fedsim@tel.uva.es
32  */
33 #include <OpenCLIPER/OpenCLIPERDataModel.hpp>
34
35 using namespace OpenCLIPER;
36 int main(int argc, char *argv[]) {
37   // Get a new OpenCLIPER app
38   std::shared_ptr<CLapp> pCLapp = std::make_shared<CLapp>();
39
40   try {
41     // Select CPU as the computing device
42     CLapp::PlatformTraits platformTraits;
43     CLapp::DeviceTraits deviceTraits;
44     deviceTraits.type=CLapp::DEVICE_TYPE_CPU;
45     pCLapp->init(platformTraits,deviceTraits);
46
47     // Load OpenCL kernel(s)
48     std::vector<std::string> kernelFileNames = {"complexElementProd.cl", "xImageSum.cl"};
49     pCLapp->loadKernels(kernelFileNames);
50
51     // Load input data from Matlab file
52     vector<string> matlabVars = {"KData", "SensitivityMapsData", "SamplingMasksData"};
53     std::shared_ptr<Data> pInputKData(new KData("inputFile.mat", matlabVars));
54
55     // Create output with suitable size
56     std::shared_ptr<Data> pOutputXData(new XData(dynamic_pointer_cast<KData>(pInputKData)));
57
58     // Register input and output in our CL app
59     // (data is sent to the computing device automatically)
60     DataHandle inHandle = pCLapp->addData(pInputKData);
61     DataHandle outHandle = pCLapp->addData(pOutputXData);
62
63     // Create new process, set its input/output data sets 
64     // and bind it to our CL app
65     std::unique_ptr<Process> pProcess(new SimpleMRIRecon(pCLapp));
66     pProcess->setInHandle(inHandle);
67     pProcess->setOutHandle(outHandle);
68
69     // Initialize & launch process
70     pProcess->init();
71     pProcess->launch();
72
73     // Get data back from computing device
74     pCLapp->device2Host(outHandle, SyncSource::BUFFER_ONLY);
75
76     // Save output data
77     auto outputData=dynamic_pointer_cast<XData>(pCLapp->getData(outHandle));
78     outputData->save("outputFrames.mat", "XData", SyncSource::BUFFER_ONLY);
79     outputData->matlabSave("outputFrames.mat", "XData", SyncSource::BUFFER_ONLY);
80     // Clean up
81     pProcess.reset(nullptr);
82     pCLapp->delData(inHandle);
83     pCLapp->delData(outHandle);
84     pCLapp = nullptr;
85   } catch (std::exception& e) {
86     std::cerr << "Error: " << e.what() << std::endl;
87   }
88 }