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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* -*- c++ -*- */
/*
* Copyright 2014,2017,2018 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "multiply_matrix_impl.h"
#include <gnuradio/io_signature.h>
#include <volk/volk.h>
namespace gr {
namespace blocks {
// Copy tags from input k to output l if A[l][k] is not zero
template <>
void multiply_matrix_impl<gr_complex>::propagate_tags_by_A(int noutput_items,
size_t ninput_ports,
size_t noutput_ports)
{
std::vector<gr::tag_t> tags;
for (size_t in_idx = 0; in_idx < ninput_ports; in_idx++) {
this->get_tags_in_window(tags, in_idx, 0, noutput_items);
for (size_t out_idx = 0; out_idx < noutput_ports; out_idx++) {
if (d_A[out_idx][in_idx] == std::complex<float>(0, 0)) {
continue;
}
for (size_t i = 0; i < tags.size(); i++) {
this->add_item_tag(out_idx, tags[i]);
}
}
}
}
// Check dimensions before copying
template <>
bool multiply_matrix_impl<gr_complex>::set_A(
const std::vector<std::vector<gr_complex>>& new_A)
{
if (d_A.size() != new_A.size()) {
GR_LOG_ALERT(d_logger, "Attempted to set matrix with invalid dimensions.");
return false;
}
for (size_t i = 0; i < d_A.size(); i++) {
if (d_A[i].size() != new_A[i].size()) {
GR_LOG_ALERT(d_logger, "Attempted to set matrix with invalid dimensions.");
return false;
}
}
d_A = new_A;
return true;
}
template <>
void multiply_matrix_impl<gr_complex>::msg_handler_A(pmt::pmt_t A)
{
if (!pmt::is_vector(A) && !pmt::is_tuple(A)) {
GR_LOG_ALERT(d_logger, "Invalid message to set A (wrong type).");
return;
}
if (pmt::length(A) != d_A.size()) {
GR_LOG_ALERT(d_logger, "Invalid message to set A (wrong size).");
return;
}
std::vector<std::vector<gr_complex>> new_A(d_A);
for (size_t i = 0; i < pmt::length(A); i++) {
pmt::pmt_t row;
if (pmt::is_vector(A)) {
row = pmt::vector_ref(A, i);
} else if (pmt::is_tuple(A)) {
row = pmt::tuple_ref(A, i);
}
if (pmt::is_vector(row) || pmt::is_tuple(row)) {
if (pmt::length(row) != d_A[0].size()) {
GR_LOG_ALERT(d_logger,
"Invalid message to set A (wrong number of columns).");
return;
}
for (size_t k = 0; k < pmt::length(row); k++) {
new_A[i][k] =
pmt::to_complex(pmt::is_vector(row) ? pmt::vector_ref(row, k)
: pmt::tuple_ref(row, k));
}
} else if (pmt::is_c32vector(row)) {
size_t row_len = 0;
const gr_complex* elements = pmt::c32vector_elements(row, row_len);
if (row_len != d_A[0].size()) {
GR_LOG_ALERT(d_logger,
"Invalid message to set A (wrong number of columns).");
return;
}
new_A[i].assign(elements, elements + row_len);
}
}
if (!set_A(new_A)) {
GR_LOG_ALERT(d_logger, "Invalid message to set A.");
}
}
// Copy tags from input k to output l if A[l][k] is not zero
template <>
void multiply_matrix_impl<float>::propagate_tags_by_A(int noutput_items,
size_t ninput_ports,
size_t noutput_ports)
{
std::vector<gr::tag_t> tags;
for (size_t in_idx = 0; in_idx < ninput_ports; in_idx++) {
get_tags_in_window(tags, in_idx, 0, noutput_items);
for (size_t out_idx = 0; out_idx < noutput_ports; out_idx++) {
if (d_A[out_idx][in_idx] == 0) {
continue;
}
for (size_t i = 0; i < tags.size(); i++) {
add_item_tag(out_idx, tags[i]);
}
}
}
}
// Check dimensions before copying
template <>
bool multiply_matrix_impl<float>::set_A(const std::vector<std::vector<float>>& new_A)
{
if (d_A.size() != new_A.size()) {
GR_LOG_ALERT(d_logger, "Attempted to set matrix with invalid dimensions.");
return false;
}
for (size_t i = 0; i < d_A.size(); i++) {
if (d_A[i].size() != new_A[i].size()) {
GR_LOG_ALERT(d_logger, "Attempted to set matrix with invalid dimensions.");
return false;
}
}
d_A = new_A;
return true;
}
template <>
void multiply_matrix_impl<float>::msg_handler_A(pmt::pmt_t A)
{
if (!pmt::is_vector(A) && !pmt::is_tuple(A)) {
GR_LOG_ALERT(d_logger, "Invalid message to set A (wrong type).");
return;
}
if (pmt::length(A) != d_A.size()) {
GR_LOG_ALERT(d_logger, "Invalid message to set A (wrong size).");
return;
}
std::vector<std::vector<float>> new_A(d_A);
for (size_t i = 0; i < pmt::length(A); i++) {
pmt::pmt_t row;
if (pmt::is_vector(A)) {
row = pmt::vector_ref(A, i);
} else if (pmt::is_tuple(A)) {
row = pmt::tuple_ref(A, i);
}
if (pmt::is_vector(row) || pmt::is_tuple(row)) {
if (pmt::length(row) != d_A[0].size()) {
GR_LOG_ALERT(d_logger,
"Invalid message to set A (wrong number of columns).");
return;
}
for (size_t k = 0; k < pmt::length(row); k++) {
new_A[i][k] =
pmt::to_double(pmt::is_vector(row) ? pmt::vector_ref(row, k)
: pmt::tuple_ref(row, k));
}
} else if (pmt::is_f32vector(row)) {
size_t row_len = 0;
const float* elements = pmt::f32vector_elements(row, row_len);
if (row_len != d_A[0].size()) {
GR_LOG_ALERT(d_logger,
"Invalid message to set A (wrong number of columns).");
return;
}
new_A[i].assign(elements, elements + row_len);
}
}
if (!set_A(new_A)) {
GR_LOG_ALERT(d_logger, "Invalid message to set A.");
}
}
template <class T>
typename multiply_matrix<T>::sptr
multiply_matrix<T>::make(std::vector<std::vector<T>> A,
gr::block::tag_propagation_policy_t tag_propagation_policy)
{
if (A.empty() || A[0].empty()) {
throw std::invalid_argument("matrix A has invalid dimensions.");
}
return gnuradio::make_block_sptr<multiply_matrix_impl<T>>(A, tag_propagation_policy);
}
template <>
multiply_matrix_impl<gr_complex>::multiply_matrix_impl(
std::vector<std::vector<gr_complex>> A,
gr::block::tag_propagation_policy_t tag_propagation_policy)
: gr::sync_block("multiply_matrix_cc",
gr::io_signature::make(A[0].size(), A[0].size(), sizeof(gr_complex)),
gr::io_signature::make(A.size(), A.size(), sizeof(gr_complex))),
d_A(A)
{
this->MSG_PORT_NAME_SET_A = "set_A";
this->set_tag_propagation_policy(tag_propagation_policy);
const int alignment_multiple = volk_get_alignment() / sizeof(gr_complex);
set_alignment(std::max(1, alignment_multiple));
pmt::pmt_t port_name = pmt::string_to_symbol("set_A");
message_port_register_in(port_name);
set_msg_handler(port_name, [this](pmt::pmt_t msg) { this->msg_handler_A(msg); });
}
template <>
multiply_matrix_impl<float>::multiply_matrix_impl(
std::vector<std::vector<float>> A,
gr::block::tag_propagation_policy_t tag_propagation_policy)
: gr::sync_block("multiply_matrix_ff",
gr::io_signature::make(A[0].size(), A[0].size(), sizeof(float)),
gr::io_signature::make(A.size(), A.size(), sizeof(float))),
d_A(A)
{
this->MSG_PORT_NAME_SET_A = "set_A";
this->set_tag_propagation_policy(tag_propagation_policy);
const int alignment_multiple = volk_get_alignment() / sizeof(float);
set_alignment(std::max(1, alignment_multiple));
pmt::pmt_t port_name = pmt::string_to_symbol("set_A");
message_port_register_in(port_name);
set_msg_handler(port_name, [this](pmt::pmt_t msg) { this->msg_handler_A(msg); });
}
template <class T>
multiply_matrix_impl<T>::~multiply_matrix_impl()
{
}
template <>
int multiply_matrix_impl<gr_complex>::work(int noutput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items)
{
for (size_t out_idx = 0; out_idx < output_items.size(); out_idx++) {
gr_complex* out = reinterpret_cast<gr_complex*>(output_items[out_idx]);
// Do input 0 first, this saves a memset
const gr_complex* in = reinterpret_cast<const gr_complex*>(input_items[0]);
volk_32fc_s32fc_multiply_32fc(out, in, d_A[out_idx][0], noutput_items);
// Then do inputs 1 through N
for (size_t in_idx = 1; in_idx < input_items.size(); in_idx++) {
in = reinterpret_cast<const gr_complex*>(input_items[in_idx]);
// Yeah, this needs VOLK-ifying (TODO)
for (int i = 0; i < noutput_items; i++) {
out[i] += in[i] * d_A[out_idx][in_idx];
}
}
}
if (tag_propagation_policy() == TPP_CUSTOM) {
propagate_tags_by_A(noutput_items, input_items.size(), output_items.size());
}
return noutput_items;
}
template <>
int multiply_matrix_impl<float>::work(int noutput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items)
{
for (size_t out_idx = 0; out_idx < output_items.size(); out_idx++) {
float* out = reinterpret_cast<float*>(output_items[out_idx]);
// Do input 0 first, this saves a memset
const float* in = reinterpret_cast<const float*>(input_items[0]);
volk_32f_s32f_multiply_32f(out, in, d_A[out_idx][0], noutput_items);
// Then do inputs 1 through N
for (size_t in_idx = 1; in_idx < input_items.size(); in_idx++) {
in = reinterpret_cast<const float*>(input_items[in_idx]);
// Yeah, this needs VOLK-ifying (TODO)
for (int i = 0; i < noutput_items; i++) {
out[i] += in[i] * d_A[out_idx][in_idx];
}
}
}
if (tag_propagation_policy() == TPP_CUSTOM) {
propagate_tags_by_A(noutput_items, input_items.size(), output_items.size());
}
return noutput_items;
}
template class multiply_matrix<float>;
template class multiply_matrix<gr_complex>;
} /* namespace blocks */
} /* namespace gr */
|