blob: 27c063cbc90913fe7520d32beb72d5508f40c37e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Anthony Barbier15d5ac82017-07-17 15:22:17 +010024#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
25#error "This example needs to be built with -DARM_COMPUTE_CL"
26#endif /* ARM_COMPUTE_CL */
27
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010030#include "arm_compute/runtime/CL/functions/CLGaussian5x5.h"
31#include "arm_compute/runtime/CL/functions/CLMedian3x3.h"
32#include "arm_compute/runtime/CL/functions/CLScale.h"
Georgios Pinitas7c3b9242018-06-21 19:01:25 +010033#include "utils/ImageLoader.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "utils/Utils.h"
35
36using namespace arm_compute;
37using namespace utils;
38
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000039class CLEventsExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010042 bool do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000044 /** [OpenCL events] **/
45 PPMLoader ppm;
46 constexpr int scale_factor = 2;
47
48 CLScheduler::get().default_init();
49
50 if(argc < 2)
51 {
52 // Print help
53 std::cout << "Usage: ./build/cl_events [input_image.ppm]\n\n";
54 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
55 // Create an empty grayscale 640x480 image
56 src.allocator()->init(TensorInfo(640, 480, Format::U8));
57 }
58 else
59 {
60 ppm.open(argv[1]);
61 ppm.init_image(src, Format::U8);
62 }
63
64 TensorInfo dst_info(src.info()->dimension(0) / scale_factor, src.info()->dimension(1) / scale_factor, Format::U8);
65
66 // Configure the temporary and destination images
67 dst.allocator()->init(dst_info);
68 tmp_scale_median.allocator()->init(dst_info);
69 tmp_median_gauss.allocator()->init(dst_info);
70
71 //Configure the functions:
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010072 scale.configure(&src, &tmp_scale_median, ScaleKernelInfo{ InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::REPLICATE });
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000073 median.configure(&tmp_scale_median, &tmp_median_gauss, BorderMode::REPLICATE);
74 gauss.configure(&tmp_median_gauss, &dst, BorderMode::REPLICATE);
75
76 // Allocate all the images
77 src.allocator()->allocate();
78 dst.allocator()->allocate();
79 tmp_scale_median.allocator()->allocate();
80 tmp_median_gauss.allocator()->allocate();
81
82 // Fill the input image with the content of the PPM image if a filename was provided:
83 if(ppm.is_open())
84 {
85 ppm.fill_image(src);
86 output_filename = std::string(argv[1]) + "_out.ppm";
87 }
88 /** [OpenCL events] **/
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010089
90 return true;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000092 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000094 // Enqueue and flush the scale OpenCL kernel:
95 scale.run();
96 // Create a synchronisation event between scale and median:
97 cl::Event scale_event = CLScheduler::get().enqueue_sync_event();
98 // Enqueue and flush the median OpenCL kernel:
99 median.run();
100 // Enqueue and flush the Gaussian OpenCL kernel:
101 gauss.run();
102
103 //Make sure all the OpenCL jobs are done executing:
104 scale_event.wait(); // Block until Scale is done executing (Median3x3 and Gaussian5x5 might still be running)
105 CLScheduler::get().sync(); // Block until Gaussian5x5 is done executing
106 }
107 void do_teardown() override
108 {
109 // Save the result to file:
110 if(!output_filename.empty())
111 {
112 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
113 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 }
115
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000116private:
117 CLImage src{}, tmp_scale_median{}, tmp_median_gauss{}, dst{};
118 CLScale scale{};
119 CLMedian3x3 median{};
120 CLGaussian5x5 gauss{};
121 std::string output_filename{};
122};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
124/** Main program for convolution test
125 *
126 * @param[in] argc Number of arguments
127 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
128 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000129int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000131 return utils::run_example<CLEventsExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132}