2016-08-17 10:36:28 -05:00
|
|
|
/*
|
2017-03-31 12:11:34 -05:00
|
|
|
Copyright (c) 2015 - present 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>
|
2016-08-25 14:16:53 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2017-10-09 13:27:11 +01:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
2017-11-03 01:44:48 +00:00
|
|
|
#include <unordered_map>
|
2017-10-09 13:27:11 +01:00
|
|
|
#include <vector>
|
2017-03-17 13:11:34 -05:00
|
|
|
#include <map>
|
2016-08-16 14:36:25 -05:00
|
|
|
|
2017-10-09 13:27:11 +01:00
|
|
|
#include <hsa/hsa.h>
|
|
|
|
|
#include <hsa/hsa_ext_amd.h>
|
|
|
|
|
#include <hsa/amd_hsa_kernel_code.h>
|
2016-10-04 22:17:18 +05:30
|
|
|
|
2017-10-23 10:24:36 +05:30
|
|
|
#include "elfio/elfio.hpp"
|
2016-10-04 22:17:18 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
2017-03-31 12:11:34 -05:00
|
|
|
#include "hip_hcc_internal.h"
|
2016-10-15 20:25:20 -05:00
|
|
|
#include "trace_helper.h"
|
2016-10-04 22:17:18 +05:30
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
//TODO Use Pool APIs from HCC to get memory regions.
|
|
|
|
|
|
2017-03-17 13:11:34 -05:00
|
|
|
#include <cassert>
|
|
|
|
|
inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
|
|
|
|
|
assert(Align != 0u && "Align can't be 0.");
|
|
|
|
|
Skew %= Align;
|
|
|
|
|
return (Value + Align - 1 - Skew) / Align * Align + Skew;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ihipKernArgInfo{
|
|
|
|
|
std::vector<uint32_t> Size;
|
|
|
|
|
std::vector<uint32_t> Align;
|
|
|
|
|
std::vector<std::string> ArgType;
|
|
|
|
|
std::vector<std::string> ArgName;
|
|
|
|
|
uint32_t totalSize;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::map<std::string,struct ihipKernArgInfo> kernelArguments;
|
|
|
|
|
|
|
|
|
|
struct MyElfNote {
|
|
|
|
|
uint32_t n_namesz = 0;
|
|
|
|
|
uint32_t n_descsz = 0;
|
|
|
|
|
uint32_t n_type = 0;
|
|
|
|
|
|
|
|
|
|
MyElfNote() = default;
|
|
|
|
|
};
|
2017-02-09 17:22:55 -06:00
|
|
|
|
|
|
|
|
struct ihipModuleSymbol_t{
|
|
|
|
|
uint64_t _object; // The kernel object.
|
|
|
|
|
uint32_t _groupSegmentSize;
|
|
|
|
|
uint32_t _privateSegmentSize;
|
2017-02-10 13:32:13 -06:00
|
|
|
std::string _name; // TODO - review for performance cost. Name is just used for debug.
|
2017-02-09 17:22:55 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
std::string ToString(hipFunction_t v)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << "0x" << std::hex << v->_object;
|
|
|
|
|
return ss.str();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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-25 14:16:53 -05:00
|
|
|
uint64_t PrintSymbolSizes(const void *emi, const char *name){
|
2017-10-09 13:27:11 +01:00
|
|
|
using namespace ELFIO;
|
|
|
|
|
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
const ELFIO::Elf64_Ehdr *ehdr = (const ELFIO::Elf64_Ehdr*)emi;
|
2016-08-25 14:16:53 -05:00
|
|
|
if(NULL == ehdr || EV_CURRENT != ehdr->e_version){}
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
const ELFIO::Elf64_Shdr * shdr =
|
|
|
|
|
(const ELFIO::Elf64_Shdr*)((char*)emi + ehdr->e_shoff);
|
2016-08-25 14:16:53 -05:00
|
|
|
for(uint16_t i=0;i<ehdr->e_shnum;++i){
|
|
|
|
|
if(shdr[i].sh_type == SHT_SYMTAB){
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
const ELFIO::Elf64_Sym *syms =
|
|
|
|
|
(const ELFIO::Elf64_Sym*)((char*)emi + shdr[i].sh_offset);
|
2016-08-25 14:16:53 -05:00
|
|
|
assert(syms);
|
|
|
|
|
uint64_t numSyms = shdr[i].sh_size/shdr[i].sh_entsize;
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
const char* strtab =
|
|
|
|
|
(const char*)((char*)emi + shdr[shdr[i].sh_link].sh_offset);
|
2016-08-25 14:16:53 -05:00
|
|
|
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){
|
2017-10-09 13:27:11 +01:00
|
|
|
using namespace ELFIO;
|
|
|
|
|
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
const ELFIO::Elf64_Ehdr *ehdr = (const ELFIO::Elf64_Ehdr*)emi;
|
|
|
|
|
const ELFIO::Elf64_Shdr *shdr = (const ELFIO::Elf64_Shdr*)((char*)emi + ehdr->e_shoff);
|
2016-08-18 11:26:55 -05:00
|
|
|
|
2016-08-25 14:16:53 -05:00
|
|
|
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-18 11:26:55 -05:00
|
|
|
|
2017-10-09 13:27:11 +01: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;
|
2016-08-24 09:47:11 -05:00
|
|
|
*module = new ihipModule_t;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
if(module == NULL){
|
2016-09-02 09:44:00 -05:00
|
|
|
return ihipLogStatus(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);
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2017-10-09 13:27:11 +01:00
|
|
|
hsa_executable_create_alt(
|
|
|
|
|
HSA_PROFILE_FULL,
|
|
|
|
|
HSA_DEFAULT_FLOAT_ROUNDING_MODE_DEFAULT,
|
|
|
|
|
nullptr,
|
|
|
|
|
&(*module)->executable);
|
2016-12-17 07:19:22 -06:00
|
|
|
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
std::ifstream file{fname};
|
|
|
|
|
|
|
|
|
|
if (!file.is_open()) {
|
2017-10-09 13:27:11 +01:00
|
|
|
return ihipLogStatus(hipErrorFileNotFound);
|
|
|
|
|
}
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
(*module)->executable = hip_impl::load_executable(
|
|
|
|
|
(*module)->executable, currentDevice->_hsaAgent, file);
|
|
|
|
|
ret = (*module)->executable.handle ? hipSuccess : hipErrorUnknown;
|
2016-08-16 14:36:25 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -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
|
|
|
|
2016-12-06 16:05:46 +00:00
|
|
|
hipError_t hipModuleUnload(hipModule_t hmod)
|
|
|
|
|
{
|
2017-10-30 20:18:41 +00:00
|
|
|
HIP_INIT_API(hmod);
|
|
|
|
|
|
2016-12-06 16:05:46 +00:00
|
|
|
// 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();
|
2016-08-23 14:19:15 -05:00
|
|
|
hipError_t ret = hipSuccess;
|
2016-08-24 09:47:11 -05:00
|
|
|
hsa_status_t status = hsa_executable_destroy(hmod->executable);
|
2016-09-02 09:31:37 -05:00
|
|
|
if(status != HSA_STATUS_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
ret = hipErrorInvalidValue;
|
|
|
|
|
}
|
2017-10-09 13:27:11 +01:00
|
|
|
// status = hsa_code_object_destroy(hmod->object);
|
|
|
|
|
// if(status != HSA_STATUS_SUCCESS)
|
|
|
|
|
// {
|
|
|
|
|
// ret = hipErrorInvalidValue;
|
|
|
|
|
// }
|
|
|
|
|
// status = hsa_memory_free(hmod->ptr);
|
|
|
|
|
// if(status != HSA_STATUS_SUCCESS)
|
|
|
|
|
// {
|
|
|
|
|
// ret = hipErrorInvalidValue;
|
|
|
|
|
// }
|
2017-03-08 13:49:50 -06:00
|
|
|
for(auto f = hmod->funcTrack.begin(); f != hmod->funcTrack.end(); ++f) {
|
2017-02-10 13:42:10 -06:00
|
|
|
delete *f;
|
|
|
|
|
}
|
2016-08-24 09:47:11 -05:00
|
|
|
delete hmod;
|
2016-09-02 09:44:00 -05:00
|
|
|
return ihipLogStatus(ret);
|
2016-08-23 14:19:15 -05:00
|
|
|
}
|
|
|
|
|
|
2017-03-08 13:49:50 -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;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2017-03-08 13:49:50 -06:00
|
|
|
if (name == nullptr){
|
2017-10-30 20:18:41 +00:00
|
|
|
return (hipErrorInvalidValue);
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2017-03-08 13:49:50 -06:00
|
|
|
if (ctx == nullptr){
|
2016-08-18 11:26:55 -05:00
|
|
|
ret = hipErrorInvalidContext;
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2017-03-08 13:49:50 -06:00
|
|
|
} else {
|
2017-02-10 13:32:13 -06:00
|
|
|
std::string str(name);
|
2017-03-08 13:49:50 -06:00
|
|
|
for(auto f = hmod->funcTrack.begin(); f != hmod->funcTrack.end(); ++f) {
|
|
|
|
|
if((*f)->_name == str) {
|
|
|
|
|
*func = *f;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-02-10 13:32:13 -06:00
|
|
|
}
|
2017-02-09 17:22:55 -06:00
|
|
|
ihipModuleSymbol_t *sym = new ihipModuleSymbol_t;
|
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;
|
|
|
|
|
|
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);
|
2016-08-18 11:26:55 -05:00
|
|
|
if(status != HSA_STATUS_SUCCESS){
|
2017-10-30 20:18:41 +00:00
|
|
|
return hipErrorNotFound;
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
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,
|
2017-02-09 17:22:55 -06:00
|
|
|
&sym->_object);
|
2016-12-17 07:21:15 -06:00
|
|
|
CHECK_HSA(status, hipErrorNotFound);
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-12-17 07:21:15 -06:00
|
|
|
status = hsa_executable_symbol_get_info(symbol,
|
|
|
|
|
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE,
|
2017-02-09 17:22:55 -06:00
|
|
|
&sym->_groupSegmentSize);
|
2016-12-17 07:21:15 -06:00
|
|
|
CHECK_HSA(status, hipErrorNotFound);
|
|
|
|
|
|
|
|
|
|
status = hsa_executable_symbol_get_info(symbol,
|
|
|
|
|
HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE,
|
2017-02-09 17:22:55 -06:00
|
|
|
&sym->_privateSegmentSize);
|
2016-12-17 07:21:15 -06:00
|
|
|
CHECK_HSA(status, hipErrorNotFound);
|
|
|
|
|
|
2017-02-10 13:32:13 -06:00
|
|
|
sym->_name = name;
|
2017-02-09 17:22:55 -06:00
|
|
|
*func = sym;
|
2017-02-10 13:32:13 -06:00
|
|
|
hmod->funcTrack.push_back(*func);
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2017-05-12 17:04:23 -05:00
|
|
|
return ret;
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
|
|
|
|
|
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);
|
2017-05-12 17:04:23 -05:00
|
|
|
return ihipLogStatus(ihipModuleGetSymbol(hfunc, hmod, name));
|
2016-08-26 10:32:01 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 12:47:25 -05:00
|
|
|
|
2017-03-31 12:11:34 -05:00
|
|
|
hipError_t ihipModuleLaunchKernel(hipFunction_t f,
|
2017-04-06 23:55:15 +00:00
|
|
|
uint32_t globalWorkSizeX, uint32_t globalWorkSizeY, uint32_t globalWorkSizeZ,
|
|
|
|
|
uint32_t localWorkSizeX, uint32_t localWorkSizeY, uint32_t localWorkSizeZ,
|
|
|
|
|
size_t sharedMemBytes, hipStream_t hStream,
|
|
|
|
|
void **kernelParams, void **extra,
|
2017-04-06 21:00:00 -05:00
|
|
|
hipEvent_t startEvent, hipEvent_t stopEvent)
|
2016-09-02 15:49:22 -05:00
|
|
|
{
|
|
|
|
|
|
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};
|
2016-10-14 23:46:29 -05:00
|
|
|
size_t kernArgSize;
|
2016-08-18 11:26:55 -05:00
|
|
|
|
2017-03-17 13:11:34 -05:00
|
|
|
if(kernelParams != NULL){
|
|
|
|
|
std::string name = f->_name;
|
|
|
|
|
struct ihipKernArgInfo pl = kernelArguments[name];
|
|
|
|
|
char* argBuf = (char*)malloc(pl.totalSize);
|
|
|
|
|
memset(argBuf, 0, pl.totalSize);
|
|
|
|
|
int index = 0;
|
|
|
|
|
for(int i=0;i<pl.Size.size();i++){
|
|
|
|
|
memcpy(argBuf + index, kernelParams[i], pl.Size[i]);
|
|
|
|
|
index += pl.Align[i];
|
|
|
|
|
}
|
|
|
|
|
config[1] = (void*)argBuf;
|
|
|
|
|
kernArgSize = pl.totalSize;
|
|
|
|
|
} else if(extra != NULL){
|
2016-08-18 11:26:55 -05:00
|
|
|
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){
|
2016-10-14 23:46:29 -05:00
|
|
|
kernArgSize = *(size_t*)(config[3]);
|
2016-09-02 15:49:22 -05:00
|
|
|
} else {
|
2017-10-30 20:18:41 +00:00
|
|
|
return hipErrorNotInitialized;
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2017-10-09 13:27:11 +01:00
|
|
|
|
2016-08-18 11:26:55 -05:00
|
|
|
}else{
|
2017-10-30 20:18:41 +00:00
|
|
|
return hipErrorInvalidValue;
|
2016-08-18 11:26:55 -05:00
|
|
|
}
|
2016-09-07 12:57:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-02 15:49:22 -05:00
|
|
|
/*
|
|
|
|
|
Kernel argument preparation.
|
|
|
|
|
*/
|
2017-02-09 17:22:55 -06:00
|
|
|
grid_launch_parm lp;
|
2017-01-23 22:33:21 -06:00
|
|
|
lp.dynamic_group_mem_bytes = sharedMemBytes; // TODO - this should be part of preLaunchKernel.
|
2017-03-31 12:11:34 -05:00
|
|
|
hStream = ihipPreLaunchKernel(hStream, dim3(globalWorkSizeX, globalWorkSizeY, globalWorkSizeZ), dim3(localWorkSizeX, localWorkSizeY, localWorkSizeZ), &lp, f->_name.c_str());
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2016-12-05 16:55:26 +05:30
|
|
|
|
2016-10-14 23:46:29 -05:00
|
|
|
hsa_kernel_dispatch_packet_t aql;
|
|
|
|
|
|
|
|
|
|
memset(&aql, 0, sizeof(aql));
|
|
|
|
|
|
|
|
|
|
//aql.completion_signal._handle = 0;
|
|
|
|
|
//aql.kernarg_address = 0;
|
|
|
|
|
|
2017-03-31 12:11:34 -05:00
|
|
|
aql.workgroup_size_x = localWorkSizeX;
|
|
|
|
|
aql.workgroup_size_y = localWorkSizeY;
|
|
|
|
|
aql.workgroup_size_z = localWorkSizeZ;
|
|
|
|
|
aql.grid_size_x = globalWorkSizeX;
|
|
|
|
|
aql.grid_size_y = globalWorkSizeY;
|
|
|
|
|
aql.grid_size_z = globalWorkSizeZ;
|
2017-02-09 17:22:55 -06:00
|
|
|
aql.group_segment_size = f->_groupSegmentSize + sharedMemBytes;
|
|
|
|
|
aql.private_segment_size = f->_privateSegmentSize;
|
|
|
|
|
aql.kernel_object = f->_object;
|
2016-12-05 20:21:33 -06:00
|
|
|
aql.setup = 3 << HSA_KERNEL_DISPATCH_PACKET_SETUP_DIMENSIONS;
|
2016-10-14 23:46:29 -05:00
|
|
|
aql.header = (HSA_PACKET_TYPE_KERNEL_DISPATCH << HSA_PACKET_HEADER_TYPE) |
|
2017-01-25 21:50:52 -06:00
|
|
|
(1 << HSA_PACKET_HEADER_BARRIER); // TODO - honor queue setting for execute_in_order
|
2016-10-14 23:46:29 -05:00
|
|
|
|
2017-01-25 21:50:52 -06:00
|
|
|
if (HCC_OPT_FLUSH) {
|
|
|
|
|
aql.header |= (HSA_FENCE_SCOPE_AGENT << HSA_PACKET_HEADER_ACQUIRE_FENCE_SCOPE) |
|
|
|
|
|
(HSA_FENCE_SCOPE_AGENT << HSA_PACKET_HEADER_RELEASE_FENCE_SCOPE);
|
|
|
|
|
} else {
|
|
|
|
|
aql.header |= (HSA_FENCE_SCOPE_SYSTEM << HSA_PACKET_HEADER_ACQUIRE_FENCE_SCOPE) |
|
|
|
|
|
(HSA_FENCE_SCOPE_SYSTEM << HSA_PACKET_HEADER_RELEASE_FENCE_SCOPE);
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-06 23:55:15 +00:00
|
|
|
|
|
|
|
|
hc::completion_future cf;
|
|
|
|
|
|
2017-10-09 13:27:11 +01:00
|
|
|
lp.av->dispatch_hsa_kernel(&aql, config[1] /* kernarg*/, kernArgSize,
|
2017-06-23 10:39:16 -05:00
|
|
|
(startEvent || stopEvent) ? &cf : nullptr
|
2017-08-08 10:15:32 +05:30
|
|
|
#if (__hcc_workweek__ > 17312)
|
2017-06-23 10:39:16 -05:00
|
|
|
, f->_name.c_str()
|
2017-07-27 23:00:58 -05:00
|
|
|
#endif
|
2017-06-23 10:39:16 -05:00
|
|
|
);
|
|
|
|
|
|
2017-04-06 23:55:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if (startEvent) {
|
2017-05-30 21:54:33 -05:00
|
|
|
startEvent->attachToCompletionFuture(&cf, hStream, hipEventTypeStartCommand);
|
2017-04-06 23:55:15 +00:00
|
|
|
}
|
|
|
|
|
if (stopEvent) {
|
2017-05-30 21:54:33 -05:00
|
|
|
stopEvent->attachToCompletionFuture (&cf, hStream, hipEventTypeStopCommand);
|
2017-04-06 23:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-05 16:55:26 +05:30
|
|
|
|
2017-03-17 13:11:34 -05:00
|
|
|
if(kernelParams != NULL){
|
|
|
|
|
free(config[1]);
|
|
|
|
|
}
|
2017-02-10 13:32:13 -06:00
|
|
|
ihipPostLaunchKernel(f->_name.c_str(), hStream, lp);
|
2016-08-16 14:36:25 -05:00
|
|
|
}
|
2016-08-19 08:49:34 -05:00
|
|
|
|
2017-03-31 12:11:34 -05:00
|
|
|
return ret;
|
2016-08-16 14:36:25 -05:00
|
|
|
}
|
2016-08-25 14:16:53 -05:00
|
|
|
|
2017-03-31 12:11:34 -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,
|
|
|
|
|
void **kernelParams, void **extra)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(f, gridDimX, gridDimY, gridDimZ,
|
|
|
|
|
blockDimX, blockDimY, blockDimZ,
|
|
|
|
|
sharedMemBytes, hStream,
|
|
|
|
|
kernelParams, extra);
|
|
|
|
|
return ihipLogStatus(ihipModuleLaunchKernel(f,
|
|
|
|
|
blockDimX * gridDimX, blockDimY * gridDimY, gridDimZ * blockDimZ,
|
|
|
|
|
blockDimX, blockDimY, blockDimZ,
|
2017-04-06 23:55:15 +00:00
|
|
|
sharedMemBytes, hStream, kernelParams, extra,
|
|
|
|
|
nullptr, nullptr));
|
2017-03-31 12:11:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hipError_t hipHccModuleLaunchKernel(hipFunction_t f,
|
|
|
|
|
uint32_t globalWorkSizeX, uint32_t globalWorkSizeY, uint32_t globalWorkSizeZ,
|
|
|
|
|
uint32_t localWorkSizeX, uint32_t localWorkSizeY, uint32_t localWorkSizeZ,
|
|
|
|
|
size_t sharedMemBytes, hipStream_t hStream,
|
2017-04-06 23:55:15 +00:00
|
|
|
void **kernelParams, void **extra,
|
2017-04-06 21:00:00 -05:00
|
|
|
hipEvent_t startEvent, hipEvent_t stopEvent)
|
2017-03-31 12:11:34 -05:00
|
|
|
{
|
|
|
|
|
HIP_INIT_API(f, globalWorkSizeX, globalWorkSizeY, globalWorkSizeZ,
|
|
|
|
|
localWorkSizeX, localWorkSizeY, localWorkSizeZ,
|
|
|
|
|
sharedMemBytes, hStream,
|
|
|
|
|
kernelParams, extra);
|
|
|
|
|
return ihipLogStatus(ihipModuleLaunchKernel(f, globalWorkSizeX, globalWorkSizeY, globalWorkSizeZ,
|
|
|
|
|
localWorkSizeX, localWorkSizeY, localWorkSizeZ,
|
2017-04-06 23:55:15 +00:00
|
|
|
sharedMemBytes, hStream, kernelParams, extra, startEvent, stopEvent));
|
2017-03-31 12:11:34 -05:00
|
|
|
}
|
2016-08-25 14:16:53 -05:00
|
|
|
|
2017-11-03 01:44:48 +00:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
struct Agent_global {
|
|
|
|
|
std::string name;
|
|
|
|
|
hipDeviceptr_t address;
|
|
|
|
|
std::uint32_t byte_cnt;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
void* address(hsa_executable_symbol_t x)
|
|
|
|
|
{
|
|
|
|
|
void* r = nullptr;
|
|
|
|
|
hsa_executable_symbol_get_info(
|
|
|
|
|
x, HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ADDRESS, &r);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
std::string name(hsa_executable_symbol_t x)
|
|
|
|
|
{
|
|
|
|
|
uint32_t sz = 0u;
|
|
|
|
|
hsa_executable_symbol_get_info(
|
|
|
|
|
x, HSA_EXECUTABLE_SYMBOL_INFO_NAME_LENGTH, &sz);
|
|
|
|
|
|
|
|
|
|
std::string r(sz, '\0');
|
|
|
|
|
hsa_executable_symbol_get_info(
|
|
|
|
|
x, HSA_EXECUTABLE_SYMBOL_INFO_NAME, &r.front());
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
std::uint32_t size(hsa_executable_symbol_t x)
|
|
|
|
|
{
|
|
|
|
|
std::uint32_t r = 0;
|
|
|
|
|
hsa_executable_symbol_get_info(
|
|
|
|
|
x, HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_SIZE, &r);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
void track(const Agent_global& x)
|
|
|
|
|
{
|
|
|
|
|
tprintf(
|
|
|
|
|
DB_MEM,
|
|
|
|
|
" add variable '%s' with ptr=%p size=%u to tracker\n",
|
|
|
|
|
x.name.c_str(),
|
|
|
|
|
x.address,
|
|
|
|
|
x.byte_cnt);
|
|
|
|
|
|
|
|
|
|
auto device = ihipGetTlsDefaultCtx()->getWriteableDevice();
|
|
|
|
|
|
|
|
|
|
hc::AmPointerInfo ptr_info(
|
|
|
|
|
nullptr,
|
|
|
|
|
x.address,
|
|
|
|
|
x.address,
|
|
|
|
|
x.byte_cnt,
|
|
|
|
|
device->_acc,
|
|
|
|
|
true,
|
|
|
|
|
false);
|
|
|
|
|
hc::am_memtracker_add(x.address, ptr_info);
|
|
|
|
|
hc::am_memtracker_update(x.address, device->_deviceId, 0u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Container = std::vector<Agent_global>>
|
|
|
|
|
inline
|
|
|
|
|
hsa_status_t copy_agent_global_variables(
|
|
|
|
|
hsa_executable_t, hsa_agent_t, hsa_executable_symbol_t x, void* out)
|
|
|
|
|
{
|
|
|
|
|
assert(out);
|
|
|
|
|
|
|
|
|
|
hsa_symbol_kind_t t = {};
|
|
|
|
|
hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_TYPE, &t);
|
|
|
|
|
|
|
|
|
|
if (t == HSA_SYMBOL_KIND_VARIABLE) {
|
|
|
|
|
static_cast<Container*>(out)->push_back(
|
|
|
|
|
Agent_global{name(x), address(x), size(x)});
|
|
|
|
|
|
|
|
|
|
track(static_cast<Container*>(out)->back());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return HSA_STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
hsa_agent_t this_agent()
|
|
|
|
|
{
|
|
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
|
|
|
|
|
|
|
|
|
if (!ctx) throw std::runtime_error{"No active HIP context."};
|
|
|
|
|
|
|
|
|
|
auto device = ctx->getDevice();
|
|
|
|
|
|
|
|
|
|
if (!device) throw std::runtime_error{"No device available for HIP."};
|
|
|
|
|
|
|
|
|
|
ihipDevice_t *currentDevice = ihipGetDevice(device->_deviceId);
|
|
|
|
|
|
|
|
|
|
if (!currentDevice) {
|
|
|
|
|
throw std::runtime_error{"No active device for HIP"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return currentDevice->_hsaAgent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
std::vector<Agent_global> read_agent_globals(hipModule_t hmodule)
|
|
|
|
|
{
|
|
|
|
|
std::vector<Agent_global> r;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hsa_executable_iterate_agent_symbols(
|
|
|
|
|
hmodule->executable, this_agent(), copy_agent_global_variables, &r);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 15:05:12 -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);
|
2016-08-25 14:16:53 -05:00
|
|
|
hipError_t ret = hipSuccess;
|
|
|
|
|
if(dptr == NULL || bytes == NULL){
|
2016-09-02 09:44:00 -05:00
|
|
|
return ihipLogStatus(hipErrorInvalidValue);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
|
|
|
|
if(name == NULL || hmod == NULL){
|
2016-09-02 09:44:00 -05:00
|
|
|
return ihipLogStatus(hipErrorNotInitialized);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
|
|
|
|
else{
|
2017-11-03 01:44:48 +00:00
|
|
|
static std::unordered_map<
|
|
|
|
|
hipModule_t, std::vector<Agent_global>> agent_globals;
|
|
|
|
|
|
|
|
|
|
// TODO: this is not particularly robust.
|
|
|
|
|
if (agent_globals.count(hmod) == 0) {
|
|
|
|
|
static std::mutex mtx;
|
|
|
|
|
std::lock_guard<std::mutex> lck{mtx};
|
|
|
|
|
|
|
|
|
|
if (agent_globals.count(hmod) == 0) {
|
|
|
|
|
agent_globals.emplace(hmod, read_agent_globals(hmod));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: This is unsafe iff some other emplacement triggers rehashing.
|
|
|
|
|
// It will have to be properly fleshed out in the future.
|
|
|
|
|
const auto it0 = agent_globals.find(hmod);
|
|
|
|
|
if (it0 == agent_globals.cend()) {
|
|
|
|
|
throw std::runtime_error{"agent_globals data structure corrupted."};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto it1 = std::find_if(
|
|
|
|
|
it0->second.cbegin(),
|
|
|
|
|
it0->second.cend(),
|
|
|
|
|
[=](const Agent_global& x) { return x.name == name; });
|
|
|
|
|
|
|
|
|
|
if (it1 == it0->second.cend()) return ihipLogStatus(hipErrorNotFound);
|
|
|
|
|
|
|
|
|
|
*dptr = it1->address;
|
|
|
|
|
*bytes = it1->byte_cnt;
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(hipSuccess);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2016-08-25 14:16:53 -05:00
|
|
|
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 {
|
2016-08-25 14:16:53 -05:00
|
|
|
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);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *ptr = (char*)p;
|
|
|
|
|
if(!ptr){
|
2016-09-02 09:44:00 -05:00
|
|
|
return ihipLogStatus(hipErrorOutOfMemory);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
|
|
|
|
(*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);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-25 14:16:53 -05:00
|
|
|
}
|
2016-09-02 09:44:00 -05:00
|
|
|
return ihipLogStatus(ret);
|
2016-08-25 14:16:53 -05:00
|
|
|
}
|
2017-05-19 17:22:14 +03:00
|
|
|
|
|
|
|
|
hipError_t hipModuleLoadDataEx(hipModule_t *module, const void *image, unsigned int numOptions, hipJitOption *options, void **optionValues)
|
|
|
|
|
{
|
|
|
|
|
return hipModuleLoadData(module, image);
|
|
|
|
|
}
|