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.
divide.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, typename Up>
42TIMEMORY_INLINE auto
43divide(Tp& _lhs, Up _rhs, type_list<>, ...) -> decltype(_lhs /= _rhs, void())
44{
45 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
46 _lhs /= _rhs;
47}
48
49template <typename Tp, typename Up, typename Vp = typename Tp::value_type,
51auto
52divide(Tp& _lhs, const Up& _rhs, type_list<>, long) -> decltype(std::begin(_lhs), void())
53{
54 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
55 auto _n = mpl::get_size(_rhs);
56 if(mpl::get_size(_lhs) < _n)
57 mpl::resize(_lhs, _n);
58
59 for(decltype(_n) i = 0; i < _n; ++i)
60 {
61 auto litr = std::begin(_lhs) + i;
62 auto ritr = std::begin(_rhs) + i;
63 divide(*litr, *ritr);
64 }
65}
66
67template <typename Tp, typename Up, typename Vp = typename Tp::value_type,
69auto
70divide(Tp& _lhs, const Up& _rhs, type_list<>, long) -> decltype(std::begin(_lhs), void())
71{
72 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
73 auto _n = mpl::get_size(_lhs);
74 for(decltype(_n) i = 0; i < _n; ++i)
75 {
76 auto litr = std::begin(_lhs) + i;
77 divide(*litr, _rhs);
78 }
79}
80
81template <typename Tp, typename Up, typename Kp = typename Tp::key_type,
82 typename Mp = typename Tp::mapped_type,
83 enable_if_t<!std::is_arithmetic<Up>::value> = 0>
84auto
85divide(Tp& _lhs, const Up& _rhs, type_list<>, int) -> decltype(std::begin(_lhs), void())
86{
87 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
88
89 for(auto litr = std::begin(_lhs); litr != std::end(_lhs); ++litr)
90 {
91 auto ritr = _rhs.find(litr->first);
92 if(ritr == std::end(_rhs))
93 continue;
94 divide(litr->second, ritr->second);
95 }
96}
97
98template <typename Tp, typename Up, typename Kp = typename Tp::key_type,
99 typename Mp = typename Tp::mapped_type,
101auto
102divide(Tp& _lhs, const Up& _rhs, type_list<>, int) -> decltype(std::begin(_lhs), void())
103{
104 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
105
106 for(auto litr = std::begin(_lhs); litr != std::end(_lhs); ++litr)
107 {
108 divide(litr->second, _rhs);
109 }
110}
111
112template <typename Tp, typename Up>
113TIMEMORY_INLINE auto
114divide(Tp&, const Up&, index_sequence<>, int)
115{}
116
117template <typename Tp, typename Up, size_t... Idx,
119auto
120divide(Tp& _lhs, const Up& _rhs, index_sequence<Idx...>, long)
121 -> decltype(std::get<0>(_lhs), void())
122{
123 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
124 TIMEMORY_FOLD_EXPRESSION(divide(std::get<Idx>(_lhs), std::get<Idx>(_rhs)));
125}
126
127template <typename Tp, typename Up, size_t... Idx,
129auto
130divide(Tp& _lhs, const Up& _rhs, index_sequence<Idx...>, long)
131 -> decltype(std::get<0>(_lhs), void())
132{
133 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
134 TIMEMORY_FOLD_EXPRESSION(divide(std::get<Idx>(_lhs), _rhs));
135}
136
137template <typename Tp, typename Up, enable_if_t<!concepts::is_null_type<Tp>::value>>
138Tp&
139divide(Tp& _lhs, const Up& _rhs)
140{
142 return _lhs;
143}
144} // namespace math
145} // namespace tim
return _hash_map end()
auto divide(Tp &_lhs, Up _rhs, type_list<>,...) -> decltype(_lhs/=_rhs, void())
Definition: divide.hpp:43
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
concept that specifies that a type is not a useful type
Definition: concepts.hpp:159
#define TIMEMORY_FOLD_EXPRESSION(...)
Definition: types.hpp:56