samskalicky commented on a change in pull request #15489: Dynamic Library Loading Support URL: https://github.com/apache/incubator-mxnet/pull/15489#discussion_r309958907
########## File path: src/common/library.cc ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2015 by Contributors + * \file library.cc + * \brief Dynamically loading accelerator library + * and accessing its functions + */ + +#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__) +#include <windows.h> +#else +#include <dlfcn.h> +#endif + +#include "library.h" + + +#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__) +/*! + * \brief Retrieve the system error message for the last-error code + * \param err string that gets the error message + */ +void win_err(char **err) { + uint32_t dw = GetLastError(); + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<char*>(err), + 0, NULL); +} +#endif + + +/*! + * \brief Loads the dynamic shared library file + * \param path library file location + * \return handle a pointer for the loaded library, nullptr if loading unsuccessful + */ +void* load_lib(const char* path) { + void *handle = nullptr; +#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; + } +#else + handle = dlopen(path, RTLD_LAZY); Review comment: Thanks @leleamol, per our offline discussion we will take the following actions: 1. Create a map to track file handles and paths to the libraries when opened 2. We'll only allow dlopen to be called once, but if the library is "reloaded" by the user, we'll skip dlopen and go through the same "library registration process" again. 3. Add code to call dlclose for each library opened in the Engine descructors for each of the engine types in: https://github.com/apache/incubator-mxnet/blob/eab6da6208fdc7b68437dadc6d7d58ee11c2f528/src/engine/naive_engine.cc#L70 https://github.com/apache/incubator-mxnet/blob/3a4698058840ff79e58709b6135c8fb757a99606/src/engine/threaded_engine.h#L327 ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
