timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
percent_diff.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
27#include "timemory/math/fwd.hpp"
31
32#include <cassert>
33#include <cmath>
34#include <limits>
35#include <utility>
36
37namespace tim
38{
39namespace math
40{
41template <typename Tp, enable_if_t<std::is_arithmetic<Tp>::value> = 0>
42TIMEMORY_INLINE Tp
43percent_diff(Tp _lhs, Tp _rhs, type_list<>, ...)
44{
45 constexpr Tp _zero = Tp(0.0);
46 constexpr Tp _one = Tp(1.0);
47 constexpr Tp _hundred = Tp(100.0);
48 Tp&& _pdiff = (_rhs > _zero) ? ((_one - (_lhs / _rhs)) * _hundred) : _zero;
49 return (_pdiff < _zero) ? _zero : _pdiff;
50}
51
52template <typename Tp,
53 enable_if_t<!std::is_arithmetic<Tp>::value && std::is_class<Tp>::value> = 0>
54TIMEMORY_INLINE auto
55percent_diff(Tp _lhs, Tp _rhs, type_list<>, ...) -> decltype(_lhs.percent_diff(_rhs))
56{
57 return _lhs.percent_diff(_rhs);
58}
59
60template <typename Tp, typename Vp = typename Tp::value_type>
61auto
62percent_diff(const Tp& _lhs, const Tp& _rhs, type_list<>, long)
63 -> decltype(std::begin(_lhs), Tp{})
64{
65 auto _nl = mpl::get_size(_lhs);
66 auto _nr = mpl::get_size(_rhs);
67 using Int_t = decltype(_nl);
68
69 auto _n = std::min<Int_t>(_nl, _nr);
70 Tp _ret{};
71 mpl::resize(_ret, _n);
72
73 // initialize
74 for(auto& itr : _ret)
75 itr = Vp{};
76
77 // compute
78 for(Int_t i = 0; i < _n; ++i)
79 {
80 auto litr = std::begin(_lhs) + i;
81 auto ritr = std::begin(_rhs) + i;
82 auto itr = std::begin(_ret) + i;
83 *itr = percent_diff(*litr, *ritr);
84 }
85 return _ret;
86}
87
88template <typename Tp, typename Kp = typename Tp::key_type,
89 typename Mp = typename Tp::mapped_type>
90auto
91percent_diff(const Tp& _lhs, const Tp& _rhs, type_list<>, int)
92 -> decltype(std::begin(_lhs), Tp{})
93{
94 assert(_lhs.size() == _rhs.size());
95 Tp _ret{};
96 for(auto litr = std::begin(_lhs); litr != std::end(_lhs); ++litr)
97 {
98 auto ritr = _rhs.find(litr->first);
99 if(ritr == std::end(_rhs))
100 {
101 _ret[litr->first] = Mp{};
102 }
103 else
104 {
105 _ret[litr->first] = percent_diff(litr->second, ritr->second);
106 }
107 }
108 return _ret;
109}
110
111template <typename Tp, size_t... Idx>
112auto
113percent_diff(const Tp& _lhs, const Tp& _rhs, index_sequence<Idx...>, ...)
114 -> decltype(std::get<0>(_lhs), Tp{})
115{
116 Tp _ret{};
117 TIMEMORY_FOLD_EXPRESSION(std::get<Idx>(_ret) =
118 percent_diff(std::get<Idx>(_lhs), std::get<Idx>(_rhs)));
119 return _ret;
120}
121
122template <typename Tp>
123Tp
124percent_diff(const Tp& _lhs, const Tp& _rhs)
125{
126 return percent_diff(_lhs, _rhs, get_index_sequence<Tp>::value, 0);
127}
128} // namespace math
129} // namespace tim
return _hash_map end()
const hash_alias_ptr_t hash_value_t std::string *& _ret
Definition: definition.hpp:300
Tp percent_diff(const Tp &, const Tp &)
auto resize(T &,...) -> void
Definition: types.hpp:929
constexpr auto get_size(const Tp &, std::tuple<>) -> size_t
Definition: types.hpp:877
Definition: kokkosp.cpp:39
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
std::integer_sequence< size_t, Idx... > index_sequence
Alias template index_sequence.
Definition: types.hpp:178
lightweight tuple-alternative for meta-programming logic
Definition: types.hpp:233
#define TIMEMORY_FOLD_EXPRESSION(...)
Definition: types.hpp:56