-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_engine_simulator.cpp
More file actions
356 lines (322 loc) · 15.8 KB
/
f_engine_simulator.cpp
File metadata and controls
356 lines (322 loc) · 15.8 KB
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
/**
* \file f_engine_simulator.cpp
*
* \brief Standalone F-Engine Simulator.
*
* \author Gareth Callanan
*
* This file is a standalone program that can be built on any machine with SPEAD2 installed. It is designed to simulate a single F-Engine output stream with all 64 F-Engine packets in the stream
*
* It can generate data at the requried output rate as long as the stream config packet size is set to 4 KiB and above.
*/
#include <iostream>
#include <utility>
#include <boost/asio.hpp>
#include <spead2/common_endian.h>
#include <spead2/common_thread_pool.h>
#include <spead2/common_defines.h>
#include <spead2/common_flavour.h>
#include <spead2/send_heap.h>
#include <spead2/send_udp.h>
#include <spead2/send_stream.h>
#include <thread>
#include <chrono>
#include <mutex>
#include <iomanip>
#include <atomic>
#include <boost/program_options.hpp>
#include <string>
#include <deque>
#define N_ANTS 64
#define N_CHANNELS 4096
#define N_X_ENGINES_PER_ANT 4
#define N_CHANNELS_PER_X_ENGINE N_CHANNELS/N_ANTS/N_X_ENGINES_PER_ANT
#define N_POL 2
#define TIME_SAMPLES_PER_PACKET 256
#define TIMESTAMP_VARIATION 5
#define FFT_SIZE (N_CHANNELS_PER_X_ENGINE*4*N_ANTS)
enum SampleDataFormat{one_ant_test,two_ant_test,ramp,all_zero};
SampleDataFormat sample_data_format = two_ant_test;
//one_ant_test specifications
#define ANTENNA 3
#define REPORTING_SPACE 10000
std::atomic<int> numSent;
using boost::asio::ip::udp;
std::mutex m;
auto start = std::chrono::high_resolution_clock::now();
int main(int argc, char** argv)
{
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("tx_port", po::value<std::string>()->default_value("8888"), "Set transmitter port")
("dest_ip", po::value<std::string>()->default_value("127.0.0.1"), "Set destination ip address")
("drop_rate", po::value<std::int32_t>()->default_value(0), "Set packet drop rate in percent(range: 0 to 100)")
("rate_fudge_factor", po::value<std::int32_t>()->default_value(150), "Set delay after each batch of 64 packets.")
("ant1", po::value<std::int32_t>()->default_value(17), "Set 1st Antenna to add non-zero data to.(range: 0 to 63)")
("ant2", po::value<std::int32_t>()->default_value(17), "Set 2nd Antenna to add non-zero data to.(range: 0 to 63)")
("num_packets_total", po::value<std::int32_t>()->default_value(1000000), "Total number of sets of 64 packets to send.")
("sync_start", po::value<int64_t>()->default_value(44040089), "Set number of accumulations")
("accumulation_length", po::value<int32_t>()->default_value(408), "Set number of accumulations")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")){
std::cout << desc << "\n";
return 1;
}
std::string txPort = vm["tx_port"].as<std::string>();
std::string destIp = vm["dest_ip"].as<std::string>();
std::int32_t dropRate = vm["drop_rate"].as<std::int32_t>();
std::int32_t accLength = vm["accumulation_length"].as<std::int32_t>();
int syncStart = vm["sync_start"].as<int64_t>();
if(dropRate < 0 || dropRate >100){
std::cout << "ERROR: Drop rate set to "<<dropRate<<". Not in range of 0 to 100." << std::endl << "Exiting Program" << std::endl;
return -1;
}
std::int32_t rateFudgeFactor = vm["rate_fudge_factor"].as<std::int32_t>();
std::int32_t ant1 = vm["ant1"].as<std::int32_t>();
if(ant1 < 0 || ant1 >63){
std::cout << "ERROR: ant1 set to "<<ant1<<". Not in range of 0 to 63." << std::endl << "Exiting Program" << std::endl;
return -1;
}
std::int32_t ant2 = vm["ant2"].as<std::int32_t>();
if(ant2 < 0 || ant2 >63){
std::cout << "ERROR: ant1 set to "<<ant2<<". Not in range of 0 to 63." << std::endl << "Exiting Program" << std::endl;
return -1;
}
std::int32_t numPackets = vm["num_packets_total"].as<std::int32_t>();
spead2::thread_pool tp(10);
udp::resolver resolver(tp.get_io_service());
udp::resolver::query query(destIp, txPort);
auto it = resolver.resolve(query);
spead2::send::udp_stream stream(tp.get_io_service(), *it, spead2::send::stream_config(9000, 0,64,64),100000);
spead2::flavour f(spead2::maximum_version, 64, 48);
//Create Descriptors
spead2::send::heap h_desc(f);
spead2::descriptor timestamp_desc;
timestamp_desc.id = 0x1600;
timestamp_desc.name = "timestamp";
timestamp_desc.description = "A number to be scaled by an appropriate scale factor, provided as a KATCP sensor, to get the number of Unix seconds since epoch of the firsttime sample used to generate data in the current SPEAD heap. Consult CAM ICD [10] for the appropriate sensor.";
timestamp_desc.format.emplace_back('u',64);
spead2::descriptor feng_id_desc;
feng_id_desc.id = 0x4101;
feng_id_desc.name = "feng_id";
feng_id_desc.description = "Uniquely identifies the F-engine source for the data. A sensor can be consulted to determine the mapping of F-engine to antenna input.";
feng_id_desc.format.emplace_back('u',64);
spead2::descriptor frequency_desc;
frequency_desc.id = 0x4103;
frequency_desc.name = "frequency";
frequency_desc.description = "'Identifies the first channel in the band of frequencies in the SPEAD heap. Can be used to reconstruct the full spectrum.";
frequency_desc.format.emplace_back('u',64);
spead2::descriptor feng_raw_desc;
feng_raw_desc.id = 0x4300;
feng_raw_desc.name = "feng_raw";
feng_raw_desc.description = "Raw channelised F-Engine Data";
feng_raw_desc.numpy_header = "{'shape': (16, 256, 2 , 2), 'fortran_order': False, 'descr': '>i1'}";
h_desc.add_descriptor(timestamp_desc);
h_desc.add_descriptor(feng_id_desc);
h_desc.add_descriptor(frequency_desc);
h_desc.add_descriptor(feng_raw_desc);
stream.async_send_heap(h_desc, [] (const boost::system::error_code &ec, spead2::item_pointer_t bytes_transferred)
{
if (ec)
std::cerr << ec.message() << '\n';
else
std::cout << "Sent " << bytes_transferred << " bytes in descriptor heap\n";
});
//Send data
std::int64_t timestamp[N_ANTS];
std::int64_t feng_id[N_ANTS];
std::int64_t frequency[N_ANTS];
std::int8_t feng_raw[N_ANTS][N_CHANNELS_PER_X_ENGINE*TIME_SAMPLES_PER_PACKET*N_POL*2] = {};
spead2::send::heap h[N_ANTS];
int8_t array_temp[8] = {1,1,1,1,1,1,1,1};//{-3,-2,-1,1,2,3,4,5};
int arrayPos = 0;
bool regen = 1;
int droppedPackets = 0;
//Generates an array of queus to make the timestamps out of order - this whole system could be done better but the out of order transmision was only added near the end
std::vector<std::uint64_t> timestampArrayOfQueues[N_ANTS];
for (int k = 0; k < TIMESTAMP_VARIATION-1; k++)
{
//std::cout << k << std::endl;
for (size_t j = 0; j < N_ANTS; j++)
{
timestampArrayOfQueues[j].push_back((syncStart*FFT_SIZE*2*TIME_SAMPLES_PER_PACKET+k));
}
}
for(size_t k = 1; k < numPackets; k++)
{
//<<<<<<<<<<START SECTION THAT GENERATES DATA>>>>>>>>>>>
if(regen == 1){
//std::cout <<timestampArrayOfQueues[0][0] << " " << timestampArrayOfQueues[0][0]/256/8192%accLength << std::endl;
regen=0;
for(int tempI = 0; tempI < 8; tempI++){
array_temp[tempI]=(array_temp[tempI]+1) %5;
}
for (size_t j = 0; j < N_ANTS; j++)
{
switch(sample_data_format){
case one_ant_test:
{
for(int i = 0; i<16*256*2*2;i++){
if(j == ANTENNA){
if(i%2==0){
feng_raw[j][i] = (int8_t)array_temp[arrayPos%8+0];
}else{
feng_raw[j][i] = (int8_t)array_temp[arrayPos%8+1];
}
}else{
feng_raw[j][i] = (int8_t)0;
}
}
}
break;
case two_ant_test:
{
for (size_t f = 0; f < N_CHANNELS_PER_X_ENGINE; f++)
{
for(int i = 0; i<256*2*2;i++){
if(j == ant1){
if((i+0)%2==0 && (i+0)%4 == 0){
feng_raw[j][f*256*2*2 + i] = (int8_t)array_temp[(arrayPos+0+f)%8];
}else if((i+1)%2==0 && (i+1)%4 == 0){
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+1+f)%8];
}else if((i+0)%2==0 && (i+0)%4 != 0){
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+2+f)%8];
}else{
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+4+f)%8];
}
}else if (j == ant2)
{
if((i+0)%2==0 && (i+0)%4 == 0){
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+3+f)%8];
}else if((i+1)%2==0 && (i+1)%4 == 0){
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+5+f)%8];
}else if((i+0)%2==0 && (i+0)%4 != 0){
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+6+f)%8];
}else{
feng_raw[j][f*256*2*2+i] = (int8_t)array_temp[(arrayPos+7+f)%8];
}
}else{
feng_raw[j][i] = (int8_t)0;
}
}
}
}
break;
case all_zero:
{
for(int i = 0; i<16*256*2*2;i++){
feng_raw[j][i] = (int8_t) (0xff & 0);
}
}
break;
case ramp:
default:
{
for(int i = 0; i<16*256*2*2;i++){
feng_raw[j][i] = (int8_t) (0xff & i);
}
}
break;
}
}
//std::cout << (int)array_temp[(arrayPos+0)%8] << " " << (int)array_temp[(arrayPos+3)%8] << std::endl;
arrayPos++;
}
//<<<<<<<<<<END SECTION THAT GENERATES DATA>>>>>>>>>>>
//<<<<<<<<<<START SECTION THAT GENERATES HEAPS>>>>>>>>>>>
numSent=0;
for(int j = N_ANTS-1; j>=0; j--){
h[j] = spead2::send::heap(f);
timestampArrayOfQueues[j].push_back(((syncStart+k)*FFT_SIZE*2*TIME_SAMPLES_PER_PACKET));
//if(k%2==0){
timestamp[j] = timestampArrayOfQueues[j].front();
timestampArrayOfQueues[j][0];
timestampArrayOfQueues[j].erase(timestampArrayOfQueues[j].begin());
//}else{
// const int randPos = std::rand() % TIMESTAMP_VARIATION;
// timestamp[j] = timestampArrayOfQueues[j].at(randPos);
// timestampArrayOfQueues[j].erase(timestampArrayOfQueues[j].begin()+randPos);
//}
//if(j == 0){
// std::cout << timestamp[j] << std::endl;
//}
//std::cout << "Timestamp" << timestamp[j] << std::endl;
feng_id[j] = j;
frequency[j] = 2048;
//std::cout << sizeof(feng_raw[j]) << std::endl;
h[j].add_item(0x1600, timestamp[j]);
h[j].add_item(0x4101, feng_id[j]);
h[j].add_item(0x4103, frequency[j]);
h[j].add_item(0x4300, &feng_raw[j],sizeof(feng_raw[j]),true);
if(j == 0){
int64_t accNum = (timestamp[j]/256/8192-syncStart)%accLength;
if(accNum==accLength-1){
regen=1;
//std::cout << "Regen" << std::endl;
}
//std::cout << "Remainder: " << accNum << ", Timestamp Diff: " << timestampArrayOfQueues[j][0]-timestamp[j] << ", Ant: " << j << std::endl;
}
//m.lock();
}
//<<<<<<<<<<END SECTION THAT GENERATES HEAPS>>>>>>>>>>>
//<<<<<<<<<<START SECTION THAT TRANSMITS HEAPS>>>>>>>>>>>
for(int j = N_ANTS-1; j>=0; j--){
if(std::rand()%100 >= dropRate){
stream.async_send_heap(h[j], [j,k] (const boost::system::error_code &ec, spead2::item_pointer_t bytes_transferred)
{
if (ec){
std::cerr << ec.message() << '\n';
std::cout << "Error Sending Data" << std::endl;
}else
{
numSent++;
//if(k%REPORTING_SPACE==0 && j == 0){
// std::clock_t current = std::clock();
// double duration = (current - start ) / (double) CLOCKS_PER_SEC;
// start=current;
// std::cout<< k << duration << " " << std::endl;
//}
//std::cout << "Sent " << bytes_transferred << " bytes in heap of f_eng:" << j <<" Packet: "<<k<< std::endl;
}
});
}else{
numSent++;
droppedPackets++;
}
//<<<<<<<<<<END SECTION THAT TRANSMITS HEAPS>>>>>>>>>>>
//<<<<<<<<<<START SECTION THAT DETERMINES WHETHER TO PRINT OUT RATE AND REGEN DATA>>>>>>>>>>>
if(k%REPORTING_SPACE==0 && j == 0){
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = now-start;
start=now;
double data_rate = (double)REPORTING_SPACE*N_ANTS*N_CHANNELS_PER_X_ENGINE*TIME_SAMPLES_PER_PACKET*N_POL*2*8/diff.count()/1000/1000/1000;
std::cout <<std::fixed<<std::setprecision(2)<<k<< " Sets Sent. Data Rate: "<< data_rate << " Gbps. 64*"<<REPORTING_SPACE <<" packets over "<< diff.count()<< "s. "<< "Drop Rate: " << ((double)droppedPackets)/(REPORTING_SPACE*64+0.0)*100 <<"%"<<std::endl;
//std::cout<< k << " sets of 64 packets sent, time per packet : "<< diff.count()/64/REPORTING_SPACE*1000*1000 << " us. Time per set of 64: "<< diff.count()/REPORTING_SPACE*1000 << "ms. Time per block: " <<diff.count()<< std::endl;
droppedPackets = 0;
}
//if(){ //Figure out the condition to make regen happen, check timestamp generation is deterministic
// regen=1;
//}
//<<<<<<<<<<END SECTION THAT DETERMINES WHETHER TO PRINT OUT RATE AND REGEN DATA>>>>>>>>>>>
}
while(true){
if(numSent>=N_ANTS){
std::chrono::microseconds timespan(rateFudgeFactor); // or whatever
std::this_thread::sleep_for(timespan);
//std::cout << numSent << std::endl;
numSent==0;
break;
}
}
}
spead2::send::heap end(f);
end.add_end();
stream.async_send_heap(end, [] (const boost::system::error_code &ec, spead2::item_pointer_t bytes_transferred) {});
stream.flush();
return 0;
}