blob: a9c508ac582abbd364b93c7ab02f1c3e8e29f211 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas7c3b9242018-06-21 19:01:25 +01002 * Copyright (c) 2017-2018 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"
29#include "arm_compute/runtime/CL/CLFunctions.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitas7c3b9242018-06-21 19:01:25 +010031#include "utils/ImageLoader.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "utils/Utils.h"
33
34using namespace arm_compute;
35using namespace utils;
36
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000037class CLEventsExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000039public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010040 bool do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000042 /** [OpenCL events] **/
43 PPMLoader ppm;
44 constexpr int scale_factor = 2;
45
46 CLScheduler::get().default_init();
47
48 if(argc < 2)
49 {
50 // Print help
51 std::cout << "Usage: ./build/cl_events [input_image.ppm]\n\n";
52 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
53 // Create an empty grayscale 640x480 image
54 src.allocator()->init(TensorInfo(640, 480, Format::U8));
55 }
56 else
57 {
58 ppm.open(argv[1]);
59 ppm.init_image(src, Format::U8);
60 }
61
62 TensorInfo dst_info(src.info()->dimension(0) / scale_factor, src.info()->dimension(1) / scale_factor, Format::U8);
63
64 // Configure the temporary and destination images
65 dst.allocator()->init(dst_info);
66 tmp_scale_median.allocator()->init(dst_info);
67 tmp_median_gauss.allocator()->init(dst_info);
68
69 //Configure the functions:
70 scale.configure(&src, &tmp_scale_median, InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::REPLICATE);
71 median.configure(&tmp_scale_median, &tmp_median_gauss, BorderMode::REPLICATE);
72 gauss.configure(&tmp_median_gauss, &dst, BorderMode::REPLICATE);
73
74 // Allocate all the images
75 src.allocator()->allocate();
76 dst.allocator()->allocate();
77 tmp_scale_median.allocator()->allocate();
78 tmp_median_gauss.allocator()->allocate();
79
80 // Fill the input image with the content of the PPM image if a filename was provided:
81 if(ppm.is_open())
82 {
83 ppm.fill_image(src);
84 output_filename = std::string(argv[1]) + "_out.ppm";
85 }
86 /** [OpenCL events] **/
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010087
88 return true;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000090 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000092 // Enqueue and flush the scale OpenCL kernel:
93 scale.run();
94 // Create a synchronisation event between scale and median:
95 cl::Event scale_event = CLScheduler::get().enqueue_sync_event();
96 // Enqueue and flush the median OpenCL kernel:
97 median.run();
98 // Enqueue and flush the Gaussian OpenCL kernel:
99 gauss.run();
100
101 //Make sure all the OpenCL jobs are done executing:
102 scale_event.wait(); // Block until Scale is done executing (Median3x3 and Gaussian5x5 might still be running)
103 CLScheduler::get().sync(); // Block until Gaussian5x5 is done executing
104 }
105 void do_teardown() override
106 {
107 // Save the result to file:
108 if(!output_filename.empty())
109 {
110 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
111 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 }
113
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000114private:
115 CLImage src{}, tmp_scale_median{}, tmp_median_gauss{}, dst{};
116 CLScale scale{};
117 CLMedian3x3 median{};
118 CLGaussian5x5 gauss{};
119 std::string output_filename{};
120};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122/** Main program for convolution test
123 *
124 * @param[in] argc Number of arguments
125 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
126 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000127int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000129 return utils::run_example<CLEventsExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130}