This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbm_copy.cc
143 lines (120 loc) · 5.01 KB
/
bm_copy.cc
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
#include <chrono>
#include <iostream>
#include <thread>
#include <gnuradio/blocks/null_sink.h>
#include <gnuradio/blocks/null_source.h>
#include <gnuradio/buffer_cpu_simple.h>
#include <gnuradio/buffer_cpu_vmcirc.h>
#include <gnuradio/flowgraph.h>
#include <gnuradio/realtime.h>
#include <gnuradio/runtime.h>
#include <gnuradio/schedulers/nbt/scheduler_nbt.h>
#include <gnuradio/streamops/copy.h>
#include <gnuradio/streamops/head.h>
#include <iostream>
#include "CLI/App.hpp"
#include "CLI/Config.hpp"
#include "CLI/Formatter.hpp"
using namespace gr;
int main(int argc, char* argv[])
{
uint64_t samples = 15000000;
unsigned int nblocks = 4;
unsigned int nthreads = 0;
int veclen = 1;
int buffer_type = 1;
int buffer_size = 32768;
bool rt_prio = false;
std::vector<unsigned int> cpu_affinity;
CLI::App app{ "App description" };
// app.add_option("-h,--help", "display help");
app.add_option("--samples", samples, "Number of Samples");
app.add_option("--veclen", veclen, "Vector Length");
app.add_option("--nblocks", nblocks, "Number of copy blocks");
app.add_option("--nthreads", nthreads, "Number of threads (0: tpb)");
app.add_option("--buffer_type",
buffer_type,
"Buffer Type (0:simple, 1:vmcirc, 2:cuda, 3:cuda_pinned");
app.add_option("--buffer_size", buffer_size, "Buffer Size in bytes");
app.add_flag("--rt_prio", rt_prio, "Enable Real-time priority");
app.add_option("--cpus",
cpu_affinity,
"Pin threads to CPUs (if nthreads > 0, will pin to 0,1,..,N");
CLI11_PARSE(app, argc, argv);
if (rt_prio && gr::enable_realtime_scheduling() != RT_OK) {
std::cout << "Error: failed to enable real-time scheduling." << std::endl;
}
{
auto src = blocks::null_source::make({ 1, sizeof(gr_complex) * veclen });
auto head =
streamops::head::make_cpu({ samples / veclen, sizeof(gr_complex) * veclen });
auto snk = blocks::null_sink::make({ 1, sizeof(gr_complex) * veclen });
std::vector<streamops::copy::sptr> copy_blks(nblocks);
for (unsigned int i = 0; i < nblocks; i++) {
copy_blks[i] = streamops::copy::make({ sizeof(gr_complex) * veclen });
}
flowgraph_sptr fg(new flowgraph());
if (buffer_type == 0) {
fg->connect(src, 0, head, 0);
fg->connect(head, 0, copy_blks[0], 0);
for (unsigned int i = 0; i < nblocks - 1; i++) {
fg->connect(copy_blks[i], 0, copy_blks[i + 1], 0);
}
fg->connect(copy_blks[nblocks - 1], 0, snk, 0);
}
else {
fg->connect(src, 0, head, 0)->set_custom_buffer(BUFFER_CPU_VMCIRC_ARGS);
fg->connect(head, 0, copy_blks[0], 0)
->set_custom_buffer(BUFFER_CPU_VMCIRC_ARGS);
for (unsigned int i = 0; i < nblocks - 1; i++) {
fg->connect(copy_blks[i], 0, copy_blks[i + 1], 0)
->set_custom_buffer(BUFFER_CPU_VMCIRC_ARGS);
}
fg->connect(copy_blks[nblocks - 1], 0, snk, 0)
->set_custom_buffer(BUFFER_CPU_VMCIRC_ARGS);
}
std::cout << "Initializing NBT scheduler with buffer size of " << buffer_size
<< std::endl;
auto sched = schedulers::scheduler_nbt::make("nbt", buffer_size);
if (buffer_type == 1) {
sched->set_default_buffer_factory(BUFFER_CPU_VMCIRC_ARGS);
}
if (nthreads > 0) {
int blks_per_thread = nblocks / nthreads;
for (unsigned int i = 0; i < nthreads; i++) {
std::vector<block_sptr> block_group;
if (i == 0) {
block_group.push_back(src);
block_group.push_back(head);
}
for (int j = 0; j < blks_per_thread; j++) {
block_group.push_back(copy_blks[i * blks_per_thread + j]);
}
if (i == nthreads - 1) {
for (unsigned int j = 0; j < (nblocks - nthreads * blks_per_thread);
j++) {
block_group.push_back(copy_blks[(i + 1) * blks_per_thread + j]);
}
block_group.push_back(snk);
}
if (cpu_affinity.empty()) {
sched->add_block_group(block_group);
}
else {
sched->add_block_group(
block_group, "group" + std::to_string(i), { cpu_affinity[i] });
}
}
}
auto rt = runtime::make();
rt->add_scheduler(sched);
rt->initialize(fg);
auto t1 = std::chrono::steady_clock::now();
rt->start();
rt->wait();
auto t2 = std::chrono::steady_clock::now();
auto time =
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() / 1e9;
std::cout << "[PROFILE_TIME]" << time << "[PROFILE_TIME]" << std::endl;
}
}