Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for free-running functions in simulation #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions include/hlslib/xilinx/Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace hlslib {
#ifdef HLSLIB_SYNTHESIS
#define HLSLIB_DATAFLOW_INIT()
#define HLSLIB_DATAFLOW_FUNCTION(func, ...) func(__VA_ARGS__)
#define HLSLIB_FREERUNNING_FUNCTION(func, ...) func(__VA_ARGS__)
#define HLSLIB_DATAFLOW_FINALIZE()
#else
namespace {
Expand Down Expand Up @@ -65,24 +66,34 @@ class _Dataflow {

public:
template <class Ret, typename... Args>
void AddFunction(Ret (*func)(Args...), non_deducible_t<Args>... args) {
threads_.emplace_back(func, passed_by(std::forward<Args>(args),
void AddDataflowFunction(Ret (*func)(Args...), non_deducible_t<Args>... args) {
df_threads_.emplace_back(func, passed_by(std::forward<Args>(args),
std::is_reference<Args>{})...);
}

template <class Ret, typename... Args>
void AddFreeRunningFunction(Ret (*func)(Args...), non_deducible_t<Args>... args) {
auto lambda = [](auto f, auto ...params){while(1){f(params...);}};
fr_threads_.emplace_back(lambda, func, passed_by(std::forward<Args>(args),
std::is_reference<Args>{})...);
}

inline void Join() {
for (auto& t : threads_) {
for (auto& t : df_threads_) {
t.join();
}
threads_.clear();
df_threads_.clear();
}

private:
std::vector<std::thread> threads_{};
std::vector<std::thread> df_threads_{};
std::vector<std::thread> fr_threads_{};
};
#define HLSLIB_DATAFLOW_INIT() ::hlslib::_Dataflow __hlslib_dataflow_context;
#define HLSLIB_DATAFLOW_FUNCTION(func, ...) \
__hlslib_dataflow_context.AddFunction(func, __VA_ARGS__)
__hlslib_dataflow_context.AddDataflowFunction(func, __VA_ARGS__)
#define HLSLIB_FREERUNNING_FUNCTION(func, ...) \
__hlslib_dataflow_context.AddFreeRunningFunction(func, __VA_ARGS__)
#define HLSLIB_DATAFLOW_FINALIZE() __hlslib_dataflow_context.Join();
} // namespace
#endif
Expand Down