From 6edae7a9ba708c2becc4b69d8c027ef8e632a3c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Fri, 5 Oct 2018 17:39:28 +0900 Subject: [PATCH] [nnkit] Use BackendPlugin (#1759) This commit replaces BackendBinder implemented in nni with BackendPlugin that nnkit_support_backend provides. Signed-off-by: Jonghyun Park --- contrib/nnkit/tools/nni/CMakeLists.txt | 1 + contrib/nnkit/tools/nni/nni.cpp | 75 ++-------------------------------- 2 files changed, 5 insertions(+), 71 deletions(-) diff --git a/contrib/nnkit/tools/nni/CMakeLists.txt b/contrib/nnkit/tools/nni/CMakeLists.txt index 6f4064e..786dc15 100644 --- a/contrib/nnkit/tools/nni/CMakeLists.txt +++ b/contrib/nnkit/tools/nni/CMakeLists.txt @@ -4,4 +4,5 @@ add_executable(nni ${SOURCES}) target_link_libraries(nni nnkit_intf_action) target_link_libraries(nni nnkit_intf_backend) target_link_libraries(nni nnkit_support_cmdline) +target_link_libraries(nni nnkit_support_backend) target_link_libraries(nni dl) diff --git a/contrib/nnkit/tools/nni/nni.cpp b/contrib/nnkit/tools/nni/nni.cpp index f754e53..95830e7 100644 --- a/contrib/nnkit/tools/nni/nni.cpp +++ b/contrib/nnkit/tools/nni/nni.cpp @@ -16,6 +16,7 @@ #include #include +#include namespace { @@ -37,81 +38,13 @@ private: } -// TODO Extract Backend-related helpers -#include - -#include - -#include -#include - -namespace -{ - -class BackendBinder -{ -private: - typedef std::unique_ptr (*Entry)(const nnkit::CmdlineArguments &); - -public: - BackendBinder(const std::string &path) - { - // NOTE Some backend (such as tflite) needs multithreading support (std::thread). - // - // std::thread in libstdc++.so includes weak symbols for pthread_XXX functions, - // and these weak symbols should be overridden by strong symbols in libpthread.so. - // If not, std::thread will not work correctly. - // - // RTLD_GLOBAL flag is necessary to allow weak symbols to be overridden. - _handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL); - assert(_handle != nullptr); - - _entry = reinterpret_cast(dlsym(_handle, "make_backend")); - assert(_entry != nullptr); - } - -public: - // Copy is not allowed to avoid double close - BackendBinder(const BackendBinder &) = delete; - BackendBinder(BackendBinder &&binder) - { - // Handle is transferd from 'binder' instance into this instance. - _handle = binder._handle; - _entry = binder._entry; - - binder._handle = nullptr; - binder._entry = nullptr; - } - -public: - ~BackendBinder() - { - if (_handle != nullptr) - { - dlclose(_handle); - } - } - -public: - std::unique_ptr make(const nnkit::CmdlineArguments &args) const - { - return _entry(args); - } - -private: - void *_handle; - Entry _entry; -}; - -} - namespace { class BackendSection : public Section { public: - BackendSection(const std::string &path) : _binder{path} + BackendSection(const std::string &path) : _plugin{path} { // DO NOTHING } @@ -119,11 +52,11 @@ public: public: std::unique_ptr initialize(void) const { - return _binder.make(args()); + return _plugin.create(args()); } private: - BackendBinder _binder; + nnkit::BackendPlugin _plugin; }; } -- 2.7.4