blob: 1b26517d9fd2d56135da17b3d63dd509df0293ba [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas7c3b9242018-06-21 19:01:25 +01002 * Copyright (c) 2016-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"
31#include "arm_compute/runtime/NEON/NEFunctions.h"
Georgios Pinitas7c3b9242018-06-21 19:01:25 +010032#include "utils/ImageLoader.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "utils/Utils.h"
34
35using namespace arm_compute;
36using namespace utils;
37
38/** Example demonstrating how to use both CL and NEON functions in the same pipeline
39 *
40 * @param[in] argc Number of arguments
41 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
42 */
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000043class NEONCLScaleMedianGaussianExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000045public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010046 bool do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000048 /** [NEON / OpenCL Interop] */
49 PPMLoader ppm;
50
51 CLScheduler::get().default_init();
52
53 if(argc < 2)
54 {
55 // Print help
56 std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n";
57 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
58 // Create an empty grayscale 640x480 image
59 src.allocator()->init(TensorInfo(640, 480, Format::U8));
60 }
61 else
62 {
63 ppm.open(argv[1]);
64 ppm.init_image(src, Format::U8);
65 }
66
67 TensorInfo scale_median_info(TensorInfo(src.info()->dimension(0) / 2, src.info()->dimension(1) / 2, Format::U8));
68
69 // Configure the temporary and destination images
70 scale_median.allocator()->init(scale_median_info);
71 median_gauss.allocator()->init(scale_median_info);
72 dst.allocator()->init(scale_median_info);
73
74 scale.configure(&src, &scale_median, InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::REPLICATE);
75 median.configure(&scale_median, &median_gauss, BorderMode::REPLICATE);
76 gauss.configure(&median_gauss, &dst, BorderMode::REPLICATE);
77
78 // Allocate all the images
79 src.allocator()->allocate();
80 scale_median.allocator()->allocate();
81 median_gauss.allocator()->allocate();
82 dst.allocator()->allocate();
83
84 // Fill the input image with the content of the PPM image if a filename was provided:
85 if(ppm.is_open())
86 {
87 ppm.fill_image(src);
88 const std::string output_filename = std::string(argv[1]) + "_out.ppm";
89 }
90 /** [NEON / OpenCL Interop] */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010091
92 return true;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000094 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000096 // Enqueue and flush the OpenCL kernel:
97 scale.run();
98
99 // Do a blocking map of the input and output buffers of the NEON function:
100 scale_median.map();
101 median_gauss.map();
102
103 // Run the NEON function:
104 median.run();
105
106 // Unmap the output buffer before it's used again by OpenCL:
107 scale_median.unmap();
108 median_gauss.unmap();
109
110 // Run the final OpenCL function:
111 gauss.run();
112
113 // Make sure all the OpenCL jobs are done executing:
114 CLScheduler::get().sync();
115 }
116 void do_teardown() override
117 {
118 // Save the result to file:
119 if(!output_filename.empty())
120 {
121 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
122 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 }
124
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000125private:
126 CLImage src{}, scale_median{}, median_gauss{}, dst{};
127 CLScale scale{};
128 NEMedian3x3 median{};
129 CLGaussian5x5 gauss{};
130 std::string output_filename{};
131};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000133/** Main program for neon/cl scale median gaussian test
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 *
135 * @param[in] argc Number of arguments
136 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
137 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000138int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000140 return utils::run_example<NEONCLScaleMedianGaussianExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141}