2016-08-17 10:36:28 -05:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015-2016 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 WARRANNTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INNCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANNY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
#include "hip_runtime.h"
|
|
|
|
|
#include "hsa/hsa.h"
|
|
|
|
|
#include "hsa/hsa_ext_amd.h"
|
2016-08-19 08:49:34 -05:00
|
|
|
#include "hsa/amd_hsa_kernel_code.h"
|
2016-08-16 14:36:25 -05:00
|
|
|
#include "hcc_detail/hip_hcc.h"
|
|
|
|
|
#include "hcc_detail/trace_helper.h"
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
//TODO Use Pool APIs from HCC to get memory regions.
|
|
|
|
|
|
|
|
|
|
namespace hipdrv{
|
2016-08-16 16:49:42 -05:00
|
|
|
|
2016-08-19 08:49:34 -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
|
|
|
|
2016-08-19 08:49:34 -05:00
|
|
|
if(segment_id != HSA_REGION_SEGMENT_GLOBAL){
|
|
|
|
|
return HSA_STATUS_SUCCESS;
|
|
|
|
|
}
|
2016-08-16 16:49:42 -05:00
|
|
|
|
2016-08-19 08:49:34 -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
|
|
|
|
2016-08-19 08:49:34 -05:00
|
|
|
hsa_region_t *reg = (hsa_region_t*)data;
|
2016-08-16 16:49:42 -05:00
|
|
|
|
2016-08-19 08:49:34 -05:00
|
|
|
if(flags & HSA_REGION_GLOBAL_FLAG_FINE_GRAINED){
|
|
|
|
|
*reg = region;
|
|
|
|
|
}
|
2016-08-16 16:49:42 -05:00
|
|
|
|
2016-08-19 08:49:34 -05:00
|
|
|
return HSA_STATUS_SUCCESS;
|
|
|
|
|
}
|
2016-08-18 11:26:55 -05:00
|
|
|
|
2016-08-19 08:49:34 -05:00
|
|
|
} // End namespace hipdrv
|
2016-08-18 11:26:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
hipError_t hipModuleLoad(hipModule *module, const char *fname){
|
|
|
|
|
HIP_INIT_API(fname);
|
|
|
|
|
hipError_t ret = hipSuccess;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
if(module == NULL){
|
2016-08-19 08:49:34 -05:00
|
|
|
return hipErrorInvalidValue;
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
|
|
|
|
if(ctx == nullptr){
|
2016-08-18 11:26:55 -05:00
|
|
|
ret = hipErrorInvalidContext;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
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-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
if(!in){
|
2016-08-18 11:26:55 -05:00
|
|
|
return hipErrorFileNotFound;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
}else{
|
|
|
|
|
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;
|
2016-08-18 11:26:55 -05:00
|
|
|
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);
|
2016-08-22 14:17:55 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorOutOfMemory;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
char *ptr = (char*)p;
|
|
|
|
|
if(!ptr){
|
2016-08-18 11:26:55 -05:00
|
|
|
return hipErrorOutOfMemory;
|
2016-08-16 14:36:25 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 16:49:42 -05:00
|
|
|
hsa_code_object_t obj;
|
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, &obj);
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorSharedObjectInitFailed;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
|
|
|
|
module->object = obj.handle;
|
|
|
|
|
|
|
|
|
|
hsa_executable_t executable;
|
|
|
|
|
status = hsa_executable_create(HSA_PROFILE_FULL, HSA_EXECUTABLE_STATE_UNFROZEN, NULL, &executable);
|
|
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorNotInitialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module->executable = executable.handle;
|
|
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 14:19:15 -05:00
|
|
|
hipError_t hipModuleUnload(hipModule hmod){
|
|
|
|
|
hsa_executable_t exec;
|
|
|
|
|
hsa_code_object_t co;
|
|
|
|
|
hipError_t ret = hipSuccess;
|
|
|
|
|
exec.handle = hmod.executable;
|
|
|
|
|
co.handle = hmod.object;
|
|
|
|
|
hsa_status_t status = hsa_executable_destroy(exec);
|
|
|
|
|
if(status != HSA_STATUS_SUCCESS){ret = hipErrorInvalidValue; }
|
|
|
|
|
status = hsa_code_object_destroy(co);
|
|
|
|
|
if(status != HSA_STATUS_SUCCESS){ret = hipErrorInvalidValue; }
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
hipError_t hipModuleGetFunction(hipFunction *func, hipModule hmod, const char *name){
|
|
|
|
|
HIP_INIT_API(name);
|
|
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
|
|
|
|
hipError_t ret = hipSuccess;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
|
|
|
|
if(name == nullptr){
|
2016-08-18 11:26:55 -05:00
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
if(ctx == nullptr){
|
2016-08-18 11:26:55 -05:00
|
|
|
ret = hipErrorInvalidContext;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
}else{
|
2016-08-18 11:26:55 -05:00
|
|
|
int deviceId = ctx->getDevice()->_deviceId;
|
|
|
|
|
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
|
|
|
|
|
hsa_agent_t gpuAgent = (hsa_agent_t)currentDevice->_hsaAgent;
|
|
|
|
|
|
|
|
|
|
hsa_status_t status;
|
|
|
|
|
hsa_executable_t executable;
|
2016-08-19 08:49:34 -05:00
|
|
|
hsa_executable_symbol_t kernel_symbol;
|
|
|
|
|
executable.handle = hmod.executable;
|
2016-08-18 11:26:55 -05:00
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorNotInitialized;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
hsa_code_object_t obj;
|
2016-08-19 08:49:34 -05:00
|
|
|
obj.handle = hmod.object;
|
2016-08-18 11:26:55 -05:00
|
|
|
status = hsa_executable_load_code_object(executable, gpuAgent, obj, NULL);
|
|
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorNotInitialized;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
status = hsa_executable_freeze(executable, NULL);
|
|
|
|
|
status = hsa_executable_get_symbol(executable, NULL, name, gpuAgent, 0, &kernel_symbol);
|
|
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorNotFound;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
status = hsa_executable_symbol_get_info(kernel_symbol,
|
2016-08-16 14:36:25 -05:00
|
|
|
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT,
|
2016-08-19 08:49:34 -05:00
|
|
|
&func->kernel);
|
|
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
|
|
|
|
return hipErrorNotFound;
|
|
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
|
|
|
|
func->symbol = kernel_symbol.handle;
|
|
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipLaunchModuleKernel(hipFunction 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,
|
|
|
|
|
void **kernelParams, void **extra){
|
2016-08-19 08:49:34 -05:00
|
|
|
HIP_INIT_API(f.kernel);
|
2016-08-18 11:26:55 -05:00
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
|
|
|
|
hipError_t ret = hipSuccess;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
if(ctx == nullptr){
|
|
|
|
|
ret = hipErrorInvalidDevice;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
}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 kernSize;
|
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
kernSize = *(size_t*)(config[3]);
|
|
|
|
|
}else{
|
|
|
|
|
return hipErrorNotInitialized;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
Kernel argument preparation.
|
|
|
|
|
*/
|
2016-08-22 14:17:55 -05:00
|
|
|
hsa_status_t status;
|
|
|
|
|
grid_launch_parm lp;
|
|
|
|
|
hStream = ihipPreLaunchKernel(hStream, 0, 0, &lp);
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
/*
|
2016-08-22 14:17:55 -05:00
|
|
|
Create signal
|
2016-08-18 11:26:55 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
hsa_signal_t signal;
|
|
|
|
|
status = hsa_signal_create(1, 0, NULL, &signal);
|
|
|
|
|
|
2016-08-22 14:17:55 -05:00
|
|
|
/*
|
|
|
|
|
Launch AQL packet
|
|
|
|
|
*/
|
|
|
|
|
hStream->launchModuleKernel(signal, blockDimX, blockDimY, blockDimZ,
|
|
|
|
|
gridDimX, gridDimY, gridDimZ, sharedMemBytes, config[1], kernSize, f.kernel);
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
/*
|
2016-08-22 14:17:55 -05:00
|
|
|
Wait for signal
|
2016-08-18 11:26:55 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
hsa_signal_value_t value = hsa_signal_wait_acquire(signal, HSA_SIGNAL_CONDITION_LT, 1, UINT64_MAX, HSA_WAIT_STATE_BLOCKED);
|
2016-08-22 14:17:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
ihipPostLaunchKernel(hStream, lp);
|
|
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-16 14:36:25 -05:00
|
|
|
return ret;
|
|
|
|
|
}
|