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.
plus.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
43plus(Tp& _lhs, const 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>
50auto
51plus(Tp& _lhs, const Up& _rhs, type_list<>, long) -> decltype(std::begin(_lhs), void())
52{
53 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
54 auto _n = mpl::get_size(_rhs);
55 if(mpl::get_size(_lhs) < _n)
56 mpl::resize(_lhs, _n);
57
58 for(decltype(_n) i = 0; i < _n; ++i)
59 {
60 auto litr = std::begin(_lhs) + i;
61 auto ritr = std::begin(_rhs) + i;
62 plus(*litr, *ritr);
63 }
64}
65
66template <typename Tp, typename Up, typename Kp = typename Tp::key_type,
67 typename Mp = typename Tp::mapped_type>
68auto
69plus(Tp& _lhs, const Up& _rhs, type_list<>, int) -> decltype(std::begin(_lhs), void())
70{
71 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
72
73 for(auto litr = std::begin(_lhs); litr != std::end(_lhs); ++litr)
74 {
75 auto ritr = _rhs.find(litr->first);
76 if(ritr == std::end(_rhs))
77 continue;
78 plus(litr->second, ritr->second);
79 }
80
81 for(auto ritr = std::begin(_rhs); ritr != std::end(_rhs); ++ritr)
82 {
83 auto litr = _lhs.find(ritr->first);
84 if(litr == std::end(_lhs))
85 continue;
86 _lhs[ritr->first] = ritr->second;
87 }
88}
89
90template <typename Tp, typename Up>
91TIMEMORY_INLINE auto
92plus(Tp&, const Up&, index_sequence<>, int)
93{}
94
95template <typename Tp, typename Up, size_t... Idx>
96auto
97plus(Tp& _lhs, const Up& _rhs, index_sequence<Idx...>, long)
98 -> decltype(std::get<0>(_lhs), void())
99{
100 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
101 TIMEMORY_FOLD_EXPRESSION(plus(std::get<Idx>(_lhs), std::get<Idx>(_rhs)));
102}
103
104template <typename Tp, typename Up, enable_if_t<!concepts::is_null_type<Tp>::value>>
105Tp&
106plus(Tp& _lhs, const Up& _rhs)
107{
108 plus(_lhs, _rhs, get_index_sequence<Tp>::value, 0);
109 return _lhs;
110}
111} // namespace math
112} // namespace tim
return _hash_map end()
Tp & plus(Tp &, const Up &)
Definition: plus.hpp:106
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
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