Dateien
rocm-systems/src/hip_module.cpp
T

406 Zeilen
14 KiB
C++

2016-08-17 10:36:28 -05:00
/*
Copyright (c) 2015-2017 Advanced Micro Devices, Inc. All rights reserved.
2016-10-15 22:55:22 +05:30
2016-08-17 10:36:28 -05:00
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:
2016-10-15 22:55:22 +05:30
2016-08-17 10:36:28 -05:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2016-10-15 22:55:22 +05:30
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
2016-08-17 10:36:28 -05:00
THE SOFTWARE.
*/
2016-08-16 14:36:25 -05:00
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <elf.h>
2016-08-16 14:36:25 -05:00
2016-10-04 22:17:18 +05:30
#include "hsa/hsa.h"
#include "hsa/hsa_ext_amd.h"
#include "hsa/amd_hsa_kernel_code.h"
#include "hip/hip_runtime.h"
#include "hip_hcc.h"
#include "trace_helper.h"
2016-10-04 22:17:18 +05:30
//TODO Use Pool APIs from HCC to get memory regions.
2016-12-17 07:19:22 -06:00
#define CHECK_HSA(hsaStatus, hipStatus) \
2016-12-17 07:21:15 -06:00
if (hsaStatus != HSA_STATUS_SUCCESS) {\
return hipStatus;\
}
#define CHECKLOG_HSA(hsaStatus, hipStatus) \
2016-12-17 07:19:22 -06:00
if (hsaStatus != HSA_STATUS_SUCCESS) {\
return ihipLogStatus(hipStatus);\
}
2016-09-02 15:49:22 -05:00
namespace hipdrv {
2016-08-16 16:49:42 -05:00
hsa_status_t findSystemRegions(hsa_region_t region, void *data){
hsa_region_segment_t segment_id;
hsa_region_get_info(region, HSA_REGION_INFO_SEGMENT, &segment_id);
2016-08-16 16:49:42 -05:00
if(segment_id != HSA_REGION_SEGMENT_GLOBAL){
return HSA_STATUS_SUCCESS;
}
2016-08-16 16:49:42 -05:00
hsa_region_global_flag_t flags;
hsa_region_get_info(region, HSA_REGION_INFO_GLOBAL_FLAGS, &flags);
2016-08-16 16:49:42 -05:00
hsa_region_t *reg = (hsa_region_t*)data;
2016-08-16 16:49:42 -05:00
if(flags & HSA_REGION_GLOBAL_FLAG_FINE_GRAINED){
*reg = region;
}
2016-08-16 16:49:42 -05:00
return HSA_STATUS_SUCCESS;
}
} // End namespace hipdrv
uint64_t PrintSymbolSizes(const void *emi, const char *name){
const Elf64_Ehdr *ehdr = (const Elf64_Ehdr*)emi;
if(NULL == ehdr || EV_CURRENT != ehdr->e_version){}
const Elf64_Shdr * shdr = (const Elf64_Shdr*)((char*)emi + ehdr->e_shoff);
for(uint16_t i=0;i<ehdr->e_shnum;++i){
if(shdr[i].sh_type == SHT_SYMTAB){
const Elf64_Sym *syms = (const Elf64_Sym*)((char*)emi + shdr[i].sh_offset);
assert(syms);
uint64_t numSyms = shdr[i].sh_size/shdr[i].sh_entsize;
const char* strtab = (const char*)((char*)emi + shdr[shdr[i].sh_link].sh_offset);
assert(strtab);
for(uint64_t i=0;i<numSyms;++i){
const char *symname = strtab + syms[i].st_name;
assert(symname);
uint64_t size = syms[i].st_size;
if(strcmp(name, symname) == 0){
return size;
}
}
}
}
return 0;
}
uint64_t ElfSize(const void *emi){
const Elf64_Ehdr *ehdr = (const Elf64_Ehdr*)emi;
const Elf64_Shdr *shdr = (const Elf64_Shdr*)((char*)emi + ehdr->e_shoff);
uint64_t max_offset = ehdr->e_shoff;
uint64_t total_size = max_offset + ehdr->e_shentsize * ehdr->e_shnum;
for(uint16_t i=0;i < ehdr->e_shnum;++i){
uint64_t cur_offset = static_cast<uint64_t>(shdr[i].sh_offset);
if(max_offset < cur_offset){
max_offset = cur_offset;
total_size = max_offset;
if(SHT_NOBITS != shdr[i].sh_type){
total_size += static_cast<uint64_t>(shdr[i].sh_size);
}
}
}
return total_size;
}
2016-12-17 07:21:15 -06:00
2016-08-26 10:32:01 -05:00
hipError_t hipModuleLoad(hipModule_t *module, const char *fname){
2016-09-02 15:49:22 -05:00
HIP_INIT_API(module, fname);
2016-08-16 14:36:25 -05:00
hipError_t ret = hipSuccess;
*module = new ihipModule_t;
if(module == NULL){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorInvalidValue);
}
2016-08-16 14:36:25 -05:00
auto ctx = ihipGetTlsDefaultCtx();
if(ctx == nullptr){
ret = hipErrorInvalidContext;
2016-08-16 14:36:25 -05:00
}else{
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
std::ifstream in(fname, std::ios::binary | std::ios::ate);
2016-10-13 11:43:49 -05:00
if(!in.is_open() ){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorFileNotFound);
2016-10-13 11:43:49 -05:00
} else {
2016-08-16 14:36:25 -05:00
size_t size = std::string::size_type(in.tellg());
void *p = NULL;
2016-08-16 16:49:42 -05:00
hsa_agent_t agent = currentDevice->_hsaAgent;
hsa_region_t sysRegion;
hsa_status_t status = hsa_agent_iterate_regions(agent, hipdrv::findSystemRegions, &sysRegion);
2016-08-16 16:49:42 -05:00
status = hsa_memory_allocate(sysRegion, size, (void**)&p);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorOutOfMemory);
}
2016-08-16 14:36:25 -05:00
char *ptr = (char*)p;
if(!ptr){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorOutOfMemory);
2016-08-16 14:36:25 -05:00
}
(*module)->ptr = p;
(*module)->size = size;
2016-08-16 14:36:25 -05:00
in.seekg(0, std::ios::beg);
std::copy(std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>(), ptr);
status = hsa_code_object_deserialize(ptr, size, NULL, &(*module)->object);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorSharedObjectInitFailed);
}
status = hsa_executable_create(HSA_PROFILE_FULL, HSA_EXECUTABLE_STATE_UNFROZEN, NULL, &(*module)->executable);
2016-12-17 07:21:15 -06:00
CHECKLOG_HSA(status, hipErrorNotInitialized);
2016-12-17 07:19:22 -06:00
status = hsa_executable_load_code_object((*module)->executable, agent, (*module)->object, NULL);
2016-12-17 07:21:15 -06:00
CHECKLOG_HSA(status, hipErrorNotInitialized);
2016-12-17 07:19:22 -06:00
status = hsa_executable_freeze((*module)->executable, NULL);
2016-12-17 07:21:15 -06:00
CHECKLOG_HSA(status, hipErrorNotInitialized);
2016-08-16 14:36:25 -05:00
}
}
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
2016-08-16 14:36:25 -05:00
}
2016-12-17 07:21:15 -06:00
hipError_t hipModuleUnload(hipModule_t hmod)
{
// TODO - improve this synchronization so it is thread-safe.
// Currently we want for all inflight activity to complete, but don't prevent another
// thread from launching new kernels before we finish this operation.
ihipSynchronize();
hipError_t ret = hipSuccess;
hsa_status_t status = hsa_executable_destroy(hmod->executable);
2016-09-02 09:31:37 -05:00
if(status != HSA_STATUS_SUCCESS)
{
ret = hipErrorInvalidValue;
}
status = hsa_code_object_destroy(hmod->object);
2016-12-05 20:21:33 -06:00
if(status != HSA_STATUS_SUCCESS)
{
ret = hipErrorInvalidValue;
}
status = hsa_memory_free(hmod->ptr);
2016-09-02 09:31:37 -05:00
if(status != HSA_STATUS_SUCCESS)
{
ret = hipErrorInvalidValue;
}
delete hmod;
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
}
2016-12-17 07:19:22 -06:00
hipError_t ihipModuleGetSymbol(hipFunction_t *func, hipModule_t hmod, const char *name){
2016-08-16 14:36:25 -05:00
auto ctx = ihipGetTlsDefaultCtx();
hipError_t ret = hipSuccess;
if(name == nullptr){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorInvalidValue);
}
2016-08-16 14:36:25 -05:00
if(ctx == nullptr){
ret = hipErrorInvalidContext;
2016-08-16 14:36:25 -05:00
}else{
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
hsa_agent_t gpuAgent = (hsa_agent_t)currentDevice->_hsaAgent;
2016-12-17 07:19:22 -06:00
hsa_status_t status;
2016-12-17 07:21:15 -06:00
hsa_executable_symbol_t symbol;
status = hsa_executable_get_symbol(hmod->executable, NULL, name, gpuAgent, 0, &symbol);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotFound);
}
2016-12-17 07:21:15 -06:00
status = hsa_executable_symbol_get_info(symbol,
2016-08-16 14:36:25 -05:00
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT,
2016-12-17 07:19:22 -06:00
&func->_object);
2016-12-17 07:21:15 -06:00
CHECK_HSA(status, hipErrorNotFound);
2016-12-17 07:21:15 -06:00
status = hsa_executable_symbol_get_info(symbol,
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE,
&func->_groupSegmentSize);
CHECK_HSA(status, hipErrorNotFound);
status = hsa_executable_symbol_get_info(symbol,
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE,
&func->_privateSegmentSize);
CHECK_HSA(status, hipErrorNotFound);
2016-12-17 08:54:09 -06:00
strncpy(func->_name, name, sizeof(func->_name));
}
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
}
2016-09-02 15:49:22 -05:00
2016-08-26 10:32:01 -05:00
hipError_t hipModuleGetFunction(hipFunction_t *hfunc, hipModule_t hmod,
2016-08-28 16:48:57 -05:00
const char *name){
2016-09-02 15:49:22 -05:00
HIP_INIT_API(hfunc, hmod, name);
2016-12-17 07:19:22 -06:00
return ihipModuleGetSymbol(hfunc, hmod, name);
2016-08-26 10:32:01 -05:00
}
2016-08-28 16:48:57 -05:00
hipError_t hipModuleLaunchKernel(hipFunction_t f,
uint32_t gridDimX, uint32_t gridDimY, uint32_t gridDimZ,
uint32_t blockDimX, uint32_t blockDimY, uint32_t blockDimZ,
uint32_t sharedMemBytes, hipStream_t hStream,
2016-09-02 15:49:22 -05:00
void **kernelParams, void **extra)
{
HIP_INIT_API(f, gridDimX, gridDimY, gridDimZ,
blockDimX, blockDimY, blockDimZ,
sharedMemBytes, hStream,
kernelParams, extra);
auto ctx = ihipGetTlsDefaultCtx();
hipError_t ret = hipSuccess;
if(ctx == nullptr){
ret = hipErrorInvalidDevice;
}else{
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
hsa_agent_t gpuAgent = (hsa_agent_t)currentDevice->_hsaAgent;
void *config[5] = {0};
size_t kernArgSize;
if(extra != NULL){
memcpy(config, extra, sizeof(size_t)*5);
if(config[0] == HIP_LAUNCH_PARAM_BUFFER_POINTER && config[2] == HIP_LAUNCH_PARAM_BUFFER_SIZE && config[4] == HIP_LAUNCH_PARAM_END){
kernArgSize = *(size_t*)(config[3]);
2016-09-02 15:49:22 -05:00
} else {
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotInitialized);
}
}else{
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorInvalidValue);
}
2016-09-07 12:57:18 -05:00
2016-09-02 15:49:22 -05:00
/*
Kernel argument preparation.
*/
2016-12-17 08:54:09 -06:00
grid_launch_parm lp; // TODO - dummy arg but values are printed during debug.
hStream = ihipPreLaunchKernel(hStream, 0, 0, &lp, f._name);
2016-12-05 16:55:26 +05:30
hsa_kernel_dispatch_packet_t aql;
memset(&aql, 0, sizeof(aql));
//aql.completion_signal._handle = 0;
//aql.kernarg_address = 0;
aql.workgroup_size_x = blockDimX;
aql.workgroup_size_y = blockDimY;
aql.workgroup_size_z = blockDimZ;
aql.grid_size_x = blockDimX * gridDimX;
aql.grid_size_y = blockDimY * gridDimY;
aql.grid_size_z = blockDimZ * gridDimZ;
2016-12-17 07:21:15 -06:00
aql.group_segment_size = f._groupSegmentSize + sharedMemBytes;
aql.private_segment_size = f._privateSegmentSize;
2016-12-17 07:19:22 -06:00
aql.kernel_object = f._object;
2016-12-05 20:21:33 -06:00
aql.setup = 3 << HSA_KERNEL_DISPATCH_PACKET_SETUP_DIMENSIONS;
aql.header = (HSA_PACKET_TYPE_KERNEL_DISPATCH << HSA_PACKET_HEADER_TYPE) |
(1 << HSA_PACKET_HEADER_BARRIER) |
(HSA_FENCE_SCOPE_SYSTEM << HSA_PACKET_HEADER_ACQUIRE_FENCE_SCOPE) |
(HSA_FENCE_SCOPE_SYSTEM << HSA_PACKET_HEADER_RELEASE_FENCE_SCOPE);
lp.av->dispatch_hsa_kernel(&aql, config[1] /* kernarg*/, kernArgSize);
2016-12-05 16:55:26 +05:30
2016-12-17 08:54:09 -06:00
ihipPostLaunchKernel(f._name, hStream, lp);
2016-08-16 14:36:25 -05:00
}
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
2016-08-16 14:36:25 -05:00
}
hipError_t hipModuleGetGlobal(hipDeviceptr_t *dptr, size_t *bytes,
2016-09-02 15:49:22 -05:00
hipModule_t hmod, const char* name)
{
HIP_INIT_API(dptr, bytes, hmod, name);
hipError_t ret = hipSuccess;
if(dptr == NULL || bytes == NULL){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorInvalidValue);
}
if(name == NULL || hmod == NULL){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotInitialized);
}
else{
2016-08-26 10:32:01 -05:00
hipFunction_t func;
2016-12-17 07:19:22 -06:00
ihipModuleGetSymbol(&func, hmod, name);
*bytes = PrintSymbolSizes(hmod->ptr, name) + sizeof(amd_kernel_code_t);
2016-12-17 07:19:22 -06:00
*dptr = reinterpret_cast<void*>(func._object);
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
}
}
2016-09-02 15:49:22 -05:00
hipError_t hipModuleLoadData(hipModule_t *module, const void *image)
{
HIP_INIT_API(module, image);
2016-08-28 16:48:57 -05:00
hipError_t ret = hipSuccess;
if(image == NULL || module == NULL){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotInitialized);
2016-12-17 07:19:22 -06:00
} else {
auto ctx = ihipGetTlsDefaultCtx();
*module = new ihipModule_t;
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
void *p;
uint64_t size = ElfSize(image);
hsa_agent_t agent = currentDevice->_hsaAgent;
hsa_region_t sysRegion;
hsa_status_t status = hsa_agent_iterate_regions(agent, hipdrv::findSystemRegions, &sysRegion);
status = hsa_memory_allocate(sysRegion, size, (void**)&p);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorOutOfMemory);
}
char *ptr = (char*)p;
if(!ptr){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorOutOfMemory);
}
(*module)->ptr = p;
(*module)->size = size;
memcpy(ptr, image, size);
status = hsa_code_object_deserialize(ptr, size, NULL, &(*module)->object);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorSharedObjectInitFailed);
}
status = hsa_executable_create(HSA_PROFILE_FULL, HSA_EXECUTABLE_STATE_UNFROZEN, NULL, &(*module)->executable);
2016-12-17 07:21:15 -06:00
CHECKLOG_HSA(status, hipErrorNotInitialized);
2016-12-17 07:19:22 -06:00
status = hsa_executable_load_code_object((*module)->executable, agent, (*module)->object, NULL);
2016-12-17 07:21:15 -06:00
CHECKLOG_HSA(status, hipErrorNotInitialized);
2016-12-17 07:19:22 -06:00
status = hsa_executable_freeze((*module)->executable, NULL);
2016-12-17 07:21:15 -06:00
CHECKLOG_HSA(status, hipErrorNotInitialized);
}
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
}