ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::compiler::BackendManager Class Reference

#include <BackendManager.h>

Public Types

using backend_create_t = backend::Backend *(*)()
 
using backend_destroy_t = void(*)(backend::Backend *)
 
using dlhandle_destroy_t = std::function< void(void *)>
 

Public Member Functions

backend::Backendget (const std::string &key)
 
const backend::Backendget (const std::string &key) const
 
const backend::BackendgetBuiltin () const
 
const std::vector< const backend::Backend * > getAll () const
 
size_t num_backends () const
 
void loadBackend (const std::string &backend)
 load backend plugin
 

Static Public Member Functions

static BackendManagerget ()
 

Detailed Description

Definition at line 31 of file BackendManager.h.

Member Typedef Documentation

◆ backend_create_t

◆ backend_destroy_t

◆ dlhandle_destroy_t

using onert::compiler::BackendManager::dlhandle_destroy_t = std::function<void(void *)>

Definition at line 36 of file BackendManager.h.

Member Function Documentation

◆ get() [1/3]

BackendManager & onert::compiler::BackendManager::get ( )
static

Definition at line 37 of file BackendManager.cc.

38{
39 static BackendManager object;
40 return object;
41}

Referenced by loadBackend(), and onert::compiler::ManualScheduler::schedule().

◆ get() [2/3]

backend::Backend * onert::compiler::BackendManager::get ( const std::string &  key)

Definition at line 130 of file BackendManager.cc.

131{
132 if (_gen_map.find(key) != _gen_map.end())
133 {
134 return _gen_map.at(key).get();
135 }
136
137 return nullptr;
138}

◆ get() [3/3]

const backend::Backend * onert::compiler::BackendManager::get ( const std::string &  key) const

Definition at line 140 of file BackendManager.cc.

141{
142 if (_gen_map.find(key) != _gen_map.end())
143 {
144 return _gen_map.at(key).get();
145 }
146
147 return nullptr;
148}

◆ getAll()

const std::vector< const backend::Backend * > onert::compiler::BackendManager::getAll ( ) const
inline

Definition at line 44 of file BackendManager.h.

45 {
46 std::vector<const backend::Backend *> v;
47 for (const auto &p : _gen_map)
48 v.emplace_back(p.second.get());
49 return v;
50 }
static BackendManager & get()

◆ getBuiltin()

const backend::Backend * onert::compiler::BackendManager::getBuiltin ( ) const

Definition at line 150 of file BackendManager.cc.

150{ return _builtin; }

◆ loadBackend()

void onert::compiler::BackendManager::loadBackend ( const std::string &  backend)

load backend plugin

Parameters
backendbackend to be loaded
Returns

Definition at line 60 of file BackendManager.cc.

61{
62 if (get(backend) != nullptr)
63 {
64 return;
65 }
66
67 const std::string backend_so = "libbackend_" + backend + SHARED_LIB_EXT;
68 void *handle = dlopen(backend_so.c_str(), RTLD_LAZY | RTLD_LOCAL);
69
70 if (handle == nullptr)
71 {
72 VERBOSE(BackendManager) << "Failed to load backend '" << backend << "' - " << dlerror() << "\n";
73 return;
74 }
75
76 VERBOSE(BackendManager) << "Successfully loaded '" << backend << "'(" << backend_so << ")\n";
77
78 {
79 // load object creator function
80 auto backend_create = (backend_create_t)dlsym(handle, "onert_backend_create");
81 if (backend_create == nullptr)
82 {
83 // TODO replace `fprintf` with `VERBOSE`
84 fprintf(stderr, "BackendManager: unable to find function `onert_backend_create` : %s\n",
85 dlerror());
86 dlclose(handle);
87 return;
88 }
89
90 // load object creator function
91 auto backend_destroy = (backend_destroy_t)dlsym(handle, "onert_backend_destroy");
92 if (backend_destroy == nullptr)
93 {
94 // TODO replace `fprintf` with `VERBOSE`
95 fprintf(stderr, "BackendManager: unable to find `function onert_backend_destroy` : %s\n",
96 dlerror());
97 dlclose(handle);
98 return;
99 }
100
101 auto backend_object =
102 std::unique_ptr<backend::Backend, backend_destroy_t>(backend_create(), backend_destroy);
103 bool initialized = backend_object->config()->initialize(); // Call initialize here?
104 if (!initialized)
105 {
106 VERBOSE(BackendManager) << backend.c_str()
107 << " backend initialization failed. Don't use this backend"
108 << std::endl;
109 dlclose(handle);
110 return;
111 }
112 _gen_map.emplace(backend_object->config()->id(), std::move(backend_object));
113 }
114
115 // Save backend handle (avoid warning by handle lost without dlclose())
116 auto u_handle = std::unique_ptr<void, dlhandle_destroy_t>{
117 handle, [id = backend, filename = backend_so](void *h) {
118 if (dlclose(h) == 0)
119 {
120 VERBOSE(BackendManager) << "Successfully unloaded '" << id << "'(" << filename << ")\n";
121 }
122 else
123 {
124 VERBOSE(BackendManager) << "Failed to unload backend '" << id << "'- " << dlerror() << "\n";
125 }
126 }};
127 _handle_map.emplace(backend, std::move(u_handle));
128}
void(*)(backend::Backend *) backend_destroy_t
backend::Backend *(*)() backend_create_t
#define VERBOSE(name, lv)
Definition Log.h:71

References get(), and VERBOSE.

◆ num_backends()

size_t onert::compiler::BackendManager::num_backends ( ) const
inline

Definition at line 51 of file BackendManager.h.

51{ return _gen_map.size(); }

The documentation for this class was generated from the following files: