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.
multiply.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
43multiply(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>
50auto
51multiply(Tp& _lhs, const Up& _rhs, type_list<>, long)
52 -> decltype((std::begin(_lhs), std::begin(_rhs)), 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 multiply(*litr, *ritr);
64 }
65}
66
67template <typename Tp, typename Up, typename Vp = typename Tp::value_type,
69auto
70multiply(Tp& _lhs, const Up& _rhs, type_list<>, long)
71 -> decltype(std::begin(_lhs), void())
72{
73 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
74 auto _n = mpl::get_size(_lhs);
75 for(decltype(_n) i = 0; i < _n; ++i)
76 {
77 auto litr = std::begin(_lhs) + i;
78 multiply(*litr, _rhs);
79 }
80}
81
82template <typename Tp, typename Up, typename Kp = typename Tp::key_type,
83 typename Mp = typename Tp::mapped_type,
85auto
86multiply(Tp& _lhs, const Up& _rhs, type_list<>, int) -> decltype(std::begin(_lhs), void())
87{
88 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
89
90 for(auto litr = std::begin(_lhs); litr != std::end(_lhs); ++litr)
91 {
92 auto ritr = _rhs.find(litr->first);
93 if(ritr == std::end(_rhs))
94 continue;
95 multiply(litr->second, ritr->second);
96 }
97}
98
99template <typename Tp, typename Up, typename Kp = typename Tp::key_type,
100 typename Mp = typename Tp::mapped_type,
102auto
103multiply(Tp& _lhs, const Up& _rhs, type_list<>, int) -> decltype(std::begin(_lhs), void())
104{
105 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
106
107 for(auto litr = std::begin(_lhs); litr != std::end(_lhs); ++litr)
108 {
109 multiply(litr->second, _rhs);
110 }
111}
112
113template <typename Tp, typename Up>
114TIMEMORY_INLINE auto
115multiply(Tp&, const Up&, index_sequence<>, int)
116{}
117
118template <typename Tp, typename Up, size_t... Idx,
120auto
121multiply(Tp& _lhs, const Up& _rhs, index_sequence<Idx...>, long)
122 -> decltype(std::get<0>(_lhs), void())
123{
124 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
125 TIMEMORY_FOLD_EXPRESSION(multiply(std::get<Idx>(_lhs), std::get<Idx>(_rhs)));
126}
127
128template <typename Tp, typename Up, size_t... Idx,
130auto
131multiply(Tp& _lhs, const Up& _rhs, index_sequence<Idx...>, long)
132 -> decltype(std::get<0>(_lhs), void())
133{
134 static_assert(!concepts::is_null_type<Tp>::value, "Error! null type");
135 TIMEMORY_FOLD_EXPRESSION(multiply(std::get<Idx>(_lhs), _rhs));
136}
137
138template <typename Tp, typename Up, enable_if_t<!concepts::is_null_type<Tp>::value>>
139Tp&
140multiply(Tp& _lhs, const Up& _rhs)
141{
143 return _lhs;
144}
145} // namespace math
146} // namespace tim
return _hash_map end()
Tp & multiply(Tp &, const Up &)
Definition: multiply.hpp:140
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