Fix hipMemset for unaligned ptr, correct offset required by ROCclr per aligment and testcase to validate unaligned ptr

Change-Id: Ib1a0f9d1556d09cf72a12a90cad2e27c6d9c6a1c


[ROCm/clr commit: e1961c838f]
This commit is contained in:
agodavar
2020-05-12 15:14:06 -04:00
committed by Anusha Godavarthy Surya
parent dcf54124a1
commit 76f840e3db
2 changed files with 153 additions and 231 deletions
+25 -15
View File
@@ -1717,7 +1717,9 @@ hipError_t ihipMemset(void* dst, int64_t value, size_t valueSize, size_t sizeByt
}
size_t offset = 0;
amd::Memory* memory = getMemoryObject(dst, offset);
auto aligned_dst = amd::alignUp(reinterpret_cast<address>(dst), sizeof(uint64_t));
amd::Memory* memory = getMemoryObject(aligned_dst, offset);
if (memory == nullptr) {
// Host alloced memory
memset(dst, value, sizeBytes);
@@ -1727,44 +1729,52 @@ hipError_t ihipMemset(void* dst, int64_t value, size_t valueSize, size_t sizeByt
hipError_t hip_error = hipSuccess;
amd::HostQueue* queue = hip::getQueue(stream);
size_t n_head_bytes = 0;
size_t n_tail_bytes = 0;
int64_t value64 = 0;
const size_t uint64ModSize = (sizeBytes % sizeof(int64_t));
if (sizeBytes/sizeof(int64_t) > 0) {
n_head_bytes = static_cast<uint8_t*>(aligned_dst) - static_cast<uint8_t*>(dst);
if (valueSize == sizeof(int8_t)) {
value = value & 0xff;
value64 = ((value << 56) | (value << 48) | (value << 40) | (value << 32)
| (value << 24) | (value << 16) | (value << 8) | (value));
} else if (valueSize == sizeof(int16_t)) {
value = value & 0xffff;
value64 = ((value << 48) | (value << 32) | (value<<16) | (value));
} else if(valueSize == sizeof(int32_t)) {
value64 = ((value << 48) | (value << 32) | (value << 16) | (value));
} else if (valueSize == sizeof(int32_t)) {
value = value & 0xffffffff;
value64 = ((value<<32) | (value));
value64 = ((value << 32) | (value));
} else if (valueSize == sizeof(int64_t)) {
value64 = value;
} else {
LogPrintfError("Unsupported Pattern size: %u \n", valueSize);
return hipErrorInvalidValue;
}
// If uint64ModSize is != 0 then we will do a second fillBuffer Command
n_tail_bytes = ((sizeBytes - n_head_bytes) % sizeof(int64_t));
// If n_tail_bytes is != 0 then we will do a second fillBuffer Command
// on the same stream below, dont wait, do the first call async.
hip_error = packFillMemoryCommand(memory, offset, value64, sizeof(int64_t),
sizeBytes - uint64ModSize, queue,
((uint64ModSize != 0) || isAsync));
if(hip_error != hipSuccess) {
sizeBytes - n_tail_bytes - n_head_bytes, queue,
((n_head_bytes != 0) || (n_tail_bytes != 0) || isAsync));
if (hip_error != hipSuccess) {
return hip_error;
}
} else {
n_head_bytes = sizeBytes;
}
if (uint64ModSize != 0) {
void* new_dst = reinterpret_cast<void*>((reinterpret_cast<address>(dst)
+ sizeBytes) - uint64ModSize);
if (n_head_bytes != 0) {
memory = getMemoryObject(dst, offset);
hip_error = packFillMemoryCommand(memory, offset, value, valueSize,
n_head_bytes , queue, isAsync);
}
if (n_tail_bytes != 0) {
void* new_dst = (reinterpret_cast<address>(dst) + sizeBytes) - n_tail_bytes;
memory = getMemoryObject(new_dst, offset);
hip_error = packFillMemoryCommand(memory, offset, value, valueSize,
uint64ModSize, queue, isAsync);
n_tail_bytes, queue, isAsync);
}
return hip_error;
}
@@ -1,16 +1,13 @@
/*
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 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
@@ -33,236 +30,151 @@ THE SOFTWARE.
* TEST: %t -N 256M --memsetval 0xa6 --memsetD32val 0xCAFEBABE --memsetD16val 0xCAFE --memsetD8val 0xCA
* HIT_END
*/
#define MAX_OFFSET 3
// To test memset on unaligned pointer
#define loop(offset, offsetMax) for (int offset = offsetMax; offset >= 0; offset --)
#include "hip/hip_runtime.h"
#include "test_common.h"
enum MemsetType {
hipMemsetTypeDefault,
hipMemsetTypeD8,
hipMemsetTypeD16,
hipMemsetTypeD32
};
bool testhipMemset(int memsetval,int p_gpuDevice)
bool testhipMemsetSmallSize(int memsetval, int p_gpuDevice)
{
size_t Nbytes = N*sizeof(char);
printf ("testhipMemset N=%zu memsetval=%2x device=%d\n", N, memsetval, p_gpuDevice);
char *A_d;
char *A_h;
bool testResult = true;
char *A_d;
char *A_h;
bool testResult = true;
for ( size_t iSize = 1; iSize < 4; iSize++ ) {
size_t Nbytes = iSize * sizeof(char);
HIPCHECK(hipMalloc(&A_d, Nbytes));
A_h = reinterpret_cast<char*> (malloc(Nbytes));
printf("testhipMemsetSmallSize N=%zu memsetval=%2x device=%d\n",
iSize, memsetval, p_gpuDevice);
HIPCHECK(hipMemset(A_d, memsetval, Nbytes));
HIPCHECK(hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
HIPCHECK ( hipMalloc(&A_d, Nbytes) );
A_h = (char*)malloc(Nbytes);
HIPCHECK ( hipMemset(A_d, memsetval, Nbytes) );
HIPCHECK ( hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetval) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetval:%02x\n", i, (int)A_h[i], (int)memsetval);
break;
}
for ( int i = 0; i < iSize; i++ ) {
if ( A_h[i] != memsetval ) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetval:%02x\n",
i, static_cast<int> (A_h[i]), static_cast<int> (memsetval));
break;
}
}
HIPCHECK(hipFree(A_d));
free(A_h);
return testResult;
HIPCHECK(hipFree(A_d));
free(A_h);
}
return testResult;
}
bool testhipMemsetD32(int memsetD32val,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetD32 N=%zu memsetD32val=%8x device=%d\n", N, memsetD32val, p_gpuDevice);
int *A_d;
int *A_h;
bool testResult = true;
HIPCHECK ( hipMalloc(&A_d, Nbytes) );
A_h = (int*)malloc(Nbytes);
HIPCHECK ( hipMemsetD32((hipDeviceptr_t)A_d, memsetD32val, N) );
HIPCHECK ( hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetD32val) {
testResult = false; printf("mismatch at index:%d computed:%08x, memsetD32val:%08x\n", i, A_h[i], memsetD32val);
break;
}
template<typename T>
bool testhipMemset(T*A_h, T*A_d, T memsetval, enum MemsetType type,
int p_gpuDevice) {
size_t Nbytes = N * sizeof(T);
bool testResult = true;
HIPCHECK(hipMalloc(&A_d, Nbytes));
A_h = reinterpret_cast<T*> (malloc(Nbytes));
loop(offset, MAX_OFFSET) {
if (type == hipMemsetTypeDefault) {
printf("testhipMemset N=%zu memsetval=%2x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemset(A_d + offset, memsetval, N - offset));
} else if (type == hipMemsetTypeD8) {
printf("testhipMemsetD8 N=%zu memsetD8val=%4x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetD8((hipDeviceptr_t)(A_d + offset), memsetval, N - offset));
} else if (type == hipMemsetTypeD16) {
printf("testhipMemsetD16 N=%zu memsetD16val=%4x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetD16((hipDeviceptr_t)(A_d + offset), memsetval, N - offset));
} else if (type == hipMemsetTypeD32) {
printf("testhipMemsetD32 N=%zu memsetD32val=%8x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetD32((hipDeviceptr_t)(A_d + offset), memsetval, N - offset));
}
HIPCHECK(hipFree(A_d));
free(A_h);
return testResult;
}
bool testhipMemsetD16(short memsetD16val,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetD16 N=%zu memsetD16val=%4x device=%d\n", N, memsetD16val, p_gpuDevice);
short *A_d;
short *A_h;
bool testResult = true;
HIPCHECK ( hipMalloc(&A_d, Nbytes) );
A_h = (short*)malloc(Nbytes);
HIPCHECK ( hipMemsetD16((hipDeviceptr_t)A_d, memsetD16val, N) );
HIPCHECK ( hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetD16val) {
testResult = false; printf("mismatch at index:%d computed:%08x, memsetD16val:%08x\n", i, A_h[i], memsetD32val);
break;
}
HIPCHECK(hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost) );
for ( int i = offset; i < N; i++ ) {
if (A_h[i] != memsetval) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetval:%02x\n",
i, static_cast<int> (A_h[i]), static_cast<int> (memsetval));
break;
}
}
HIPCHECK(hipFree(A_d));
free(A_h);
return testResult;
}
HIPCHECK(hipFree(A_d));
free(A_h);
return testResult;
}
bool testhipMemsetD8(char memsetD8val,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetD8 N=%zu memsetD8val=%4x device=%d\n", N, memsetD8val, p_gpuDevice);
char *A_d;
char *A_h;
bool testResult = true;
HIPCHECK ( hipMalloc(&A_d, Nbytes) );
A_h = (char*)malloc(Nbytes);
HIPCHECK ( hipMemsetD8((hipDeviceptr_t)A_d, memsetD8val, N) );
HIPCHECK ( hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetD8val) {
testResult = false; printf("mismatch at index:%d computed:%08x, memsetD8val:%08x\n", i, A_h[i], memsetD8val);
break;
}
template<typename T>
bool testhipMemsetAsync(T*A_h, T*A_d, T memsetval, enum MemsetType type,
int p_gpuDevice) {
size_t Nbytes = N * sizeof(T);
bool testResult = true;
HIPCHECK(hipMalloc(reinterpret_cast<void**> (&A_d), Nbytes));
A_h = reinterpret_cast<T*> (malloc(Nbytes));
hipStream_t stream;
HIPCHECK(hipStreamCreate(&stream));
loop(offset, MAX_OFFSET) {
if (type == hipMemsetTypeDefault) {
printf("testhipMemsetAsync N=%zu memsetval=%2x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetAsync(A_d+offset, memsetval, Nbytes-offset, stream));
} else if (type == hipMemsetTypeD8) {
printf("testhipMemsetD8Async N=%zu memsetD8val=%2x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetD8Async((hipDeviceptr_t)(A_d + offset), memsetval, N - offset, stream));
} else if (type == hipMemsetTypeD16) {
printf("testhipMemsetD16Async N=%zu memsetD16val=%8x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetD16Async((hipDeviceptr_t)(A_d + offset), memsetval, N - offset, stream));
} else if (type == hipMemsetTypeD32) {
printf("testhipMemsetD32Async N=%zu memsetD32val=%8x device=%d\n",
(N - offset), memsetval, p_gpuDevice);
HIPCHECK(hipMemsetD32Async((hipDeviceptr_t)(A_d + offset), memsetval, N - offset, stream));
}
HIPCHECK(hipFree(A_d));
free(A_h);
return testResult;
}
HIPCHECK(hipStreamSynchronize(stream));
HIPCHECK(hipMemcpy(A_h, reinterpret_cast<void*> (A_d), Nbytes, hipMemcpyDeviceToHost));
bool testhipMemsetAsync(int memsetval,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetAsync N=%zu memsetval=%2x device=%d\n", N, memsetval, p_gpuDevice);
char *A_d;
char *A_h;
bool testResult = true;
HIPCHECK ( hipMalloc((void**)&A_d, Nbytes) );
A_h = (char*)malloc(Nbytes);
hipStream_t stream;
HIPCHECK(hipStreamCreate(&stream));
HIPCHECK ( hipMemsetAsync(A_d, memsetval, Nbytes, stream ));
HIPCHECK ( hipStreamSynchronize(stream));
HIPCHECK ( hipMemcpy(A_h, (void*)A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetval) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetval:%02x\n", i, (int)A_h[i], (int)memsetval);
break;
}
for ( int i = offset; i < N; i++ ) {
if (A_h[i] != memsetval) {
testResult = false;
printf("mismatch at index:%d computed:%02x\n", i, static_cast<int> (A_h[i]));
break;
}
}
HIPCHECK(hipFree((void*)A_d));
HIPCHECK(hipStreamDestroy(stream));
free(A_h);
return testResult;
}
HIPCHECK(hipFree(reinterpret_cast<void*> (A_d)) );
HIPCHECK(hipStreamDestroy(stream));
free(A_h);
return testResult;
}
bool testhipMemsetD32Async(int memsetD32val,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetD32Async N=%zu memsetval=%8x device=%d\n", N, memsetD32val, p_gpuDevice);
int *A_d;
int *A_h;
bool testResult = true;
int main(int argc, char *argv[]) {
HipTest::parseStandardArguments(argc, argv, true);
bool testResult = true;
char * cA_d;
char * cA_h;
int16_t * siA_d;
int16_t * siA_h;
int32_t * iA_d;
int32_t * iA_h;
HIPCHECK(hipSetDevice(p_gpuDevice));
testResult &= testhipMemsetSmallSize(memsetval, p_gpuDevice);
HIPCHECK ( hipMalloc((void**)&A_d, Nbytes) );
A_h = (int*)malloc(Nbytes);
hipStream_t stream;
HIPCHECK(hipStreamCreate(&stream));
HIPCHECK ( hipMemsetD32Async((hipDeviceptr_t)A_d, memsetD32val, N, stream ));
HIPCHECK ( hipStreamSynchronize(stream));
HIPCHECK ( hipMemcpy(A_h, (void*)A_d, Nbytes, hipMemcpyDeviceToHost));
testResult &= testhipMemset(cA_h, cA_d, memsetval, hipMemsetTypeDefault, p_gpuDevice);
testResult &= testhipMemset(iA_h, iA_d, memsetD32val, hipMemsetTypeD32, p_gpuDevice);
testResult &= testhipMemset(siA_h, siA_d, memsetD16val, hipMemsetTypeD16, p_gpuDevice);
testResult &= testhipMemset(cA_h, cA_d, memsetD8val, hipMemsetTypeD8, p_gpuDevice);
for (int i=0; i<N; i++) {
if (A_h[i] != memsetD32val) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetD32val:%02x\n", i, A_h[i], memsetD32val);
break;
}
}
HIPCHECK(hipFree((void*)A_d));
HIPCHECK(hipStreamDestroy(stream));
free(A_h);
return testResult;
}
bool testhipMemsetD16Async(short memsetD16val,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetD16Async N=%zu memsetval=%8x device=%d\n", N, memsetD16val, p_gpuDevice);
short *A_d;
short *A_h;
bool testResult = true;
HIPCHECK ( hipMalloc((void**)&A_d, Nbytes) );
A_h = (short*)malloc(Nbytes);
hipStream_t stream;
HIPCHECK(hipStreamCreate(&stream));
HIPCHECK ( hipMemsetD16Async((hipDeviceptr_t)A_d, memsetD16val, N, stream ));
HIPCHECK ( hipStreamSynchronize(stream));
HIPCHECK ( hipMemcpy(A_h, (void*)A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetD16val) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetD16val:%02x\n", i, A_h[i], memsetD16val);
break;
}
}
HIPCHECK(hipFree((void*)A_d));
HIPCHECK(hipStreamDestroy(stream));
free(A_h);
return testResult;
}
bool testhipMemsetD8Async(char memsetD8val,int p_gpuDevice)
{
size_t Nbytes = N*sizeof(int);
printf ("testhipMemsetD8Async N=%zu memsetD8val=%2x device=%d\n", N, memsetD8val, p_gpuDevice);
char *A_d;
char *A_h;
bool testResult = true;
HIPCHECK ( hipMalloc((void**)&A_d, Nbytes) );
A_h = (char*)malloc(Nbytes);
hipStream_t stream;
HIPCHECK(hipStreamCreate(&stream));
HIPCHECK ( hipMemsetD8Async((hipDeviceptr_t)A_d, memsetD8val, N, stream ));
HIPCHECK ( hipStreamSynchronize(stream));
HIPCHECK ( hipMemcpy(A_h, (void*)A_d, Nbytes, hipMemcpyDeviceToHost));
for (int i=0; i<N; i++) {
if (A_h[i] != memsetD8val) {
testResult = false;
printf("mismatch at index:%d computed:%02x, memsetD8val:%02x\n", i, A_h[i], memsetD8val);
break;
}
}
HIPCHECK(hipFree((void*)A_d));
HIPCHECK(hipStreamDestroy(stream));
free(A_h);
return testResult;
}
int main(int argc, char *argv[])
{
HipTest::parseStandardArguments(argc, argv, true);
bool testResult = true;
HIPCHECK(hipSetDevice(p_gpuDevice));
testResult &= testhipMemset(memsetval, p_gpuDevice);
testResult &= testhipMemsetAsync(memsetval, p_gpuDevice);
testResult &= testhipMemsetD32(memsetD32val, p_gpuDevice);
testResult &= testhipMemsetD32Async(memsetD32val, p_gpuDevice);
testResult &= testhipMemsetD16(memsetD16val, p_gpuDevice);
testResult &= testhipMemsetD16Async(memsetD16val, p_gpuDevice);
testResult &= testhipMemsetD8(memsetD8val, p_gpuDevice);
testResult &= testhipMemsetD8Async(memsetD8val, p_gpuDevice);
if (testResult) passed();
failed("Output Mismatch\n");
testResult &= testhipMemsetAsync(cA_h, cA_d, memsetval, hipMemsetTypeDefault, p_gpuDevice);
testResult &= testhipMemsetAsync(iA_h, iA_d, memsetD32val, hipMemsetTypeD32, p_gpuDevice);
testResult &= testhipMemsetAsync(siA_h, siA_d, memsetD16val, hipMemsetTypeD16, p_gpuDevice);
testResult &= testhipMemsetAsync(cA_h, cA_d, memsetD8val, hipMemsetTypeD8, p_gpuDevice);
if (testResult) passed();
failed("Output Mismatch\n");
}