!26671 Adapt dlopen macro to windows.

Merge pull request !26671 from ZPaC/adapt-dlopen-to-windows
This commit is contained in:
i-robot 2021-11-25 03:29:59 +00:00 committed by Gitee
commit 7501c11b1f
2 changed files with 30 additions and 16 deletions

View File

@ -17,18 +17,10 @@
#ifndef MINDSPORE_CCSRC_RUNTIME_HARDWARE_COLLECTIVE_COLLECTIVE_LIB_LOADER_H_
#define MINDSPORE_CCSRC_RUNTIME_HARDWARE_COLLECTIVE_COLLECTIVE_LIB_LOADER_H_
#ifndef _WIN32
#include "utils/dlopen_macro.h"
#else
#include <windows.h>
#undef ERROR
#undef SM_DEBUG
#undef Yield
#include "utils/log_adapter.h"
#endif
#include <string>
#include <memory>
#include <vector>
#include "utils/dlopen_macro.h"
namespace mindspore {
namespace device {

View File

@ -16,17 +16,31 @@
#ifndef MINDSPORE_CCSRC_UTILS_DLOPEN_MACRO_H
#define MINDSPORE_CCSRC_UTILS_DLOPEN_MACRO_H
#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#undef ERROR
#undef SM_DEBUG
#undef Yield
#endif
#include <string>
#include <functional>
#include "utils/log_adapter.h"
#define PLUGIN_METHOD(name, return_type, params...) \
extern "C" { \
__attribute__((visibility("default"))) return_type Plugin##name(params); \
} \
constexpr const char *k##name##Name = "Plugin" #name; \
using name##FunObj = std::function<return_type(params)>; \
#ifndef _WIN32
#define PORTABLE_EXPORT __attribute__((visibility("default")))
#else
#define PORTABLE_EXPORT __declspec(dllexport)
#endif
#define PLUGIN_METHOD(name, return_type, params...) \
extern "C" { \
PORTABLE_EXPORT return_type Plugin##name(params); \
} \
constexpr const char *k##name##Name = "Plugin" #name; \
using name##FunObj = std::function<return_type(params)>; \
using name##FunPtr = return_type (*)(params);
#define ORIGIN_METHOD(name, return_type, params...) \
@ -38,15 +52,23 @@
using name##FunPtr = return_type (*)(params);
inline static std::string GetDlErrorMsg() {
#ifndef _WIN32
const char *result = dlerror();
return (result == nullptr) ? "Unknown" : result;
#else
return std::to_string(GetLastError());
#endif
}
template <class T>
static T DlsymWithCast(void *handle, const char *symbol_name) {
#ifndef _WIN32
T symbol = reinterpret_cast<T>(dlsym(handle, symbol_name));
#else
T symbol = reinterpret_cast<T>(GetProcAddress(reinterpret_cast<HINSTANCE__ *>(handle), symbol_name));
#endif
if (symbol == nullptr) {
MS_LOG(EXCEPTION) << "Dlsym symbol " << symbol_name << " failed, result = " << GetDlErrorMsg();
MS_LOG(EXCEPTION) << "Dynamically load symbol " << symbol_name << " failed, result = " << GetDlErrorMsg();
}
return symbol;
}