Skip to content

Commit

Permalink
Fully separate emulator class out into shared object library format
Browse files Browse the repository at this point in the history
  • Loading branch information
aloeliger committed Feb 9, 2023
1 parent 3a9155e commit b91e03f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions emulator.cc
Original file line number Diff line number Diff line change
@@ -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_);
}
}
83 changes: 26 additions & 57 deletions emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,34 @@
#include <any>
#include <dlfcn.h>

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

0 comments on commit b91e03f

Please sign in to comment.