-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_view.hpp
239 lines (227 loc) · 6.05 KB
/
matrix_view.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma once
#include "iterate.hpp"
namespace
{
// matrix view class template
template<std::input_or_output_iterator It>
struct matrix_view
{
// type definitions
using value_type = std::iter_value_t<It>;
using reference_type = std::reference_wrapper<value_type>;
using const_reference_type = const std::reference_wrapper<value_type>;
// field members
size_t number_of_rows, number_of_columns;
It begin;
// ctors/dtor
matrix_view()
:number_of_rows{ 0Ui64 },
number_of_columns{ 0Ui64 },
begin{}
{
}
matrix_view(size_t nr, size_t nc, It beg)
:number_of_rows{ nr },
number_of_columns{ nc },
begin{ beg }
{
}
virtual ~matrix_view() = default;
// operator overloadings
friend constexpr auto operator<<(std::ostream& os, const matrix_view<It>& m) -> std::ostream&
{
os << "\n(" << m.number_of_rows << " x " << m.number_of_columns << ")\n";
for (auto i{ 0Ui64 }; i < m.number_of_rows; ++i)
{
iterate(
std::next(m.begin, i * m.number_of_columns),
m.number_of_columns,
1Ui64,
[&os](auto& value)
{
os << value << ':' << reinterpret_cast<uintptr_t>(&value) << '\t';
}
);
os << '\n';
}
return os;
}
friend constexpr auto operator<<(std::ofstream& os, const matrix_view<It>& m) -> std::ofstream&
{
os << "\n(" << m.number_of_rows << " x " << m.number_of_columns << ")\n";
for (auto i{ 0Ui64 }; i < m.number_of_rows; ++i)
{
iterate(
std::next(m.begin, i * m.number_of_columns),
m.number_of_columns,
1Ui64,
[&os](auto& value)
{
os << value << ':' << reinterpret_cast<uintptr_t>(&value) << '\t';
}
);
os << '\n';
}
return os;
}
friend constexpr auto operator<<(std::wostream& os, const matrix_view<It>& m) -> std::wostream&
{
os << L"\n(" << m.number_of_rows << L" x " << m.number_of_columns << L")\n";
for (auto i{ 0Ui64 }; i < m.number_of_rows; ++i)
{
iterate(
std::next(m.begin, i * m.number_of_columns),
m.number_of_columns,
1Ui64,
[&os](auto& value)
{
os << value << L':' << reinterpret_cast<uintptr_t>(&value) << L'\t';
}
);
os << L'\n';
}
}
friend constexpr auto operator<<(std::wofstream& os, const matrix_view<It>& m) -> std::wofstream&
{
os << L"\n(" << m.number_of_rows << L" x " << m.number_of_columns << L")\n";
for (auto i{ 0Ui64 }; i < m.number_of_rows; ++i)
{
iterate(
std::next(m.begin, i * m.number_of_columns),
m.number_of_columns,
1Ui64,
[&os](auto& value)
{
os << value << L':' << reinterpret_cast<uintptr_t>(&value) << L'\t';
}
);
os << L'\n';
}
}
constexpr auto operator()(size_t i, size_t j) -> value_type&
{
return *std::next(begin, i * number_of_columns + j);
}
constexpr auto operator()(size_t i, size_t j) const-> value_type
{
return *std::next(begin, i * number_of_columns + j);
}
// methods
constexpr auto size() const->size_t { return number_of_columns * number_of_rows; }
constexpr auto row(size_t i) ->std::vector<reference_type>
{
std::vector<reference_type> rowi{};
rowi.reserve(number_of_columns);
iterate(
std::next(begin, i * number_of_columns),
number_of_columns,
1Ui64,
[&rowi](auto& value)
{
rowi.emplace_back(std::ref(value));
}
);
return rowi;
}
constexpr auto row(size_t i) const->std::vector<const_reference_type>
{
std::vector<const_reference_type> rowi{};
rowi.reserve(number_of_columns);
iterate(
std::next(begin, i * number_of_columns),
number_of_columns,
1Ui64,
[&rowi](auto& value)
{
rowi.emplace_back(std::cref(value));
}
);
return rowi;
}
constexpr auto column(size_t j) ->std::vector<reference_type>
{
std::vector<reference_type> colj{};
colj.reserve(number_of_rows);
iterate(
std::next(begin, j),
number_of_rows,
number_of_columns,
[&colj](auto& value)
{
colj.emplace_back(std::ref(value));
}
);
return colj;
}
constexpr auto column(size_t j) const->std::vector<const_reference_type>
{
std::vector<const_reference_type> colj{};
colj.reserve(number_of_rows);
iterate(
std::next(begin, j),
number_of_rows,
number_of_columns,
[&colj](auto& value)
{
colj.emplace_back(std::cref(value));
}
);
return colj;
}
};
// blocking operations space
namespace blocking
{
// blocking synchronous operations space
namespace synchronous
{
// blocking synchronous eager matrix multiplication
template<std::input_or_output_iterator LhsIt,
std::input_or_output_iterator RhsIt,
typename Res = std::common_type_t<std::iter_value_t<LhsIt>, std::iter_value_t<RhsIt>>
> auto multiplies(matrix_view<LhsIt> lhs, matrix_view<RhsIt> rhs)
-> std::tuple<size_t, size_t, std::vector<Res>>
{
auto result{ std::make_tuple(0Ui64, 0Ui64, std::vector<Res>{}) };
if (lhs.number_of_columns == rhs.number_of_rows)
{
std::get<0Ui64>(result) = lhs.number_of_rows;
std::get<1Ui64>(result) = rhs.number_of_columns;
std::get<2Ui64>(result).resize(lhs.number_of_rows * rhs.number_of_columns, Res{});
for (auto i = 0Ui64; i < lhs.number_of_rows; ++i)
{
auto lhs_row_i{ lhs.row(i) };
for (auto j = 0Ui64; j < rhs.number_of_columns; ++j)
{
auto rhs_col_j{ rhs.column(j) };
std::get<2Ui64>(result).data()[i * rhs.number_of_columns + j] =
std::transform_reduce(
std::execution::seq,
lhs_row_i.begin(),
lhs_row_i.end(),
rhs_col_j.begin(),
(Res)0,
std::plus<Res>{},
[](auto& l, auto& r)
{
return l.get() * r.get();
}
);
}
}
}
return result;
}
}
// blocking asynchronous operations space
// (futures - CPU mode:kernel mode - interruptible OS threads)
namespace asynchronous {}
}
// non-blocking operations space
namespace non_blocking
{
// non-blocking asynchronous operations space
// (coroutines - CPU mode:user mode - uninterruptible lightweight non-OS threads)
namespace asynchronous {}
}
}