From 36c6e0019dbdea263ab15b52c23578d54dd72a54 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Wed, 11 Apr 2018 15:58:48 +0530 Subject: [PATCH 1/4] hipMemset refactoring [ROCm/hip commit: 412a35be20985c113b60c4993670375717f07e49] --- projects/hip/src/hip_memory.cpp | 181 +++++++++++--------------------- 1 file changed, 62 insertions(+), 119 deletions(-) diff --git a/projects/hip/src/hip_memory.cpp b/projects/hip/src/hip_memory.cpp index ffa9f0cfdd..0644f8de99 100644 --- a/projects/hip/src/hip_memory.cpp +++ b/projects/hip/src/hip_memory.cpp @@ -1542,6 +1542,63 @@ void ihipMemsetKernel(hipStream_t stream, T* ptr, T val, size_t sizeBytes) { sizeBytes, std::move(val)); } +typedef enum ihipMemsetCopyDataType { + ihipMemsetCopyDataTypeChar = 0, + ihipMemsetCopyDataTypeShort = 1, + ihipMemsetCopyDataTypeInt = 2 +}ihipMemsetCopyDataType; + +hipError_t ihipMemset(void* dst, int value, size_t sizeBytes, hipStream_t stream, enum ihipMemsetCopyDataType copyDataType ) +{ + hipError_t e = hipSuccess; + + if (stream) { + if(copyDataType == ihipMemsetCopyDataTypeChar){ + if ((sizeBytes & 0x3) == 0) { + // use a faster dword-per-workitem copy: + try { + value = value & 0xff; + uint32_t value32 = (value << 24) | (value << 16) | (value << 8) | (value) ; + ihipMemsetKernel (stream, static_cast (dst), value32, sizeBytes/sizeof(uint32_t)); + } + catch (std::exception &ex) { + e = hipErrorInvalidValue; + } + } else { + // use a slow byte-per-workitem copy: + try { + ihipMemsetKernel (stream, static_cast (dst), value, sizeBytes); + } + catch (std::exception &ex) { + e = hipErrorInvalidValue; + } + if (HIP_API_BLOCKING) { + tprintf (DB_SYNC, "%s LAUNCH_BLOCKING wait for hipMemsetAsync.\n", ToString(stream).c_str()); + stream->locked_wait(); + } + } + } else { + if(copyDataType == ihipMemsetCopyDataTypeInt) { // 4 Bytes value + try { + ihipMemsetKernel (stream, static_cast (dst), value, sizeBytes); + } catch (std::exception &ex) { + e = hipErrorInvalidValue; + } + } else if(copyDataType == ihipMemsetCopyDataTypeShort) { + try { + value = value & 0xffff; + ihipMemsetKernel (stream, static_cast (dst), value, sizeBytes); + } catch (std::exception &ex) { + e = hipErrorInvalidValue; + } + } + } + } else { + e = hipErrorInvalidValue; + } + return e; +}; + // 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) { @@ -1551,35 +1608,7 @@ hipError_t hipMemsetAsync(void* dst, int value, size_t sizeBytes, hipStream_t st stream = ihipSyncAndResolveStream(stream); - if (stream) { - if ((sizeBytes & 0x3) == 0) { - // use a faster dword-per-workitem copy: - try { - value = value & 0xff; - uint32_t value32 = (value << 24) | (value << 16) | (value << 8) | (value); - ihipMemsetKernel(stream, static_cast(dst), value32, - sizeBytes / sizeof(uint32_t)); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } else { - // use a slow byte-per-workitem copy: - try { - ihipMemsetKernel(stream, static_cast(dst), value, sizeBytes); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } - - if (HIP_API_BLOCKING) { - tprintf(DB_SYNC, "%s LAUNCH_BLOCKING wait for hipMemsetAsync.\n", - ToString(stream).c_str()); - stream->locked_wait(); - } - } else { - e = hipErrorInvalidValue; - } - + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); return ihipLogStatus(e); }; @@ -1590,42 +1619,12 @@ hipError_t hipMemset(void* dst, int value, size_t sizeBytes) { hipError_t e = hipSuccess; hipStream_t stream = hipStreamNull; - // TODO - call an ihip memset so HIP_TRACE is correct. - stream = ihipSyncAndResolveStream(stream); - if (stream) { - if ((sizeBytes & 0x3) == 0) { - // use a faster dword-per-workitem copy: - try { - value = value & 0xff; - uint32_t value32 = (value << 24) | (value << 16) | (value << 8) | (value); - ihipMemsetKernel(stream, static_cast(dst), value32, - sizeBytes / sizeof(uint32_t)); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } else { - // use a slow byte-per-workitem copy: - try { - ihipMemsetKernel(stream, static_cast(dst), value, sizeBytes); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } - // TODO - is hipMemset supposed to be async? + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); stream->locked_wait(); - - if (HIP_LAUNCH_BLOCKING) { - tprintf(DB_SYNC, "'%s' LAUNCH_BLOCKING wait for memset in %s.\n", __func__, - ToString(stream).c_str()); - stream->locked_wait(); - tprintf(DB_SYNC, "'%s' LAUNCH_BLOCKING memset completed in %s.\n", __func__, - ToString(stream).c_str()); - } } else { e = hipErrorInvalidValue; - } - + } return ihipLogStatus(e); } @@ -1635,39 +1634,11 @@ hipError_t hipMemset2D(void* dst, size_t pitch, int value, size_t width, size_t hipError_t e = hipSuccess; hipStream_t stream = hipStreamNull; - // TODO - call an ihip memset so HIP_TRACE is correct. stream = ihipSyncAndResolveStream(stream); - if (stream) { size_t sizeBytes = pitch * height; - if ((sizeBytes & 0x3) == 0) { - // use a faster dword-per-workitem copy: - try { - value = value & 0xff; - uint32_t value32 = (value << 24) | (value << 16) | (value << 8) | (value); - ihipMemsetKernel(stream, static_cast(dst), value32, - sizeBytes / sizeof(uint32_t)); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } else { - // use a slow byte-per-workitem copy: - try { - ihipMemsetKernel(stream, static_cast(dst), value, sizeBytes); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } - // TODO - is hipMemset supposed to be async? + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); stream->locked_wait(); - - if (HIP_LAUNCH_BLOCKING) { - tprintf(DB_SYNC, "'%s' LAUNCH_BLOCKING wait for memset in %s.\n", __func__, - ToString(stream).c_str()); - stream->locked_wait(); - tprintf(DB_SYNC, "'%s' LAUNCH_BLOCKING memset completed in %s.\n", __func__, - ToString(stream).c_str()); - } } else { e = hipErrorInvalidValue; } @@ -1681,41 +1652,13 @@ hipError_t hipMemsetD8(hipDeviceptr_t dst, unsigned char value, size_t sizeBytes hipError_t e = hipSuccess; hipStream_t stream = hipStreamNull; - // TODO - call an ihip memset so HIP_TRACE is correct. stream = ihipSyncAndResolveStream(stream); - if (stream) { - if ((sizeBytes & 0x3) == 0) { - // use a faster dword-per-workitem copy: - try { - uint32_t value32 = (value << 24) | (value << 16) | (value << 8) | (value); - ihipMemsetKernel(stream, static_cast(dst), value32, - sizeBytes / sizeof(uint32_t)); - } catch (std::exception& ex) { - std::cout << ex.what() << std::endl; - e = hipErrorInvalidValue; - } - } else { - // use a slow byte-per-workitem copy: - try { - ihipMemsetKernel(stream, static_cast(dst), value, sizeBytes); - } catch (std::exception& ex) { - e = hipErrorInvalidValue; - } - } + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); stream->locked_wait(); - - if (HIP_LAUNCH_BLOCKING) { - tprintf(DB_SYNC, "'%s' LAUNCH_BLOCKING wait for memset in %s.\n", __func__, - ToString(stream).c_str()); - stream->locked_wait(); - tprintf(DB_SYNC, "'%s' LAUNCH_BLOCKING memset completed in %s.\n", __func__, - ToString(stream).c_str()); - } } else { e = hipErrorInvalidValue; } - return ihipLogStatus(e); } From 6c4236dfb6a71ceb071c4a12d501ed9db0cd861c Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Wed, 11 Apr 2018 19:01:53 +0530 Subject: [PATCH 2/4] Fix hipMemset stream resolution [ROCm/hip commit: 294bf50f6861d2ffec2857f4e4f0967decc2363c] --- projects/hip/src/hip_memory.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/hip/src/hip_memory.cpp b/projects/hip/src/hip_memory.cpp index 0644f8de99..52a88e8b88 100644 --- a/projects/hip/src/hip_memory.cpp +++ b/projects/hip/src/hip_memory.cpp @@ -1619,6 +1619,7 @@ hipError_t hipMemset(void* dst, int value, size_t sizeBytes) { hipError_t e = hipSuccess; hipStream_t stream = hipStreamNull; + stream = ihipSyncAndResolveStream(stream); if (stream) { e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); stream->locked_wait(); From abe14442a54f72c650b5b284f8e6a9118d8479b7 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Thu, 12 Apr 2018 09:29:22 +0530 Subject: [PATCH 3/4] Changed ihipMemsetCopyDataType to ihipMemsetDataType [ROCm/hip commit: 3d6eb758287289798e09a69f232cf839ae025220] --- projects/hip/src/hip_memory.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/projects/hip/src/hip_memory.cpp b/projects/hip/src/hip_memory.cpp index 52a88e8b88..ff97869842 100644 --- a/projects/hip/src/hip_memory.cpp +++ b/projects/hip/src/hip_memory.cpp @@ -1542,18 +1542,18 @@ void ihipMemsetKernel(hipStream_t stream, T* ptr, T val, size_t sizeBytes) { sizeBytes, std::move(val)); } -typedef enum ihipMemsetCopyDataType { - ihipMemsetCopyDataTypeChar = 0, - ihipMemsetCopyDataTypeShort = 1, - ihipMemsetCopyDataTypeInt = 2 +typedef enum ihipMemsetDataType { + ihipMemsetDataTypeChar = 0, + ihipMemsetDataTypeShort = 1, + ihipMemsetDataTypeInt = 2 }ihipMemsetCopyDataType; -hipError_t ihipMemset(void* dst, int value, size_t sizeBytes, hipStream_t stream, enum ihipMemsetCopyDataType copyDataType ) +hipError_t ihipMemset(void* dst, int value, size_t sizeBytes, hipStream_t stream, enum ihipMemsetDataType copyDataType ) { hipError_t e = hipSuccess; if (stream) { - if(copyDataType == ihipMemsetCopyDataTypeChar){ + if(copyDataType == ihipMemsetDataTypeChar){ if ((sizeBytes & 0x3) == 0) { // use a faster dword-per-workitem copy: try { @@ -1572,19 +1572,15 @@ hipError_t ihipMemset(void* dst, int value, size_t sizeBytes, hipStream_t strea catch (std::exception &ex) { e = hipErrorInvalidValue; } - if (HIP_API_BLOCKING) { - tprintf (DB_SYNC, "%s LAUNCH_BLOCKING wait for hipMemsetAsync.\n", ToString(stream).c_str()); - stream->locked_wait(); - } } } else { - if(copyDataType == ihipMemsetCopyDataTypeInt) { // 4 Bytes value + if(copyDataType == ihipMemsetDataTypeInt) { // 4 Bytes value try { ihipMemsetKernel (stream, static_cast (dst), value, sizeBytes); } catch (std::exception &ex) { e = hipErrorInvalidValue; } - } else if(copyDataType == ihipMemsetCopyDataTypeShort) { + } else if(copyDataType == ihipMemsetDataTypeShort) { try { value = value & 0xffff; ihipMemsetKernel (stream, static_cast (dst), value, sizeBytes); @@ -1593,6 +1589,10 @@ hipError_t ihipMemset(void* dst, int value, size_t sizeBytes, hipStream_t strea } } } + if (HIP_API_BLOCKING) { + tprintf (DB_SYNC, "%s LAUNCH_BLOCKING wait for hipMemsetAsync.\n", ToString(stream).c_str()); + stream->locked_wait(); + } } else { e = hipErrorInvalidValue; } @@ -1608,7 +1608,7 @@ hipError_t hipMemsetAsync(void* dst, int value, size_t sizeBytes, hipStream_t st stream = ihipSyncAndResolveStream(stream); - e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetDataTypeChar); return ihipLogStatus(e); }; @@ -1621,7 +1621,7 @@ hipError_t hipMemset(void* dst, int value, size_t sizeBytes) { hipStream_t stream = hipStreamNull; stream = ihipSyncAndResolveStream(stream); if (stream) { - e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetDataTypeChar); stream->locked_wait(); } else { e = hipErrorInvalidValue; @@ -1638,7 +1638,7 @@ hipError_t hipMemset2D(void* dst, size_t pitch, int value, size_t width, size_t stream = ihipSyncAndResolveStream(stream); if (stream) { size_t sizeBytes = pitch * height; - e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetDataTypeChar); stream->locked_wait(); } else { e = hipErrorInvalidValue; @@ -1655,7 +1655,7 @@ hipError_t hipMemsetD8(hipDeviceptr_t dst, unsigned char value, size_t sizeBytes hipStream_t stream = hipStreamNull; stream = ihipSyncAndResolveStream(stream); if (stream) { - e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetCopyDataTypeChar); + e = ihipMemset(dst, value, sizeBytes, stream, ihipMemsetDataTypeChar); stream->locked_wait(); } else { e = hipErrorInvalidValue; From 89511823f0883042f17b77ec820f49a3a2f01d4f Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Thu, 12 Apr 2018 10:27:19 +0530 Subject: [PATCH 4/4] Correct missed ihipMemsetCopyDataType change [ROCm/hip commit: 16c89d101a34d5116f19a15b6f44ce8b57cc3758] --- projects/hip/src/hip_memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/hip/src/hip_memory.cpp b/projects/hip/src/hip_memory.cpp index ff97869842..e6d6ebd09e 100644 --- a/projects/hip/src/hip_memory.cpp +++ b/projects/hip/src/hip_memory.cpp @@ -1546,7 +1546,7 @@ typedef enum ihipMemsetDataType { ihipMemsetDataTypeChar = 0, ihipMemsetDataTypeShort = 1, ihipMemsetDataTypeInt = 2 -}ihipMemsetCopyDataType; +}ihipMemsetDataType; hipError_t ihipMemset(void* dst, int value, size_t sizeBytes, hipStream_t stream, enum ihipMemsetDataType copyDataType ) {