Files
rocm-systems/src/hip_module.cpp
T

463 строки
15 KiB
C++
Исходник Обычный вид История

2016-08-17 10:36:28 -05:00
/*
Copyright (c) 2015-2016 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-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-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);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(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
}
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-08-26 10:32:01 -05:00
hipError_t ihipModuleGetFunction(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{
2016-09-02 15:49:22 -05:00
*func = new ihipFunction_t(name);
2016-12-05 20:21:33 -06:00
hmod->registerFunction(*func);
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
hsa_agent_t gpuAgent = (hsa_agent_t)currentDevice->_hsaAgent;
hsa_status_t status;
status = hsa_executable_load_code_object(hmod->executable, gpuAgent, hmod->object, NULL);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotInitialized);
}
status = hsa_executable_freeze(hmod->executable, NULL);
2016-09-02 15:49:22 -05:00
status = hsa_executable_get_symbol(hmod->executable, NULL, name, gpuAgent, 0, &(*func)->_kernelSymbol);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotFound);
}
2016-09-02 15:49:22 -05:00
status = hsa_executable_symbol_get_info((*func)->_kernelSymbol,
2016-08-16 14:36:25 -05:00
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT,
2016-09-02 15:49:22 -05:00
&(*func)->_kernel);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotFound);
}
}
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-08-26 10:32:01 -05:00
return ihipModuleGetFunction(hfunc, hmod, name);
}
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
uint32_t groupSegmentSize;
2016-09-07 15:48:40 -05:00
hsa_status_t status = hsa_executable_symbol_get_info(f->_kernelSymbol,
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE,
&groupSegmentSize);
2016-12-05 20:21:33 -06:00
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
2016-09-07 12:57:18 -05:00
2016-09-02 15:49:22 -05:00
uint32_t privateSegmentSize;
2016-09-07 15:48:40 -05:00
status = hsa_executable_symbol_get_info(f->_kernelSymbol,
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE,
&privateSegmentSize);
2016-12-05 20:21:33 -06:00
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
2016-09-02 15:49:22 -05:00
privateSegmentSize += sharedMemBytes;
2016-09-07 12:57:18 -05:00
2016-09-02 15:49:22 -05:00
/*
Kernel argument preparation.
*/
grid_launch_parm lp;
2016-09-02 15:49:22 -05:00
hStream = ihipPreLaunchKernel(hStream, 0, 0, &lp, f->_kernelName);
2016-12-05 16:55:26 +05:30
#if USE_DISPATCH_HSA_KERNEL
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;
aql.group_segment_size = groupSegmentSize;
aql.private_segment_size = privateSegmentSize;
aql.kernel_object = f->_kernel;
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
#else
/*
Create signal
*/
hsa_signal_t signal;
status = hsa_signal_create(1, 0, NULL, &signal);
2016-12-05 20:21:33 -06:00
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
/*
Allocate kernarg
*/
void *kern = nullptr;
hsa_amd_memory_pool_t *pool = reinterpret_cast<hsa_amd_memory_pool_t*>(lp.av->get_hsa_kernarg_region());
status = hsa_amd_memory_pool_allocate(*pool, kernArgSize, 0, &kern);
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
status = hsa_amd_agents_allow_access(1, (hsa_agent_t*)lp.av->get_hsa_agent(), 0, kern);
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
memcpy(kern, config[1], kernArgSize);
2016-12-05 16:55:26 +05:30
/*
Launch AQL packet
*/
hStream->launchModuleKernel(*lp.av, signal, blockDimX, blockDimY, blockDimZ,
2016-12-05 20:21:33 -06:00
gridDimX, gridDimY, gridDimZ, groupSegmentSize, privateSegmentSize, kern, kernArgSize, f->_kernel);
2016-12-05 16:55:26 +05:30
/*
Wait for signal
*/
hsa_signal_value_t value = hsa_signal_wait_acquire(signal, HSA_SIGNAL_CONDITION_LT, 1, UINT64_MAX, HSA_WAIT_STATE_BLOCKED);
2016-12-05 20:21:33 -06:00
/*
Destroy kernarg
*/
status = hsa_amd_memory_pool_free(kern);
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
/*
Destroy the signal
*/
status = hsa_signal_destroy(signal);
if(status != HSA_STATUS_SUCCESS){
return ihipLogStatus(hipErrorNotFound);
}
2016-12-05 16:55:26 +05:30
#endif // USE_DISPATCH_HSA_KERNEL
ihipPostLaunchKernel(f->_kernelName, 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;
ihipModuleGetFunction(&func, hmod, name);
*bytes = PrintSymbolSizes(hmod->ptr, name) + sizeof(amd_kernel_code_t);
2016-09-02 15:49:22 -05:00
*dptr = reinterpret_cast<void*>(func->_kernel);
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);
}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);
if(status != HSA_STATUS_SUCCESS){
2016-09-02 09:44:00 -05:00
return ihipLogStatus(hipErrorNotInitialized);
}
}
2016-09-02 09:44:00 -05:00
return ihipLogStatus(ret);
}
2016-08-28 16:48:57 -05:00