[nnkit] Use BackendPlugin (#1759)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 5 Oct 2018 08:39:28 +0000 (17:39 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 5 Oct 2018 08:39:28 +0000 (17:39 +0900)
This commit replaces BackendBinder implemented in nni with BackendPlugin
that nnkit_support_backend provides.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/nnkit/tools/nni/CMakeLists.txt
contrib/nnkit/tools/nni/nni.cpp

index 6f4064e..786dc15 100644 (file)
@@ -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)
index f754e53..95830e7 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <nnkit/CmdlineArguments.h>
 #include <nnkit/VectorArguments.h>
+#include <nnkit/BackendPlugin.h>
 
 namespace
 {
@@ -37,81 +38,13 @@ private:
 
 }
 
-// TODO Extract Backend-related helpers
-#include <nnkit/Backend.h>
-
-#include <memory>
-
-#include <dlfcn.h>
-#include <assert.h>
-
-namespace
-{
-
-class BackendBinder
-{
-private:
-  typedef std::unique_ptr<nnkit::Backend> (*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<Entry>(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<nnkit::Backend> 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<nnkit::Backend> initialize(void) const
   {
-    return _binder.make(args());
+    return _plugin.create(args());
   }
 
 private:
-  BackendBinder _binder;
+  nnkit::BackendPlugin _plugin;
 };
 
 }