diff --git a/Makefile b/Makefile index 68cc9b3..dcdd379 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CXXFLAGS := -O3 -fPIC -std=$(CPP_STANDARD) .PHONY: clean -emulator_interface.so: emulator.h +emulator_interface.so: emulator.cc emulator.h $(CXX) $(CXXFLAGS) -shared $^ -o $@ clean: diff --git a/emulator.cc b/emulator.cc new file mode 100644 index 0000000..a00d533 --- /dev/null +++ b/emulator.cc @@ -0,0 +1,46 @@ +#include "emulator.h" + +using namespace hls4mlEmulator; + +ModelLoader::ModelLoader(std::string model_name) +{ + model_name_ = std::move(model_name); + model_name_.append(".so"); +} + +ModelLoader::~ModelLoader(){ + dlclose(model_lib_); +} + +Model* ModelLoader::load_model() +{ + model_lib_ = dlopen(model_name_.c_str(), RTLD_LAZY | RTLD_LOCAL); + if (!model_lib_) { + std::cerr << "Cannot load library: " << dlerror() << std::endl; + throw "hls4ml emulator load_model() failure!"; + } + + create_model_cls* create_model = (create_model_cls*) dlsym(model_lib_, "create_model"); + const char* dlsym_error = dlerror(); + if (dlsym_error) { + std::cerr << "Cannot load symbol 'create_model': " << dlsym_error << std::endl; + throw "hls4ml emulator create_model() failure!"; + } + + model_ = create_model(); + + return model_; +} + +void ModelLoader::destroy_model() +{ + destroy_model_cls* destroy = (destroy_model_cls*) dlsym(model_lib_, "destroy_model"); + const char* dlsym_error = dlerror(); + if (dlsym_error) { + std::cerr << "Cannot load symbol destroy_model: " << dlsym_error << std::endl; + throw "hls4ml emulator destroy_model() failure!"; + } + if (model_ != nullptr) { + destroy(model_); + } +} \ No newline at end of file diff --git a/emulator.h b/emulator.h index cf7df2d..af27502 100644 --- a/emulator.h +++ b/emulator.h @@ -6,65 +6,34 @@ #include #include -class HLS4MLModel { -public: - virtual void prepare_input(std::any input) = 0; - virtual void predict() = 0; - virtual void read_result(std::any result) = 0; - virtual ~HLS4MLModel() = default; -}; - -typedef HLS4MLModel* create_model_cls(); -typedef void destroy_model_cls(HLS4MLModel*); - -class ModelLoader { -private: - std::string model_name_; - void* model_lib_; - HLS4MLModel* model_ = nullptr; - -public: - ModelLoader(std::string model_name) { - model_name_ = std::move(model_name); - model_name_.append(".so"); - } - - ~ModelLoader() { - dlclose(model_lib_); - } - - HLS4MLModel* load_model() { - model_lib_ = dlopen(model_name_.c_str(), RTLD_LAZY | RTLD_LOCAL); - if (!model_lib_) { - std::cerr << "Cannot load library: " << dlerror() << std::endl; - throw "hls4ml emulator load_model() failure!"; - } - - create_model_cls* create_model = (create_model_cls*) dlsym(model_lib_, "create_model"); - const char* dlsym_error = dlerror(); - if (dlsym_error) { - std::cerr << "Cannot load symbol 'create_model': " << dlsym_error << std::endl; - throw "hls4ml emulator create_model() failure!"; - } - - model_ = create_model(); +namespace hls4mlEmulator +{ + class Model { + public: + virtual void prepare_input(std::any input) = 0; + virtual void predict() = 0; + virtual void read_result(std::any result) = 0; + virtual ~Model() = default; + }; + + typedef Model* create_model_cls(); + typedef void destroy_model_cls(Model*); + + class ModelLoader { + private: + std::string model_name_; + void* model_lib_; + Model* model_ = nullptr; + + public: + ModelLoader(std::string model_name); - return model_; - } - - void destroy_model() { - destroy_model_cls* destroy = (destroy_model_cls*) dlsym(model_lib_, "destroy_model"); - const char* dlsym_error = dlerror(); - if (dlsym_error) { - std::cerr << "Cannot load symbol destroy_model: " << dlsym_error << std::endl; - throw "hls4ml emulator destroy_model() failure!"; - } - if (model_ != nullptr) { - destroy(model_); - } - } + ~ModelLoader(); -}; + Model* load_model(); + void destroy_model(); + }; +} #endif