파일
rocm-systems/src/hip_memory.cpp
T

1040 라인
31 KiB
C++
Raw 일반 보기 히스토리

2016-03-24 07:04:01 -05:00
/*
2016-10-12 19:14:17 -05:00
Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
2016-10-15 22:55:22 +05:30
2016-10-12 19:14:17 -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-10-12 19:14:17 -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
2016-10-12 19:14:17 -05:00
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
THE SOFTWARE.
*/
2016-03-24 07:04:01 -05:00
#include <hc_am.hpp>
2016-10-04 22:17:18 +05:30
#include "hsa/hsa.h"
#include "hsa/hsa_ext_amd.h"
#include "hip/hip_runtime.h"
#include "hip_hcc.h"
#include "trace_helper.h"
2016-11-17 11:57:53 -06:00
#include "hip/hcc_detail/hip_texture.h"
2016-10-04 22:17:18 +05:30
2016-03-24 09:28:46 -05:00
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Memory
//
//
hipError_t hipPointerGetAttributes(hipPointerAttribute_t *attributes, void* ptr)
{
HIP_INIT_API(attributes, ptr);
2016-03-24 09:28:46 -05:00
hipError_t e = hipSuccess;
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t status = hc::am_memtracker_getinfo(&amPointerInfo, ptr);
if (status == AM_SUCCESS) {
attributes->memoryType = amPointerInfo._isInDeviceMem ? hipMemoryTypeDevice: hipMemoryTypeHost;
attributes->hostPointer = amPointerInfo._hostPointer;
attributes->devicePointer = amPointerInfo._devicePointer;
attributes->isManaged = 0;
if(attributes->memoryType == hipMemoryTypeHost){
attributes->hostPointer = ptr;
}
if(attributes->memoryType == hipMemoryTypeDevice){
attributes->devicePointer = ptr;
}
attributes->allocationFlags = amPointerInfo._appAllocationFlags;
attributes->device = amPointerInfo._appId;
if (attributes->device < 0) {
e = hipErrorInvalidDevice;
}
} else {
attributes->memoryType = hipMemoryTypeDevice;
attributes->hostPointer = 0;
attributes->devicePointer = 0;
attributes->device = -1;
attributes->isManaged = 0;
attributes->allocationFlags = 0;
e = hipErrorUnknown; // TODO - should be hipErrorInvalidValue ?
}
return ihipLogStatus(e);
}
hipError_t hipHostGetDevicePointer(void **devicePointer, void *hostPointer, unsigned flags)
{
HIP_INIT_API(devicePointer, hostPointer, flags);
2016-03-24 09:28:46 -05:00
hipError_t e = hipSuccess;
2016-06-10 20:12:46 -05:00
*devicePointer = NULL;
2016-03-24 09:28:46 -05:00
// Flags must be 0:
if (flags != 0) {
e = hipErrorInvalidValue;
} else {
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t status = hc::am_memtracker_getinfo(&amPointerInfo, hostPointer);
if (status == AM_SUCCESS) {
*devicePointer = amPointerInfo._devicePointer;
} else {
e = hipErrorMemoryAllocation;
}
}
return ihipLogStatus(e);
}
hipError_t hipMalloc(void** ptr, size_t sizeBytes)
{
HIP_INIT_API(ptr, sizeBytes);
hipError_t hip_status = hipSuccess;
2016-10-11 12:09:58 -05:00
// return NULL pointer when malloc size is 0
if (sizeBytes == 0)
{
*ptr = NULL;
2016-09-22 10:39:17 -05:00
return ihipLogStatus(hipSuccess);
}
2016-10-11 12:09:58 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-03-24 09:28:46 -05:00
2016-08-07 21:46:51 -05:00
if (ctx) {
auto device = ctx->getWriteableDevice();
2016-03-24 09:28:46 -05:00
const unsigned am_flags = 0;
*ptr = hc::am_alloc(sizeBytes, device->_acc, am_flags);
2016-03-24 09:28:46 -05:00
2016-11-06 10:36:08 -06:00
2016-03-24 09:28:46 -05:00
if (sizeBytes && (*ptr == NULL)) {
hip_status = hipErrorMemoryAllocation;
} else {
hc::am_memtracker_update(*ptr, device->_deviceId, 0);
int peerCnt=0;
{
LockedAccessor_CtxCrit_t crit(ctx->criticalData());
2016-11-09 21:38:43 -06:00
// the peerCnt always stores self so make sure the trace actually
peerCnt = crit->peerCnt();
tprintf(DB_MEM, " allocated device_mem ptr:%p size:%zu on dev:%d and allowed %d other peer(s) access\n",
2016-11-06 10:36:08 -06:00
*ptr, sizeBytes, device->_deviceId, peerCnt-1);
if (peerCnt > 1) {
2016-11-06 10:36:08 -06:00
//printf ("peer self access\n");
2016-11-06 10:36:08 -06:00
// TODOD - remove me:
for (auto iter = crit->_peers.begin(); iter!=crit->_peers.end(); iter++) {
tprintf (DB_MEM, " allow access to peer: %s%s\n", (*iter)->toString().c_str(), (iter == crit->_peers.begin()) ? " (self)":"");
};
hsa_status_t e = hsa_amd_agents_allow_access(crit->peerCnt(), crit->peerAgents(), NULL, *ptr);
if (e != HSA_STATUS_SUCCESS) {
hip_status = hipErrorMemoryAllocation;
}
}
}
2016-03-24 09:28:46 -05:00
}
} else {
hip_status = hipErrorMemoryAllocation;
}
2016-09-01 18:00:31 -05:00
2016-03-24 09:28:46 -05:00
return ihipLogStatus(hip_status);
}
void ihipReadSingleEnv(int *var_ptr, const char *var_name1, const char *description)
{
char * env = getenv(var_name1);
// Default is set when variable is initialized (at top of this file), so only override if we find
// an environment variable.
if (env) {
long int v = strtol(env, NULL, 0);
*var_ptr = (int) (v);
}
if (HIP_PRINT_ENV) {
printf ("%-30s = %2d : %s\n", var_name1, *var_ptr, description);
}
}
2016-03-24 09:28:46 -05:00
hipError_t hipHostMalloc(void** ptr, size_t sizeBytes, unsigned int flags)
{
HIP_INIT_API(ptr, sizeBytes, flags);
hipError_t hip_status = hipSuccess;
2016-08-07 21:46:51 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-03-24 09:28:46 -05:00
2016-11-10 10:49:44 -06:00
if (sizeBytes == 0) {
hip_status = hipSuccess;
// TODO - should size of 0 return err or be siliently ignored?
} else if ((ctx==nullptr) || (ptr == nullptr)) {
hip_status = hipErrorInvalidValue;
} else {
unsigned trueFlags = flags;
if (flags == hipHostMallocDefault) {
trueFlags = hipHostMallocMapped | hipHostMallocWriteCombined;
}
2016-11-10 10:49:44 -06:00
const unsigned supportedFlags = hipHostMallocPortable | hipHostMallocMapped | hipHostMallocWriteCombined;
// Read from environment variable of HIP_COHERENT_HOST_ALLOC
int coherent_alloc=0;
ihipReadSingleEnv(&coherent_alloc, "HIP_COHERENT_HOST_ALLOC", "Flag to force allocate finegrained system memory");
2016-11-10 10:49:44 -06:00
if (flags & ~supportedFlags) {
hip_status = hipErrorInvalidValue;
}
else {
2016-11-10 10:49:44 -06:00
auto device = ctx->getWriteableDevice();
if(coherent_alloc){
// Force to allocate finedgrained system memory
*ptr = hc::am_alloc(sizeBytes, device->_acc, amHostPinned);
if(sizeBytes < 1 && (*ptr == NULL)){
hip_status = hipErrorMemoryAllocation;
} else {
hc::am_memtracker_update(*ptr, device->_deviceId, amHostCoherent);
}
tprintf(DB_MEM, " %s: finegrained system memory ptr=%p\n", __func__, *ptr);
}
else{
// TODO - am_alloc requires writeable __acc, perhaps could be refactored?
// TODO - hipHostMallocMapped is be ignored on ROCM - all memory is mapped to host address space as WC.
*ptr = hc::am_alloc(sizeBytes, device->_acc, amHostPinned);
if (*ptr == NULL) {
hip_status = hipErrorMemoryAllocation;
} else {
hc::am_memtracker_update(*ptr, device->_deviceId, flags);
// TODO-hipHostMallocPortable should map the host memory into all contexts, regardless of peer status.
int peerCnt=0;
{
LockedAccessor_CtxCrit_t crit(ctx->criticalData());
peerCnt = crit->peerCnt();
if (peerCnt > 1) {
hsa_amd_agents_allow_access(crit->peerCnt(), crit->peerAgents(), NULL, *ptr);
}
}
tprintf(DB_MEM, "allocated pinned_host ptr:%p size:%zu on dev:%d and allow access to %d other peer(s)\n", *ptr, sizeBytes, device->_deviceId, peerCnt-1);
}
2016-03-24 09:28:46 -05:00
}
}
}
return ihipLogStatus(hip_status);
}
hipError_t hipMallocHost(void** ptr, size_t sizeBytes)
{
return hipHostMalloc(ptr, sizeBytes, 0);
}
hipError_t hipHostAlloc(void** ptr, size_t sizeBytes, unsigned int flags)
{
return hipHostMalloc(ptr, sizeBytes, flags);
};
2016-07-21 12:29:56 +05:30
// width in bytes
2016-10-11 12:09:58 -05:00
hipError_t hipMallocPitch(void** ptr, size_t* pitch, size_t width, size_t height)
{
HIP_INIT_API(ptr, pitch, width, height);
2016-07-21 12:29:56 +05:30
hipError_t hip_status = hipSuccess;
2016-07-21 12:29:56 +05:30
if(width == 0 || height == 0)
return ihipLogStatus(hipErrorUnknown);
2016-07-21 12:29:56 +05:30
// hardcoded 128 bytes
*pitch = ((((int)width-1)/128) + 1)*128;
const size_t sizeBytes = (*pitch)*height;
2016-07-21 12:29:56 +05:30
auto ctx = ihipGetTlsDefaultCtx();
2016-07-21 12:29:56 +05:30
//err = hipMalloc(ptr, (*pitch)*height);
if (ctx) {
auto device = ctx->getWriteableDevice();
2016-07-21 12:29:56 +05:30
const unsigned am_flags = 0;
*ptr = hc::am_alloc(sizeBytes, device->_acc, am_flags);
2016-07-21 12:29:56 +05:30
if (sizeBytes && (*ptr == NULL)) {
2016-07-21 12:29:56 +05:30
hip_status = hipErrorMemoryAllocation;
} else {
hc::am_memtracker_update(*ptr, device->_deviceId, 0);
{
LockedAccessor_CtxCrit_t crit(ctx->criticalData());
if (crit->peerCnt() > 1) { // peerCnt includes self so only call allow_access if other peers involved:
hsa_status_t hsa_status = hsa_amd_agents_allow_access(crit->peerCnt(), crit->peerAgents(), NULL, *ptr);
if (hsa_status != HSA_STATUS_SUCCESS) {
hip_status = hipErrorMemoryAllocation;
}
}
}
2016-07-21 12:29:56 +05:30
}
} else {
hip_status = hipErrorMemoryAllocation;
2016-07-21 12:29:56 +05:30
}
return ihipLogStatus(hip_status);
2016-07-21 12:29:56 +05:30
}
2016-10-11 12:09:58 -05:00
hipChannelFormatDesc hipCreateChannelDesc(int x, int y, int z, int w, hipChannelFormatKind f)
{
hipChannelFormatDesc cd;
cd.x = x; cd.y = y; cd.z = z; cd.w = w;
cd.f = f;
return cd;
2016-07-21 12:29:56 +05:30
}
hipError_t hipMallocArray(hipArray** array, const hipChannelFormatDesc* desc,
2016-10-11 12:09:58 -05:00
size_t width, size_t height, unsigned int flags)
{
2016-07-21 12:29:56 +05:30
HIP_INIT_API(array, desc, width, height, flags);
hipError_t hip_status = hipSuccess;
2016-08-07 21:46:51 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-07-21 12:29:56 +05:30
*array = (hipArray*)malloc(sizeof(hipArray));
array[0]->width = width;
array[0]->height = height;
array[0]->f = desc->f;
void ** ptr = &array[0]->data;
2016-08-07 21:46:51 -05:00
if (ctx) {
auto device = ctx->getWriteableDevice();
const unsigned am_flags = 0;
const size_t size = width*height;
switch(desc->f) {
case hipChannelFormatKindSigned:
*ptr = hc::am_alloc(size*sizeof(int), device->_acc, am_flags);
break;
case hipChannelFormatKindUnsigned:
*ptr = hc::am_alloc(size*sizeof(unsigned int), device->_acc, am_flags);
break;
case hipChannelFormatKindFloat:
*ptr = hc::am_alloc(size*sizeof(float), device->_acc, am_flags);
break;
case hipChannelFormatKindNone:
*ptr = hc::am_alloc(size*sizeof(size_t), device->_acc, am_flags);
break;
default:
hip_status = hipErrorUnknown;
break;
}
if (size && (*ptr == NULL)) {
hip_status = hipErrorMemoryAllocation;
} else {
hc::am_memtracker_update(*ptr, device->_deviceId, 0);
{
LockedAccessor_CtxCrit_t crit(ctx->criticalData());
if (crit->peerCnt() > 1) { // peerCnt includes self so only call allow_access if other peers involved:
hsa_status_t hsa_status = hsa_amd_agents_allow_access(crit->peerCnt(), crit->peerAgents(), NULL, *ptr);
if (hsa_status != HSA_STATUS_SUCCESS) {
hip_status = hipErrorMemoryAllocation;
}
}
}
}
2016-07-21 12:29:56 +05:30
} else {
hip_status = hipErrorMemoryAllocation;
}
return ihipLogStatus(hip_status);
}
2016-03-24 09:28:46 -05:00
hipError_t hipHostGetFlags(unsigned int* flagsPtr, void* hostPtr)
{
HIP_INIT_API(flagsPtr, hostPtr);
hipError_t hip_status = hipSuccess;
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t status = hc::am_memtracker_getinfo(&amPointerInfo, hostPtr);
if(status == AM_SUCCESS){
*flagsPtr = amPointerInfo._appAllocationFlags;
if(*flagsPtr == 0){
hip_status = hipErrorInvalidValue;
}
else{
hip_status = hipSuccess;
}
tprintf(DB_MEM, " %s: host ptr=%p\n", __func__, hostPtr);
}else{
hip_status = hipErrorInvalidValue;
}
return ihipLogStatus(hip_status);
2016-03-24 09:28:46 -05:00
}
hipError_t hipHostRegister(void *hostPtr, size_t sizeBytes, unsigned int flags)
{
HIP_INIT_API(hostPtr, sizeBytes, flags);
2016-04-15 10:32:01 -05:00
hipError_t hip_status = hipSuccess;
2016-03-24 09:28:46 -05:00
2016-08-07 21:46:51 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-04-15 10:32:01 -05:00
if(hostPtr == NULL){
return ihipLogStatus(hipErrorInvalidValue);
}
2016-04-15 10:08:10 -05:00
2016-04-15 10:32:01 -05:00
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t am_status = hc::am_memtracker_getinfo(&amPointerInfo, hostPtr);
if(am_status == AM_SUCCESS){
2016-04-15 10:32:01 -05:00
hip_status = hipErrorHostMemoryAlreadyRegistered;
} else {
2016-08-07 21:46:51 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-04-15 10:32:01 -05:00
if(hostPtr == NULL){
return ihipLogStatus(hipErrorInvalidValue);
}
if (ctx) {
auto device = ctx->getWriteableDevice();
2016-04-15 10:32:01 -05:00
if(flags == hipHostRegisterDefault || flags == hipHostRegisterPortable || flags == hipHostRegisterMapped){
std::vector<hc::accelerator>vecAcc;
for(int i=0;i<g_deviceCnt;i++){
2016-08-07 21:46:51 -05:00
vecAcc.push_back(ihipGetDevice(i)->_acc);
2016-04-15 10:32:01 -05:00
}
am_status = hc::am_memory_host_lock(device->_acc, hostPtr, sizeBytes, &vecAcc[0], vecAcc.size());
tprintf(DB_MEM, " %s registered ptr=%p\n", __func__, hostPtr);
2016-04-15 10:32:01 -05:00
if(am_status == AM_SUCCESS){
hip_status = hipSuccess;
} else {
2016-04-15 10:32:01 -05:00
hip_status = hipErrorMemoryAllocation;
}
} else {
2016-04-15 10:32:01 -05:00
hip_status = hipErrorInvalidValue;
}
}
}
return ihipLogStatus(hip_status);
}
2016-03-24 09:28:46 -05:00
hipError_t hipHostUnregister(void *hostPtr)
{
HIP_INIT_API(hostPtr);
2016-08-07 21:46:51 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-04-15 10:32:01 -05:00
hipError_t hip_status = hipSuccess;
if(hostPtr == NULL){
hip_status = hipErrorInvalidValue;
}else{
auto device = ctx->getWriteableDevice();
am_status_t am_status = hc::am_memory_host_unlock(device->_acc, hostPtr);
tprintf(DB_MEM, " %s unregistered ptr=%p\n", __func__, hostPtr);
2016-04-15 10:32:01 -05:00
if(am_status != AM_SUCCESS){
hip_status = hipErrorHostMemoryNotRegistered;
2016-04-15 10:32:01 -05:00
}
}
return ihipLogStatus(hip_status);
2016-03-24 09:28:46 -05:00
}
hipError_t hipMemcpyToSymbol(const char* symbolName, const void *src, size_t count, size_t offset, hipMemcpyKind kind)
{
HIP_INIT_API(symbolName, src, count, offset, kind);
2016-10-11 13:29:46 -05:00
if(symbolName == nullptr)
{
2016-10-11 13:29:46 -05:00
return ihipLogStatus(hipErrorInvalidSymbol);
}
2016-03-24 09:28:46 -05:00
2016-10-11 13:29:46 -05:00
auto ctx = ihipGetTlsDefaultCtx();
2016-03-24 09:28:46 -05:00
2016-10-11 12:09:58 -05:00
hc::accelerator acc = ctx->getDevice()->_acc;
2016-10-11 13:29:46 -05:00
void *ptr = acc.get_symbol_address(symbolName);
tprintf(DB_MEM, " symbol '%s' resolved to address:%p\n", symbolName, ptr);
2016-10-11 13:29:46 -05:00
if(ptr == nullptr)
{
return ihipLogStatus(hipErrorInvalidSymbol);
}
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
stream->locked_copySync(ptr, src, count + offset, kind);
2016-03-24 09:28:46 -05:00
return ihipLogStatus(hipSuccess);
}
2016-10-11 13:29:46 -05:00
hipError_t hipMemcpyToSymbolAsync(const char* symbolName, const void *src, size_t count, size_t offset, hipMemcpyKind kind, hipStream_t stream)
{
HIP_INIT_API(symbolName, src, count, offset, kind, stream);
if(symbolName == nullptr)
{
return ihipLogStatus(hipErrorInvalidSymbol);
}
hipError_t e = hipSuccess;
auto ctx = ihipGetTlsDefaultCtx();
hc::accelerator acc = ctx->getDevice()->_acc;
void *ptr = acc.get_symbol_address(symbolName);
tprintf(DB_MEM, " symbol '%s' resolved to address:%p\n", symbolName, ptr);
2016-10-11 13:29:46 -05:00
if(ptr == nullptr)
{
return ihipLogStatus(hipErrorInvalidSymbol);
}
if (stream) {
try {
stream->locked_copyAsync(ptr, src, count + offset, kind);
}
catch (ihipException ex) {
e = ex._code;
}
} else {
e = hipErrorInvalidValue;
}
return ihipLogStatus(e);
}
2016-03-24 09:28:46 -05:00
//---
hipError_t hipMemcpy(void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind)
{
HIP_INIT_API(dst, src, sizeBytes, kind);
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
hc::completion_future marker;
hipError_t e = hipSuccess;
try {
2016-03-28 09:46:40 -05:00
stream->locked_copySync(dst, src, sizeBytes, kind);
2016-03-24 09:28:46 -05:00
}
catch (ihipException ex) {
e = ex._code;
}
2016-08-26 13:11:01 -05:00
return ihipLogStatus(e);
}
hipError_t hipMemcpyHtoD(hipDeviceptr_t dst, void* src, size_t sizeBytes)
2016-08-26 13:11:01 -05:00
{
HIP_INIT_API(dst, src, sizeBytes);
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
hc::completion_future marker;
hipError_t e = hipSuccess;
try {
stream->locked_copySync((void*)dst, (void*)src, sizeBytes, hipMemcpyHostToDevice, false);
}
catch (ihipException ex) {
e = ex._code;
}
return ihipLogStatus(e);
}
hipError_t hipMemcpyDtoH(void* dst, hipDeviceptr_t src, size_t sizeBytes)
2016-08-26 13:11:01 -05:00
{
HIP_INIT_API(dst, src, sizeBytes);
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
hc::completion_future marker;
hipError_t e = hipSuccess;
try {
stream->locked_copySync((void*)dst, (void*)src, sizeBytes, hipMemcpyDeviceToHost, false);
}
catch (ihipException ex) {
e = ex._code;
}
2016-03-24 09:28:46 -05:00
2016-08-26 13:11:01 -05:00
return ihipLogStatus(e);
}
hipError_t hipMemcpyDtoD(hipDeviceptr_t dst, hipDeviceptr_t src, size_t sizeBytes)
2016-08-26 13:11:01 -05:00
{
HIP_INIT_API(dst, src, sizeBytes);
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
hc::completion_future marker;
hipError_t e = hipSuccess;
try {
stream->locked_copySync((void*)dst, (void*)src, sizeBytes, hipMemcpyDeviceToDevice, false);
}
catch (ihipException ex) {
e = ex._code;
}
return ihipLogStatus(e);
}
hipError_t hipMemcpyHtoH(void* dst, void* src, size_t sizeBytes)
2016-08-26 13:11:01 -05:00
{
HIP_INIT_API(dst, src, sizeBytes);
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
hc::completion_future marker;
hipError_t e = hipSuccess;
try {
stream->locked_copySync((void*)dst, (void*)src, sizeBytes, hipMemcpyHostToHost, false);
}
catch (ihipException ex) {
e = ex._code;
}
2016-03-24 09:28:46 -05:00
return ihipLogStatus(e);
}
2016-11-10 10:49:44 -06:00
// Internal copy sync:
namespace hip_internal {
hipError_t memcpyAsync (void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind, hipStream_t stream)
{
2016-03-24 09:28:46 -05:00
hipError_t e = hipSuccess;
stream = ihipSyncAndResolveStream(stream);
if ((dst == NULL) || (src == NULL)) {
e= hipErrorInvalidValue;
} else if (stream) {
try {
stream->locked_copyAsync(dst, src, sizeBytes, kind);
2016-03-24 09:28:46 -05:00
}
catch (ihipException ex) {
e = ex._code;
}
} else {
e = hipErrorInvalidValue;
}
2016-11-10 10:49:44 -06:00
return e;
2016-03-24 09:28:46 -05:00
}
2016-11-10 10:49:44 -06:00
} // end namespace hip_internal
2016-03-24 09:28:46 -05:00
2016-11-10 10:49:44 -06:00
hipError_t hipMemcpyAsync(void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind, hipStream_t stream)
2016-09-09 10:21:52 -05:00
{
2016-11-10 10:49:44 -06:00
HIP_INIT_API(dst, src, sizeBytes, kind, stream);
2016-09-09 10:21:52 -05:00
2016-11-10 10:49:44 -06:00
return ihipLogStatus(hip_internal::memcpyAsync(dst, src, sizeBytes, kind, stream));
2016-09-09 10:21:52 -05:00
2016-11-10 10:49:44 -06:00
}
2016-09-09 10:21:52 -05:00
2016-11-10 10:49:44 -06:00
hipError_t hipMemcpyHtoDAsync(hipDeviceptr_t dst, void* src, size_t sizeBytes, hipStream_t stream)
{
HIP_INIT_API(dst, src, sizeBytes, stream);
2016-09-09 10:21:52 -05:00
2016-11-10 10:49:44 -06:00
return ihipLogStatus(hip_internal::memcpyAsync(dst, src, sizeBytes, hipMemcpyHostToDevice, stream));
2016-09-09 10:21:52 -05:00
}
hipError_t hipMemcpyDtoDAsync(hipDeviceptr_t dst, hipDeviceptr_t src, size_t sizeBytes, hipStream_t stream)
{
HIP_INIT_API(dst, src, sizeBytes, stream);
2016-11-10 10:49:44 -06:00
return ihipLogStatus(hip_internal::memcpyAsync(dst, src, sizeBytes, hipMemcpyDeviceToDevice, stream));
2016-09-09 10:21:52 -05:00
}
hipError_t hipMemcpyDtoHAsync(void* dst, hipDeviceptr_t src, size_t sizeBytes, hipStream_t stream)
{
HIP_INIT_API(dst, src, sizeBytes, stream);
2016-11-10 10:49:44 -06:00
return ihipLogStatus(hip_internal::memcpyAsync(dst, src, sizeBytes, hipMemcpyDeviceToHost, stream));
2016-09-09 10:21:52 -05:00
}
// TODO - review and optimize
2016-07-21 12:29:56 +05:30
hipError_t hipMemcpy2D(void* dst, size_t dpitch, const void* src, size_t spitch,
size_t width, size_t height, hipMemcpyKind kind) {
2016-07-21 12:29:56 +05:30
HIP_INIT_API(dst, dpitch, src, spitch, width, height, kind);
2016-07-21 12:29:56 +05:30
if(width > dpitch || width > spitch)
return ihipLogStatus(hipErrorUnknown);
2016-07-21 12:29:56 +05:30
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
2016-07-21 12:29:56 +05:30
hc::completion_future marker;
2016-07-21 12:29:56 +05:30
hipError_t e = hipSuccess;
2016-07-21 12:29:56 +05:30
try {
for(int i = 0; i < height; ++i) {
stream->locked_copySync((unsigned char*)dst + i*dpitch, (unsigned char*)src + i*spitch, width, kind);
}
}
catch (ihipException ex) {
e = ex._code;
2016-07-21 12:29:56 +05:30
}
return ihipLogStatus(e);
2016-07-21 12:29:56 +05:30
}
hipError_t hipMemcpy2DToArray(hipArray* dst, size_t wOffset, size_t hOffset, const void* src,
size_t spitch, size_t width, size_t height, hipMemcpyKind kind) {
HIP_INIT_API(dst, wOffset, hOffset, src, spitch, width, height, kind);
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
hc::completion_future marker;
hipError_t e = hipSuccess;
size_t byteSize;
if(dst) {
switch(dst[0].f) {
case hipChannelFormatKindSigned:
byteSize = sizeof(int);
break;
case hipChannelFormatKindUnsigned:
byteSize = sizeof(unsigned int);
break;
case hipChannelFormatKindFloat:
byteSize = sizeof(float);
break;
case hipChannelFormatKindNone:
byteSize = sizeof(size_t);
break;
default:
byteSize = 0;
break;
}
} else {
return ihipLogStatus(hipErrorUnknown);
2016-07-21 12:29:56 +05:30
}
if((wOffset + width > (dst->width * byteSize)) || width > spitch) {
return ihipLogStatus(hipErrorUnknown);
}
2016-07-21 12:29:56 +05:30
size_t src_w = spitch;
size_t dst_w = (dst->width)*byteSize;
2016-07-21 12:29:56 +05:30
try {
for(int i = 0; i < height; ++i) {
stream->locked_copySync((unsigned char*)dst->data + i*dst_w, (unsigned char*)src + i*src_w, width, kind);
}
}
catch (ihipException ex) {
e = ex._code;
2016-07-21 12:29:56 +05:30
}
return ihipLogStatus(e);
2016-07-21 12:29:56 +05:30
}
hipError_t hipMemcpyToArray(hipArray* dst, size_t wOffset, size_t hOffset,
const void* src, size_t count, hipMemcpyKind kind) {
2016-07-21 12:29:56 +05:30
HIP_INIT_API(dst, wOffset, hOffset, src, count, kind);
2016-07-21 12:29:56 +05:30
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
2016-07-21 12:29:56 +05:30
hc::completion_future marker;
2016-07-21 12:29:56 +05:30
hipError_t e = hipSuccess;
2016-07-21 12:29:56 +05:30
try {
stream->locked_copySync((char *)dst->data + wOffset, src, count, kind);
}
catch (ihipException ex) {
e = ex._code;
}
2016-07-21 12:29:56 +05:30
return ihipLogStatus(e);
2016-07-21 12:29:56 +05:30
}
2016-08-30 17:29:50 -05:00
// TODO - make member function of stream?
template <typename T>
hc::completion_future
2016-10-11 12:09:58 -05:00
ihipMemsetKernel(hipStream_t stream,
2016-08-30 17:29:50 -05:00
LockedAccessor_StreamCrit_t &crit,
T * ptr, T val, size_t sizeBytes)
{
int wg = std::min((unsigned)8, stream->getDevice()->_computeUnits);
const int threads_per_wg = 256;
int threads = wg * threads_per_wg;
if (threads > sizeBytes) {
threads = ((sizeBytes + threads_per_wg - 1) / threads_per_wg) * threads_per_wg;
}
hc::extent<1> ext(threads);
auto ext_tile = ext.tile(threads_per_wg);
hc::completion_future cf =
hc::parallel_for_each(
2016-08-30 17:29:50 -05:00
crit->_av,
ext_tile,
[=] (hc::tiled_index<1> idx)
__attribute__((hc))
{
int offset = amp_get_global_id(0);
// TODO-HCC - change to hc_get_local_size()
int stride = amp_get_local_size(0) * hc_get_num_groups(0) ;
for (int i=offset; i<sizeBytes; i+=stride) {
ptr[i] = val;
}
});
return cf;
}
2016-03-24 09:28:46 -05:00
// TODO-sync: function is async unless target is pinned host memory - then these are fully sync.
hipError_t hipMemsetAsync(void* dst, int value, size_t sizeBytes, hipStream_t stream )
{
HIP_INIT_API(dst, value, sizeBytes, stream);
hipError_t e = hipSuccess;
stream = ihipSyncAndResolveStream(stream);
if (stream) {
2016-08-30 17:29:50 -05:00
auto crit = stream->lockopen_preKernelCommand();
2016-03-24 09:28:46 -05:00
hc::completion_future cf ;
if ((sizeBytes & 0x3) == 0) {
2016-04-18 20:49:33 -05:00
// use a faster dword-per-workitem copy:
2016-03-24 09:28:46 -05:00
try {
value = value & 0xff;
unsigned value32 = (value << 24) | (value << 16) | (value << 8) | (value) ;
2016-08-30 17:29:50 -05:00
cf = ihipMemsetKernel<unsigned> (stream, crit, static_cast<unsigned*> (dst), value32, sizeBytes/sizeof(unsigned));
2016-03-24 09:28:46 -05:00
}
catch (std::exception &ex) {
e = hipErrorInvalidValue;
}
} else {
// use a slow byte-per-workitem copy:
try {
2016-08-30 17:29:50 -05:00
cf = ihipMemsetKernel<char> (stream, crit, static_cast<char*> (dst), value, sizeBytes);
2016-03-24 09:28:46 -05:00
}
catch (std::exception &ex) {
e = hipErrorInvalidValue;
}
}
2016-09-26 16:32:35 -05:00
stream->lockclose_postKernelCommand(&crit->_av);
2016-03-24 09:28:46 -05:00
if (HIP_LAUNCH_BLOCKING) {
2016-04-18 20:49:33 -05:00
tprintf (DB_SYNC, "'%s' LAUNCH_BLOCKING wait for memset [stream:%p].\n", __func__, (void*)stream);
2016-03-24 09:28:46 -05:00
cf.wait();
2016-04-18 20:49:33 -05:00
tprintf (DB_SYNC, "'%s' LAUNCH_BLOCKING memset completed [stream:%p].\n", __func__, (void*)stream);
2016-03-24 09:28:46 -05:00
}
} else {
e = hipErrorInvalidValue;
}
return ihipLogStatus(e);
};
hipError_t hipMemset(void* dst, int value, size_t sizeBytes )
{
2016-04-25 11:05:30 -05:00
hipStream_t stream = hipStreamNull;
2016-03-24 09:28:46 -05:00
// TODO - call an ihip memset so HIP_TRACE is correct.
2016-04-25 11:05:30 -05:00
HIP_INIT_API(dst, value, sizeBytes, stream);
hipError_t e = hipSuccess;
stream = ihipSyncAndResolveStream(stream);
if (stream) {
2016-08-30 17:29:50 -05:00
auto crit = stream->lockopen_preKernelCommand();
2016-04-25 11:05:30 -05:00
hc::completion_future cf ;
if ((sizeBytes & 0x3) == 0) {
// use a faster dword-per-workitem copy:
try {
value = value & 0xff;
unsigned value32 = (value << 24) | (value << 16) | (value << 8) | (value) ;
2016-08-30 17:29:50 -05:00
cf = ihipMemsetKernel<unsigned> (stream, crit, static_cast<unsigned*> (dst), value32, sizeBytes/sizeof(unsigned));
2016-04-25 11:05:30 -05:00
}
catch (std::exception &ex) {
e = hipErrorInvalidValue;
}
} else {
// use a slow byte-per-workitem copy:
try {
2016-08-30 17:29:50 -05:00
cf = ihipMemsetKernel<char> (stream, crit, static_cast<char*> (dst), value, sizeBytes);
2016-04-25 11:05:30 -05:00
}
catch (std::exception &ex) {
e = hipErrorInvalidValue;
}
}
2016-09-26 16:32:35 -05:00
// TODO - is hipMemset supposed to be async?
2016-04-25 11:05:30 -05:00
cf.wait();
2016-09-26 16:32:35 -05:00
stream->lockclose_postKernelCommand(&crit->_av);
2016-04-25 11:05:30 -05:00
if (HIP_LAUNCH_BLOCKING) {
tprintf (DB_SYNC, "'%s' LAUNCH_BLOCKING wait for memset [stream:%p].\n", __func__, (void*)stream);
cf.wait();
tprintf (DB_SYNC, "'%s' LAUNCH_BLOCKING memset completed [stream:%p].\n", __func__, (void*)stream);
}
} else {
e = hipErrorInvalidValue;
}
return ihipLogStatus(e);
2016-03-24 09:28:46 -05:00
}
hipError_t hipMemGetInfo (size_t *free, size_t *total)
{
HIP_INIT_API(free, total);
hipError_t e = hipSuccess;
2016-08-07 21:46:51 -05:00
ihipCtx_t * ctx = ihipGetTlsDefaultCtx();
if (ctx) {
auto device = ctx->getWriteableDevice();
2016-03-24 09:28:46 -05:00
if (total) {
*total = device->_props.totalGlobalMem;
2016-03-24 09:28:46 -05:00
}
else {
e = hipErrorInvalidValue;
}
2016-03-24 09:28:46 -05:00
if (free) {
// TODO - replace with kernel-level for reporting free memory:
size_t deviceMemSize, hostMemSize, userMemSize;
hc::am_memtracker_sizeinfo(device->_acc, &deviceMemSize, &hostMemSize, &userMemSize);
*free = device->_props.totalGlobalMem - deviceMemSize;
2016-03-24 09:28:46 -05:00
}
else {
e = hipErrorInvalidValue;
}
2016-03-24 09:28:46 -05:00
} else {
e = hipErrorInvalidDevice;
}
return ihipLogStatus(e);
}
hipError_t hipFree(void* ptr)
{
HIP_INIT_API(ptr);
hipError_t hipStatus = hipErrorInvalidDevicePointer;
// Synchronize to ensure all work has finished.
2016-08-07 21:46:51 -05:00
ihipGetTlsDefaultCtx()->locked_waitAllStreams(); // ignores non-blocking streams, this waits for all activity to finish.
2016-03-24 09:28:46 -05:00
if (ptr) {
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t status = hc::am_memtracker_getinfo(&amPointerInfo, ptr);
if(status == AM_SUCCESS){
if(amPointerInfo._hostPointer == NULL){
hc::am_free(ptr);
hipStatus = hipSuccess;
}
}
2016-06-10 20:12:46 -05:00
} else {
// free NULL pointer succeeds and is common technique to initialize runtime
hipStatus = hipSuccess;
2016-03-24 09:28:46 -05:00
}
return ihipLogStatus(hipStatus);
}
hipError_t hipHostFree(void* ptr)
{
HIP_INIT_API(ptr);
// Synchronize to ensure all work has finished.
2016-08-07 21:46:51 -05:00
ihipGetTlsDefaultCtx()->locked_waitAllStreams(); // ignores non-blocking streams, this waits for all activity to finish.
2016-03-24 09:28:46 -05:00
2016-06-10 20:12:46 -05:00
hipError_t hipStatus = hipErrorInvalidValue;
2016-03-24 09:28:46 -05:00
if (ptr) {
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t status = hc::am_memtracker_getinfo(&amPointerInfo, ptr);
if(status == AM_SUCCESS){
if(amPointerInfo._hostPointer == ptr){
hc::am_free(ptr);
hipStatus = hipSuccess;
}
}
2016-06-10 20:12:46 -05:00
} else {
// free NULL pointer succeeds and is common technique to initialize runtime
hipStatus = hipSuccess;
2016-03-24 09:28:46 -05:00
}
return ihipLogStatus(hipStatus);
};
hipError_t hipFreeHost(void* ptr)
{
return hipHostFree(ptr);
}
2016-07-21 12:29:56 +05:30
hipError_t hipFreeArray(hipArray* array)
{
HIP_INIT_API(array);
2016-07-21 12:29:56 +05:30
hipError_t hipStatus = hipErrorInvalidDevicePointer;
2016-07-21 12:29:56 +05:30
// Synchronize to ensure all work has finished.
ihipGetTlsDefaultCtx()->locked_waitAllStreams(); // ignores non-blocking streams, this waits for all activity to finish.
2016-07-21 12:29:56 +05:30
if(array->data) {
hc::accelerator acc;
hc::AmPointerInfo amPointerInfo(NULL, NULL, 0, acc, 0, 0);
am_status_t status = hc::am_memtracker_getinfo(&amPointerInfo, array->data);
if(status == AM_SUCCESS){
if(amPointerInfo._hostPointer == NULL){
hc::am_free(array->data);
hipStatus = hipSuccess;
}
}
2016-07-21 12:29:56 +05:30
}
return ihipLogStatus(hipStatus);
2016-07-21 12:29:56 +05:30
}
2016-03-24 09:28:46 -05:00