Skip to content

Commit

Permalink
Change major symbols to be created on load, and return model smart po…
Browse files Browse the repository at this point in the history
…inter
  • Loading branch information
aloeliger committed Feb 10, 2023
1 parent 8776ef5 commit 94ff681
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
6 changes: 2 additions & 4 deletions include/hls4ml/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <any>
#include <dlfcn.h>
#include <memory>

namespace hls4mlEmulator
{
Expand All @@ -23,11 +24,9 @@ namespace hls4mlEmulator
private:
std::string model_name_;
void* model_lib_;
Model* model_ = nullptr;

public:
ModelLoader(std::string model_name);
//prevent copying
ModelLoader(ModelLoader const&) = delete;
ModelLoader& operator=(ModelLoader const&) = delete;
//prevent move constructor/assignment
Expand All @@ -36,8 +35,7 @@ namespace hls4mlEmulator

~ModelLoader();

Model* load_model();
void destroy_model();
std::shared_ptr<Model> load_model();
};
}

Expand Down
41 changes: 24 additions & 17 deletions src/hls4ml/emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,42 @@ ModelLoader::~ModelLoader(){
dlclose(model_lib_);
}

Model* ModelLoader::load_model()

std::shared_ptr<Model> ModelLoader::load_model()
{
//Open the model .so
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!";
throw std::runtime_error("hls4ml emulator model library dlopen failure!");
}

//bind the model .so's "create_model" function to "create_model" so we can make a model later
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!";
throw std::runtime_error("hls4ml emulator failed to load 'create_model' symbol!");
}

model_ = create_model();

return model_;
}

void ModelLoader::destroy_model()
{
//bind the model .so's "destroy_model" function to "destroy" so the model can also be destroyed later
destroy_model_cls* destroy = (destroy_model_cls*) dlsym(model_lib_, "destroy_model");
const char* dlsym_error = dlerror();
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_);
throw std::runtime_error("hls4ml emulator failed to load 'destroy_model' symbol!");
}

//Create a/the model from our specific implementation in the loaded .so
Model* model = create_model();
//Hand over a shared pointer to the model
//Also hand over it's destructor, which is, in essence, the "destroy" function/symbol we created earlier
return std::shared_ptr<Model>(
model,
[destroy](Model* m)
{
if (m != nullptr)
{
destroy(m);
}
}
);
}

0 comments on commit 94ff681

Please sign in to comment.