mxnet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-mxnet] mseth10 commented on a change in pull request #15762: Refactor LibraryInitializer so it's thread safe. Fixes random sporadical concurrency crashes.
Date Thu, 08 Aug 2019 20:28:07 GMT
mseth10 commented on a change in pull request #15762: Refactor LibraryInitializer so it's thread
safe. Fixes random sporadical concurrency crashes.
URL: https://github.com/apache/incubator-mxnet/pull/15762#discussion_r312228337
 
 

 ##########
 File path: src/initialize.cc
 ##########
 @@ -41,65 +67,188 @@ static void SegfaultLogger(int sig) {
 }
 #endif
 
-class LibraryInitializer {
- public:
-  LibraryInitializer() {
-    dmlc::InitLogging("mxnet");
-#if MXNET_USE_SIGNAL_HANDLER && DMLC_LOG_STACK_TRACE
-    struct sigaction sa;
-    sigaction(SIGSEGV, nullptr, &sa);
-    if (sa.sa_handler == nullptr) {
-        signal(SIGSEGV, SegfaultLogger);
+// pthread_atfork handlers, delegated to LibraryInitializer members.
+
+void pthread_atfork_prepare() {
+  LibraryInitializer* library_initializer = LibraryInitializer::Get();
+  library_initializer->atfork_prepare();
+}
+
+void pthread_atfork_parent() {
+  LibraryInitializer* library_initializer = LibraryInitializer::Get();
+  library_initializer->atfork_parent();
+}
+
+void pthread_atfork_child() {
+  LibraryInitializer* library_initializer = LibraryInitializer::Get();
+  library_initializer->atfork_child();
+}
+
+// LibraryInitializer member functions
+
+LibraryInitializer::LibraryInitializer()
+  : original_pid_(common::current_process_id()),
+    mp_worker_nthreads_(dmlc::GetEnv("MXNET_MP_WORKER_NTHREADS", 1)),
+    cpu_worker_nthreads_(dmlc::GetEnv("MXNET_CPU_WORKER_NTHREADS", 1)),
+    mp_cv_num_threads_(dmlc::GetEnv("MXNET_MP_OPENCV_NUM_THREADS", 0)) {
+  dmlc::InitLogging("mxnet");
+  engine::OpenMP::Get();   // force OpenMP initialization
+  install_signal_handlers();
+  install_pthread_atfork_handlers();
+}
+
+LibraryInitializer::~LibraryInitializer() {
+  close_open_libs();
+}
+
+bool LibraryInitializer::lib_is_loaded(const std::string& path) const {
+  return loaded_libs.count(path) > 0;
+}
+
+/*!
+ * \brief Loads the dynamic shared library file
+ * \param path library file location
+ * \return handle a pointer for the loaded library, throws dmlc::error if library can't be
loaded
+ */
+void* LibraryInitializer::lib_load(const char* path) {
+  void *handle = nullptr;
+  // check if library was already loaded
+  if (!lib_is_loaded(path)) {
+    // if not, load it
+#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
+    handle = LoadLibrary(path);
+    if (!handle) {
+      char *err_msg = nullptr;
+      win_err(&err_msg);
+      LOG(FATAL) << "Error loading library: '" << path << "'\n" <<
err_msg;
+      LocalFree(err_msg);
+      return nullptr;
     }
-#endif
+#else
+    handle = dlopen(path, RTLD_LAZY);
+    if (!handle) {
+      LOG(FATAL) << "Error loading library: '" << path << "'\n" <<
dlerror();
+      return nullptr;
+    }
+#endif  // _WIN32 or _WIN64 or __WINDOWS__
+    // then store the pointer to the library
+    loaded_libs[path] = handle;
+  } else {
+    loaded_libs.at(path);
 
 Review comment:
   assign this to handle?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message