Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017, 2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Anthony Barbier | 15d5ac8 | 2017-07-17 15:22:17 +0100 | [diff] [blame] | 24 | #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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #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 | |
| 33 | using namespace arm_compute; |
| 34 | using namespace utils; |
| 35 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 36 | class CLEventsExample : public Example |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 38 | public: |
| 39 | void do_setup(int argc, char **argv) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 41 | /** [OpenCL events] **/ |
| 42 | PPMLoader ppm; |
| 43 | constexpr int scale_factor = 2; |
| 44 | |
| 45 | CLScheduler::get().default_init(); |
| 46 | |
| 47 | if(argc < 2) |
| 48 | { |
| 49 | // Print help |
| 50 | std::cout << "Usage: ./build/cl_events [input_image.ppm]\n\n"; |
| 51 | std::cout << "No input_image provided, creating a dummy 640x480 image\n"; |
| 52 | // Create an empty grayscale 640x480 image |
| 53 | src.allocator()->init(TensorInfo(640, 480, Format::U8)); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | ppm.open(argv[1]); |
| 58 | ppm.init_image(src, Format::U8); |
| 59 | } |
| 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 | |
| 79 | // Fill the input image with the content of the PPM image if a filename was provided: |
| 80 | if(ppm.is_open()) |
| 81 | { |
| 82 | ppm.fill_image(src); |
| 83 | output_filename = std::string(argv[1]) + "_out.ppm"; |
| 84 | } |
| 85 | /** [OpenCL events] **/ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 87 | void do_run() override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 89 | // Enqueue and flush the scale OpenCL kernel: |
| 90 | scale.run(); |
| 91 | // Create a synchronisation event between scale and median: |
| 92 | cl::Event scale_event = CLScheduler::get().enqueue_sync_event(); |
| 93 | // Enqueue and flush the median OpenCL kernel: |
| 94 | median.run(); |
| 95 | // Enqueue and flush the Gaussian OpenCL kernel: |
| 96 | gauss.run(); |
| 97 | |
| 98 | //Make sure all the OpenCL jobs are done executing: |
| 99 | scale_event.wait(); // Block until Scale is done executing (Median3x3 and Gaussian5x5 might still be running) |
| 100 | CLScheduler::get().sync(); // Block until Gaussian5x5 is done executing |
| 101 | } |
| 102 | void do_teardown() override |
| 103 | { |
| 104 | // Save the result to file: |
| 105 | if(!output_filename.empty()) |
| 106 | { |
| 107 | save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM |
| 108 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 111 | private: |
| 112 | CLImage src{}, tmp_scale_median{}, tmp_median_gauss{}, dst{}; |
| 113 | CLScale scale{}; |
| 114 | CLMedian3x3 median{}; |
| 115 | CLGaussian5x5 gauss{}; |
| 116 | std::string output_filename{}; |
| 117 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
| 119 | /** Main program for convolution test |
| 120 | * |
| 121 | * @param[in] argc Number of arguments |
| 122 | * @param[in] argv Arguments ( [optional] Path to PPM image to process ) |
| 123 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 124 | int main(int argc, char **argv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 126 | return utils::run_example<CLEventsExample>(argc, argv); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | } |