blob: 768f620622b383d3b7d4c0629e6892e2967557a6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 */
24#define ARM_COMPUTE_CL /* So that OpenCL exceptions get caught too */
25#include "arm_compute/core/Types.h"
26#include "arm_compute/runtime/CL/CLFunctions.h"
27#include "arm_compute/runtime/CL/CLScheduler.h"
28#include "utils/Utils.h"
29
30using namespace arm_compute;
31using namespace utils;
32
33void main_cl_events(int argc, const char **argv)
34{
35 /** [OpenCL events] **/
36 PPMLoader ppm;
37 CLImage src, tmp_scale_median, tmp_median_gauss, dst;
38 constexpr int scale_factor = 2;
39
40 CLScheduler::get().default_init();
41
42 if(argc < 2)
43 {
44 // Print help
45 std::cout << "Usage: ./build/cl_events [input_image.ppm]\n\n";
46 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
47 // Create an empty grayscale 640x480 image
48 src.allocator()->init(TensorInfo(640, 480, Format::U8));
49 }
50 else
51 {
52 ppm.open(argv[1]);
53 ppm.init_image(src, Format::U8);
54 }
55
56 // Declare and configure the functions to create the following pipeline: scale -> median -> gauss
57 CLScale scale;
58 CLMedian3x3 median;
59 CLGaussian5x5 gauss;
60
61 TensorInfo dst_info(src.info()->dimension(0) / scale_factor, src.info()->dimension(1) / scale_factor, Format::U8);
62
63 // Configure the temporary and destination images
64 dst.allocator()->init(dst_info);
65 tmp_scale_median.allocator()->init(dst_info);
66 tmp_median_gauss.allocator()->init(dst_info);
67
68 //Configure the functions:
69 scale.configure(&src, &tmp_scale_median, InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::REPLICATE);
70 median.configure(&tmp_scale_median, &tmp_median_gauss, BorderMode::REPLICATE);
71 gauss.configure(&tmp_median_gauss, &dst, BorderMode::REPLICATE);
72
73 // Allocate all the images
74 src.allocator()->allocate();
75 dst.allocator()->allocate();
76 tmp_scale_median.allocator()->allocate();
77 tmp_median_gauss.allocator()->allocate();
78 // Fill the input image with the content of the PPM image if a filename was provided:
79 if(ppm.is_open())
80 {
81 ppm.fill_image(src);
82 }
83
84 // Enqueue and flush the scale OpenCL kernel:
85 scale.run();
86 // Create a synchronisation event between scale and median:
87 cl::Event scale_event = CLScheduler::get().enqueue_sync_event();
88 // Enqueue and flush the median OpenCL kernel:
89 median.run();
90 // Enqueue and flush the Gaussian OpenCL kernel:
91 gauss.run();
92
93 //Make sure all the OpenCL jobs are done executing:
94 scale_event.wait(); // Block until Scale is done executing (Median3x3 and Gaussian5x5 might still be running)
95 CLScheduler::get().sync(); // Block until Gaussian5x5 is done executing
96
97 // Save the result to file:
98 if(ppm.is_open())
99 {
100 const std::string output_filename = std::string(argv[1]) + "_out.ppm";
101 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
102 }
103 /** [OpenCL events] **/
104}
105
106/** Main program for convolution test
107 *
108 * @param[in] argc Number of arguments
109 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
110 */
111int main(int argc, const char **argv)
112{
113 return utils::run_example(argc, argv, main_cl_events);
114}