blob: 8a2d11814e22f4bdd2e5bd0a31ed40d71ff74bd3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco5ca74092018-02-08 16:21:54 +00002 * Copyright (c) 2017-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 */
24#include "Utils.h"
25
26#include <cctype>
27#include <cerrno>
28#include <iomanip>
29#include <string>
30
31namespace arm_compute
32{
33namespace utils
34{
35namespace
36{
37/* Advance the iterator to the first character which is not a comment
38 *
39 * @param[in,out] fs Stream to drop comments from
40 */
41void discard_comments(std::ifstream &fs)
42{
43 while(fs.peek() == '#')
44 {
45 fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
46 }
47}
48
49/* Advance the string iterator to the next character which is neither a space or a comment
50 *
51 * @param[in,out] fs Stream to drop comments from
52 */
53void discard_comments_and_spaces(std::ifstream &fs)
54{
55 while(true)
56 {
57 discard_comments(fs);
58
59 if(isspace(fs.peek()) == 0)
60 {
61 break;
62 }
63
64 fs.ignore(1);
65 }
66}
67} // namespace
68
Anthony Barbier6db0ff52018-01-05 10:59:12 +000069#ifndef BENCHMARK_EXAMPLES
70int run_example(int argc, char **argv, Example &example)
71{
72 std::cout << "\n"
73 << argv[0] << "\n\n";
74
75 try
76 {
77 example.do_setup(argc, argv);
78 example.do_run();
79 example.do_teardown();
80
81 std::cout << "\nTest passed\n";
82 return 0;
83 }
84#ifdef ARM_COMPUTE_CL
85 catch(cl::Error &err)
86 {
87 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
88 std::cerr << std::endl
89 << "ERROR " << err.what() << "(" << err.err() << ")" << std::endl;
90 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
91 }
92#endif /* ARM_COMPUTE_CL */
93 catch(std::runtime_error &err)
94 {
95 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
96 std::cerr << std::endl
97 << "ERROR " << err.what() << " " << (errno ? strerror(errno) : "") << std::endl;
98 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
99 }
100
101 std::cout << "\nTest FAILED\n";
102
103 return -1;
104}
105#endif /* BENCHMARK_EXAMPLES */
106
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107void draw_detection_rectangle(ITensor *tensor, const DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b)
108{
109 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(tensor, Format::RGB888);
110
111 uint8_t *top = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y)) + tensor->buffer();
112 uint8_t *bottom = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y + rect.height)) + tensor->buffer();
113 uint8_t *left = top;
114 uint8_t *right = tensor->info()->offset_element_in_bytes(Coordinates(rect.x + rect.width, rect.y)) + tensor->buffer();
115 size_t stride = tensor->info()->strides_in_bytes()[Window::DimY];
116
117 for(size_t x = 0; x < rect.width; ++x)
118 {
119 top[0] = r;
120 top[1] = g;
121 top[2] = b;
122 bottom[0] = r;
123 bottom[1] = g;
124 bottom[2] = b;
125
126 top += 3;
127 bottom += 3;
128 }
129
130 for(size_t y = 0; y < rect.height; ++y)
131 {
132 left[0] = r;
133 left[1] = g;
134 left[2] = b;
135 right[0] = r;
136 right[1] = g;
137 right[2] = b;
138
139 left += stride;
140 right += stride;
141 }
142}
143
144std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs)
145{
146 // Check the PPM magic number is valid
147 std::array<char, 2> magic_number{ { 0 } };
148 fs >> magic_number[0] >> magic_number[1];
149 ARM_COMPUTE_ERROR_ON_MSG(magic_number[0] != 'P' || magic_number[1] != '6', "Invalid file type");
150 ARM_COMPUTE_UNUSED(magic_number);
151
152 discard_comments_and_spaces(fs);
153
154 unsigned int width = 0;
155 fs >> width;
156
157 discard_comments_and_spaces(fs);
158
159 unsigned int height = 0;
160 fs >> height;
161
162 discard_comments_and_spaces(fs);
163
164 int max_val = 0;
165 fs >> max_val;
166
167 discard_comments(fs);
168
169 ARM_COMPUTE_ERROR_ON_MSG(isspace(fs.peek()) == 0, "Invalid PPM header");
170 fs.ignore(1);
171
172 return std::make_tuple(width, height, max_val);
173}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100174
175std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs) //NOLINT
176{
177 std::vector<unsigned long> shape; // NOLINT
178
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100179 // Read header
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000180 std::string header = npy::read_header(fs);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100181
182 // Parse header
183 bool fortran_order = false;
184 std::string typestr;
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000185 npy::parse_header(header, typestr, fortran_order, shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100186
187 if(!fortran_order)
188 {
189 std::reverse(shape.begin(), shape.end());
190 }
191
192 return std::make_tuple(shape, fortran_order, typestr);
193}
Gian Marco5ca74092018-02-08 16:21:54 +0000194
195/** This function returns the amount of memory free reading from /proc/meminfo
196 *
197 * @return The free memory in kB
198 */
199uint64_t get_mem_free_from_meminfo()
200{
201 std::string line_attribute;
202 std::ifstream file_meminfo("/proc/meminfo");
203
204 if(file_meminfo.is_open())
205 {
206 while(!(file_meminfo >> line_attribute).fail())
207 {
208 //Test if is the line containing MemFree
209 if(line_attribute == "MemFree:")
210 {
211 uint64_t mem_available;
212 if(!(file_meminfo >> mem_available).fail())
213 {
214 return mem_available;
215 }
216 else
217 {
218 return 0;
219 }
220 }
221 // if it's not MemFree ignore rest of the line
222 file_meminfo.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
223 }
224 }
225 // Nothing found or an error during opening the file
226 return 0;
227}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228} // namespace utils
229} // namespace arm_compute