From a3bc29976b97598fb0347551ad28e645602b323b 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/clr commit: 40846f6f8ed5cc0030edf14887e4efe5ad4d8caa] --- projects/clr/hipamd/src/hip_memory.cpp | 181 +++++++++---------------- 1 file changed, 62 insertions(+), 119 deletions(-) diff --git a/projects/clr/hipamd/src/hip_memory.cpp b/projects/clr/hipamd/src/hip_memory.cpp index ffa9f0cfdd..0644f8de99 100644 --- a/projects/clr/hipamd/src/hip_memory.cpp +++ b/projects/clr/hipamd/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 aff8d83cdfeda856e9476fa848bef5e9a4acc9b8 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/clr commit: 88073a17c1015ec8cfe2337786f7acca9b0fa6b7] --- projects/clr/hipamd/src/hip_memory.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/clr/hipamd/src/hip_memory.cpp b/projects/clr/hipamd/src/hip_memory.cpp index 0644f8de99..52a88e8b88 100644 --- a/projects/clr/hipamd/src/hip_memory.cpp +++ b/projects/clr/hipamd/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 312b764721a9c4a2069c0d22472e96df3413179c 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/clr commit: 2658963f5bd29e5f15628d69a2c45f1f17fcb196] --- projects/clr/hipamd/src/hip_memory.cpp | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/projects/clr/hipamd/src/hip_memory.cpp b/projects/clr/hipamd/src/hip_memory.cpp index 52a88e8b88..ff97869842 100644 --- a/projects/clr/hipamd/src/hip_memory.cpp +++ b/projects/clr/hipamd/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 cf6322da63b2fb0b8f4b28f007ecd01b6c060e08 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/clr commit: 2ee80397f2f4cd9f4adce24d666463ebf092c121] --- projects/clr/hipamd/src/hip_memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/clr/hipamd/src/hip_memory.cpp b/projects/clr/hipamd/src/hip_memory.cpp index ff97869842..e6d6ebd09e 100644 --- a/projects/clr/hipamd/src/hip_memory.cpp +++ b/projects/clr/hipamd/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 ) {