-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgalapagos_interface.hpp
More file actions
685 lines (565 loc) · 20.5 KB
/
galapagos_interface.hpp
File metadata and controls
685 lines (565 loc) · 20.5 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//===============================
// AUTHOR : Naif Tarafdar
// CREATE DATE : April 20, 2019
//===============================
#ifndef __GALAPAGOS_INTERFACE_HPP__ // if x.h hasn't been included yet...
#define __GALAPAGOS_INTERFACE_HPP__
#include <cstddef>
#include <cstring>
#include "ap_int.h"
#include "hls_stream.h"
#include "ap_utils.h"
#include <memory>
#include <mutex>
#include <condition_variable>
#include <list>
#include <iterator>
#include "common.hpp"
#include "galapagos_packet.h"
#if LOG_LEVEL > 0
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/fmt/bin_to_hex.h"
#endif
#define JUMBO_FRAME
#ifdef JUMBO_FRAME
#define MAX_BUFFER 1124 //flits approximately of MTU size
#else
#define MAX_BUFFER 180 //flits approximately of MTU size
#endif
namespace galapagos{
/** Struct of buffer used to store entire packets
*/
typedef struct{
char data[(MAX_BUFFER+1)*64];
short dest;
short id;
size_t size;
}buffer;
/** @brief Class for the Galapagos Interface.
Threadsafe for single producer, single consumer. Gives user an interface to read/write flits and entire packets.
Packets internally are stored in linked-list and can be spliced to other linked-lists.
The flit interface reads from the head packet of the linked-list and writes to the tail packet.
@author Naif Tarafdar
@date April 20, 2019
*/
template <class T>
class interface{
private:
void prepare(std::string _name);
std::mutex mutex;
std::condition_variable cv;
#if LOG_LEVEL > 0
std::shared_ptr<spdlog::logger> logger;
#endif
std::list <galapagos::buffer> packets;
//needed for size
std::mutex write_in_prog_mutex;
// Keeps track of in progress operations
// In progress means that we are reading/writing a single flit from the middle
// of a packet
size_t write_in_prog_addr;
size_t read_in_prog_addr;
//Pointer to list node that are operation is in progress of
std::list<galapagos::buffer>::iterator curr_read_it;
galapagos::buffer curr_write;
short write_id;
short write_dest;
short read_id;
short read_dest;
std::vector <size_t> filter_indices;
char filter[MAX_BUFFER];
bool filter_status;
void filter_function(char *packet, int size);
public:
#if LOG_LEVEL > 0
interface(std::string _name, std::shared_ptr<spdlog::logger> _logger);
#endif
interface(std::string _name);
std::string name;
void write(galapagos::stream_packet <T> gps);
galapagos::stream_packet<T> read();
bool empty();
size_t size();
void packet_write(char * data, int size, short dest, short id);
void packet_write_partial(char * data, int size, bool assert_last);
char * packet_read(size_t * size, short * dest, short * id);
void packet_read(char * mem, size_t * size, short * dest, short * id);
char * packet_read(size_t * _size);
void set_filter(size_t pos, char byte);
short get_head_dest();
//used to splice into another list
std::mutex * get_mutex();
std::list <galapagos::buffer> * get_packets();
std::condition_variable * get_cv();
void splice(galapagos::interface<T> * _interface);
//used when writing to network socket
std::list<buffer>::iterator get_unsafe_head_buffer();
void delete_unsafe_head_buffer();
std::list<buffer> * get_unsafe_list();
};
}
#if LOG_LEVEL > 0
/**Constructor for galapagos::interface
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] name of stream, used for logging purposes
@param[in] shared_ptr of logger used globally to log
*/
template <class T>
galapagos::interface<T>::interface(
std::string _name,
std::shared_ptr<spdlog::logger> _logger
){
logger = _logger;
logger->info("Making Galapagos Interface:{0}", name);
prepare(_name);
}
#endif
template <class T>
galapagos::interface<T>::interface(
std::string _name
){
prepare(_name);
}
template <class T>
void galapagos::interface<T>::prepare(
std::string _name
){
name = _name;
read_in_prog_addr = 0;
write_in_prog_addr = 0;
curr_read_it = packets.end();
filter_status = false;
}
template <class T>
short galapagos::interface<T>::get_head_dest(){
std::unique_lock<std::mutex> lock(mutex);
return packets.begin()->dest;
}
/**Read single flit for galapagos::interface
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@returns single flit of galapagos packet
*/
template <class T>
galapagos::stream_packet <T> galapagos::interface<T>::read(){
//if not in progress get a new available buffer from beginning of list
{
std::unique_lock<std::mutex> lock(mutex);
if(!read_in_prog_addr)
{
while (packets.empty()) {
cv.wait(lock);
}
#if LOG_LEVEL > 0
logger->debug("New Flit Read:{0}", name);
#endif
curr_read_it = packets.begin();
read_in_prog_addr = sizeof(T);
}
}
//once buffer available return flit of buffer and update address
//
//
assert(curr_read_it != packets.end());
galapagos::stream_packet <T> gps;
memcpy((char *)&gps.data, (char *)curr_read_it->data + read_in_prog_addr, sizeof(T));
{
//since using read_in_prog_addr in size calc we need this to be atomic
std::unique_lock<std::mutex> lock(mutex);
read_in_prog_addr += sizeof(T);
}
gps.dest = curr_read_it->dest;
gps.id = curr_read_it->id;
assert(read_in_prog_addr <= (curr_read_it->size + sizeof(T)));
if(read_in_prog_addr == (curr_read_it->size + sizeof(T))){
std::unique_lock<std::mutex> lock(mutex);
gps.last = 1;
read_in_prog_addr = 0;
packets.erase(curr_read_it);
#if LOG_LEVEL > 0
logger->debug("Interface:{0} read last flit");
#endif
curr_read_it = packets.end();
}
else{
gps.last = 0;
}
#if LOG_LEVEL > 0
logger->debug("Interface:{0} read data:{1:x}, dest:{2:x}, last:{3:d}, at address:{4:d}, size of packet:{5:d}", name, gps.data, gps.dest, gps.last, read_in_prog_addr, curr_read_it->size);
#endif
return gps;
}
/**Helper function for filtering packets
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] gps single flit of galapagos packet to write
*/
template <class T>
void galapagos::interface<T>::filter_function(char *packet, int size){
if(filter_status){
bool pass = true;
for(unsigned int i=0; i<filter_indices.size(); i++){
pass= pass & (filter[filter_indices[i]] == packet[filter_indices[i]]);
if(!pass)
break;
}
if(pass){
#if LOG_LEVEL > 0
logger->info("FILTER********************");
logger->info("{0}:packet:{1:n}", name, spdlog::to_hex(packet, packet+size));
#endif
}
}
}
/**Write single flit for galapagos::interface
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] gps single flit of galapagos packet to write
*/
template <class T>
void galapagos::interface<T>::write(galapagos::stream_packet <T> gps){
//if not in progress get a new available buffer from end of list
if(!write_in_prog_addr)
{
#if LOG_LEVEL > 0
logger->info("New Flit Write:{0}", name);
#endif
std::lock_guard<std::mutex> guard(mutex);
curr_write.dest = gps.dest;
curr_write.id = gps.id;
write_in_prog_addr = sizeof(T);
}
//once buffer available write flit to buffer and update address
memcpy((char *)curr_write.data + write_in_prog_addr, (char *)&gps.data, sizeof(T));
{
std::lock_guard<std::mutex> guard(write_in_prog_mutex);
write_in_prog_addr += sizeof(T);
}
//last flit, moving to list
if (gps.last){
{
std::lock_guard<std::mutex> guard(write_in_prog_mutex);
curr_write.size = write_in_prog_addr - sizeof(T);
T header;
header = galapagos::range(sizeof(T)*8 - 1, 32, 0, 0); //last = 0
header = galapagos::range(31, 24, header, curr_write.dest);
header = galapagos::range(23, 16, header, curr_write.id);
header = galapagos::range(15, 0, header, curr_write.size/sizeof(T));
memcpy((char *)curr_write.data, (char *)&header, sizeof(T));
}
{
std::lock_guard<std::mutex> guard(mutex);
write_in_prog_addr = 0;
packets.push_back(std::move(curr_write));
filter_function(curr_write.data, curr_write.size);
cv.notify_one();
}
//once buffer pushed and available for consumption
#if LOG_LEVEL > 0
logger->info("Interface:{0} write last flit, adding to list, with dest:{1:x}, with size:{2:d}, packets size:{3:d}", name, curr_write.dest, curr_write.size, size());
logger->flush();
#endif
}
else{
#if LOG_LEVEL > 0
logger->info("Interface:{0} write flit data:{1:x}", name, gps.data);
logger->flush();
#endif
}
}
template <class T>
std::list<galapagos::buffer>::iterator galapagos::interface<T>::get_unsafe_head_buffer(){
return packets.begin();
}
template <class T>
std::list<galapagos::buffer> * galapagos::interface<T>::get_unsafe_list(){
return &packets;
}
template <class T>
void galapagos::interface<T>::delete_unsafe_head_buffer(){
packets.erase(packets.begin());
}
/**Gets number of packets in list, plus if any packets are being written in progress
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@returns size of buffer in head of list
*/
template <class T>
size_t galapagos::interface<T>::size(){
size_t ret;
std::lock_guard<std::mutex> guard(mutex);
ret = packets.size();
//std::lock_guard<std::mutex> _guard(write_in_prog_mutex);
//if(write_in_prog_addr>0)
//ret++;
return ret;
}
/**Executes a packet write, this function is not portable between CPU and FPGA. Please be careful to rewrite CPU functions to use an individual flit write when porting to HLS
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] data buffer to be written
@param[in] size size of buffer
*/
template <class T>
void galapagos::interface<T>::packet_write(char * data, int size, short dest, short id){
#if LOG_LEVEL > 0
logger->debug("Start Stream {0} batch_write (CPU only)", name);
#endif
//size must be divisible by 8
// assert(size % 8 == 0);
//data must fit in MTU
assert((size) <= (MAX_BUFFER));
curr_write.size = size * sizeof(T);
//curr_write.size = size;
curr_write.dest = dest;
curr_write.id = id;
T header;
// header.range(sizeof(T)*8-1,32) = 0; //last = 0
// header.range(31,24) = dest;
// header.range(23,16) = id;
// header.range(15,0) = size;
header = galapagos::range(sizeof(T)*8 - 1, 32, 0, 0); //last = 0
header = galapagos::range(31, 24, header, dest);
header = galapagos::range(23, 16, header, id);
header = galapagos::range(15, 0, header, size);
memcpy((char *)curr_write.data, &header, sizeof(T));
memcpy((char *)curr_write.data + sizeof(T), (char *)data, size * sizeof(T));
std::lock_guard<std::mutex> guard(mutex);
packets.push_back(std::move(curr_write));
cv.notify_one();
filter_function(data, size);
#if LOG_LEVEL > 0
logger->debug("Stream {0} batch_write (CPU only) of {1:d} bytes, size of packets is{2:d}", name, size*sizeof(T), packets.size());
#endif
}
/**Executes a partial packet write (the regular write should be called prior to initialize), this function is not portable between CPU and FPGA. Please be careful to rewrite CPU functions to use an individual flit write when porting to HLS
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] data buffer to be written
@param[in] size size of buffer
*/
template <class T>
void galapagos::interface<T>::packet_write_partial(char * data, int size, bool assert_last){
#if LOG_LEVEL > 0
logger->debug("Start Stream {0} batch_write (CPU only)", name);
#endif
//size must be divisible by 8
// assert(size % 8 == 0);
//data must fit in MTU
assert((size) <= (MAX_BUFFER));
//curr_write.size = size;
// curr_write.dest = dest;
// curr_write.id = id;
//once buffer available write flit to buffer and update address
memcpy((char *)curr_write.data + write_in_prog_addr, data, size*sizeof(T));
{
std::lock_guard<std::mutex> guard(write_in_prog_mutex);
write_in_prog_addr += size * sizeof(T);
}
//last flit, moving to list
if (assert_last){
{
std::lock_guard<std::mutex> guard(write_in_prog_mutex);
curr_write.size = write_in_prog_addr - sizeof(T);
T header;
header = galapagos::range(sizeof(T)*8 - 1, 32, 0, 0); //last = 0
header = galapagos::range(31, 24, header, curr_write.dest);
header = galapagos::range(23, 16, header, curr_write.id);
header = galapagos::range(15, 0, header, curr_write.size/sizeof(T));
memcpy((char *)curr_write.data, (char *)&header, sizeof(T));
}
{
std::lock_guard<std::mutex> guard(mutex);
write_in_prog_addr = 0;
packets.push_back(std::move(curr_write));
filter_function(curr_write.data, curr_write.size);
cv.notify_one();
#if LOG_LEVEL > 0
logger->debug("Stream {0} last partial_batch_write (CPU only) of {1:d} bytes, size of packets is{2:d}", name, size*sizeof(T), packets.size());
#endif
}
}
#if LOG_LEVEL > 0
else{
logger->debug("Stream {0} partial_batch_write (CPU only) of {1:d} bytes, size of packets is{2:d}", name, size*sizeof(T), packets.size());
}
#endif
}
/**Executes a packet read, this function is not portable between CPU and FPGA. Please be careful to rewrite CPU functions to use an individual flit write when porting to HLS
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[out] _size size of buffer
@param[out] _dest dest of buffer
@param[out] _id id of buffer
@returns buffer pointing to batch of data
*/
template <class T>
char * galapagos::interface<T>::packet_read(size_t * _size, short * _dest, short * _id){
char * ret;
{
std::unique_lock<std::mutex> lock(mutex);
if(!read_in_prog_addr)
{
while (packets.empty()) {
cv.wait(lock);
}
#if LOG_LEVEL > 0
logger->debug("New Stream Read:{0}", name);
#endif
curr_read_it = packets.begin();
read_in_prog_addr = sizeof(T);
}
}
size_t buff_size = curr_read_it->size - read_in_prog_addr + sizeof(T);
*_dest = curr_read_it->dest;
*_id = curr_read_it->id;
*_size = buff_size;
ret = (char *)malloc(buff_size);
memcpy((char *)ret, (char *)curr_read_it->data + read_in_prog_addr, buff_size);
{
std::unique_lock<std::mutex> lock(mutex);
read_in_prog_addr = 0;
packets.erase(curr_read_it);
curr_read_it = packets.end();
}
return ret;
}
/**Executes a packet read, this function is not portable between CPU and FPGA. Please be careful to rewrite CPU functions to use an individual flit write when porting to HLS
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] mem memory to store read data in. No bounds checking (memory must be large enough to hold data)
@param[out] _size size of buffer
@param[out] _dest dest of buffer
@param[out] _id id of buffer
@returns buffer pointing to batch of data
*/
template <class T>
void galapagos::interface<T>::packet_read(char * mem, size_t * _size, short * _dest, short * _id){
char * ret;
{
std::unique_lock<std::mutex> lock(mutex);
if(!read_in_prog_addr)
{
while (packets.empty()) {
cv.wait(lock);
}
#if LOG_LEVEL > 0
logger->debug("New Stream Read:{0}", name);
#endif
curr_read_it = packets.begin();
read_in_prog_addr = sizeof(T);
}
}
size_t buff_size = curr_read_it->size - read_in_prog_addr + sizeof(T);
*_dest = curr_read_it->dest;
*_id = curr_read_it->id;
*_size = buff_size;
// ret = (char *)malloc(buff_size);
memcpy((char *)mem, (char *)curr_read_it->data + read_in_prog_addr, buff_size);
{
std::unique_lock<std::mutex> lock(mutex);
read_in_prog_addr = 0;
packets.erase(curr_read_it);
curr_read_it = packets.end();
}
// return ret;
}
/**Executes a packet read, this function is not portable between CPU and FPGA. Please be careful to rewrite CPU functions to use an individual flit write when porting to HLS
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[out] _size size of buffer
@returns buffer pointing to batch of data
*/
template <class T>
char * galapagos::interface<T>::packet_read(size_t * _size){
char * ret;
{
std::unique_lock<std::mutex> lock(mutex);
if(!read_in_prog_addr)
{
while (packets.empty()) {
cv.wait(lock);
}
#if LOG_LEVEL > 0
logger->debug("New Stream Read:{0}", name);
#endif
curr_read_it = packets.begin();
read_in_prog_addr = sizeof(T);
}
}
size_t buff_size = curr_read_it->size - read_in_prog_addr + sizeof(T);
*_size = buff_size;
ret = (char *)malloc(buff_size);
memcpy((char *)ret, (char *)curr_read_it->data + read_in_prog_addr, buff_size);
{
std::unique_lock<std::mutex> lock(mutex);
read_in_prog_addr = 0;
packets.erase(curr_read_it);
curr_read_it = packets.end();
}
return ret;
}
/**Get if buffer is empty
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@returns bool representing if buffer is empty
*/
template <class T>
bool galapagos::interface<T>::empty(){
bool ret = (size()==0);
return ret;
}
/**Set a filter for logging when streaming out on the packet level
@tparam pos postion of byte in packet to check
@tparam byte actual byte value
*/
template <class T>
void galapagos::interface<T>::set_filter(size_t pos, char byte){
filter_status = true;
assert(pos < MAX_BUFFER);
filter[pos] = byte;
filter_indices.push_back(pos);
assert(filter_indices.size() <= MAX_BUFFER);
}
/**Get pointer to mutex, needed to splice
@returns pointer to mutex for list
*/
template <class T>
std::mutex * galapagos::interface<T>::get_mutex(){
return &mutex;
}
/**Get pointer to list of packets, needed to splice
@returns pointer to list of packets
*/
template <class T>
std::list <galapagos::buffer> * galapagos::interface<T>::get_packets(){
return &packets;
}
/**Get pointer to list of cv, needed to splice
@returns pointer to list of packets
*/
template <class T>
std::condition_variable * galapagos::interface<T>::get_cv(){
return &cv;
}
/**Splices last element of _interface->packets into beginning of packets. The splice moves pointer and should be O(1)
@tparam _interface is the interface which we are reading from
*/
template <class T>
void galapagos::interface<T>::splice(galapagos::interface<T> * _interface){
#if LOG_LEVEL > 0
logger->debug("Start Spliced from {0} to {1}", _interface->name, name);
#endif
{
std::unique_lock<std::mutex> _lock(*(_interface->get_mutex()));
while (_interface->get_packets()->empty()) {
_interface->get_cv()->wait(_lock);
}
}
std::unique_lock<std::mutex> _lock(*(_interface->get_mutex()));
std::list <galapagos::buffer>::iterator _it = _interface->get_packets()->begin();
//_it--;
//at this point we have safely got last element of other interface
std::unique_lock<std::mutex> lock(mutex);
packets.splice(packets.end(), *(_interface->get_packets()), _it);
cv.notify_one();
_it = packets.end();
_it--;
#if LOG_LEVEL > 0
logger->debug("Spliced from {0} to {1} of size {2:d}", _interface->name, name, _it->size);
#endif
}
typedef galapagos::interface <ap_uint<PACKET_DATA_LENGTH> > galapagos_interface;
//typedef galapagos::interface <T> galapagos_interface;
#endif