P4 to Git Change 1986255 by kjayapra@4_HIPWS_RCCM_ROCM on 2019/08/20 17:06:44
SWDEV-198194 - Making some code common between static and dynamically created module handling.
Affected files ...
... //depot/stg/opencl/drivers/opencl/api/hip/hip_internal.hpp#37 edit
... //depot/stg/opencl/drivers/opencl/api/hip/hip_module.cpp#34 edit
... //depot/stg/opencl/drivers/opencl/api/hip/hip_platform.cpp#38 edit
[ROCm/hip commit: 15685fcc88]
Этот коммит содержится в:
@@ -129,6 +129,10 @@ public:
|
||||
amd::Memory* amd_mem_obj_;
|
||||
};
|
||||
|
||||
struct DeviceModules {
|
||||
std::vector< std::pair< hipModule_t, bool > >* modules;
|
||||
};
|
||||
|
||||
struct DeviceFunction {
|
||||
std::string deviceName;
|
||||
std::vector< std::pair< hipModule_t, bool > >* modules;
|
||||
@@ -142,6 +146,7 @@ public:
|
||||
std::vector<RegisteredVar> rvars;
|
||||
};
|
||||
private:
|
||||
std::unordered_map<hipModule_t, DeviceModules >mods_;
|
||||
std::unordered_map<const void*, DeviceFunction > functions_;
|
||||
std::unordered_map<std::string, DeviceVar > vars_;
|
||||
|
||||
@@ -154,7 +159,11 @@ public:
|
||||
return *platform_;
|
||||
}
|
||||
|
||||
void unregisterVar(hipModule_t hmod);
|
||||
void registerMod(hipModule_t hmod, const DeviceModules& dmod);
|
||||
void unregisterMod(hipModule_t hmod);
|
||||
std::vector< std::pair< hipModule_t, bool > >* findModules(hipModule_t hmod);
|
||||
|
||||
void unregisterVar(std::vector< std::pair<hipModule_t, bool> >* modules);
|
||||
|
||||
void registerVar(const void* hostvar, const DeviceVar& var);
|
||||
void registerFunction(const void* hostFunction, const DeviceFunction& func);
|
||||
@@ -170,6 +179,9 @@ public:
|
||||
void popExec(ihipExec_t& exec);
|
||||
};
|
||||
|
||||
std::vector< std::pair<hipModule_t, bool> >* ihipModuleLoadModule(const void* image);
|
||||
bool ihipModuleUnload(std::vector< std::pair<hipModule_t, bool> >* modules);
|
||||
|
||||
extern std::vector<amd::Context*> g_devices;
|
||||
extern hipError_t ihipDeviceGetCount(int* count);
|
||||
extern int ihipGetDevice();
|
||||
|
||||
@@ -51,7 +51,7 @@ static uint64_t ElfSize(const void *emi)
|
||||
return total_size;
|
||||
}
|
||||
|
||||
hipError_t hipModuleLoad(hipModule_t *module, const char *fname)
|
||||
hipError_t hipModuleLoad(hipModule_t* module, const char* fname)
|
||||
{
|
||||
HIP_INIT_API(module, fname);
|
||||
|
||||
@@ -70,27 +70,38 @@ hipError_t hipModuleLoad(hipModule_t *module, const char *fname)
|
||||
HIP_RETURN(ihipModuleLoadData(module, tmp.data()));
|
||||
}
|
||||
|
||||
bool ihipModuleUnregisterGlobal(hipModule_t hmod) {
|
||||
PlatformState::instance().unregisterVar(hmod);
|
||||
bool ihipModuleUnload(std::vector< std::pair<hipModule_t, bool> >* modules) {
|
||||
|
||||
if (modules == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PlatformState::instance().unregisterVar(modules);
|
||||
|
||||
std::for_each(modules->begin(), modules->end(), [](std::pair<hipModule_t, bool> module) {
|
||||
if (module.first != nullptr) {
|
||||
as_amd(reinterpret_cast<cl_program>(module.first))->release();
|
||||
}
|
||||
});
|
||||
|
||||
PlatformState::instance().unregisterMod((*modules)[0].first);
|
||||
delete modules;
|
||||
return true;
|
||||
}
|
||||
|
||||
hipError_t hipModuleUnload(hipModule_t hmod)
|
||||
{
|
||||
hipError_t hipModuleUnload(hipModule_t hmod) {
|
||||
HIP_INIT_API(hmod);
|
||||
|
||||
if (hmod == nullptr) {
|
||||
std::vector< std::pair<hipModule_t, bool> >* modules
|
||||
= PlatformState::instance().findModules(hmod);
|
||||
if (modules == nullptr) {
|
||||
HIP_RETURN(hipErrorUnknown);
|
||||
}
|
||||
|
||||
amd::Program* program = as_amd(reinterpret_cast<cl_program>(hmod));
|
||||
|
||||
if(!ihipModuleUnregisterGlobal(hmod)) {
|
||||
if (!ihipModuleUnload(modules)) {
|
||||
HIP_RETURN(hipErrorUnknown);
|
||||
}
|
||||
|
||||
program->release();
|
||||
|
||||
HIP_RETURN(hipSuccess);
|
||||
}
|
||||
|
||||
@@ -105,57 +116,74 @@ extern bool __hipExtractCodeObjectFromFatBinary(const void* data,
|
||||
const std::vector<const char*>& devices,
|
||||
std::vector<std::pair<const void*, size_t>>& code_objs);
|
||||
|
||||
bool ihipModuleRegisterGlobal(amd::Program* program, hipModule_t* module) {
|
||||
bool ihipModuleRegisterGlobal(amd::Program* program, int device_id,
|
||||
std::vector< std::pair<hipModule_t, bool> >* modules) {
|
||||
|
||||
size_t var_size = 0;
|
||||
hipDeviceptr_t device_ptr = nullptr;
|
||||
std::vector<std::string> var_names;
|
||||
|
||||
device::Program* dev_program
|
||||
= program->getDeviceProgram(*hip::getCurrentContext()->devices()[0]);
|
||||
= program->getDeviceProgram(*(g_devices[device_id]->devices()[0]));
|
||||
|
||||
if (!dev_program->getGlobalVarFromCodeObj(&var_names)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto it = var_names.begin(); it != var_names.end(); ++it) {
|
||||
auto modules = new std::vector<std::pair<hipModule_t, bool> >{g_devices.size()};
|
||||
for (size_t dev = 0; dev < g_devices.size(); ++dev) {
|
||||
modules->at(dev) = std::make_pair(*module, false);
|
||||
}
|
||||
|
||||
PlatformState::DeviceVar dvar{nullptr, it->c_str(), 0, modules,
|
||||
std::vector<PlatformState::RegisteredVar>{ g_devices.size()}};
|
||||
std::vector<PlatformState::RegisteredVar>{ g_devices.size()} };
|
||||
PlatformState::instance().registerVar(it->c_str(), dvar);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
hipError_t ihipModuleLoadData(hipModule_t *module, const void *image)
|
||||
{
|
||||
std::vector< std::pair<hipModule_t, bool> >* ihipModuleLoadModule(const void* image) {
|
||||
std::vector<const char*> devices;
|
||||
std::vector<std::pair<const void*, size_t>> code_objs;
|
||||
if (__hipExtractCodeObjectFromFatBinary(image, {hip::getCurrentContext()->devices()[0]->info().name_}, code_objs))
|
||||
image = code_objs[0].first;
|
||||
|
||||
amd::Program* program = new amd::Program(*hip::getCurrentContext());
|
||||
if (program == NULL) {
|
||||
return hipErrorOutOfMemory;
|
||||
for (size_t dev = 0; dev < g_devices.size(); ++dev) {
|
||||
amd::Context* ctx = g_devices[dev];
|
||||
devices.push_back(ctx->devices()[0]->info().name_);
|
||||
}
|
||||
|
||||
program->setVarInfoCallBack(&getSvarInfo);
|
||||
|
||||
if (CL_SUCCESS != program->addDeviceProgram(*hip::getCurrentContext()->devices()[0], image, ElfSize(image)) ||
|
||||
CL_SUCCESS != program->build(hip::getCurrentContext()->devices(), nullptr, nullptr, nullptr)) {
|
||||
return hipErrorUnknown;
|
||||
if (!__hipExtractCodeObjectFromFatBinary(image, devices, code_objs)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
*module = reinterpret_cast<hipModule_t>(as_cl(program));
|
||||
|
||||
if (!ihipModuleRegisterGlobal(program, module)) {
|
||||
return hipErrorUnknown;
|
||||
auto programs = new std::vector< std::pair<hipModule_t, bool> >{g_devices.size()};
|
||||
for (size_t dev = 0; dev < g_devices.size(); ++dev) {
|
||||
amd::Context* ctx = g_devices[dev];
|
||||
amd::Program* program = new amd::Program(*ctx);
|
||||
if (program == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (CL_SUCCESS == program->addDeviceProgram(*ctx->devices()[0], code_objs[dev].first, code_objs[dev].second)) {
|
||||
programs->at(dev) = std::make_pair(reinterpret_cast<hipModule_t>(as_cl(program)), false);
|
||||
}
|
||||
}
|
||||
|
||||
PlatformState::DeviceModules dmod { programs };
|
||||
PlatformState::instance().registerMod((*dmod.modules)[0].first, dmod);
|
||||
|
||||
return programs;
|
||||
}
|
||||
|
||||
hipError_t ihipModuleLoadData(hipModule_t* module, const void* image) {
|
||||
std::vector< std::pair<hipModule_t, bool> >* modules = ihipModuleLoadModule(image);
|
||||
|
||||
for (size_t deviceId = 0; deviceId < modules->size(); ++deviceId) {
|
||||
amd::Program* program = as_amd(reinterpret_cast<cl_program>((*modules)[deviceId].first));
|
||||
program->setVarInfoCallBack(&getSvarInfo);
|
||||
program->build(g_devices[deviceId]->devices(), nullptr, nullptr, nullptr);
|
||||
(*modules)[deviceId].second = true;
|
||||
|
||||
if (static_cast<size_t>(ihipGetDevice()) == deviceId) {
|
||||
*module = reinterpret_cast<hipModule_t>(as_cl(program));
|
||||
}
|
||||
|
||||
if (!ihipModuleRegisterGlobal(program, deviceId, modules)) {
|
||||
return hipErrorUnknown;
|
||||
}
|
||||
}
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
|
||||
@@ -139,39 +139,15 @@ extern "C" std::vector< std::pair<hipModule_t, bool> >* __hipRegisterFatBinary(c
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<const char*> devices;
|
||||
std::vector<std::pair<const void*, size_t>> code_objs;
|
||||
for (size_t dev = 0; dev < g_devices.size(); ++dev) {
|
||||
amd::Context* ctx = g_devices[dev];
|
||||
devices.push_back(ctx->devices()[0]->info().name_);
|
||||
}
|
||||
|
||||
if (!__hipExtractCodeObjectFromFatBinary((char*)fbwrapper->binary, devices, code_objs)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto programs = new std::vector< std::pair<hipModule_t, bool> >{g_devices.size()};
|
||||
for (size_t dev = 0; dev < g_devices.size(); ++dev) {
|
||||
amd::Context* ctx = g_devices[dev];
|
||||
amd::Program* program = new amd::Program(*ctx);
|
||||
if (program == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (CL_SUCCESS == program->addDeviceProgram(*ctx->devices()[0], code_objs[dev].first, code_objs[dev].second)) {
|
||||
programs->at(dev) = std::make_pair(reinterpret_cast<hipModule_t>(as_cl(program)) , false);
|
||||
}
|
||||
}
|
||||
|
||||
return programs;
|
||||
return ihipModuleLoadModule(fbwrapper->binary);
|
||||
}
|
||||
|
||||
void PlatformState::unregisterVar(hipModule_t hmod) {
|
||||
void PlatformState::unregisterVar(std::vector< std::pair<hipModule_t, bool> >* modules) {
|
||||
amd::ScopedLock lock(lock_);
|
||||
auto it = vars_.begin();
|
||||
while (it != vars_.end()) {
|
||||
DeviceVar& dvar = it->second;
|
||||
if ((*dvar.modules)[0].first == hmod) {
|
||||
delete dvar.modules;
|
||||
if (dvar.modules == modules) {
|
||||
vars_.erase(it++);
|
||||
} else {
|
||||
++it;
|
||||
@@ -179,6 +155,28 @@ void PlatformState::unregisterVar(hipModule_t hmod) {
|
||||
}
|
||||
}
|
||||
|
||||
void PlatformState::registerMod(hipModule_t hmod, const DeviceModules& rmod) {
|
||||
amd::ScopedLock lock(lock_);
|
||||
mods_.insert(std::make_pair(hmod, rmod));
|
||||
}
|
||||
|
||||
void PlatformState::unregisterMod(hipModule_t hmod) {
|
||||
amd::ScopedLock lock(lock_);
|
||||
auto it = mods_.find(hmod);
|
||||
if (it != mods_.cend()) {
|
||||
mods_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector< std::pair< hipModule_t, bool > >* PlatformState::findModules(hipModule_t hmod) {
|
||||
amd::ScopedLock lock(lock_);
|
||||
const auto it = mods_.find(hmod);
|
||||
if (it == mods_.cend()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second.modules;
|
||||
}
|
||||
|
||||
void PlatformState::registerVar(const void* hostvar,
|
||||
const DeviceVar& rvar) {
|
||||
amd::ScopedLock lock(lock_);
|
||||
@@ -387,12 +385,7 @@ extern "C" void __hipUnregisterFatBinary(std::vector< std::pair<hipModule_t, boo
|
||||
{
|
||||
HIP_INIT();
|
||||
|
||||
std::for_each(modules->begin(), modules->end(), [](std::pair<hipModule_t, bool> module){
|
||||
if (module.first != nullptr) {
|
||||
as_amd(reinterpret_cast<cl_program>(module.first))->release();
|
||||
}
|
||||
});
|
||||
delete modules;
|
||||
ihipModuleUnload(modules);
|
||||
}
|
||||
|
||||
extern "C" hipError_t hipConfigureCall(
|
||||
|
||||
Ссылка в новой задаче
Block a user