Skip to content

Commit

Permalink
Quartus custom Stream & distinction between g++ (nnet::stream) and i+…
Browse files Browse the repository at this point in the history
…+ (ihc::stream)
  • Loading branch information
bo3z committed Jul 26, 2022
1 parent 2233a68 commit e003234
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
36 changes: 36 additions & 0 deletions hls4ml/templates/quartus/ac_types/stream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef NNET_STREAM_H
#define NNET_STREAM_H

#include <deque>

namespace nnet {

/*
* A struct with the same high-level functionality as Intel's HLS ihc::stream
* This struct is used during GCC compilation / hls4ml model.predict(...)
* This is because GCC does not have access to HLS source files (ihc::stream)
* Software-wise, this struct behaves like a first-in, first-out (FIFO) buffer
* However, it cannot be used for HLS synthesis, since it uses dynamic memory allocation (deque)
*/
template<typename T>
struct stream {
private:
std::deque<T> _data;

public:
stream() {}

T read() {
T element = _data.front();
_data.pop_front();
return element;
}

void write(const T& element) {
_data.push_back(element);
}
};

}

#endif
28 changes: 28 additions & 0 deletions hls4ml/templates/quartus/firmware/defines.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
#ifndef DEFINES_H_
#define DEFINES_H_

/*
* Intel HLS makes use of three streaming interfaces:
* (1) stream_in - used as the main input to a component
* (2) stream_out - used as the main output of a component
* (3) stream - allows both reading and writing; used for inter-component connections
* ihc::stream has a implicitly deleted constructor and therefore, cannot be used as the output of a function/component
* Therefore, variables of type 'stream' are always passed by reference
*/

#ifndef __INTELFPGA_COMPILER__

#include "ac_int.h"
#include "ac_fixed.h"
#define hls_register

#include "stream.h"
template<typename T>
using stream = nnet::stream<T>;
template<typename T>
using stream_in = nnet::stream<T>;
template<typename T>
using stream_out = nnet::stream<T>;

#else

#include "HLS/hls.h"
#include "HLS/ac_int.h"
#include "HLS/ac_fixed.h"

template<typename T>
using stream = ihc::stream<T>;
template<typename T>
using stream_in = ihc::stream_in<T>;
template<typename T>
using stream_out = ihc::stream_out<T>;

#endif

//hls-fpga-machine-learning insert numbers
Expand Down

0 comments on commit e003234

Please sign in to comment.