-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemulator.h
68 lines (54 loc) · 1.66 KB
/
emulator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef HLS4ML_EMULATOR_H_
#define HLS4ML_EMULATOR_H_
#include <iostream>
#include <string>
#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;
};
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 = model_name;
_model_name.append(".so");
}
~ModelLoader() {
dlclose(_model_lib);
}
HLS4MLModel* load_model() {
_model_lib = dlopen(_model_name.c_str(), RTLD_LAZY);
if (!_model_lib) {
std::cerr << "Cannot load library: " << dlerror() << std::endl;
return nullptr;
}
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;
return nullptr;
}
_model = create_model();
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;
}
if (_model != nullptr) {
destroy(_model);
}
}
};
#endif