Files
rocm-systems/projects/clr/hipamd/src/hip_module.cpp
T

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

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"
#include "hcc_detail/hip_hcc.h"
#include "hcc_detail/trace_helper.h"
#include <fstream>
2016-08-16 16:49:42 -05:00
hsa_status_t FindRegions(hsa_region_t region, void *data){
hsa_region_segment_t segment_id;
hsa_region_get_info(region, HSA_REGION_INFO_SEGMENT, &segment_id);
if(segment_id != HSA_REGION_SEGMENT_GLOBAL){
return HSA_STATUS_SUCCESS;
}
hsa_region_global_flag_t flags;
hsa_region_get_info(region, HSA_REGION_INFO_GLOBAL_FLAGS, &flags);
hsa_region_t *reg = (hsa_region_t*)data;
if(flags & HSA_REGION_GLOBAL_FLAG_FINE_GRAINED){
*reg = region;
}
return HSA_STATUS_SUCCESS;
}
2016-08-16 14:36:25 -05:00
hipError_t hipModuleLoad(hipModule *module, const char *fname){
HIP_INIT_API(fname);
hipError_t ret = hipSuccess;
auto ctx = ihipGetTlsDefaultCtx();
if(ctx == nullptr){
ret = hipErrorInvalidDevice;
}else{
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
std::ifstream in(fname, std::ios::binary | std::ios::ate);
if(!in){
std::cout<<"Couldn't read file "<<fname<<std::endl;
}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;
hsa_status_t status = hsa_agent_iterate_regions(agent, FindRegions, &sysRegion);
assert(status == HSA_STATUS_SUCCESS);
status = hsa_memory_allocate(sysRegion, size, (void**)&p);
2016-08-16 14:36:25 -05:00
char *ptr = (char*)p;
if(!ptr){
std::cout<<"Error: failed to allocate memory for code object"<<std::endl;
}
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);
*module = obj.handle;
assert(status == HSA_STATUS_SUCCESS);
}
}
return ret;
}
hipError_t hipModuleGetFunction(hipFunction *func, hipModule hmod, const char *name){
HIP_INIT_API(name);
auto ctx = ihipGetTlsDefaultCtx();
hipError_t ret = hipSuccess;
if(ctx == nullptr){
ret = hipErrorInvalidDevice;
}else{
int deviceId = ctx->getDevice()->_deviceId;
ihipDevice_t *currentDevice = ihipGetDevice(deviceId);
2016-08-16 16:49:42 -05:00
hsa_agent_t gpuAgent = (hsa_agent_t)currentDevice->_hsaAgent;
2016-08-16 14:36:25 -05:00
hsa_status_t status;
hsa_executable_symbol_t kernel_symbol;
hsa_executable_t executable;
status = hsa_executable_create(HSA_PROFILE_FULL, HSA_EXECUTABLE_STATE_UNFROZEN, NULL, &executable);
2016-08-16 16:49:42 -05:00
2016-08-16 14:36:25 -05:00
assert(status == HSA_STATUS_SUCCESS);
hsa_code_object_t obj;
obj.handle = hmod;
2016-08-16 16:49:42 -05:00
status = hsa_executable_load_code_object(executable, gpuAgent, obj, NULL);
2016-08-16 14:36:25 -05:00
assert(status == HSA_STATUS_SUCCESS);
status = hsa_executable_freeze(executable, NULL);
assert(status == HSA_STATUS_SUCCESS);
2016-08-16 16:49:42 -05:00
status = hsa_executable_get_symbol(executable, NULL, name, gpuAgent, 0, &kernel_symbol);
2016-08-16 14:36:25 -05:00
assert(status == HSA_STATUS_SUCCESS);
status = hsa_executable_symbol_get_info(kernel_symbol,
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT,
func);
assert(status == HSA_STATUS_SUCCESS);
}
return ret;
}