Skip to content

Commit

Permalink
Merge pull request #6 from missirol/devel_modelLoader
Browse files Browse the repository at this point in the history
add default ctor and `reset` method to `ModelLoader`
  • Loading branch information
aloeliger authored Jan 10, 2025
2 parents 17790de + 4abd8ad commit db92d84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ $(EMULATOR_LIB): src/hls4ml/emulator.cc
$(CXX) $(CXXFLAGS) $(INCLUDES) -shared $^ -o $@

clean:
rm $(EMULATOR_LIB)
rm -f $(EMULATOR_LIB)
7 changes: 5 additions & 2 deletions include/hls4ml/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace hls4mlEmulator
void* model_lib_;

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

~ModelLoader();

std::string const& model_name() const { return model_name_; }

void reset(std::string const& model_name = "");

std::shared_ptr<Model> load_model();
};
}

#endif

29 changes: 18 additions & 11 deletions src/hls4ml/emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

using namespace hls4mlEmulator;

ModelLoader::ModelLoader(std::string model_name)
{
model_lib_ = nullptr; // Set to null for defined destructor behavior in case of failed load
model_name_ = std::move(model_name);
model_name_.append(".so");
ModelLoader::ModelLoader(std::string const& model_name)
: model_lib_{nullptr},
model_name_{model_name} {}

ModelLoader::~ModelLoader() {
reset();
}

ModelLoader::~ModelLoader(){
if (model_lib_ != nullptr)
dlclose(model_lib_);
void ModelLoader::reset(std::string const& model_name) {
model_name_ = model_name;
if (model_lib_ != nullptr) {
dlclose(model_lib_);
model_lib_ = nullptr;
}
}

std::shared_ptr<Model> ModelLoader::load_model() {
if (model_name_.empty()) {
throw std::runtime_error("uninitialised model name: failed to load hls4ml emulator model !");
}

std::shared_ptr<Model> ModelLoader::load_model()
{
//Open the model .so
model_lib_ = dlopen(model_name_.c_str(), RTLD_LAZY | RTLD_LOCAL);
std::string const model_lib_name = model_name_ + ".so";
model_lib_ = dlopen(model_lib_name.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (!model_lib_) {
std::cerr << "Cannot load library: " << dlerror() << std::endl;
throw std::runtime_error("hls4ml emulator model library dlopen failure!");
Expand Down

0 comments on commit db92d84

Please sign in to comment.