Anthony Barbier | c9afce5 | 2018-01-26 16:07:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #include "InstrumentsStats.h" |
| 25 | |
| 26 | namespace arm_compute |
| 27 | { |
| 28 | namespace test |
| 29 | { |
| 30 | namespace framework |
| 31 | { |
| 32 | InstrumentsStats::InstrumentsStats(const std::vector<Measurement> &measurements) |
| 33 | : _min(nullptr), _max(nullptr), _median(nullptr), _mean(measurements.begin()->value().is_floating_point), _stddev(0.0) |
| 34 | { |
| 35 | auto cmp_measurements = [](const Measurement & a, const Measurement & b) |
| 36 | { |
| 37 | return a.value() < b.value(); |
| 38 | }; |
| 39 | |
| 40 | auto add_measurements = [](Measurement::Value a, const Measurement & b) |
| 41 | { |
| 42 | return a + b.value(); |
| 43 | }; |
| 44 | |
| 45 | //Calculate min & max |
| 46 | const auto values = std::minmax_element(measurements.begin(), measurements.end(), cmp_measurements); |
| 47 | _min = &(*values.first); |
| 48 | _max = &(*values.second); |
| 49 | |
| 50 | // Calculate the median value |
| 51 | auto copy = measurements; |
| 52 | std::nth_element(copy.begin(), copy.begin() + (copy.size() / 2), copy.end(), cmp_measurements); |
| 53 | _median = ©[copy.size() / 2]; |
| 54 | |
| 55 | Measurement::Value sum_values = std::accumulate(measurements.begin(), measurements.end(), Measurement::Value(_min->value().is_floating_point), add_measurements); |
| 56 | |
| 57 | // Calculate the relative standard deviation |
| 58 | _mean = sum_values / measurements.size(); |
| 59 | std::vector<Measurement::Value> diff(measurements.size(), _min->value().is_floating_point); |
| 60 | std::transform(measurements.begin(), measurements.end(), diff.begin(), [&](const Measurement & x) |
| 61 | { |
| 62 | return x.value() - _mean; |
| 63 | }); |
| 64 | auto sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), Measurement::Value(_min->value().is_floating_point)); |
| 65 | auto variance = sq_sum / measurements.size(); |
| 66 | _stddev = Measurement::Value::relative_standard_deviation(variance, _mean); |
| 67 | } |
| 68 | } // namespace framework |
| 69 | } // namespace test |
| 70 | } // namespace arm_compute |