Compute Library
 21.02
cl_events.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 #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 
28 #include "arm_compute/core/Types.h"
33 #include "utils/ImageLoader.h"
34 #include "utils/Utils.h"
35 
36 using namespace arm_compute;
37 using namespace utils;
38 
39 class CLEventsExample : public Example
40 {
41 public:
42  bool do_setup(int argc, char **argv) override
43  {
44  /** [OpenCL events] **/
45  PPMLoader ppm;
46  constexpr int scale_factor = 2;
47 
49 
50  if(argc < 2)
51  {
52  // Print help
53  std::cout << "Usage: ./build/cl_events [input_image.ppm]\n\n";
54  std::cout << "No input_image provided, creating a dummy 640x480 image\n";
55  // Create an empty grayscale 640x480 image
56  src.allocator()->init(TensorInfo(640, 480, Format::U8));
57  }
58  else
59  {
60  ppm.open(argv[1]);
62  }
63 
64  TensorInfo dst_info(src.info()->dimension(0) / scale_factor, src.info()->dimension(1) / scale_factor, Format::U8);
65 
66  // Configure the temporary and destination images
67  dst.allocator()->init(dst_info);
68  tmp_scale_median.allocator()->init(dst_info);
69  tmp_median_gauss.allocator()->init(dst_info);
70 
71  //Configure the functions:
73  median.configure(&tmp_scale_median, &tmp_median_gauss, BorderMode::REPLICATE);
74  gauss.configure(&tmp_median_gauss, &dst, BorderMode::REPLICATE);
75 
76  // Allocate all the images
77  src.allocator()->allocate();
78  dst.allocator()->allocate();
79  tmp_scale_median.allocator()->allocate();
80  tmp_median_gauss.allocator()->allocate();
81 
82  // Fill the input image with the content of the PPM image if a filename was provided:
83  if(ppm.is_open())
84  {
85  ppm.fill_image(src);
86  output_filename = std::string(argv[1]) + "_out.ppm";
87  }
88  /** [OpenCL events] **/
89 
90  return true;
91  }
92  void do_run() override
93  {
94  // Enqueue and flush the scale OpenCL kernel:
95  scale.run();
96  // Create a synchronisation event between scale and median:
97  cl::Event scale_event = CLScheduler::get().enqueue_sync_event();
98  // Enqueue and flush the median OpenCL kernel:
99  median.run();
100  // Enqueue and flush the Gaussian OpenCL kernel:
101  gauss.run();
102 
103  //Make sure all the OpenCL jobs are done executing:
104  scale_event.wait(); // Block until Scale is done executing (Median3x3 and Gaussian5x5 might still be running)
105  CLScheduler::get().sync(); // Block until Gaussian5x5 is done executing
106  }
107  void do_teardown() override
108  {
109  // 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  }
114  }
115 
116 private:
117  CLImage src{}, tmp_scale_median{}, tmp_median_gauss{}, dst{};
118  CLScale scale{};
119  CLMedian3x3 median{};
120  CLGaussian5x5 gauss{};
121  std::string output_filename{};
122 };
123 
124 /** Main program for convolution test
125  *
126  * @param[in] argc Number of arguments
127  * @param[in] argv Arguments ( [optional] Path to PPM image to process )
128  */
129 int main(int argc, char **argv)
130 {
131  return utils::run_example<CLEventsExample>(argc, argv);
132 }
void save_to_ppm(T &tensor, const std::string &ppm_filename)
Template helper function to save a tensor image to a PPM file.
Definition: Utils.h:547
Basic function to execute median filter.
Definition: CLMedian3x3.h:45
int main(int argc, char **argv)
Main program for convolution test.
Definition: cl_events.cpp:129
void run() override final
Run the kernels contained in the function.
static CLScheduler & get()
Access the scheduler singleton.
void open(const std::string &filename) override
Open an image file and reads its metadata (Width, height)
Definition: ImageLoader.h:381
1 channel, 1 U8 per channel
Output values are defined to match the source pixel whose center is nearest to the sample position...
SimpleTensor< float > src
Definition: DFT.cpp:155
Basic function to run CLScaleKernel.
Definition: CLScale.h:44
Copyright (c) 2017-2021 Arm Limited.
bool is_open() override
Return true if the image file is currently open.
Definition: ImageLoader.h:377
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
cl::Event enqueue_sync_event()
Enqueues a marker into the associated command queue and return the event.
Definition: CLScheduler.cpp:77
Abstract Example class.
Definition: Utils.h:78
void configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info)
Initialize the function&#39;s source, destination, interpolation type and border_mode.
Definition: NEScale.cpp:104
void fill_image(T &image)
Fill an image with the content of the currently open image file.
Definition: ImageLoader.h:177
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:72
Basic function to execute gaussian filter 5x5.
Definition: CLGaussian5x5.h:53
Pixels outside the image are assumed to have the same value as the closest image pixel.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
void default_init(ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr)
Initialises the context and command queue used by the scheduler to default values and sets a default ...
void init_image(T &image, Format format)
Initialise an image&#39;s metadata with the dimensions of the image file currently open.
Definition: ImageLoader.h:161
PPM Image loader concrete implementation.
Definition: ImageLoader.h:367
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41