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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/* -*- c++ -*- */
/*
* Copyright 2007 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mb_mblock.h>
#include <mb_protocol_class.h>
#include <mb_message.h>
#include <mb_class_registry.h>
#include <iostream>
#include <sstream>
#include <bitset>
static pmt_t s_in = pmt_intern("in");
static pmt_t s_out = pmt_intern("out");
static pmt_t s_data = pmt_intern("data");
static pmt_t s_start = pmt_intern("start");
static pmt_t s_send_batch = pmt_intern("send-batch");
static pmt_t s_long0 = pmt_from_long(0);
static std::string
str(long x)
{
std::ostringstream s;
s << x;
return s.str();
}
/*!
* \brief mblock used for QA.
*
* Messages arriving on "in" consist of a pair containing a (long)
* message number in the car, and a (long) bitmap in the cdr. For
* each message received on "in", a new message is sent on "out". The
* new message is the same format as the input, but the bitmap in
* the cdr has a "1" or'd into it that corresponds to the bit number
* specified in the constructor.
*
* The bitmap can be used by the ultimate receiver to confirm
* traversal of a set of blocks, if the blocks are assigned unique bit
* numbers.
*/
class qa_bitset : public mb_mblock
{
mb_port_sptr d_in;
mb_port_sptr d_out;
int d_bitno;
public:
qa_bitset(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
void handle_message(mb_message_sptr msg);
};
qa_bitset::qa_bitset(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
d_bitno = pmt_to_long(user_arg); // The bit we are to set
d_in = define_port("in", "qa-bitset", false, mb_port::EXTERNAL);
d_out = define_port("out", "qa-bitset", true, mb_port::EXTERNAL);
}
void
qa_bitset::handle_message(mb_message_sptr msg)
{
if (pmt_eq(msg->port_id(), s_in) && pmt_eq(msg->signal(), s_data)){
d_out->send(s_data,
pmt_cons(pmt_car(msg->data()),
pmt_from_long((1L << d_bitno) | pmt_to_long(pmt_cdr(msg->data())))));
}
}
REGISTER_MBLOCK_CLASS(qa_bitset);
// ------------------------------------------------------------------------
/*!
* \brief mblock used for QA. Compose two qa_bitset mblocks.
*/
class qa_bitset2 : public mb_mblock
{
mb_port_sptr d_in;
mb_port_sptr d_out;
public:
qa_bitset2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
};
qa_bitset2::qa_bitset2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
long bitno = pmt_to_long(user_arg); // The bit we are to set
d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
define_component("bs0", "qa_bitset", pmt_from_long(bitno));
define_component("bs1", "qa_bitset", pmt_from_long(bitno + 1));
connect("self", "in", "bs0", "in");
connect("bs0", "out", "bs1", "in");
connect("bs1", "out", "self", "out");
}
REGISTER_MBLOCK_CLASS(qa_bitset2);
// ------------------------------------------------------------------------
/*!
* \brief mblock used for QA. Compose two qa_bitset2 mblocks.
*/
class qa_bitset4 : public mb_mblock
{
mb_port_sptr d_in;
mb_port_sptr d_out;
public:
qa_bitset4(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
};
qa_bitset4::qa_bitset4(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
long bitno = pmt_to_long(user_arg); // The bit we are to set
d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
define_component("bs0", "qa_bitset2", pmt_from_long(bitno));
define_component("bs1", "qa_bitset2", pmt_from_long(bitno + 2));
connect("self", "in", "bs0", "in");
connect("bs0", "out", "bs1", "in");
connect("bs1", "out", "self", "out");
}
REGISTER_MBLOCK_CLASS(qa_bitset4);
// ------------------------------------------------------------------------
/*!
* \brief mblock used for QA. Compose two qa_bitset4 mblocks.
*/
class qa_bitset8 : public mb_mblock
{
mb_port_sptr d_in;
mb_port_sptr d_out;
public:
qa_bitset8(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
};
qa_bitset8::qa_bitset8(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
long bitno = pmt_to_long(user_arg); // The bit we are to set
d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
define_component("bs0", "qa_bitset4", pmt_from_long(bitno));
define_component("bs1", "qa_bitset4", pmt_from_long(bitno + 4));
connect("self", "in", "bs0", "in");
connect("bs0", "out", "bs1", "in");
connect("bs1", "out", "self", "out");
}
REGISTER_MBLOCK_CLASS(qa_bitset8);
// ------------------------------------------------------------------------
/*!
* \brief mblock used for QA. Compose two qa_bitset8 mblocks.
*/
class qa_bitset16 : public mb_mblock
{
mb_port_sptr d_in;
mb_port_sptr d_out;
public:
qa_bitset16(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
};
qa_bitset16::qa_bitset16(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
long bitno = pmt_to_long(user_arg); // The bit we are to set
d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
define_component("bs0", "qa_bitset8", pmt_from_long(bitno));
define_component("bs1", "qa_bitset8", pmt_from_long(bitno + 8));
connect("self", "in", "bs0", "in");
connect("bs0", "out", "bs1", "in");
connect("bs1", "out", "self", "out");
}
REGISTER_MBLOCK_CLASS(qa_bitset16);
// ------------------------------------------------------------------------
/*!
* \brief mblock used for QA. Compose two qa_bitset16 mblocks.
*/
class qa_bitset32 : public mb_mblock
{
mb_port_sptr d_in;
mb_port_sptr d_out;
public:
qa_bitset32(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
};
qa_bitset32::qa_bitset32(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
long bitno = pmt_to_long(user_arg); // The bit we are to set
d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
define_component("bs0", "qa_bitset16", pmt_from_long(bitno));
define_component("bs1", "qa_bitset16", pmt_from_long(bitno + 16));
connect("self", "in", "bs0", "in");
connect("bs0", "out", "bs1", "in");
connect("bs1", "out", "self", "out");
}
REGISTER_MBLOCK_CLASS(qa_bitset32);
// ------------------------------------------------------------------------
class qa_bitset_src : public mb_mblock
{
mb_port_sptr d_cs_top;
mb_port_sptr d_cs;
mb_port_sptr d_out;
long d_msg_number; // starting message number
long d_nmsgs_to_send; // # of messages to send
long d_batch_size; // # of messages to send per batch
public:
qa_bitset_src(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
void handle_message(mb_message_sptr msg);
protected:
void send_one();
void send_batch();
};
qa_bitset_src::qa_bitset_src(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
d_msg_number = pmt_to_long(pmt_nth(0, user_arg));
d_nmsgs_to_send = pmt_to_long(pmt_nth(1, user_arg));
d_batch_size = pmt_to_long(pmt_nth(2, user_arg));
d_cs_top = define_port("cs_top", "qa-bitset-cs", true, mb_port::EXTERNAL);
d_cs = define_port("cs", "qa-bitset-cs", true, mb_port::EXTERNAL);
d_out = define_port("out", "qa-bitset", true, mb_port::EXTERNAL);
}
void
qa_bitset_src::handle_message(mb_message_sptr msg)
{
if ((pmt_eq(msg->port_id(), d_cs_top->port_symbol())
|| pmt_eq(msg->port_id(), d_cs->port_symbol()))
&& pmt_eq(msg->signal(), s_send_batch)){
send_batch();
}
}
void
qa_bitset_src::send_batch()
{
for (int i = 0; i < d_batch_size; i++)
send_one();
}
void
qa_bitset_src::send_one()
{
if (d_nmsgs_to_send > 0){
pmt_t msg_number = pmt_from_long(d_msg_number++);
d_out->send(s_data, pmt_cons(msg_number, s_long0));
}
if (--d_nmsgs_to_send <= 0)
exit();
}
REGISTER_MBLOCK_CLASS(qa_bitset_src);
// ------------------------------------------------------------------------
class qa_bitset_sink : public mb_mblock
{
// Maximum number of messages we can track
static const size_t MAX_MSGS = 1 * 1024 * 1024;
mb_port_sptr d_cs0;
mb_port_sptr d_cs1;
mb_port_sptr d_cs2;
mb_port_sptr d_cs3;
mb_port_sptr d_in0;
mb_port_sptr d_in1;
mb_port_sptr d_in2;
mb_port_sptr d_in3;
long d_nmsgs_to_recv; // # of messages to receive
long d_batch_size; // # of messages to receive per batch
uint32_t d_expected_mask;
std::bitset<MAX_MSGS> d_bitset;
long d_nrecvd;
public:
qa_bitset_sink(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
void handle_message(mb_message_sptr msg);
protected:
void receive_one(mb_message_sptr msg);
};
qa_bitset_sink::qa_bitset_sink(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg),
d_nrecvd(0)
{
d_nmsgs_to_recv = pmt_to_long(pmt_nth(0, user_arg));
d_batch_size = pmt_to_long(pmt_nth(1, user_arg));
d_expected_mask = pmt_to_long(pmt_nth(2, user_arg));
if (d_nmsgs_to_recv > (long) MAX_MSGS)
throw std::out_of_range("qa_bitset_sink: nmsgs_to_recv is too big");
if (d_batch_size < 1)
throw std::out_of_range("qa_bitset_sink: batch_size must be >= 1");
d_cs0 = define_port("cs0", "qa-bitset-cs", true, mb_port::EXTERNAL);
d_cs1 = define_port("cs1", "qa-bitset-cs", true, mb_port::EXTERNAL);
d_cs2 = define_port("cs2", "qa-bitset-cs", true, mb_port::EXTERNAL);
d_cs3 = define_port("cs3", "qa-bitset-cs", true, mb_port::EXTERNAL);
d_in0 = define_port("in0", "qa-bitset", false, mb_port::EXTERNAL);
d_in1 = define_port("in1", "qa-bitset", false, mb_port::EXTERNAL);
d_in2 = define_port("in2", "qa-bitset", false, mb_port::EXTERNAL);
d_in3 = define_port("in3", "qa-bitset", false, mb_port::EXTERNAL);
}
void
qa_bitset_sink::handle_message(mb_message_sptr msg)
{
if ((pmt_eq(msg->port_id(), d_in0->port_symbol())
|| pmt_eq(msg->port_id(), d_in1->port_symbol())
|| pmt_eq(msg->port_id(), d_in2->port_symbol())
|| pmt_eq(msg->port_id(), d_in3->port_symbol()))
&& pmt_eq(msg->signal(), s_data)){
receive_one(msg);
}
}
void
qa_bitset_sink::receive_one(mb_message_sptr msg)
{
long msg_number = pmt_to_long(pmt_car(msg->data()));
uint32_t mask = pmt_to_long(pmt_cdr(msg->data()));
// std::cout << msg->data() << std::endl;
d_nrecvd++;
if (d_nrecvd % d_batch_size == d_batch_size - 1){
d_cs0->send(s_send_batch);
d_cs1->send(s_send_batch);
d_cs2->send(s_send_batch);
d_cs3->send(s_send_batch);
}
if (msg_number >= d_nmsgs_to_recv){
std::cerr << "qa_bitset_sink::receive_one: msg_number too big ("
<< msg_number << ")\n";
shutdown_all(PMT_F);
return;
}
if (mask != d_expected_mask){
fprintf(stderr,
"qa_bitset_sink::receive_one: Wrong mask. Expected 0x%08x, got 0x%08x\n",
d_expected_mask, mask);
shutdown_all(PMT_F);
return;
}
if (d_bitset.test((size_t) msg_number)){
std::cerr << "qa_bitset_sink::receive_one: duplicate msg_number ("
<< msg_number << ")\n";
shutdown_all(PMT_F);
return;
}
d_bitset.set((size_t) msg_number);
if (d_nrecvd == d_nmsgs_to_recv)
shutdown_all(PMT_T); // we're done!
}
REGISTER_MBLOCK_CLASS(qa_bitset_sink);
// ------------------------------------------------------------------------
class qa_bitset_top : public mb_mblock
{
static const int NPIPES = 4;
std::vector<mb_port_sptr> d_cs;
long d_nmsgs; // # of messages to send
long d_batch_size; // # of messages to receive per batch
public:
qa_bitset_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
void initial_transition();
};
qa_bitset_top::qa_bitset_top(mb_runtime *runtime,
const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg)
{
d_nmsgs = pmt_to_long(pmt_nth(0, user_arg));
d_nmsgs = (d_nmsgs / NPIPES) * NPIPES;
d_batch_size = pmt_to_long(pmt_nth(1, user_arg));
/*
* We build NPIPES sources which feed NPIPES pipelines, each of which
* consists of 8-mblocks. All pipelines feed into a single sink
* which keeps track the results.
*/
for (int i = 0; i < NPIPES; i++){
d_cs.push_back(define_port("cs"+str(i), "qa-bitset-cs", false, mb_port::INTERNAL));
// sources of test messages
define_component("src"+str(i), "qa_bitset_src",
pmt_list3(pmt_from_long(i * d_nmsgs/NPIPES),
pmt_from_long(d_nmsgs/NPIPES),
pmt_from_long(d_batch_size)));
// 8-mblock processing pipelines
define_component("pipeline"+str(i), "qa_bitset8", pmt_from_long(0));
}
// sink for output of pipelines
define_component("sink", "qa_bitset_sink",
pmt_list3(pmt_from_long(d_nmsgs),
pmt_from_long(d_batch_size * NPIPES),
pmt_from_long(0x000000ff)));
for (int i = 0; i < NPIPES; i++){
connect("self", "cs"+str(i), "src"+str(i), "cs_top");
connect("src"+str(i), "out", "pipeline"+str(i), "in");
connect("src"+str(i), "cs", "sink", "cs"+str(i));
connect("pipeline"+str(i), "out", "sink", "in"+str(i));
}
}
void
qa_bitset_top::initial_transition()
{
for (int i = 0; i < NPIPES; i++){
d_cs[i]->send(s_send_batch); // prime the pump
d_cs[i]->send(s_send_batch);
}
}
REGISTER_MBLOCK_CLASS(qa_bitset_top);
|