blob: 24ad7c180eee563df89305855594d96d2b380d53 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbier6db0ff52018-01-05 10:59:12 +00002 * 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 "utils/Utils.h"
32
33using namespace arm_compute;
34using namespace utils;
35
36/** Gaussian 3x3 matrix
37 */
38const int16_t gaussian3x3[] =
39{
40 1, 2, 1,
41 2, 4, 2,
42 1, 2, 1
43};
44
45/** Gaussian 5x5 matrix
46 */
47const int16_t gaussian5x5[] =
48{
49 1, 4, 6, 4, 1,
50 4, 16, 24, 16, 4,
51 6, 24, 36, 24, 6,
52 4, 16, 24, 16, 4,
53 1, 4, 6, 4, 1
54};
55
Anthony Barbier6db0ff52018-01-05 10:59:12 +000056class CLConvolutionExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
Anthony Barbier6db0ff52018-01-05 10:59:12 +000058public:
59 void do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +000061 PPMLoader ppm;
62
63 CLScheduler::get().default_init();
64
65 if(argc < 2)
66 {
67 // Print help
68 std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n";
69 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
70 // Create an empty grayscale 640x480 image
71 src.allocator()->init(TensorInfo(640, 480, Format::U8));
72 }
73 else
74 {
75 ppm.open(argv[1]);
76 ppm.init_image(src, Format::U8);
77 }
78
79 // Configure the temporary and destination images
80 tmp.allocator()->init(*src.info());
81 dst.allocator()->init(*src.info());
82
83 // Apply a Gaussian 3x3 filter to the source image followed by a Gaussian 5x5:
84 conv3x3.configure(&src, &tmp, gaussian3x3, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
85 conv5x5.configure(&tmp, &dst, gaussian5x5, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
86
87 // Allocate all the images
88 src.allocator()->allocate();
89 tmp.allocator()->allocate();
90 dst.allocator()->allocate();
91 // Fill the input image with the content of the PPM image if a filename was provided:
92 if(ppm.is_open())
93 {
94 ppm.fill_image(src);
95 output_filename = std::string(argv[1]) + "_out.ppm";
96 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +000098 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000100 // Execute the functions:
101 conv3x3.run();
102 conv5x5.run();
103
104 // Make sure all the OpenCL jobs are done executing:
105 CLScheduler::get().sync();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000107 void do_teardown() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000109 // 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 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000115 CLImage src{}, tmp{}, dst{};
116 CLConvolution3x3 conv3x3{};
117 CLConvolution5x5 conv5x5{};
118 std::string output_filename{};
119};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
121/** Main program for convolution test
122 *
123 * @param[in] argc Number of arguments
124 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
125 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000126int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127{
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000128 return utils::run_example<CLConvolutionExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129}