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.
max.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
43max(Tp _lhs, Tp _rhs, type_list<>)
44{
45 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
46 return (_rhs < _lhs) ? _lhs : _rhs;
47}
48
49template <typename Tp, typename Vp = typename Tp::value_type>
50auto
51max(const Tp& _lhs, const Tp& _rhs, type_list<>, ...) -> decltype(std::begin(_lhs), Tp{})
52{
53 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
54 auto _nl = mpl::get_size(_lhs);
55 auto _nr = mpl::get_size(_rhs);
56 using Int_t = decltype(_nl);
57 assert(_nl == _nr);
58
59 auto _n = std::min<Int_t>(_nl, _nr);
60 Tp _ret{};
61 mpl::resize(_ret, _n);
62
63 for(Int_t i = 0; i < _n; ++i)
64 {
65 auto litr = std::begin(_lhs) + i;
66 auto ritr = std::begin(_rhs) + i;
67 auto itr = std::begin(_ret) + i;
68 *itr = ::tim::math::max(*litr, *ritr);
69 }
70 return _ret;
71}
72
73template <typename Tp, typename Kp = typename Tp::key_type,
74 typename Mp = typename Tp::mapped_type>
75auto
76max(const Tp& _lhs, const Tp& _rhs, type_list<>) -> decltype(std::begin(_lhs), Tp{})
77{
78 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
79 auto _nl = mpl::get_size(_lhs);
80 auto _nr = mpl::get_size(_rhs);
81 using Int_t = decltype(_nl);
82 assert(_nl == _nr);
83
84 auto _n = std::min<Int_t>(_nl, _nr);
85 Tp _ret{};
86 mpl::resize(_ret, _n);
87
88 for(Int_t i = 0; i < _n; ++i)
89 {
90 auto litr = std::begin(_lhs) + i;
91 auto ritr = std::begin(_rhs) + i;
92 auto itr = std::begin(_ret) + i;
93 itr->second = ::tim::math::max(litr->second, ritr->second);
94 }
95 return _ret;
96}
97
98template <typename Tp, size_t... Idx>
99auto
100max(const Tp& _lhs, const Tp& _rhs, index_sequence<Idx...>)
101 -> decltype(std::get<0>(_lhs), Tp{})
102{
103 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
104 Tp _ret{};
106 std::get<Idx>(_ret) = ::tim::math::max(std::get<Idx>(_lhs), std::get<Idx>(_rhs)));
107 return _ret;
108}
109
110template <typename Tp>
111Tp
112max(const Tp& _lhs, const Tp& _rhs)
113{
114 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
115 return max(_lhs, _rhs, get_index_sequence<Tp>::value);
116}
117} // namespace math
118} // namespace tim
const hash_alias_ptr_t hash_value_t std::string *& _ret
Definition: definition.hpp:300
Tp max(const Tp &, const Tp &)
Definition: max.hpp:112
auto max(const Tp &_lhs, const Tp &_rhs, index_sequence< Idx... >) -> decltype(std::get< 0 >(_lhs), Tp{})
Definition: max.hpp:100
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