SWDEV-286322 - Fix EOL
Change-Id: Ie266135ebf754b5af27c1dc768bda1ca0245d936
This commit is contained in:
gecommit door
Julia Jiang
bovenliggende
4dedd2436d
commit
fee2250433
+782
-782
Diff onderdrukt omdat het te groot bestand
Laad Diff
+156
-156
@@ -1,156 +1,156 @@
|
||||
/*
|
||||
Copyright (c) 2015-2020 - present Advanced Micro Devices, Inc. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef HIP_CODE_OBJECT_HPP
|
||||
#define HIP_CODE_OBJECT_HPP
|
||||
|
||||
#include "hip_global.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip/hip_runtime_api.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "device/device.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
//Forward Declaration for friend usage
|
||||
class PlatformState;
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Code Object base class
|
||||
class CodeObject {
|
||||
public:
|
||||
virtual ~CodeObject() {}
|
||||
|
||||
// Functions to add_dev_prog and build
|
||||
static hipError_t add_program(int deviceId, hipModule_t hmod, const void* binary_ptr,
|
||||
size_t binary_size);
|
||||
static hipError_t build_module(hipModule_t hmod, const std::vector<amd::Device*>& devices);
|
||||
|
||||
// Given an file desc and file size, extracts to code object for corresponding devices,
|
||||
// return code_objs{binary_ptr, binary_size}, which could be used to determine foffset
|
||||
static hipError_t ExtractCodeObjectFromFile(amd::Os::FileDesc fdesc, size_t fsize,
|
||||
const void ** image, const std::vector<std::string>& device_names,
|
||||
std::vector<std::pair<const void*, size_t>>& code_objs);
|
||||
|
||||
// Given an ptr to memory, extracts to code object for corresponding devices,
|
||||
// returns code_objs{binary_ptr, binary_size} and uniform resource indicator
|
||||
static hipError_t ExtractCodeObjectFromMemory(const void* data,
|
||||
const std::vector<std::string>& device_names,
|
||||
std::vector<std::pair<const void*, size_t>>& code_objs,
|
||||
std::string& uri);
|
||||
|
||||
static uint64_t ElfSize(const void* emi);
|
||||
|
||||
protected:
|
||||
//Given an ptr to image or file, extracts to code object
|
||||
//for corresponding devices
|
||||
static hipError_t extractCodeObjectFromFatBinary(const void*,
|
||||
const std::vector<std::string>&,
|
||||
std::vector<std::pair<const void*, size_t>>&);
|
||||
|
||||
CodeObject() {}
|
||||
private:
|
||||
friend const std::vector<hipModule_t>& modules();
|
||||
};
|
||||
|
||||
//Dynamic Code Object
|
||||
class DynCO : public CodeObject {
|
||||
amd::Monitor dclock_{"Guards Dynamic Code object", true};
|
||||
|
||||
public:
|
||||
DynCO() : device_id_(ihipGetDevice()) {}
|
||||
virtual ~DynCO();
|
||||
|
||||
//LoadsCodeObject and its data
|
||||
hipError_t loadCodeObject(const char* fname, const void* image=nullptr);
|
||||
hipModule_t module() { return fb_info_->Module(ihipGetDevice()); };
|
||||
|
||||
//Gets GlobalVar/Functions from a dynamically loaded code object
|
||||
hipError_t getDynFunc(hipFunction_t* hfunc, std::string func_name);
|
||||
hipError_t getDeviceVar(DeviceVar** dvar, std::string var_name);
|
||||
|
||||
// Device ID Check to check if module is launched in the same device it was loaded.
|
||||
inline void CheckDeviceIdMatch() {
|
||||
if (device_id_ != ihipGetDevice()) {
|
||||
guarantee(false, "Device mismatch from where this module is loaded");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int device_id_;
|
||||
FatBinaryInfo* fb_info_;
|
||||
|
||||
//Maps for vars/funcs, could be keyed in with std::string name
|
||||
std::unordered_map<std::string, Function*> functions_;
|
||||
std::unordered_map<std::string, Var*> vars_;
|
||||
|
||||
//Populate Global Vars/Funcs from an code object(@ module_load)
|
||||
hipError_t populateDynGlobalFuncs();
|
||||
hipError_t populateDynGlobalVars();
|
||||
};
|
||||
|
||||
//Static Code Object
|
||||
class StatCO: public CodeObject {
|
||||
amd::Monitor sclock_{"Guards Static Code object", true};
|
||||
public:
|
||||
StatCO();
|
||||
virtual ~StatCO();
|
||||
|
||||
//Add/Remove/Digest Fat Binaries passed to us from "__hipRegisterFatBinary"
|
||||
FatBinaryInfo** addFatBinary(const void* data, bool initialized);
|
||||
hipError_t removeFatBinary(FatBinaryInfo** module);
|
||||
hipError_t digestFatBinary(const void* data, FatBinaryInfo*& programs);
|
||||
|
||||
//Register vars/funcs given to use from __hipRegister[Var/Func/ManagedVar]
|
||||
hipError_t registerStatFunction(const void* hostFunction, Function* func);
|
||||
hipError_t registerStatGlobalVar(const void* hostVar, Var* var);
|
||||
hipError_t registerStatManagedVar(Var *var);
|
||||
|
||||
//Retrive Vars/Funcs for a given hostSidePtr(const void*), unless stated otherwise.
|
||||
hipError_t getStatFunc(hipFunction_t* hfunc, const void* hostFunction, int deviceId);
|
||||
hipError_t getStatFuncAttr(hipFuncAttributes* func_attr, const void* hostFunction, int deviceId);
|
||||
hipError_t getStatGlobalVar(const void* hostVar, int deviceId, hipDeviceptr_t* dev_ptr,
|
||||
size_t* size_ptr);
|
||||
|
||||
//Managed variable is a defined symbol in code object
|
||||
//pointer to the alocated managed memory has to be copied to the address of symbol
|
||||
hipError_t initStatManagedVarDevicePtr(int deviceId);
|
||||
private:
|
||||
friend class ::PlatformState;
|
||||
//Populated during __hipRegisterFatBinary
|
||||
std::unordered_map<const void*, FatBinaryInfo*> modules_;
|
||||
//Populated during __hipRegisterFuncs
|
||||
std::unordered_map<const void*, Function*> functions_;
|
||||
//Populated during __hipRegisterVars
|
||||
std::unordered_map<const void*, Var*> vars_;
|
||||
//Populated during __hipRegisterManagedVar
|
||||
std::vector<Var*> managedVars_;
|
||||
std::unordered_map<int, bool> managedVarsDevicePtrInitalized_;
|
||||
};
|
||||
|
||||
}; // namespace hip
|
||||
|
||||
#endif /* HIP_CODE_OBJECT_HPP */
|
||||
/*
|
||||
Copyright (c) 2015-2020 - present Advanced Micro Devices, Inc. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef HIP_CODE_OBJECT_HPP
|
||||
#define HIP_CODE_OBJECT_HPP
|
||||
|
||||
#include "hip_global.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip/hip_runtime_api.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "device/device.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
//Forward Declaration for friend usage
|
||||
class PlatformState;
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Code Object base class
|
||||
class CodeObject {
|
||||
public:
|
||||
virtual ~CodeObject() {}
|
||||
|
||||
// Functions to add_dev_prog and build
|
||||
static hipError_t add_program(int deviceId, hipModule_t hmod, const void* binary_ptr,
|
||||
size_t binary_size);
|
||||
static hipError_t build_module(hipModule_t hmod, const std::vector<amd::Device*>& devices);
|
||||
|
||||
// Given an file desc and file size, extracts to code object for corresponding devices,
|
||||
// return code_objs{binary_ptr, binary_size}, which could be used to determine foffset
|
||||
static hipError_t ExtractCodeObjectFromFile(amd::Os::FileDesc fdesc, size_t fsize,
|
||||
const void ** image, const std::vector<std::string>& device_names,
|
||||
std::vector<std::pair<const void*, size_t>>& code_objs);
|
||||
|
||||
// Given an ptr to memory, extracts to code object for corresponding devices,
|
||||
// returns code_objs{binary_ptr, binary_size} and uniform resource indicator
|
||||
static hipError_t ExtractCodeObjectFromMemory(const void* data,
|
||||
const std::vector<std::string>& device_names,
|
||||
std::vector<std::pair<const void*, size_t>>& code_objs,
|
||||
std::string& uri);
|
||||
|
||||
static uint64_t ElfSize(const void* emi);
|
||||
|
||||
protected:
|
||||
//Given an ptr to image or file, extracts to code object
|
||||
//for corresponding devices
|
||||
static hipError_t extractCodeObjectFromFatBinary(const void*,
|
||||
const std::vector<std::string>&,
|
||||
std::vector<std::pair<const void*, size_t>>&);
|
||||
|
||||
CodeObject() {}
|
||||
private:
|
||||
friend const std::vector<hipModule_t>& modules();
|
||||
};
|
||||
|
||||
//Dynamic Code Object
|
||||
class DynCO : public CodeObject {
|
||||
amd::Monitor dclock_{"Guards Dynamic Code object", true};
|
||||
|
||||
public:
|
||||
DynCO() : device_id_(ihipGetDevice()) {}
|
||||
virtual ~DynCO();
|
||||
|
||||
//LoadsCodeObject and its data
|
||||
hipError_t loadCodeObject(const char* fname, const void* image=nullptr);
|
||||
hipModule_t module() { return fb_info_->Module(ihipGetDevice()); };
|
||||
|
||||
//Gets GlobalVar/Functions from a dynamically loaded code object
|
||||
hipError_t getDynFunc(hipFunction_t* hfunc, std::string func_name);
|
||||
hipError_t getDeviceVar(DeviceVar** dvar, std::string var_name);
|
||||
|
||||
// Device ID Check to check if module is launched in the same device it was loaded.
|
||||
inline void CheckDeviceIdMatch() {
|
||||
if (device_id_ != ihipGetDevice()) {
|
||||
guarantee(false, "Device mismatch from where this module is loaded");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int device_id_;
|
||||
FatBinaryInfo* fb_info_;
|
||||
|
||||
//Maps for vars/funcs, could be keyed in with std::string name
|
||||
std::unordered_map<std::string, Function*> functions_;
|
||||
std::unordered_map<std::string, Var*> vars_;
|
||||
|
||||
//Populate Global Vars/Funcs from an code object(@ module_load)
|
||||
hipError_t populateDynGlobalFuncs();
|
||||
hipError_t populateDynGlobalVars();
|
||||
};
|
||||
|
||||
//Static Code Object
|
||||
class StatCO: public CodeObject {
|
||||
amd::Monitor sclock_{"Guards Static Code object", true};
|
||||
public:
|
||||
StatCO();
|
||||
virtual ~StatCO();
|
||||
|
||||
//Add/Remove/Digest Fat Binaries passed to us from "__hipRegisterFatBinary"
|
||||
FatBinaryInfo** addFatBinary(const void* data, bool initialized);
|
||||
hipError_t removeFatBinary(FatBinaryInfo** module);
|
||||
hipError_t digestFatBinary(const void* data, FatBinaryInfo*& programs);
|
||||
|
||||
//Register vars/funcs given to use from __hipRegister[Var/Func/ManagedVar]
|
||||
hipError_t registerStatFunction(const void* hostFunction, Function* func);
|
||||
hipError_t registerStatGlobalVar(const void* hostVar, Var* var);
|
||||
hipError_t registerStatManagedVar(Var *var);
|
||||
|
||||
//Retrive Vars/Funcs for a given hostSidePtr(const void*), unless stated otherwise.
|
||||
hipError_t getStatFunc(hipFunction_t* hfunc, const void* hostFunction, int deviceId);
|
||||
hipError_t getStatFuncAttr(hipFuncAttributes* func_attr, const void* hostFunction, int deviceId);
|
||||
hipError_t getStatGlobalVar(const void* hostVar, int deviceId, hipDeviceptr_t* dev_ptr,
|
||||
size_t* size_ptr);
|
||||
|
||||
//Managed variable is a defined symbol in code object
|
||||
//pointer to the alocated managed memory has to be copied to the address of symbol
|
||||
hipError_t initStatManagedVarDevicePtr(int deviceId);
|
||||
private:
|
||||
friend class ::PlatformState;
|
||||
//Populated during __hipRegisterFatBinary
|
||||
std::unordered_map<const void*, FatBinaryInfo*> modules_;
|
||||
//Populated during __hipRegisterFuncs
|
||||
std::unordered_map<const void*, Function*> functions_;
|
||||
//Populated during __hipRegisterVars
|
||||
std::unordered_map<const void*, Var*> vars_;
|
||||
//Populated during __hipRegisterManagedVar
|
||||
std::vector<Var*> managedVars_;
|
||||
std::unordered_map<int, bool> managedVarsDevicePtrInitalized_;
|
||||
};
|
||||
|
||||
}; // namespace hip
|
||||
|
||||
#endif /* HIP_CODE_OBJECT_HPP */
|
||||
|
||||
+158
-158
@@ -1,158 +1,158 @@
|
||||
#include "hip_fatbin.hpp"
|
||||
|
||||
#include "hip_code_object.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
FatBinaryDeviceInfo::~FatBinaryDeviceInfo() {
|
||||
if (program_ != nullptr) {
|
||||
program_->release();
|
||||
program_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FatBinaryInfo::FatBinaryInfo(const char* fname, const void* image)
|
||||
: fdesc_(amd::Os::FDescInit()), fsize_(0), image_(image), uri_(std::string()) {
|
||||
|
||||
if (fname != nullptr) {
|
||||
fname_ = std::string(fname);
|
||||
} else {
|
||||
fname_ = std::string();
|
||||
}
|
||||
|
||||
fatbin_dev_info_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
FatBinaryInfo::~FatBinaryInfo() {
|
||||
|
||||
for (auto& fbd: fatbin_dev_info_) {
|
||||
delete fbd;
|
||||
}
|
||||
|
||||
if (fdesc_ > 0) {
|
||||
if (fsize_ && !amd::Os::MemoryUnmapFile(image_, fsize_)) {
|
||||
guarantee(false, "Cannot unmap file");
|
||||
}
|
||||
if (!amd::Os::CloseFileHandle(fdesc_)) {
|
||||
guarantee(false, "Cannot close file");
|
||||
}
|
||||
}
|
||||
|
||||
fname_ = std::string();
|
||||
fdesc_ = amd::Os::FDescInit();
|
||||
fsize_ = 0;
|
||||
image_ = nullptr;
|
||||
uri_ = std::string();
|
||||
}
|
||||
|
||||
hipError_t FatBinaryInfo::ExtractFatBinary(const std::vector<hip::Device*>& devices) {
|
||||
hipError_t hip_error = hipSuccess;
|
||||
std::vector<std::pair<const void*, size_t>> code_objs;
|
||||
|
||||
// Copy device names for Extract Code object File
|
||||
std::vector<std::string> device_names;
|
||||
device_names.reserve(devices.size());
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
device_names.push_back(devices[dev_idx]->devices()[0]->isa().isaName());
|
||||
}
|
||||
|
||||
// We are given file name, get the file desc and file size
|
||||
if (fname_.size() > 0) {
|
||||
// Get File Handle & size of the file.
|
||||
if (!amd::Os::GetFileHandle(fname_.c_str(), &fdesc_, &fsize_)) {
|
||||
return hipErrorFileNotFound;
|
||||
}
|
||||
if (fsize_ == 0) {
|
||||
return hipErrorInvalidKernelFile;
|
||||
}
|
||||
|
||||
// Extract the code object from file
|
||||
hip_error = CodeObject::ExtractCodeObjectFromFile(fdesc_, fsize_, &image_,
|
||||
device_names, code_objs);
|
||||
|
||||
} else if (image_ != nullptr) {
|
||||
// We are directly given image pointer directly, try to extract file desc & file Size
|
||||
hip_error = CodeObject::ExtractCodeObjectFromMemory(image_,
|
||||
device_names, code_objs, uri_);
|
||||
} else {
|
||||
return hipErrorInvalidValue;
|
||||
}
|
||||
|
||||
if (hip_error == hipErrorNoBinaryForGpu) {
|
||||
guarantee(false, "hipErrorNoBinaryForGpu: Couldn't find binary for current devices!");
|
||||
return hip_error;
|
||||
}
|
||||
|
||||
if (hip_error == hipErrorInvalidKernelFile) {
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
// the image type is no CLANG_OFFLOAD_BUNDLER, image for current device directly passed
|
||||
fatbin_dev_info_[devices[dev_idx]->deviceId()]
|
||||
= new FatBinaryDeviceInfo(image_, CodeObject::ElfSize(image_), 0);
|
||||
}
|
||||
} else if(hip_error == hipSuccess) {
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
// Calculate the offset wrt binary_image and the original image
|
||||
size_t offset_l
|
||||
= (reinterpret_cast<address>(const_cast<void*>(code_objs[dev_idx].first))
|
||||
- reinterpret_cast<address>(const_cast<void*>(image_)));
|
||||
|
||||
fatbin_dev_info_[devices[dev_idx]->deviceId()]
|
||||
= new FatBinaryDeviceInfo(code_objs[dev_idx].first, code_objs[dev_idx].second, offset_l);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
fatbin_dev_info_[devices[dev_idx]->deviceId()]->program_
|
||||
= new amd::Program(*devices[dev_idx]->asContext());
|
||||
if (fatbin_dev_info_[devices[dev_idx]->deviceId()]->program_ == NULL) {
|
||||
return hipErrorOutOfMemory;
|
||||
}
|
||||
}
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t FatBinaryInfo::AddDevProgram(const int device_id) {
|
||||
// Device Id bounds Check
|
||||
DeviceIdCheck(device_id);
|
||||
|
||||
FatBinaryDeviceInfo* fbd_info = fatbin_dev_info_[device_id];
|
||||
// If fat binary was already added, skip this step and return success
|
||||
if (fbd_info->add_dev_prog_ == false) {
|
||||
amd::Context* ctx = g_devices[device_id]->asContext();
|
||||
if (CL_SUCCESS != fbd_info->program_->addDeviceProgram(*ctx->devices()[0],
|
||||
fbd_info->binary_image_,
|
||||
fbd_info->binary_size_, false,
|
||||
nullptr, nullptr, fdesc_,
|
||||
fbd_info->binary_offset_, uri_)) {
|
||||
return hipErrorInvalidKernelFile;
|
||||
}
|
||||
fbd_info->add_dev_prog_ = true;
|
||||
}
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t FatBinaryInfo::BuildProgram(const int device_id) {
|
||||
|
||||
// Device Id Check and Add DeviceProgram if not added so far
|
||||
DeviceIdCheck(device_id);
|
||||
IHIP_RETURN_ONFAIL(AddDevProgram(device_id));
|
||||
|
||||
// If Program was already built skip this step and return success
|
||||
FatBinaryDeviceInfo* fbd_info = fatbin_dev_info_[device_id];
|
||||
if (fbd_info->prog_built_ == false) {
|
||||
if(CL_SUCCESS != fbd_info->program_->build(g_devices[device_id]->devices(),
|
||||
nullptr, nullptr, nullptr,
|
||||
kOptionChangeable, kNewDevProg)) {
|
||||
return hipErrorSharedObjectInitFailed;
|
||||
}
|
||||
fbd_info->prog_built_ = true;
|
||||
}
|
||||
|
||||
if (!fbd_info->program_->load()) {
|
||||
return hipErrorSharedObjectInitFailed;
|
||||
}
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
} //namespace : hip
|
||||
#include "hip_fatbin.hpp"
|
||||
|
||||
#include "hip_code_object.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
FatBinaryDeviceInfo::~FatBinaryDeviceInfo() {
|
||||
if (program_ != nullptr) {
|
||||
program_->release();
|
||||
program_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FatBinaryInfo::FatBinaryInfo(const char* fname, const void* image)
|
||||
: fdesc_(amd::Os::FDescInit()), fsize_(0), image_(image), uri_(std::string()) {
|
||||
|
||||
if (fname != nullptr) {
|
||||
fname_ = std::string(fname);
|
||||
} else {
|
||||
fname_ = std::string();
|
||||
}
|
||||
|
||||
fatbin_dev_info_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
FatBinaryInfo::~FatBinaryInfo() {
|
||||
|
||||
for (auto& fbd: fatbin_dev_info_) {
|
||||
delete fbd;
|
||||
}
|
||||
|
||||
if (fdesc_ > 0) {
|
||||
if (fsize_ && !amd::Os::MemoryUnmapFile(image_, fsize_)) {
|
||||
guarantee(false, "Cannot unmap file");
|
||||
}
|
||||
if (!amd::Os::CloseFileHandle(fdesc_)) {
|
||||
guarantee(false, "Cannot close file");
|
||||
}
|
||||
}
|
||||
|
||||
fname_ = std::string();
|
||||
fdesc_ = amd::Os::FDescInit();
|
||||
fsize_ = 0;
|
||||
image_ = nullptr;
|
||||
uri_ = std::string();
|
||||
}
|
||||
|
||||
hipError_t FatBinaryInfo::ExtractFatBinary(const std::vector<hip::Device*>& devices) {
|
||||
hipError_t hip_error = hipSuccess;
|
||||
std::vector<std::pair<const void*, size_t>> code_objs;
|
||||
|
||||
// Copy device names for Extract Code object File
|
||||
std::vector<std::string> device_names;
|
||||
device_names.reserve(devices.size());
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
device_names.push_back(devices[dev_idx]->devices()[0]->isa().isaName());
|
||||
}
|
||||
|
||||
// We are given file name, get the file desc and file size
|
||||
if (fname_.size() > 0) {
|
||||
// Get File Handle & size of the file.
|
||||
if (!amd::Os::GetFileHandle(fname_.c_str(), &fdesc_, &fsize_)) {
|
||||
return hipErrorFileNotFound;
|
||||
}
|
||||
if (fsize_ == 0) {
|
||||
return hipErrorInvalidKernelFile;
|
||||
}
|
||||
|
||||
// Extract the code object from file
|
||||
hip_error = CodeObject::ExtractCodeObjectFromFile(fdesc_, fsize_, &image_,
|
||||
device_names, code_objs);
|
||||
|
||||
} else if (image_ != nullptr) {
|
||||
// We are directly given image pointer directly, try to extract file desc & file Size
|
||||
hip_error = CodeObject::ExtractCodeObjectFromMemory(image_,
|
||||
device_names, code_objs, uri_);
|
||||
} else {
|
||||
return hipErrorInvalidValue;
|
||||
}
|
||||
|
||||
if (hip_error == hipErrorNoBinaryForGpu) {
|
||||
guarantee(false, "hipErrorNoBinaryForGpu: Couldn't find binary for current devices!");
|
||||
return hip_error;
|
||||
}
|
||||
|
||||
if (hip_error == hipErrorInvalidKernelFile) {
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
// the image type is no CLANG_OFFLOAD_BUNDLER, image for current device directly passed
|
||||
fatbin_dev_info_[devices[dev_idx]->deviceId()]
|
||||
= new FatBinaryDeviceInfo(image_, CodeObject::ElfSize(image_), 0);
|
||||
}
|
||||
} else if(hip_error == hipSuccess) {
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
// Calculate the offset wrt binary_image and the original image
|
||||
size_t offset_l
|
||||
= (reinterpret_cast<address>(const_cast<void*>(code_objs[dev_idx].first))
|
||||
- reinterpret_cast<address>(const_cast<void*>(image_)));
|
||||
|
||||
fatbin_dev_info_[devices[dev_idx]->deviceId()]
|
||||
= new FatBinaryDeviceInfo(code_objs[dev_idx].first, code_objs[dev_idx].second, offset_l);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t dev_idx = 0; dev_idx < devices.size(); ++dev_idx) {
|
||||
fatbin_dev_info_[devices[dev_idx]->deviceId()]->program_
|
||||
= new amd::Program(*devices[dev_idx]->asContext());
|
||||
if (fatbin_dev_info_[devices[dev_idx]->deviceId()]->program_ == NULL) {
|
||||
return hipErrorOutOfMemory;
|
||||
}
|
||||
}
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t FatBinaryInfo::AddDevProgram(const int device_id) {
|
||||
// Device Id bounds Check
|
||||
DeviceIdCheck(device_id);
|
||||
|
||||
FatBinaryDeviceInfo* fbd_info = fatbin_dev_info_[device_id];
|
||||
// If fat binary was already added, skip this step and return success
|
||||
if (fbd_info->add_dev_prog_ == false) {
|
||||
amd::Context* ctx = g_devices[device_id]->asContext();
|
||||
if (CL_SUCCESS != fbd_info->program_->addDeviceProgram(*ctx->devices()[0],
|
||||
fbd_info->binary_image_,
|
||||
fbd_info->binary_size_, false,
|
||||
nullptr, nullptr, fdesc_,
|
||||
fbd_info->binary_offset_, uri_)) {
|
||||
return hipErrorInvalidKernelFile;
|
||||
}
|
||||
fbd_info->add_dev_prog_ = true;
|
||||
}
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t FatBinaryInfo::BuildProgram(const int device_id) {
|
||||
|
||||
// Device Id Check and Add DeviceProgram if not added so far
|
||||
DeviceIdCheck(device_id);
|
||||
IHIP_RETURN_ONFAIL(AddDevProgram(device_id));
|
||||
|
||||
// If Program was already built skip this step and return success
|
||||
FatBinaryDeviceInfo* fbd_info = fatbin_dev_info_[device_id];
|
||||
if (fbd_info->prog_built_ == false) {
|
||||
if(CL_SUCCESS != fbd_info->program_->build(g_devices[device_id]->devices(),
|
||||
nullptr, nullptr, nullptr,
|
||||
kOptionChangeable, kNewDevProg)) {
|
||||
return hipErrorSharedObjectInitFailed;
|
||||
}
|
||||
fbd_info->prog_built_ = true;
|
||||
}
|
||||
|
||||
if (!fbd_info->program_->load()) {
|
||||
return hipErrorSharedObjectInitFailed;
|
||||
}
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
} //namespace : hip
|
||||
|
||||
@@ -1,87 +1,87 @@
|
||||
#ifndef HIP_FAT_BINARY_HPP
|
||||
#define HIP_FAT_BINARY_HPP
|
||||
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip/hip_runtime_api.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Fat Binary Per Device info
|
||||
class FatBinaryDeviceInfo {
|
||||
public:
|
||||
FatBinaryDeviceInfo (const void* binary_image, size_t binary_size, size_t binary_offset)
|
||||
: binary_image_(binary_image), binary_size_(binary_size),
|
||||
binary_offset_(binary_offset), program_(nullptr),
|
||||
add_dev_prog_(false), prog_built_(false) {}
|
||||
|
||||
~FatBinaryDeviceInfo();
|
||||
|
||||
private:
|
||||
const void* binary_image_; // binary image ptr
|
||||
size_t binary_size_; // binary image size
|
||||
size_t binary_offset_; // image offset from original
|
||||
|
||||
amd::Program* program_; // reinterpreted as hipModule_t
|
||||
friend class FatBinaryInfo;
|
||||
|
||||
//Control Variables
|
||||
bool add_dev_prog_;
|
||||
bool prog_built_;
|
||||
};
|
||||
|
||||
|
||||
// Fat Binary Info
|
||||
class FatBinaryInfo {
|
||||
public:
|
||||
FatBinaryInfo(const char* fname, const void* image);
|
||||
~FatBinaryInfo();
|
||||
|
||||
// Loads Fat binary from file or image, unbundles COs for devices.
|
||||
hipError_t ExtractFatBinary(const std::vector<hip::Device*>& devices);
|
||||
hipError_t AddDevProgram(const int device_id);
|
||||
hipError_t BuildProgram(const int device_id);
|
||||
|
||||
|
||||
// Device Id bounds check
|
||||
inline void DeviceIdCheck(const int device_id) const {
|
||||
guarantee(device_id >= 0, "Invalid DeviceId less than 0");
|
||||
guarantee(static_cast<size_t>(device_id) < fatbin_dev_info_.size(), "Invalid DeviceId, greater than no of fatbin device info!");
|
||||
}
|
||||
|
||||
// Getter Methods
|
||||
amd::Program* GetProgram(int device_id) {
|
||||
DeviceIdCheck(device_id);
|
||||
return fatbin_dev_info_[device_id]->program_;
|
||||
}
|
||||
|
||||
hipModule_t Module(int device_id) const {
|
||||
DeviceIdCheck(device_id);
|
||||
return reinterpret_cast<hipModule_t>(as_cl(fatbin_dev_info_[device_id]->program_));
|
||||
}
|
||||
|
||||
hipError_t GetModule(int device_id, hipModule_t* hmod) const {
|
||||
DeviceIdCheck(device_id);
|
||||
*hmod = reinterpret_cast<hipModule_t>(as_cl(fatbin_dev_info_[device_id]->program_));
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string fname_; // File name
|
||||
amd::Os::FileDesc fdesc_; // File descriptor
|
||||
size_t fsize_; // Total file size
|
||||
|
||||
// Even when file is passed image will be mmapped till ~desctructor.
|
||||
const void* image_; // Image
|
||||
|
||||
// Only used for FBs where image is directly passed
|
||||
std::string uri_; // Uniform resource indicator
|
||||
|
||||
// Per Device Info, like corresponding binary ptr, size.
|
||||
std::vector<FatBinaryDeviceInfo*> fatbin_dev_info_;
|
||||
};
|
||||
|
||||
}; /* namespace hip */
|
||||
|
||||
#endif /* HIP_FAT_BINARY_HPP */
|
||||
#ifndef HIP_FAT_BINARY_HPP
|
||||
#define HIP_FAT_BINARY_HPP
|
||||
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip/hip_runtime_api.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Fat Binary Per Device info
|
||||
class FatBinaryDeviceInfo {
|
||||
public:
|
||||
FatBinaryDeviceInfo (const void* binary_image, size_t binary_size, size_t binary_offset)
|
||||
: binary_image_(binary_image), binary_size_(binary_size),
|
||||
binary_offset_(binary_offset), program_(nullptr),
|
||||
add_dev_prog_(false), prog_built_(false) {}
|
||||
|
||||
~FatBinaryDeviceInfo();
|
||||
|
||||
private:
|
||||
const void* binary_image_; // binary image ptr
|
||||
size_t binary_size_; // binary image size
|
||||
size_t binary_offset_; // image offset from original
|
||||
|
||||
amd::Program* program_; // reinterpreted as hipModule_t
|
||||
friend class FatBinaryInfo;
|
||||
|
||||
//Control Variables
|
||||
bool add_dev_prog_;
|
||||
bool prog_built_;
|
||||
};
|
||||
|
||||
|
||||
// Fat Binary Info
|
||||
class FatBinaryInfo {
|
||||
public:
|
||||
FatBinaryInfo(const char* fname, const void* image);
|
||||
~FatBinaryInfo();
|
||||
|
||||
// Loads Fat binary from file or image, unbundles COs for devices.
|
||||
hipError_t ExtractFatBinary(const std::vector<hip::Device*>& devices);
|
||||
hipError_t AddDevProgram(const int device_id);
|
||||
hipError_t BuildProgram(const int device_id);
|
||||
|
||||
|
||||
// Device Id bounds check
|
||||
inline void DeviceIdCheck(const int device_id) const {
|
||||
guarantee(device_id >= 0, "Invalid DeviceId less than 0");
|
||||
guarantee(static_cast<size_t>(device_id) < fatbin_dev_info_.size(), "Invalid DeviceId, greater than no of fatbin device info!");
|
||||
}
|
||||
|
||||
// Getter Methods
|
||||
amd::Program* GetProgram(int device_id) {
|
||||
DeviceIdCheck(device_id);
|
||||
return fatbin_dev_info_[device_id]->program_;
|
||||
}
|
||||
|
||||
hipModule_t Module(int device_id) const {
|
||||
DeviceIdCheck(device_id);
|
||||
return reinterpret_cast<hipModule_t>(as_cl(fatbin_dev_info_[device_id]->program_));
|
||||
}
|
||||
|
||||
hipError_t GetModule(int device_id, hipModule_t* hmod) const {
|
||||
DeviceIdCheck(device_id);
|
||||
*hmod = reinterpret_cast<hipModule_t>(as_cl(fatbin_dev_info_[device_id]->program_));
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string fname_; // File name
|
||||
amd::Os::FileDesc fdesc_; // File descriptor
|
||||
size_t fsize_; // Total file size
|
||||
|
||||
// Even when file is passed image will be mmapped till ~desctructor.
|
||||
const void* image_; // Image
|
||||
|
||||
// Only used for FBs where image is directly passed
|
||||
std::string uri_; // Uniform resource indicator
|
||||
|
||||
// Per Device Info, like corresponding binary ptr, size.
|
||||
std::vector<FatBinaryDeviceInfo*> fatbin_dev_info_;
|
||||
};
|
||||
|
||||
}; /* namespace hip */
|
||||
|
||||
#endif /* HIP_FAT_BINARY_HPP */
|
||||
|
||||
+196
-196
@@ -1,196 +1,196 @@
|
||||
#include "hip_global.hpp"
|
||||
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "hip_code_object.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Device Vars
|
||||
DeviceVar::DeviceVar(std::string name, hipModule_t hmod) : shadowVptr(nullptr), name_(name),
|
||||
amd_mem_obj_(nullptr), device_ptr_(nullptr),
|
||||
size_(0) {
|
||||
amd::Program* program = as_amd(reinterpret_cast<cl_program>(hmod));
|
||||
device::Program* dev_program = program->getDeviceProgram(*hip::getCurrentDevice()->devices()[0]);
|
||||
if (dev_program == nullptr) {
|
||||
LogPrintfError("Cannot get Device Program for module: 0x%x \n", hmod);
|
||||
guarantee(false, "Cannot get Device Program");
|
||||
}
|
||||
|
||||
if(!dev_program->createGlobalVarObj(&amd_mem_obj_, &device_ptr_, &size_, name.c_str())) {
|
||||
LogPrintfError("Cannot create Global Var obj for symbol: %s \n", name.c_str());
|
||||
guarantee(false, "Cannot create GlobalVar Obj");
|
||||
}
|
||||
|
||||
// Handle size 0 symbols
|
||||
if (size_ != 0) {
|
||||
if (amd_mem_obj_ == nullptr || device_ptr_ == nullptr) {
|
||||
LogPrintfError("Cannot get memory for creating device Var: %s", name.c_str());
|
||||
guarantee(false, "Cannot get memory for creating device var");
|
||||
}
|
||||
amd::MemObjMap::AddMemObj(device_ptr_, amd_mem_obj_);
|
||||
}
|
||||
}
|
||||
|
||||
DeviceVar::~DeviceVar() {
|
||||
if (amd_mem_obj_ != nullptr) {
|
||||
amd::MemObjMap::RemoveMemObj(device_ptr_);
|
||||
amd_mem_obj_->release();
|
||||
}
|
||||
|
||||
if (shadowVptr != nullptr) {
|
||||
textureReference* texRef = reinterpret_cast<textureReference*>(shadowVptr);
|
||||
delete texRef;
|
||||
shadowVptr = nullptr;
|
||||
}
|
||||
|
||||
device_ptr_ = nullptr;
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
//Device Functions
|
||||
DeviceFunc::DeviceFunc(std::string name, hipModule_t hmod) : dflock_("function lock"),
|
||||
name_(name), kernel_(nullptr) {
|
||||
amd::Program* program = as_amd(reinterpret_cast<cl_program>(hmod));
|
||||
|
||||
const amd::Symbol *symbol = program->findSymbol(name.c_str());
|
||||
if (symbol == nullptr) {
|
||||
LogPrintfError("Cannot find Symbol with name: %s \n", name.c_str());
|
||||
guarantee(false, "Cannot find Symbol");
|
||||
}
|
||||
|
||||
kernel_ = new amd::Kernel(*program, *symbol, name);
|
||||
if (kernel_ == nullptr) {
|
||||
LogPrintfError("Cannot create kernel with name: %s \n", name.c_str());
|
||||
guarantee(false, "Cannot Create kernel");
|
||||
}
|
||||
}
|
||||
|
||||
DeviceFunc::~DeviceFunc() {
|
||||
if (kernel_ != nullptr) {
|
||||
kernel_->release();
|
||||
}
|
||||
}
|
||||
|
||||
//Abstract functions
|
||||
Function::Function(std::string name, FatBinaryInfo** modules)
|
||||
: name_(name), modules_(modules) {
|
||||
dFunc_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
Function::~Function() {
|
||||
for (auto& elem : dFunc_) {
|
||||
delete elem;
|
||||
}
|
||||
name_ = "";
|
||||
modules_ = nullptr;
|
||||
}
|
||||
|
||||
hipError_t Function::getDynFunc(hipFunction_t* hfunc, hipModule_t hmod) {
|
||||
guarantee((dFunc_.size() == g_devices.size()), "dFunc Size mismatch");
|
||||
if (dFunc_[ihipGetDevice()] == nullptr) {
|
||||
dFunc_[ihipGetDevice()] = new DeviceFunc(name_, hmod);
|
||||
}
|
||||
*hfunc = dFunc_[ihipGetDevice()]->asHipFunction();
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t Function::getStatFunc(hipFunction_t* hfunc, int deviceId) {
|
||||
guarantee(modules_ != nullptr, "Module not initialized");
|
||||
|
||||
hipModule_t hmod = nullptr;
|
||||
IHIP_RETURN_ONFAIL((*modules_)->BuildProgram(deviceId));
|
||||
IHIP_RETURN_ONFAIL((*modules_)->GetModule(deviceId, &hmod));
|
||||
|
||||
if (dFunc_[deviceId] == nullptr) {
|
||||
dFunc_[deviceId] = new DeviceFunc(name_, hmod);
|
||||
}
|
||||
*hfunc = dFunc_[deviceId]->asHipFunction();
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t Function::getStatFuncAttr(hipFuncAttributes* func_attr, int deviceId) {
|
||||
guarantee((modules_ != nullptr), "Module not initialized");
|
||||
|
||||
hipModule_t hmod = nullptr;
|
||||
IHIP_RETURN_ONFAIL((*modules_)->BuildProgram(deviceId));
|
||||
IHIP_RETURN_ONFAIL((*modules_)->GetModule(deviceId, &hmod));
|
||||
|
||||
if (dFunc_[deviceId] == nullptr) {
|
||||
dFunc_[deviceId] = new DeviceFunc(name_, hmod);
|
||||
}
|
||||
|
||||
const std::vector<amd::Device*>& devices = amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false);
|
||||
|
||||
amd::Kernel* kernel = dFunc_[deviceId]->kernel();
|
||||
const device::Kernel::WorkGroupInfo* wginfo = kernel->getDeviceKernel(*devices[deviceId])->workGroupInfo();
|
||||
func_attr->sharedSizeBytes = static_cast<int>(wginfo->localMemSize_);
|
||||
func_attr->binaryVersion = static_cast<int>(kernel->signature().version());
|
||||
func_attr->cacheModeCA = 0;
|
||||
func_attr->constSizeBytes = 0;
|
||||
func_attr->localSizeBytes = wginfo->privateMemSize_;
|
||||
func_attr->maxDynamicSharedSizeBytes = static_cast<int>(wginfo->availableLDSSize_
|
||||
- wginfo->localMemSize_);
|
||||
|
||||
func_attr->maxThreadsPerBlock = static_cast<int>(wginfo->size_);
|
||||
func_attr->numRegs = static_cast<int>(wginfo->usedVGPRs_);
|
||||
func_attr->preferredShmemCarveout = 0;
|
||||
func_attr->ptxVersion = 30;
|
||||
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
//Abstract Vars
|
||||
Var::Var(std::string name, DeviceVarKind dVarKind, size_t size, int type, int norm,
|
||||
FatBinaryInfo** modules) : name_(name), dVarKind_(dVarKind), size_(size),
|
||||
type_(type), norm_(norm), modules_(modules) {
|
||||
dVar_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
Var::Var(std::string name, DeviceVarKind dVarKind, void *pointer, size_t size,
|
||||
unsigned align, FatBinaryInfo** modules) : name_(name), dVarKind_(dVarKind),
|
||||
size_(size), modules_(modules), managedVarPtr_(pointer), align_(align) {
|
||||
dVar_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
Var::~Var() {
|
||||
for (auto& elem : dVar_) {
|
||||
delete elem;
|
||||
}
|
||||
modules_ = nullptr;
|
||||
}
|
||||
|
||||
hipError_t Var::getDeviceVar(DeviceVar** dvar, int deviceId, hipModule_t hmod) {
|
||||
guarantee((deviceId >= 0), "Invalid DeviceId, less than zero");
|
||||
guarantee((static_cast<size_t>(deviceId) < g_devices.size()),
|
||||
"Invalid DeviceId, greater than no of code objects");
|
||||
guarantee((dVar_.size() == g_devices.size()),
|
||||
"Device Var not initialized to size");
|
||||
|
||||
if (dVar_[deviceId] == nullptr) {
|
||||
dVar_[deviceId] = new DeviceVar(name_, hmod);
|
||||
}
|
||||
|
||||
*dvar = dVar_[deviceId];
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t Var::getStatDeviceVar(DeviceVar** dvar, int deviceId) {
|
||||
guarantee((deviceId >= 0) , "Invalid DeviceId, less than zero");
|
||||
guarantee((static_cast<size_t>(deviceId) < g_devices.size()),
|
||||
"Invalid DeviceId, greater than no of code objects");
|
||||
if (dVar_[deviceId] == nullptr) {
|
||||
hipModule_t hmod = nullptr;
|
||||
IHIP_RETURN_ONFAIL((*modules_)->BuildProgram(deviceId));
|
||||
IHIP_RETURN_ONFAIL((*modules_)->GetModule(deviceId, &hmod));
|
||||
dVar_[deviceId] = new DeviceVar(name_, hmod);
|
||||
}
|
||||
*dvar = dVar_[deviceId];
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
}; //namespace: hip
|
||||
#include "hip_global.hpp"
|
||||
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "hip_code_object.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Device Vars
|
||||
DeviceVar::DeviceVar(std::string name, hipModule_t hmod) : shadowVptr(nullptr), name_(name),
|
||||
amd_mem_obj_(nullptr), device_ptr_(nullptr),
|
||||
size_(0) {
|
||||
amd::Program* program = as_amd(reinterpret_cast<cl_program>(hmod));
|
||||
device::Program* dev_program = program->getDeviceProgram(*hip::getCurrentDevice()->devices()[0]);
|
||||
if (dev_program == nullptr) {
|
||||
LogPrintfError("Cannot get Device Program for module: 0x%x \n", hmod);
|
||||
guarantee(false, "Cannot get Device Program");
|
||||
}
|
||||
|
||||
if(!dev_program->createGlobalVarObj(&amd_mem_obj_, &device_ptr_, &size_, name.c_str())) {
|
||||
LogPrintfError("Cannot create Global Var obj for symbol: %s \n", name.c_str());
|
||||
guarantee(false, "Cannot create GlobalVar Obj");
|
||||
}
|
||||
|
||||
// Handle size 0 symbols
|
||||
if (size_ != 0) {
|
||||
if (amd_mem_obj_ == nullptr || device_ptr_ == nullptr) {
|
||||
LogPrintfError("Cannot get memory for creating device Var: %s", name.c_str());
|
||||
guarantee(false, "Cannot get memory for creating device var");
|
||||
}
|
||||
amd::MemObjMap::AddMemObj(device_ptr_, amd_mem_obj_);
|
||||
}
|
||||
}
|
||||
|
||||
DeviceVar::~DeviceVar() {
|
||||
if (amd_mem_obj_ != nullptr) {
|
||||
amd::MemObjMap::RemoveMemObj(device_ptr_);
|
||||
amd_mem_obj_->release();
|
||||
}
|
||||
|
||||
if (shadowVptr != nullptr) {
|
||||
textureReference* texRef = reinterpret_cast<textureReference*>(shadowVptr);
|
||||
delete texRef;
|
||||
shadowVptr = nullptr;
|
||||
}
|
||||
|
||||
device_ptr_ = nullptr;
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
//Device Functions
|
||||
DeviceFunc::DeviceFunc(std::string name, hipModule_t hmod) : dflock_("function lock"),
|
||||
name_(name), kernel_(nullptr) {
|
||||
amd::Program* program = as_amd(reinterpret_cast<cl_program>(hmod));
|
||||
|
||||
const amd::Symbol *symbol = program->findSymbol(name.c_str());
|
||||
if (symbol == nullptr) {
|
||||
LogPrintfError("Cannot find Symbol with name: %s \n", name.c_str());
|
||||
guarantee(false, "Cannot find Symbol");
|
||||
}
|
||||
|
||||
kernel_ = new amd::Kernel(*program, *symbol, name);
|
||||
if (kernel_ == nullptr) {
|
||||
LogPrintfError("Cannot create kernel with name: %s \n", name.c_str());
|
||||
guarantee(false, "Cannot Create kernel");
|
||||
}
|
||||
}
|
||||
|
||||
DeviceFunc::~DeviceFunc() {
|
||||
if (kernel_ != nullptr) {
|
||||
kernel_->release();
|
||||
}
|
||||
}
|
||||
|
||||
//Abstract functions
|
||||
Function::Function(std::string name, FatBinaryInfo** modules)
|
||||
: name_(name), modules_(modules) {
|
||||
dFunc_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
Function::~Function() {
|
||||
for (auto& elem : dFunc_) {
|
||||
delete elem;
|
||||
}
|
||||
name_ = "";
|
||||
modules_ = nullptr;
|
||||
}
|
||||
|
||||
hipError_t Function::getDynFunc(hipFunction_t* hfunc, hipModule_t hmod) {
|
||||
guarantee((dFunc_.size() == g_devices.size()), "dFunc Size mismatch");
|
||||
if (dFunc_[ihipGetDevice()] == nullptr) {
|
||||
dFunc_[ihipGetDevice()] = new DeviceFunc(name_, hmod);
|
||||
}
|
||||
*hfunc = dFunc_[ihipGetDevice()]->asHipFunction();
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t Function::getStatFunc(hipFunction_t* hfunc, int deviceId) {
|
||||
guarantee(modules_ != nullptr, "Module not initialized");
|
||||
|
||||
hipModule_t hmod = nullptr;
|
||||
IHIP_RETURN_ONFAIL((*modules_)->BuildProgram(deviceId));
|
||||
IHIP_RETURN_ONFAIL((*modules_)->GetModule(deviceId, &hmod));
|
||||
|
||||
if (dFunc_[deviceId] == nullptr) {
|
||||
dFunc_[deviceId] = new DeviceFunc(name_, hmod);
|
||||
}
|
||||
*hfunc = dFunc_[deviceId]->asHipFunction();
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t Function::getStatFuncAttr(hipFuncAttributes* func_attr, int deviceId) {
|
||||
guarantee((modules_ != nullptr), "Module not initialized");
|
||||
|
||||
hipModule_t hmod = nullptr;
|
||||
IHIP_RETURN_ONFAIL((*modules_)->BuildProgram(deviceId));
|
||||
IHIP_RETURN_ONFAIL((*modules_)->GetModule(deviceId, &hmod));
|
||||
|
||||
if (dFunc_[deviceId] == nullptr) {
|
||||
dFunc_[deviceId] = new DeviceFunc(name_, hmod);
|
||||
}
|
||||
|
||||
const std::vector<amd::Device*>& devices = amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false);
|
||||
|
||||
amd::Kernel* kernel = dFunc_[deviceId]->kernel();
|
||||
const device::Kernel::WorkGroupInfo* wginfo = kernel->getDeviceKernel(*devices[deviceId])->workGroupInfo();
|
||||
func_attr->sharedSizeBytes = static_cast<int>(wginfo->localMemSize_);
|
||||
func_attr->binaryVersion = static_cast<int>(kernel->signature().version());
|
||||
func_attr->cacheModeCA = 0;
|
||||
func_attr->constSizeBytes = 0;
|
||||
func_attr->localSizeBytes = wginfo->privateMemSize_;
|
||||
func_attr->maxDynamicSharedSizeBytes = static_cast<int>(wginfo->availableLDSSize_
|
||||
- wginfo->localMemSize_);
|
||||
|
||||
func_attr->maxThreadsPerBlock = static_cast<int>(wginfo->size_);
|
||||
func_attr->numRegs = static_cast<int>(wginfo->usedVGPRs_);
|
||||
func_attr->preferredShmemCarveout = 0;
|
||||
func_attr->ptxVersion = 30;
|
||||
|
||||
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
//Abstract Vars
|
||||
Var::Var(std::string name, DeviceVarKind dVarKind, size_t size, int type, int norm,
|
||||
FatBinaryInfo** modules) : name_(name), dVarKind_(dVarKind), size_(size),
|
||||
type_(type), norm_(norm), modules_(modules) {
|
||||
dVar_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
Var::Var(std::string name, DeviceVarKind dVarKind, void *pointer, size_t size,
|
||||
unsigned align, FatBinaryInfo** modules) : name_(name), dVarKind_(dVarKind),
|
||||
size_(size), modules_(modules), managedVarPtr_(pointer), align_(align) {
|
||||
dVar_.resize(g_devices.size());
|
||||
}
|
||||
|
||||
Var::~Var() {
|
||||
for (auto& elem : dVar_) {
|
||||
delete elem;
|
||||
}
|
||||
modules_ = nullptr;
|
||||
}
|
||||
|
||||
hipError_t Var::getDeviceVar(DeviceVar** dvar, int deviceId, hipModule_t hmod) {
|
||||
guarantee((deviceId >= 0), "Invalid DeviceId, less than zero");
|
||||
guarantee((static_cast<size_t>(deviceId) < g_devices.size()),
|
||||
"Invalid DeviceId, greater than no of code objects");
|
||||
guarantee((dVar_.size() == g_devices.size()),
|
||||
"Device Var not initialized to size");
|
||||
|
||||
if (dVar_[deviceId] == nullptr) {
|
||||
dVar_[deviceId] = new DeviceVar(name_, hmod);
|
||||
}
|
||||
|
||||
*dvar = dVar_[deviceId];
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
hipError_t Var::getStatDeviceVar(DeviceVar** dvar, int deviceId) {
|
||||
guarantee((deviceId >= 0) , "Invalid DeviceId, less than zero");
|
||||
guarantee((static_cast<size_t>(deviceId) < g_devices.size()),
|
||||
"Invalid DeviceId, greater than no of code objects");
|
||||
if (dVar_[deviceId] == nullptr) {
|
||||
hipModule_t hmod = nullptr;
|
||||
IHIP_RETURN_ONFAIL((*modules_)->BuildProgram(deviceId));
|
||||
IHIP_RETURN_ONFAIL((*modules_)->GetModule(deviceId, &hmod));
|
||||
dVar_[deviceId] = new DeviceVar(name_, hmod);
|
||||
}
|
||||
*dvar = dVar_[deviceId];
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
}; //namespace: hip
|
||||
|
||||
+119
-119
@@ -1,119 +1,119 @@
|
||||
#ifndef HIP_GLOBAL_HPP
|
||||
#define HIP_GLOBAL_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "hip/hip_runtime_api.h"
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "hip_fatbin.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Forward Declaration
|
||||
class CodeObject;
|
||||
|
||||
//Device Structures
|
||||
class DeviceVar {
|
||||
public:
|
||||
DeviceVar(std::string name, hipModule_t hmod);
|
||||
~DeviceVar();
|
||||
|
||||
//Accessors for device ptr and size, populated during constructor.
|
||||
hipDeviceptr_t device_ptr() const { return device_ptr_; }
|
||||
size_t size() const { return size_; }
|
||||
std::string name() const { return name_; }
|
||||
void* shadowVptr;
|
||||
|
||||
private:
|
||||
std::string name_; //Name of the var
|
||||
amd::Memory* amd_mem_obj_; //amd_mem_obj abstraction
|
||||
hipDeviceptr_t device_ptr_; //Device Pointer
|
||||
size_t size_; //Size of the var
|
||||
};
|
||||
|
||||
class DeviceFunc {
|
||||
public:
|
||||
DeviceFunc(std::string name, hipModule_t hmod);
|
||||
~DeviceFunc();
|
||||
|
||||
amd::Monitor dflock_;
|
||||
|
||||
//Converts DeviceFunc to hipFunction_t(used by app) and vice versa.
|
||||
hipFunction_t asHipFunction() { return reinterpret_cast<hipFunction_t>(this); }
|
||||
static DeviceFunc* asFunction(hipFunction_t f) { return reinterpret_cast<DeviceFunc*>(f); }
|
||||
|
||||
//Accessor for kernel_ and name_ populated during constructor.
|
||||
std::string name() const { return name_; }
|
||||
amd::Kernel* kernel() const { return kernel_; }
|
||||
|
||||
private:
|
||||
std::string name_; //name of the func(not unique identifier)
|
||||
amd::Kernel* kernel_; //Kernel ptr referencing to ROCclr Symbol
|
||||
};
|
||||
|
||||
//Abstract Structures
|
||||
class Function {
|
||||
public:
|
||||
Function(std::string name, FatBinaryInfo** modules=nullptr);
|
||||
~Function();
|
||||
|
||||
//Return DeviceFunc for this this dynamically loaded module
|
||||
hipError_t getDynFunc(hipFunction_t* hfunc, hipModule_t hmod);
|
||||
|
||||
//Return Device Func & attr . Generate/build if not already done so.
|
||||
hipError_t getStatFunc(hipFunction_t *hfunc, int deviceId);
|
||||
hipError_t getStatFuncAttr(hipFuncAttributes* func_attr, int deviceId);
|
||||
void resize_dFunc(size_t size) { dFunc_.resize(size); }
|
||||
FatBinaryInfo** moduleInfo() { return modules_; };
|
||||
|
||||
private:
|
||||
std::vector<DeviceFunc*> dFunc_; //DeviceFuncObj per Device
|
||||
std::string name_; //name of the func(not unique identifier)
|
||||
FatBinaryInfo** modules_; // static module where it is referenced
|
||||
};
|
||||
|
||||
class Var {
|
||||
public:
|
||||
//Types of variable
|
||||
enum DeviceVarKind {
|
||||
DVK_Variable = 0,
|
||||
DVK_Surface,
|
||||
DVK_Texture,
|
||||
DVK_Managed
|
||||
};
|
||||
|
||||
Var(std::string name, DeviceVarKind dVarKind, size_t size, int type, int norm,
|
||||
FatBinaryInfo** modules = nullptr);
|
||||
|
||||
Var(std::string name, DeviceVarKind dVarKind, void *pointer, size_t size, unsigned align,
|
||||
FatBinaryInfo** modules = nullptr);
|
||||
|
||||
~Var();
|
||||
|
||||
//Return DeviceVar for this dynamically loaded module
|
||||
hipError_t getDeviceVar(DeviceVar** dvar, int deviceId, hipModule_t hmod);
|
||||
|
||||
//Return DeviceVar for module Generate/build if not already done so.
|
||||
hipError_t getStatDeviceVar(DeviceVar** dvar, int deviceId);
|
||||
void resize_dVar(size_t size) { dVar_.resize(size); }
|
||||
|
||||
FatBinaryInfo** moduleInfo() { return modules_; };
|
||||
void* getManagedVarPtr() { return managedVarPtr_; };
|
||||
private:
|
||||
std::vector<DeviceVar*> dVar_; // DeviceVarObj per Device
|
||||
std::string name_; // Variable name (not unique identifier)
|
||||
DeviceVarKind dVarKind_; // Variable kind
|
||||
size_t size_; // Size of the variable
|
||||
int type_; // Type(Textures/Surfaces only)
|
||||
int norm_; // Type(Textures/Surfaces only)
|
||||
FatBinaryInfo** modules_; // static module where it is referenced
|
||||
|
||||
void *managedVarPtr_; // Managed memory pointer with size_ & align_
|
||||
unsigned int align_; // Managed memory alignment
|
||||
};
|
||||
|
||||
}; //namespace: hip
|
||||
#endif /* HIP_GLOBAL_HPP */
|
||||
#ifndef HIP_GLOBAL_HPP
|
||||
#define HIP_GLOBAL_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "hip/hip_runtime_api.h"
|
||||
#include "hip/hip_runtime.h"
|
||||
#include "hip_internal.hpp"
|
||||
#include "hip_fatbin.hpp"
|
||||
#include "platform/program.hpp"
|
||||
|
||||
namespace hip {
|
||||
|
||||
//Forward Declaration
|
||||
class CodeObject;
|
||||
|
||||
//Device Structures
|
||||
class DeviceVar {
|
||||
public:
|
||||
DeviceVar(std::string name, hipModule_t hmod);
|
||||
~DeviceVar();
|
||||
|
||||
//Accessors for device ptr and size, populated during constructor.
|
||||
hipDeviceptr_t device_ptr() const { return device_ptr_; }
|
||||
size_t size() const { return size_; }
|
||||
std::string name() const { return name_; }
|
||||
void* shadowVptr;
|
||||
|
||||
private:
|
||||
std::string name_; //Name of the var
|
||||
amd::Memory* amd_mem_obj_; //amd_mem_obj abstraction
|
||||
hipDeviceptr_t device_ptr_; //Device Pointer
|
||||
size_t size_; //Size of the var
|
||||
};
|
||||
|
||||
class DeviceFunc {
|
||||
public:
|
||||
DeviceFunc(std::string name, hipModule_t hmod);
|
||||
~DeviceFunc();
|
||||
|
||||
amd::Monitor dflock_;
|
||||
|
||||
//Converts DeviceFunc to hipFunction_t(used by app) and vice versa.
|
||||
hipFunction_t asHipFunction() { return reinterpret_cast<hipFunction_t>(this); }
|
||||
static DeviceFunc* asFunction(hipFunction_t f) { return reinterpret_cast<DeviceFunc*>(f); }
|
||||
|
||||
//Accessor for kernel_ and name_ populated during constructor.
|
||||
std::string name() const { return name_; }
|
||||
amd::Kernel* kernel() const { return kernel_; }
|
||||
|
||||
private:
|
||||
std::string name_; //name of the func(not unique identifier)
|
||||
amd::Kernel* kernel_; //Kernel ptr referencing to ROCclr Symbol
|
||||
};
|
||||
|
||||
//Abstract Structures
|
||||
class Function {
|
||||
public:
|
||||
Function(std::string name, FatBinaryInfo** modules=nullptr);
|
||||
~Function();
|
||||
|
||||
//Return DeviceFunc for this this dynamically loaded module
|
||||
hipError_t getDynFunc(hipFunction_t* hfunc, hipModule_t hmod);
|
||||
|
||||
//Return Device Func & attr . Generate/build if not already done so.
|
||||
hipError_t getStatFunc(hipFunction_t *hfunc, int deviceId);
|
||||
hipError_t getStatFuncAttr(hipFuncAttributes* func_attr, int deviceId);
|
||||
void resize_dFunc(size_t size) { dFunc_.resize(size); }
|
||||
FatBinaryInfo** moduleInfo() { return modules_; };
|
||||
|
||||
private:
|
||||
std::vector<DeviceFunc*> dFunc_; //DeviceFuncObj per Device
|
||||
std::string name_; //name of the func(not unique identifier)
|
||||
FatBinaryInfo** modules_; // static module where it is referenced
|
||||
};
|
||||
|
||||
class Var {
|
||||
public:
|
||||
//Types of variable
|
||||
enum DeviceVarKind {
|
||||
DVK_Variable = 0,
|
||||
DVK_Surface,
|
||||
DVK_Texture,
|
||||
DVK_Managed
|
||||
};
|
||||
|
||||
Var(std::string name, DeviceVarKind dVarKind, size_t size, int type, int norm,
|
||||
FatBinaryInfo** modules = nullptr);
|
||||
|
||||
Var(std::string name, DeviceVarKind dVarKind, void *pointer, size_t size, unsigned align,
|
||||
FatBinaryInfo** modules = nullptr);
|
||||
|
||||
~Var();
|
||||
|
||||
//Return DeviceVar for this dynamically loaded module
|
||||
hipError_t getDeviceVar(DeviceVar** dvar, int deviceId, hipModule_t hmod);
|
||||
|
||||
//Return DeviceVar for module Generate/build if not already done so.
|
||||
hipError_t getStatDeviceVar(DeviceVar** dvar, int deviceId);
|
||||
void resize_dVar(size_t size) { dVar_.resize(size); }
|
||||
|
||||
FatBinaryInfo** moduleInfo() { return modules_; };
|
||||
void* getManagedVarPtr() { return managedVarPtr_; };
|
||||
private:
|
||||
std::vector<DeviceVar*> dVar_; // DeviceVarObj per Device
|
||||
std::string name_; // Variable name (not unique identifier)
|
||||
DeviceVarKind dVarKind_; // Variable kind
|
||||
size_t size_; // Size of the variable
|
||||
int type_; // Type(Textures/Surfaces only)
|
||||
int norm_; // Type(Textures/Surfaces only)
|
||||
FatBinaryInfo** modules_; // static module where it is referenced
|
||||
|
||||
void *managedVarPtr_; // Managed memory pointer with size_ & align_
|
||||
unsigned int align_; // Managed memory alignment
|
||||
};
|
||||
|
||||
}; //namespace: hip
|
||||
#endif /* HIP_GLOBAL_HPP */
|
||||
|
||||
Verwijs in nieuw issue
Block a user