2016-03-24 07:04:01 -05:00
|
|
|
/*
|
2016-08-08 11:55:57 -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-03-24 07:04:01 -05:00
|
|
|
|
2016-03-24 04:57:30 -05:00
|
|
|
#include "hip_runtime.h"
|
|
|
|
|
#include "hcc_detail/hip_hcc.h"
|
|
|
|
|
#include "hcc_detail/trace_helper.h"
|
|
|
|
|
#include <hsa.h>
|
|
|
|
|
#include <hc_am.hpp>
|
|
|
|
|
#include <hsa_ext_amd.h>
|
2016-03-24 09:28:46 -05:00
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
|
// Memory
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
hipError_t hipPointerGetAttributes(hipPointerAttribute_t *attributes, void* ptr)
|
|
|
|
|
{
|
2016-07-22 15:46:55 +05:30
|
|
|
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)
|
|
|
|
|
{
|
2016-07-22 15:46:55 +05:30
|
|
|
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-09-01 13:06:55 -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-09-01 13:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-08-07 21:46:51 -05:00
|
|
|
if (ctx) {
|
2016-08-08 11:55:57 -05:00
|
|
|
auto device = ctx->getWriteableDevice();
|
2016-03-24 09:28:46 -05:00
|
|
|
const unsigned am_flags = 0;
|
2016-08-08 11:55:57 -05:00
|
|
|
*ptr = hc::am_alloc(sizeBytes, device->_acc, am_flags);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
if (sizeBytes && (*ptr == NULL)) {
|
|
|
|
|
hip_status = hipErrorMemoryAllocation;
|
|
|
|
|
} else {
|
2016-08-08 14:54:38 -05:00
|
|
|
hc::am_memtracker_update(*ptr, device->_deviceId, 0);
|
2016-04-06 16:44:31 -05:00
|
|
|
{
|
2016-08-08 11:55:57 -05:00
|
|
|
LockedAccessor_CtxCrit_t crit(ctx->criticalData());
|
2016-04-15 10:08:10 -05:00
|
|
|
if (crit->peerCnt()) {
|
|
|
|
|
hsa_amd_agents_allow_access(crit->peerCnt(), crit->peerAgents(), NULL, *ptr);
|
2016-04-06 16:44:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
hip_status = hipErrorMemoryAllocation;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 18:00:31 -05:00
|
|
|
//printf (" hipMalloc allocated %p\n", *ptr);
|
|
|
|
|
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hip_status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-08-07 21:46:51 -05:00
|
|
|
if(ctx){
|
2016-08-08 11:55:57 -05:00
|
|
|
// am_alloc requires writeable __acc, perhaps could be refactored?
|
|
|
|
|
auto device = ctx->getWriteableDevice();
|
2016-03-24 09:28:46 -05:00
|
|
|
if(flags == hipHostMallocDefault){
|
2016-08-08 11:55:57 -05:00
|
|
|
*ptr = hc::am_alloc(sizeBytes, device->_acc, amHostPinned);
|
2016-04-16 17:10:13 -05:00
|
|
|
if(sizeBytes < 1 && (*ptr == NULL)){
|
2016-03-24 09:28:46 -05:00
|
|
|
hip_status = hipErrorMemoryAllocation;
|
2016-08-08 11:55:57 -05:00
|
|
|
} else {
|
2016-08-08 14:54:38 -05:00
|
|
|
hc::am_memtracker_update(*ptr, device->_deviceId, amHostPinned);
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
tprintf(DB_MEM, " %s: pinned ptr=%p\n", __func__, *ptr);
|
|
|
|
|
} else if(flags & hipHostMallocMapped){
|
2016-08-08 11:55:57 -05:00
|
|
|
*ptr = hc::am_alloc(sizeBytes, device->_acc, amHostPinned);
|
2016-03-24 09:28:46 -05:00
|
|
|
if(sizeBytes && (*ptr == NULL)){
|
|
|
|
|
hip_status = hipErrorMemoryAllocation;
|
|
|
|
|
}else{
|
2016-08-08 14:54:38 -05:00
|
|
|
hc::am_memtracker_update(*ptr, device->_deviceId, flags);
|
2016-04-06 16:44:31 -05:00
|
|
|
{
|
2016-08-08 11:55:57 -05:00
|
|
|
LockedAccessor_CtxCrit_t crit(ctx->criticalData());
|
2016-04-15 10:08:10 -05:00
|
|
|
if (crit->peerCnt()) {
|
|
|
|
|
hsa_amd_agents_allow_access(crit->peerCnt(), crit->peerAgents(), NULL, *ptr);
|
2016-04-06 16:44:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
tprintf(DB_MEM, " %s: pinned ptr=%p\n", __func__, *ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ihipLogStatus(hip_status);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 12:29:56 +05:30
|
|
|
// width in bytes
|
2016-08-08 11:55:57 -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
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipError_t hip_status = hipSuccess;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
if(width == 0 || height == 0)
|
|
|
|
|
return ihipLogStatus(hipErrorUnknown);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
// hardcoded 128 bytes
|
|
|
|
|
*pitch = ((((int)width-1)/128) + 1)*128;
|
|
|
|
|
const size_t sizeBytes = (*pitch)*height;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
//err = hipMalloc(ptr, (*pitch)*height);
|
|
|
|
|
if (ctx) {
|
|
|
|
|
auto device = ctx->getWriteableDevice();
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
const unsigned am_flags = 0;
|
|
|
|
|
*ptr = hc::am_alloc(sizeBytes, device->_acc, am_flags);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
if (sizeBytes && (*ptr == NULL)) {
|
2016-07-21 12:29:56 +05:30
|
|
|
hip_status = hipErrorMemoryAllocation;
|
2016-08-08 11:55:57 -05:00
|
|
|
} else {
|
2016-08-08 14:54:38 -05:00
|
|
|
hc::am_memtracker_update(*ptr, device->_deviceId, 0);
|
2016-08-08 11:55:57 -05:00
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
2016-08-08 11:55:57 -05:00
|
|
|
} else {
|
|
|
|
|
hip_status = hipErrorMemoryAllocation;
|
2016-07-21 12:29:56 +05:30
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
return ihipLogStatus(hip_status);
|
2016-07-21 12:29:56 +05:30
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -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
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipError_t hipMallocArray(hipArray** array, const hipChannelFormatDesc* desc,
|
|
|
|
|
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) {
|
2016-08-08 11:55:57 -05:00
|
|
|
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 {
|
2016-08-08 14:54:38 -05:00
|
|
|
hc::am_memtracker_update(*ptr, device->_deviceId, 0);
|
2016-08-08 11:55:57 -05:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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);
|
|
|
|
|
|
2016-04-15 10:42:31 -05:00
|
|
|
if(am_status == AM_SUCCESS){
|
2016-04-15 10:32:01 -05:00
|
|
|
hip_status = hipErrorHostMemoryAlreadyRegistered;
|
2016-08-08 11:55:57 -05:00
|
|
|
} 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);
|
|
|
|
|
}
|
2016-08-08 11:55:57 -05:00
|
|
|
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
|
|
|
}
|
2016-08-08 11:55:57 -05:00
|
|
|
am_status = hc::am_memory_host_lock(device->_acc, hostPtr, sizeBytes, &vecAcc[0], vecAcc.size());
|
2016-04-15 10:32:01 -05:00
|
|
|
if(am_status == AM_SUCCESS){
|
|
|
|
|
hip_status = hipSuccess;
|
2016-08-08 11:55:57 -05:00
|
|
|
} else {
|
2016-04-15 10:32:01 -05:00
|
|
|
hip_status = hipErrorMemoryAllocation;
|
|
|
|
|
}
|
2016-08-08 11:55:57 -05:00
|
|
|
} 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{
|
2016-08-08 11:55:57 -05:00
|
|
|
auto device = ctx->getWriteableDevice();
|
|
|
|
|
am_status_t am_status = hc::am_memory_host_unlock(device->_acc, hostPtr);
|
2016-04-15 10:32:01 -05:00
|
|
|
if(am_status != AM_SUCCESS){
|
2016-04-15 10:42:31 -05:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
#ifdef USE_MEMCPYTOSYMBOL
|
2016-08-08 11:55:57 -05:00
|
|
|
if(kind != hipMemcpyHostToDevice)
|
|
|
|
|
{
|
|
|
|
|
return ihipLogStatus(hipErrorInvalidValue);
|
|
|
|
|
}
|
|
|
|
|
auto ctx = ihipGetTlsDefaultCtx();
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
//hsa_signal_t depSignal;
|
2016-08-07 21:46:51 -05:00
|
|
|
//int depSignalCnt = ctx._default_stream->preCopyCommand(NULL, &depSignal, ihipCommandCopyH2D);
|
2016-03-24 09:28:46 -05:00
|
|
|
assert(0); // Need to properly synchronize the copy - do something with depSignal if != NULL.
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
ctx->_acc.memcpy_symbol(symbolName, (void*) src,count, offset);
|
2016-03-24 09:28:46 -05:00
|
|
|
#endif
|
|
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 15:05:12 -05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 15:05:12 -05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 15:05:12 -05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 15:05:12 -05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipMemcpyAsync(void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind, hipStream_t stream)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(dst, src, sizeBytes, kind, stream);
|
|
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
stream = ihipSyncAndResolveStream(stream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((dst == NULL) || (src == NULL)) {
|
|
|
|
|
e= hipErrorInvalidValue;
|
|
|
|
|
} else if (stream) {
|
|
|
|
|
try {
|
|
|
|
|
stream->copyAsync(dst, src, sizeBytes, kind);
|
|
|
|
|
}
|
|
|
|
|
catch (ihipException ex) {
|
|
|
|
|
e = ex._code;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 10:21:52 -05:00
|
|
|
hipError_t hipMemcpyHtoDAsync(hipDeviceptr_t dst, void* src, size_t sizeBytes, hipStream_t stream)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(dst, src, sizeBytes, stream);
|
|
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
stream = ihipSyncAndResolveStream(stream);
|
|
|
|
|
|
|
|
|
|
hipMemcpyKind kind = hipMemcpyHostToDevice;
|
|
|
|
|
|
|
|
|
|
if ((dst == NULL) || (src == NULL)) {
|
|
|
|
|
e= hipErrorInvalidValue;
|
|
|
|
|
} else if (stream) {
|
|
|
|
|
try {
|
|
|
|
|
stream->copyAsync((void*)dst, src, sizeBytes, kind);
|
|
|
|
|
}
|
|
|
|
|
catch (ihipException ex) {
|
|
|
|
|
e = ex._code;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipMemcpyDtoDAsync(hipDeviceptr_t dst, hipDeviceptr_t src, size_t sizeBytes, hipStream_t stream)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(dst, src, sizeBytes, stream);
|
|
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
hipMemcpyKind kind = hipMemcpyDeviceToDevice;
|
|
|
|
|
|
|
|
|
|
stream = ihipSyncAndResolveStream(stream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((dst == NULL) || (src == NULL)) {
|
|
|
|
|
e= hipErrorInvalidValue;
|
|
|
|
|
} else if (stream) {
|
|
|
|
|
try {
|
|
|
|
|
stream->copyAsync((void*)dst, (void*)src, sizeBytes, kind);
|
|
|
|
|
}
|
|
|
|
|
catch (ihipException ex) {
|
|
|
|
|
e = ex._code;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipMemcpyDtoHAsync(void* dst, hipDeviceptr_t src, size_t sizeBytes, hipStream_t stream)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(dst, src, sizeBytes, stream);
|
|
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
stream = ihipSyncAndResolveStream(stream);
|
|
|
|
|
|
|
|
|
|
hipMemcpyKind kind = hipMemcpyDeviceToHost;
|
|
|
|
|
|
|
|
|
|
if ((dst == NULL) || (src == NULL)) {
|
|
|
|
|
e= hipErrorInvalidValue;
|
|
|
|
|
} else if (stream) {
|
|
|
|
|
try {
|
|
|
|
|
stream->copyAsync(dst, (void*)src, sizeBytes, kind);
|
|
|
|
|
}
|
|
|
|
|
catch (ihipException ex) {
|
|
|
|
|
e = ex._code;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 08:40:47 -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,
|
2016-08-08 11:55:57 -05:00
|
|
|
size_t width, size_t height, hipMemcpyKind kind) {
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
HIP_INIT_API(dst, dpitch, src, spitch, width, height, kind);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
if(width > dpitch || width > spitch)
|
|
|
|
|
return ihipLogStatus(hipErrorUnknown);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hc::completion_future marker;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipError_t e = hipSuccess;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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,
|
2016-08-08 11:55:57 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
if((wOffset + width > (dst->width * byteSize)) || width > spitch) {
|
|
|
|
|
return ihipLogStatus(hipErrorUnknown);
|
|
|
|
|
}
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
size_t src_w = spitch;
|
|
|
|
|
size_t dst_w = (dst->width)*byteSize;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
return ihipLogStatus(e);
|
2016-07-21 12:29:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipMemcpyToArray(hipArray* dst, size_t wOffset, size_t hOffset,
|
2016-08-08 11:55:57 -05:00
|
|
|
const void* src, size_t count, hipMemcpyKind kind) {
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
HIP_INIT_API(dst, wOffset, hOffset, src, count, kind);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipStream_t stream = ihipSyncAndResolveStream(hipStreamNull);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hc::completion_future marker;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipError_t e = hipSuccess;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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?
|
2016-08-08 12:07:12 -05:00
|
|
|
template <typename T>
|
|
|
|
|
hc::completion_future
|
2016-08-30 17:29:50 -05:00
|
|
|
ihipMemsetKernel(hipStream_t stream,
|
|
|
|
|
LockedAccessor_StreamCrit_t &crit,
|
|
|
|
|
T * ptr, T val, size_t sizeBytes)
|
2016-08-08 12:07:12 -05:00
|
|
|
{
|
2016-08-08 14:54:38 -05:00
|
|
|
int wg = std::min((unsigned)8, stream->getDevice()->_computeUnits);
|
2016-08-08 12:07:12 -05:00
|
|
|
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,
|
2016-08-08 12:07:12 -05:00
|
|
|
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-03-28 21:41:47 -05:00
|
|
|
stream->lockclose_postKernelCommand(cf);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cf.wait();
|
|
|
|
|
|
|
|
|
|
stream->lockclose_postKernelCommand(cf);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2016-08-08 11:55:57 -05:00
|
|
|
auto device = ctx->getWriteableDevice();
|
2016-03-24 09:28:46 -05:00
|
|
|
if (total) {
|
2016-08-08 11:55:57 -05:00
|
|
|
*total = device->_props.totalGlobalMem;
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
2016-09-17 23:54:20 +05:30
|
|
|
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;
|
2016-08-08 11:55:57 -05:00
|
|
|
hc::am_memtracker_sizeinfo(device->_acc, &deviceMemSize, &hostMemSize, &userMemSize);
|
2016-07-22 10:40:58 -05:00
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
*free = device->_props.totalGlobalMem - deviceMemSize;
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
2016-09-17 23:54:20 +05:30
|
|
|
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;
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
// 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
|
2016-07-22 10:40:58 -05:00
|
|
|
hipStatus = hipSuccess;
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(hipStatus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipHostFree(void* ptr)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(ptr);
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
// 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
|
2016-07-22 10:40:58 -05:00
|
|
|
hipStatus = hipSuccess;
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(hipStatus);
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-21 12:29:56 +05:30
|
|
|
hipError_t hipFreeArray(hipArray* array)
|
|
|
|
|
{
|
2016-08-08 11:55:57 -05:00
|
|
|
HIP_INIT_API(array);
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipError_t hipStatus = hipErrorInvalidDevicePointer;
|
2016-07-21 12:29:56 +05:30
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
// 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
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
return ihipLogStatus(hipStatus);
|
2016-07-21 12:29:56 +05:30
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-07-22 10:40:58 -05:00
|
|
|
// Stubs of threadfence operations
|
|
|
|
|
__device__ void __threadfence_block(void){
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__device__ void __threadfence(void){
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__device__ void __threadfence_system(void){
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
2016-03-24 04:57:30 -05:00
|
|
|
|