From 5d710c880290c3da0025159bf1be7fc66ac0c459 Mon Sep 17 00:00:00 2001 From: Maneesh Gupta Date: Mon, 17 Oct 2016 12:48:05 +0530 Subject: [PATCH 01/16] Update packaging scripts to handle updated HCC package versioning Change-Id: Id93267b7cf9fd05e60004cc6c45b5fc7d10b3130 --- hipamd/CMakeLists.txt | 2 ++ hipamd/packaging/hip_hcc.txt | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hipamd/CMakeLists.txt b/hipamd/CMakeLists.txt index b8cf550547..9ea2e0f877 100644 --- a/hipamd/CMakeLists.txt +++ b/hipamd/CMakeLists.txt @@ -76,6 +76,8 @@ if(HIP_PLATFORM STREQUAL "hcc") message(FATAL_ERROR "Don't know where to find HCC. Please specify abolute path using -DHCC_HOME") endif() add_to_config(_buildInfo HCC_VERSION) + string(REPLACE "-" ";" HCC_VERSION_LIST ${HCC_VERSION}) + list(GET HCC_VERSION_LIST 0 HCC_PACKAGE_VERSION) # Determine HSA_PATH if(NOT DEFINED HSA_PATH) diff --git a/hipamd/packaging/hip_hcc.txt b/hipamd/packaging/hip_hcc.txt index 459ecd449a..5801554f7c 100644 --- a/hipamd/packaging/hip_hcc.txt +++ b/hipamd/packaging/hip_hcc.txt @@ -28,9 +28,9 @@ set(CPACK_GENERATOR "TGZ;DEB;RPM") set(CPACK_BINARY_DEB "ON") set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_BINARY_DIR}/postinst;${PROJECT_BINARY_DIR}/prerm") if(@COMPILE_HIP_ATP_MARKER@) - set(CPACK_DEBIAN_PACKAGE_DEPENDS "hip_base (= ${CPACK_PACKAGE_VERSION}), hcc_lc (= @HCC_VERSION@), rocm-profiler") + set(CPACK_DEBIAN_PACKAGE_DEPENDS "hip_base (= ${CPACK_PACKAGE_VERSION}), hcc_lc (= @HCC_PACKAGE_VERSION@), rocm-profiler") else() - set(CPACK_DEBIAN_PACKAGE_DEPENDS "hip_base (= ${CPACK_PACKAGE_VERSION}), hcc_lc (= @HCC_VERSION@)") + set(CPACK_DEBIAN_PACKAGE_DEPENDS "hip_base (= ${CPACK_PACKAGE_VERSION}), hcc_lc (= @HCC_PACKAGE_VERSION@)") endif() set(CPACK_BINARY_RPM "ON") set(CPACK_RPM_PACKAGE_ARCHITECTURE "x86_64") @@ -38,9 +38,9 @@ set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${PROJECT_BINARY_DIR}/postinst") set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${PROJECT_BINARY_DIR}/prerm") set(CPACK_RPM_PACKAGE_AUTOREQPROV " no") if(@COMPILE_HIP_ATP_MARKER@) - set(CPACK_RPM_PACKAGE_REQUIRES "hip_base = ${CPACK_PACKAGE_VERSION}, hcc_lc = @HCC_VERSION@, rocm-profiler") + set(CPACK_RPM_PACKAGE_REQUIRES "hip_base = ${CPACK_PACKAGE_VERSION}, hcc_lc = @HCC_PACKAGE_VERSION@, rocm-profiler") else() - set(CPACK_RPM_PACKAGE_REQUIRES "hip_base = ${CPACK_PACKAGE_VERSION}, hcc_lc = @HCC_VERSION@") + set(CPACK_RPM_PACKAGE_REQUIRES "hip_base = ${CPACK_PACKAGE_VERSION}, hcc_lc = @HCC_PACKAGE_VERSION@") endif() set(CPACK_SOURCE_GENERATOR "TGZ") include(CPack) From 9d7a82d6bfa0db62036805b6f5097dbe2e058984 Mon Sep 17 00:00:00 2001 From: Aditya Atluri Date: Tue, 18 Oct 2016 10:09:52 -0500 Subject: [PATCH 02/16] make sure the global variable does not give zero Change-Id: I299c4e74740e5df155a562b0ee609ac6539a9b28 --- hipamd/tests/src/kernel/hipTestMallocKernel.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/hipamd/tests/src/kernel/hipTestMallocKernel.cpp b/hipamd/tests/src/kernel/hipTestMallocKernel.cpp index efd38b5ad2..826f6164c3 100644 --- a/hipamd/tests/src/kernel/hipTestMallocKernel.cpp +++ b/hipamd/tests/src/kernel/hipTestMallocKernel.cpp @@ -51,6 +51,7 @@ int main() HIP_ASSERT(hipMemcpy(dPtr, hPtr, SIZE, hipMemcpyHostToDevice)); hipLaunchKernel(Alloc, dim3(1,1,1), dim3(NUM,1,1), 0, 0, dPtr); HIP_ASSERT(hipMemcpy(hPtr, dPtr, SIZE, hipMemcpyDeviceToHost)); + assert(hPtr[0] != 0); hipLaunchKernel(Free, dim3(1,1,1), dim3(NUM,1,1), 0, 0, dPtr); HIP_ASSERT(hipFree(dPtr)); for(uint32_t i=1;i Date: Tue, 18 Oct 2016 22:38:04 +0300 Subject: [PATCH 03/16] [hipcc] A workaround for [SWDEV-105366] HCC's omitting of system includes while compiling kernel code leads to 'file not found' error. Missing includes are set explicitly. Workaround is switched on by default, to disable it set HCC_SYS_INCLUDES_WA=0. WA will be removed after fixing [SWDEV-105366]. --- hipamd/bin/hipcc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hipamd/bin/hipcc b/hipamd/bin/hipcc index f536937455..ebce3414ae 100755 --- a/hipamd/bin/hipcc +++ b/hipamd/bin/hipcc @@ -91,6 +91,18 @@ if ($HIP_PLATFORM eq "hcc") { $HIPCC=$HCC; $HIPCXXFLAGS = $HCCFLAGS; + #### GCC system includes workaround #### + $WA = $ENV{'HCC_SYS_INCLUDES_WA'}; + if (!defined($WA) || ${WA} ne "0") { + my $GCC_CUR_VER = `gcc -dumpversion`; + my $GPP_CUR_VER = `g++ -dumpversion`; + $GCC_CUR_VER =~ s/\R//g; + $GPP_CUR_VER =~ s/\R//g; + if (${GCC_CUR_VER} eq ${GPP_CUR_VER}) { + $HIPCXXFLAGS .= "-I/usr/include/x86_64-linux-gnu -I/usr/include/x86_64-linux-gnu/c++/${GCC_CUR_VER} -I/usr/include/c++/${GCC_CUR_VER}"; + } + } + $HIPCXXFLAGS .= " -I$HIP_PATH/include/hip/hcc_detail/cuda"; $HIPCXXFLAGS .= " -I$HSA_PATH/include"; $HIPCXXFLAGS .= " -Wno-deprecated-register"; From 403cdf93c31c0f53cd93efb516aade4e6d4a32a4 Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Sat, 15 Oct 2016 20:25:20 -0500 Subject: [PATCH 04/16] Move some internal headers from "include/hip/" to src. Change-Id: I7041bd5c803d9318979f4a7c1d658445c614691e --- hipamd/docs/markdown/hip_kernel_language.md | 11 ++++++----- hipamd/src/hip_context.cpp | 4 ++-- hipamd/src/hip_device.cpp | 4 ++-- hipamd/src/hip_error.cpp | 4 ++-- hipamd/src/hip_event.cpp | 4 ++-- hipamd/src/hip_hcc.cpp | 4 ++-- hipamd/{include/hip/hcc_detail => src}/hip_hcc.h | 2 +- hipamd/src/hip_memory.cpp | 4 ++-- hipamd/src/hip_module.cpp | 4 ++-- hipamd/src/hip_peer.cpp | 4 ++-- hipamd/src/hip_stream.cpp | 4 ++-- hipamd/{include/hip/hcc_detail => src}/hip_util.h | 0 hipamd/{include/hip/hcc_detail => src}/trace_helper.h | 0 13 files changed, 25 insertions(+), 24 deletions(-) rename hipamd/{include/hip/hcc_detail => src}/hip_hcc.h (99%) rename hipamd/{include/hip/hcc_detail => src}/hip_util.h (100%) rename hipamd/{include/hip/hcc_detail => src}/trace_helper.h (100%) diff --git a/hipamd/docs/markdown/hip_kernel_language.md b/hipamd/docs/markdown/hip_kernel_language.md index 4b8762025e..3b13cde08a 100644 --- a/hipamd/docs/markdown/hip_kernel_language.md +++ b/hipamd/docs/markdown/hip_kernel_language.md @@ -231,10 +231,10 @@ typedef struct dim3 { ``` ## Memory-Fence Instructions -HIP support for __threadfence(), __threadfence_block() and __threadfence_system() is under development. -The stubs for the threadfence routines are defined in hcc_details/hip_runtime.h. -Applications that use these threadfence features should disable both of the L1 and L2 caches by: -"export HSA_DISABLE_CACHE=1" +HIP supports __threadfence() and __threadfence_block(). + +Applications that use threadfence_system can disable the L1 and L2 caches on the GPU by: +"export HSA_DISABLE_CACHE=1". See the hip_porting_guide.md#threadfence_system for more information. ## Synchronization Functions The __syncthreads() built-in function is supported in HIP. The __syncthreads_count(int), __syncthreads_and(int) and __syncthreads_or(int) functions are under development. @@ -602,7 +602,8 @@ The printf function is under development. ## Device-Side Dynamic Global Memory Allocation -Device-side dynamic global memory allocation is not supported. +Device-side dynamic global memory allocation is under development. HIP now includes a preliminary +implementation of malloc and free that can be called from device functions. ## `__launch_bounds__` GPU multiprocessors have a fixed pool of resources (primarily registers and shared memory) that are shared among the active warps. Using more resources can increase the kernel’s IPC, but it reduces the resources available for other warps and limits the number of warps that can run simultaneously. Thus, GPUs exhibit a complex relationship between resource usage and performance. `__launch_bounds__` allows the application to provide usage hints that influence the resources (primarily registers) employed by the generated code. It’s a function attribute that must be attached to a `__global__` function: diff --git a/hipamd/src/hip_context.cpp b/hipamd/src/hip_context.cpp index 4f45f049f6..a72ae9b58d 100644 --- a/hipamd/src/hip_context.cpp +++ b/hipamd/src/hip_context.cpp @@ -26,8 +26,8 @@ THE SOFTWARE. #include #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" // Stack of contexts thread_local std::stack tls_ctxStack; diff --git a/hipamd/src/hip_device.cpp b/hipamd/src/hip_device.cpp index 6e36b935c8..fda5724d7f 100644 --- a/hipamd/src/hip_device.cpp +++ b/hipamd/src/hip_device.cpp @@ -21,8 +21,8 @@ THE SOFTWARE. */ #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" //------------------------------------------------------------------------------------------------- //Devices diff --git a/hipamd/src/hip_error.cpp b/hipamd/src/hip_error.cpp index 72e7ab0084..60c45cc1f7 100644 --- a/hipamd/src/hip_error.cpp +++ b/hipamd/src/hip_error.cpp @@ -21,8 +21,8 @@ THE SOFTWARE. */ #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" //------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------- diff --git a/hipamd/src/hip_event.cpp b/hipamd/src/hip_event.cpp index 441918d6c4..5494fc32f2 100644 --- a/hipamd/src/hip_event.cpp +++ b/hipamd/src/hip_event.cpp @@ -21,8 +21,8 @@ THE SOFTWARE. */ #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" //------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------- diff --git a/hipamd/src/hip_hcc.cpp b/hipamd/src/hip_hcc.cpp index 8bf7f1091c..4fcf427a3c 100644 --- a/hipamd/src/hip_hcc.cpp +++ b/hipamd/src/hip_hcc.cpp @@ -44,8 +44,8 @@ THE SOFTWARE. #include "libhsakmt/hsakmt.h" #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" diff --git a/hipamd/include/hip/hcc_detail/hip_hcc.h b/hipamd/src/hip_hcc.h similarity index 99% rename from hipamd/include/hip/hcc_detail/hip_hcc.h rename to hipamd/src/hip_hcc.h index 9e7499e4ac..d8045d4cb7 100644 --- a/hipamd/include/hip/hcc_detail/hip_hcc.h +++ b/hipamd/src/hip_hcc.h @@ -25,7 +25,7 @@ THE SOFTWARE. #include #include -#include "hip/hcc_detail/hip_util.h" +#include "hip_util.h" #if defined(__HCC__) && (__hcc_workweek__ < 16354) diff --git a/hipamd/src/hip_memory.cpp b/hipamd/src/hip_memory.cpp index 8b030799fb..9e0473f02c 100644 --- a/hipamd/src/hip_memory.cpp +++ b/hipamd/src/hip_memory.cpp @@ -25,8 +25,8 @@ THE SOFTWARE. #include "hsa/hsa_ext_amd.h" #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" //------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------- diff --git a/hipamd/src/hip_module.cpp b/hipamd/src/hip_module.cpp index c8368abeec..f7ac35c77b 100644 --- a/hipamd/src/hip_module.cpp +++ b/hipamd/src/hip_module.cpp @@ -30,8 +30,8 @@ THE SOFTWARE. #include "hsa/amd_hsa_kernel_code.h" #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" //TODO Use Pool APIs from HCC to get memory regions. diff --git a/hipamd/src/hip_peer.cpp b/hipamd/src/hip_peer.cpp index 4f3227de82..e66a0d2971 100644 --- a/hipamd/src/hip_peer.cpp +++ b/hipamd/src/hip_peer.cpp @@ -23,8 +23,8 @@ THE SOFTWARE. #include #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" // Peer access functions. diff --git a/hipamd/src/hip_stream.cpp b/hipamd/src/hip_stream.cpp index 9bb615ebf7..3b1d6af038 100644 --- a/hipamd/src/hip_stream.cpp +++ b/hipamd/src/hip_stream.cpp @@ -21,8 +21,8 @@ THE SOFTWARE. */ #include "hip/hip_runtime.h" -#include "hip/hcc_detail/hip_hcc.h" -#include "hip/hcc_detail/trace_helper.h" +#include "hip_hcc.h" +#include "trace_helper.h" //------------------------------------------------------------------------------------------------- diff --git a/hipamd/include/hip/hcc_detail/hip_util.h b/hipamd/src/hip_util.h similarity index 100% rename from hipamd/include/hip/hcc_detail/hip_util.h rename to hipamd/src/hip_util.h diff --git a/hipamd/include/hip/hcc_detail/trace_helper.h b/hipamd/src/trace_helper.h similarity index 100% rename from hipamd/include/hip/hcc_detail/trace_helper.h rename to hipamd/src/trace_helper.h From 7aef2b0eca2dfcc80afaf0bdabd488c38605cafa Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Sat, 15 Oct 2016 20:34:29 -0500 Subject: [PATCH 05/16] Enhance .gitignore Change-Id: I03ae62bad2db181538a4487163ac604229862d16 --- hipamd/.gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hipamd/.gitignore b/hipamd/.gitignore index 450d3bc41b..67fa40f563 100644 --- a/hipamd/.gitignore +++ b/hipamd/.gitignore @@ -5,6 +5,12 @@ *.Po hip-amdinternal HIP-Examples +lib +packages bin/hipInfo +bin/hipBusBandwidth +bin/hipDispatchLatency +bin/hipify-clang + samples/1_Utils/hipInfo/hipInfo From 54b20dbe0de8424a7a8aa186dd38254654ae3d9a Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Sat, 15 Oct 2016 21:15:31 -0500 Subject: [PATCH 06/16] Update docs for event, review event TODO. Change-Id: Iec491f9f22df163f01c0af6639fcbe33c81acdcc --- hipamd/include/hip/hcc_detail/hip_runtime_api.h | 8 ++++++-- hipamd/src/hip_event.cpp | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/hipamd/include/hip/hcc_detail/hip_runtime_api.h b/hipamd/include/hip/hcc_detail/hip_runtime_api.h index fb44e2dc0a..6788cfbd14 100644 --- a/hipamd/include/hip/hcc_detail/hip_runtime_api.h +++ b/hipamd/include/hip/hcc_detail/hip_runtime_api.h @@ -626,8 +626,12 @@ hipError_t hipStreamGetFlags(hipStream_t stream, unsigned int *flags); * * @param[in,out] event Returns the newly created event. * @param[in] flags Flags to control event behavior. Valid values are #hipEventDefault, #hipEventBlockingSync, #hipEventDisableTiming, #hipEventInterprocess - * - * @warning On HCC platform, flags must be #hipEventDefault. + + * #hipEventDefault : Default flag. The event will use active synchronization and will support timing. Blocking synchronization provides lowest possible latency at the expense of dedicating a CPU to poll on the eevent. + * #hipEventBlockingSync : The event will use blocking synchronization : if hipEventSynchronize is called on this event, the thread will block until the event completes. This can increase latency for the synchroniation but can result in lower power and more resources for other CPU threads. + * #hipEventDisableTiming : Disable recording of timing information. On ROCM platform, timing information is always recorded and this flag has no performance benefit. + + * @warning On HCC platform, hipEventInterprocess support is under development. Use of this flag will return an error. * * @returns #hipSuccess, #hipErrorInitializationError, #hipErrorInvalidValue, #hipErrorLaunchFailure, #hipErrorMemoryAllocation * diff --git a/hipamd/src/hip_event.cpp b/hipamd/src/hip_event.cpp index 5494fc32f2..f5ad5e0f31 100644 --- a/hipamd/src/hip_event.cpp +++ b/hipamd/src/hip_event.cpp @@ -34,15 +34,16 @@ hipError_t ihipEventCreate(hipEvent_t* event, unsigned flags) { hipError_t e = hipSuccess; - // TODO - support hipEventDefault, hipEventBlockingSync, hipEventDisableTiming - if (flags == 0) { + // TODO-IPC - support hipEventInterprocess. + unsigned supportedFlags = hipEventDefault | hipEventBlockingSync | hipEventDisableTiming; + if ((flags & ~supportedFlags) != 0) { ihipEvent_t *eh = new ihipEvent_t(); eh->_state = hipEventStatusCreated; eh->_stream = NULL; eh->_flags = flags; eh->_timestamp = 0; - *event = eh; // TODO - allocat the event directly, no copy needed. + *event = eh; } else { e = hipErrorInvalidValue; } @@ -152,7 +153,6 @@ hipError_t hipEventElapsedTime(float *ms, hipEvent_t start, hipEvent_t stop) int64_t tickDiff = (stop_eh->_timestamp - start_eh->_timestamp); - // TODO-move this to a variable saved with each agent. uint64_t freqHz; hsa_system_get_info(HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY, &freqHz); if (freqHz) { From 214673a4fba1bbed16b4dbefee8588b2c6840fcb Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Sat, 15 Oct 2016 22:12:19 -0500 Subject: [PATCH 07/16] Fix event flag detection. Change-Id: I0b0ba66c2339021320fe3d7760fdad1a0490a76b --- hipamd/src/hip_event.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/hipamd/src/hip_event.cpp b/hipamd/src/hip_event.cpp index f5ad5e0f31..74fe487968 100644 --- a/hipamd/src/hip_event.cpp +++ b/hipamd/src/hip_event.cpp @@ -36,7 +36,7 @@ hipError_t ihipEventCreate(hipEvent_t* event, unsigned flags) // TODO-IPC - support hipEventInterprocess. unsigned supportedFlags = hipEventDefault | hipEventBlockingSync | hipEventDisableTiming; - if ((flags & ~supportedFlags) != 0) { + if ((flags & ~supportedFlags) == 0) { ihipEvent_t *eh = new ihipEvent_t(); eh->_state = hipEventStatusCreated; @@ -180,11 +180,7 @@ hipError_t hipEventQuery(hipEvent_t event) { HIP_INIT_API(event); - - // TODO-stream - need to read state of signal here: The event may have become ready after recording.. - // TODO-HCC - use get_hsa_signal here. - - if (event->_state == hipEventStatusRecording) { + if ((event->_state == hipEventStatusRecording) && (!event->_marker.is_ready())) { return ihipLogStatus(hipErrorNotReady); } else { return ihipLogStatus(hipSuccess); From 0787c41ac0f0430312950eeb401dffc8092aa8dc Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Mon, 17 Oct 2016 06:43:40 -0500 Subject: [PATCH 08/16] Remove some TODO items Change-Id: I7e9de2e43a8584f8dc9ee6d45c8ed00ca465f591 --- hipamd/include/hip/hcc_detail/hip_runtime_api.h | 2 ++ hipamd/src/hip_hcc.cpp | 13 +++++-------- hipamd/util/vim/hip.vim | 2 ++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/hipamd/include/hip/hcc_detail/hip_runtime_api.h b/hipamd/include/hip/hcc_detail/hip_runtime_api.h index 6788cfbd14..55c7cfd847 100644 --- a/hipamd/include/hip/hcc_detail/hip_runtime_api.h +++ b/hipamd/include/hip/hcc_detail/hip_runtime_api.h @@ -692,6 +692,8 @@ hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream); * the function will return immediately and the completion_future resources will be released later, when the hipDevice is synchronized. * * @see hipEventCreate, hipEventCreateWithFlags, hipEventQuery, hipEventSynchronize, hipEventRecord, hipEventElapsedTime + * + * @returns #hipSuccess */ hipError_t hipEventDestroy(hipEvent_t event); diff --git a/hipamd/src/hip_hcc.cpp b/hipamd/src/hip_hcc.cpp index 4fcf427a3c..bdb0f869ba 100644 --- a/hipamd/src/hip_hcc.cpp +++ b/hipamd/src/hip_hcc.cpp @@ -277,7 +277,6 @@ void ihipStream_t::locked_waitEvent(hipEvent_t event) { LockedAccessor_StreamCrit_t crit(_criticalData); - // TODO - check state of event here: crit->_av.create_blocking_marker(event->_marker); } @@ -706,11 +705,10 @@ hipError_t ihipDevice_t::initProperties(hipDeviceProp_t* prop) prop->clockRate *= 1000.0; // convert Mhz to Khz. DeviceErrorCheck(err); - //uint64_t counterHz; - //err = hsa_system_get_info(HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY, &counterHz); - //DeviceErrorCheck(err); - //prop->clockInstructionRate = counterHz / 1000; - prop->clockInstructionRate = 100*1000; /* TODO-RT - hard-code until HSART has function to properly report clock */ + uint64_t counterHz; + err = hsa_system_get_info(HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY, &counterHz); + DeviceErrorCheck(err); + prop->clockInstructionRate = counterHz / 1000; // Get Agent BDFID (bus/device/function ID) uint16_t bdf_id = 1; @@ -718,7 +716,6 @@ hipError_t ihipDevice_t::initProperties(hipDeviceProp_t* prop) DeviceErrorCheck(err); // BDFID is 16bit uint: [8bit - BusID | 5bit - Device ID | 3bit - Function/DomainID] - // TODO/Clarify: cudaDeviceProp::pciDomainID how to report? // prop->pciDomainID = bdf_id & 0x7; prop->pciDeviceID = (bdf_id>>3) & 0x1F; prop->pciBusID = (bdf_id>>8) & 0xFF; @@ -789,7 +786,7 @@ hipError_t ihipDevice_t::initProperties(hipDeviceProp_t* prop) prop->arch.hasFloatAtomicAdd = 0; prop->arch.hasGlobalInt64Atomics = 1; prop->arch.hasSharedInt64Atomics = 1; - prop->arch.hasDoubles = 1; // TODO - true for Fiji. + prop->arch.hasDoubles = 1; prop->arch.hasWarpVote = 1; prop->arch.hasWarpBallot = 1; prop->arch.hasWarpShuffle = 1; diff --git a/hipamd/util/vim/hip.vim b/hipamd/util/vim/hip.vim index 17faecfdeb..b73066a786 100644 --- a/hipamd/util/vim/hip.vim +++ b/hipamd/util/vim/hip.vim @@ -177,8 +177,10 @@ syn keyword hipFlags hipMemcpyDeviceToDevice syn keyword hipFlags hipMemcpyDefault syn keyword hipFlags hipReadModeElementType syn keyword hipFlags hipSuccess +syn keyword hipFlags hipErrorNotReady syn keyword hipFlags hipTextureType1D + syn keyword hipFlags hipHostMallocDefault syn keyword hipFlags hipHostMallocPortable syn keyword hipFlags hipHostMallocMapped From c7303d5924e8c0d4178417a4794e65f3c24ca0fc Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Mon, 17 Oct 2016 19:59:36 -0500 Subject: [PATCH 09/16] Add hipDeviceSchedule* support to queue wait Change-Id: Iffa7a356500b026f3737c3f5719ca9f62b10d855 --- .../include/hip/hcc_detail/hip_runtime_api.h | 21 +++++++--- hipamd/src/hip_device.cpp | 22 ++++++++++- hipamd/src/hip_hcc.cpp | 39 +++++++++++++++++-- hipamd/src/hip_hcc.h | 7 +++- 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/hipamd/include/hip/hcc_detail/hip_runtime_api.h b/hipamd/include/hip/hcc_detail/hip_runtime_api.h index 55c7cfd847..1a40f6bd21 100644 --- a/hipamd/include/hip/hcc_detail/hip_runtime_api.h +++ b/hipamd/include/hip/hcc_detail/hip_runtime_api.h @@ -95,10 +95,12 @@ enum hipLimit_t #define hipHostRegisterIoMemory 0x4 ///< Not supported. -#define hipDeviceScheduleAuto 0x0 -#define hipDeviceScheduleSpin 0x1 -#define hipDeviceScheduleYield 0x2 -#define hipDeviceBlockingSync 0x4 +#define hipDeviceScheduleAuto 0x0 ///< Automatically select between Spin and Yield +#define hipDeviceScheduleSpin 0x1 ///< Dedicate a CPU core to spin-wait. Provides lowest latency, but burns a CPU core and may consume more power. +#define hipDeviceScheduleYield 0x2 ///< Yield the CPU to the operating system when waiting. May increase latency, but lowers power and is friendlier to other threads in the system. +#define hipDeviceScheduleBlockingSync 0x4 +#define hipDeviceScheduleMask 0x7 + #define hipDeviceMapHost 0x8 #define hipDeviceLmemResizeToMax 0x16 @@ -383,9 +385,18 @@ hipError_t hipDeviceSetSharedMemConfig ( hipSharedMemConfig config ); * * @param [in] flags * + * The schedule flags impact how HIP waits for the completion of a command running on a device. + * hipDeviceScheduleSpin : HIP runtime will actively spin in the thread which submitted the work until the command completes. This offers the lowest latency, but will consume a CPU core and may increase power. + * hipDeviceScheduleYield : The HIP runtime will yield the CPU to system so that other tasks can use it. This may increase latency to detect the completion but will consume less power and is friendlier to other tasks in the system. + * hipDeviceScheduleBlockingSync : On ROCm platform, this is a synonym for hipDeviceScheduleYield. + * hipDeviceScheduleAuto : Use a hueristic to select between Spin and Yield modes. If the number of HIP contexts is greater than the number of logical processors in the system, use Spin scheduling. Else use Yield scheduling. + * + * + * hipDeviceMapHost : Allow mapping host memory. On ROCM, this is always allowed and the flag is ignored. + * hipDeviceLmemResizeToMax : @warning ROCm silently ignores this flag. + * * @returns #hipSuccess, #hipErrorInvalidDevice, #hipErrorSetOnActiveProcess * - * Note: Only hipDeviceScheduleAuto and hipDeviceMapHost are supported * */ hipError_t hipSetDeviceFlags ( unsigned flags); diff --git a/hipamd/src/hip_device.cpp b/hipamd/src/hip_device.cpp index fda5724d7f..7168ccfbe6 100644 --- a/hipamd/src/hip_device.cpp +++ b/hipamd/src/hip_device.cpp @@ -268,7 +268,7 @@ hipError_t hipSetDeviceFlags( unsigned int flags) { HIP_INIT_API(flags); - hipError_t e; + hipError_t e = hipSuccess; auto * ctx = ihipGetTlsDefaultCtx(); @@ -276,7 +276,25 @@ hipError_t hipSetDeviceFlags( unsigned int flags) // TODO : Review error handling behavior for this function, it often returns ErrorSetOnActiveProcess if (ctx) { ctx->_ctxFlags = ctx->_ctxFlags | flags; - e = hipSuccess; + if (flags & hipDeviceScheduleMask) { + switch (hipDeviceScheduleMask) { + case hipDeviceScheduleAuto: + case hipDeviceScheduleSpin: + case hipDeviceScheduleYield: + case hipDeviceScheduleBlockingSync: + e = hipSuccess; + break; + default: + e = hipErrorInvalidValue; + break; + } + } + + unsigned supportedFlags = hipDeviceScheduleMask | hipDeviceMapHost | hipDeviceLmemResizeToMax; + + if (flags & ~supportedFlags) { + e = hipErrorInvalidValue; + } } else { e = hipErrorInvalidDevice; } diff --git a/hipamd/src/hip_hcc.cpp b/hipamd/src/hip_hcc.cpp index bdb0f869ba..2d46c1ead8 100644 --- a/hipamd/src/hip_hcc.cpp +++ b/hipamd/src/hip_hcc.cpp @@ -66,7 +66,7 @@ int HIP_ATP_MARKER= 0; int HIP_DB= 0; int HIP_VISIBLE_DEVICES = 0; /* Contains a comma-separated sequence of GPU identifiers */ int HIP_NUM_KERNELS_INFLIGHT = 128; -int HIP_BLOCKING_SYNC = 0; +int HIP_FORCE_BLOCKING_SYNC = 0; #define HIP_USE_PRODUCT_NAME 0 //#define DISABLE_COPY_EXT 1 @@ -82,6 +82,7 @@ bool g_visible_device = false; unsigned g_deviceCnt; std::vector g_hip_visible_devices; hsa_agent_t g_cpu_agent; +unsigned g_numLogicalThreads; /* Implementation of malloc and free device functions. @@ -240,6 +241,19 @@ ihipStream_t::ihipStream_t(ihipCtx_t *ctx, hc::accelerator_view av, unsigned int _ctx(ctx), _criticalData(av) { + unsigned schedBits = ctx->_ctxFlags & hipDeviceScheduleMask; + + switch (schedBits) { + case hipDeviceScheduleAuto : _scheduleMode = Auto; break; + case hipDeviceScheduleSpin : _scheduleMode = Spin; break; + case hipDeviceScheduleYield : _scheduleMode = Yield; break; + case hipDeviceScheduleBlockingSync : _scheduleMode = Yield; break; + default:_scheduleMode = Auto; + }; + + + + tprintf(DB_SYNC, " streamCreate: stream=%p\n", this); }; @@ -256,7 +270,23 @@ void ihipStream_t::wait(LockedAccessor_StreamCrit_t &crit, bool assertQueueEmpty { if (! assertQueueEmpty) { tprintf (DB_SYNC, "stream %p wait for queue-empty..\n", this); - crit->_av.wait(HIP_BLOCKING_SYNC ? hc::hcWaitModeBlocked : hc::hcWaitModeActive); + hc::hcWaitMode waitMode = hc::hcWaitModeActive; + + if (_scheduleMode == Auto) { + if (g_deviceCnt > g_numLogicalThreads) { + waitMode = hc::hcWaitModeActive; + } else { + waitMode = hc::hcWaitModeBlocked; + } + } else if (_scheduleMode == Spin) { + waitMode = hc::hcWaitModeActive; + } else if (_scheduleMode == Yield) { + waitMode = hc::hcWaitModeBlocked; + } else { + assert(0); // bad wait mode. + } + + crit->_av.wait(HIP_FORCE_BLOCKING_SYNC ? hc::hcWaitModeBlocked : waitMode); } crit->_kernelCnt = 0; @@ -1090,7 +1120,7 @@ void ihipInit() READ_ENV_I(release, HIP_VISIBLE_DEVICES, CUDA_VISIBLE_DEVICES, "Only devices whose index is present in the secquence are visible to HIP applications and they are enumerated in the order of secquence" ); - READ_ENV_I(release, HIP_BLOCKING_SYNC, 0, "Use blocking synchronization for stream waits. This may increase latency but is friendlier to other processes. If 0, spin-wait."); + READ_ENV_I(release, HIP_FORCE_BLOCKING_SYNC, 0, "Force blocking synchronization for stream waits. This may increase latency but is friendlier to other processes. If 0, used ."); READ_ENV_I(release, HIP_NUM_KERNELS_INFLIGHT, 128, "Max number of inflight kernels per stream before active synchronization is forced."); @@ -1174,13 +1204,14 @@ void ihipInit() g_deviceCnt++; } } + g_numLogicalThreads = std::thread::hardware_concurrency(); // If HIP_VISIBLE_DEVICES is not set, make sure all devices are initialized if(!g_visible_device) { assert(deviceCnt == g_deviceCnt); } - tprintf(DB_SYNC, "pid=%u %-30s\n", getpid(), ""); + tprintf(DB_SYNC, "pid=%u %-30s g_numLogicalThreads=%u\n", getpid(), "", g_numLogicalThreads); } diff --git a/hipamd/src/hip_hcc.h b/hipamd/src/hip_hcc.h index d8045d4cb7..4e05450b19 100644 --- a/hipamd/src/hip_hcc.h +++ b/hipamd/src/hip_hcc.h @@ -384,11 +384,14 @@ typedef ihipStreamCriticalBase_t ihipStreamCritical_t; typedef LockedAccessor LockedAccessor_StreamCrit_t; + //--- // Internal stream structure. class ihipStream_t { public: -typedef uint64_t SeqNum_t ; + enum ScheduleMode {Auto, Spin, Yield}; + typedef uint64_t SeqNum_t ; + ihipStream_t(ihipCtx_t *ctx, hc::accelerator_view av, unsigned int flags); ~ihipStream_t(); @@ -457,6 +460,8 @@ private: // Data // Friends: friend std::ostream& operator<<(std::ostream& os, const ihipStream_t& s); friend hipError_t hipStreamQuery(hipStream_t); + + ScheduleMode _scheduleMode; }; From 4acb7dd9478db3266b965781fdaac716f6526cf8 Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Tue, 18 Oct 2016 22:24:40 -0500 Subject: [PATCH 10/16] Several fixes/improvements to directed tests. Change-Id: Ieb32569ddbf5795d2ab3392165a7ddc22693dc62 --- hipamd/tests/src/hipEnvVarDriver.cpp | 2 +- hipamd/tests/src/hipFuncSetDeviceFlags.cpp | 4 +++- .../runtimeApi/multiThread/hipMultiThreadStreams2.cpp | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/hipamd/tests/src/hipEnvVarDriver.cpp b/hipamd/tests/src/hipEnvVarDriver.cpp index ea379724d9..1edb158aa3 100644 --- a/hipamd/tests/src/hipEnvVarDriver.cpp +++ b/hipamd/tests/src/hipEnvVarDriver.cpp @@ -90,7 +90,7 @@ int main() { setenv("CUDA_VISIBLE_DEVICES",(char*)std::to_string(i).c_str(),1); if (devPCINum[i] != getDevicePCIBusNumRemote(0)) { std::cout << "The returned PciBusID is not correct"<< std::endl; - std::cout << "Expected "<< devPCINum[i] << ", but get " << getDevicePCIBusNum << endl; + std::cout << "Expected "<< devPCINum[i] << ", but get " << getDevicePCIBusNum(i) << endl; exit(-1); } else { continue; diff --git a/hipamd/tests/src/hipFuncSetDeviceFlags.cpp b/hipamd/tests/src/hipFuncSetDeviceFlags.cpp index ccc81b11ac..9bda91f113 100644 --- a/hipamd/tests/src/hipFuncSetDeviceFlags.cpp +++ b/hipamd/tests/src/hipFuncSetDeviceFlags.cpp @@ -38,8 +38,10 @@ int main() HIPCHECK(hipSetDevice(j)); for(int i=0;i<4;i++){ - flag = 1 < i; + flag = 1 << i; + printf ("Flag=%x\n", flag); HIPCHECK(hipSetDeviceFlags(flag)); + //HIPCHECK_API(hipSetDeviceFlags(flag), hipErrorInvalidValue); } flag = 0; diff --git a/hipamd/tests/src/runtimeApi/multiThread/hipMultiThreadStreams2.cpp b/hipamd/tests/src/runtimeApi/multiThread/hipMultiThreadStreams2.cpp index bc28ebfd16..6f431d0bb4 100644 --- a/hipamd/tests/src/runtimeApi/multiThread/hipMultiThreadStreams2.cpp +++ b/hipamd/tests/src/runtimeApi/multiThread/hipMultiThreadStreams2.cpp @@ -90,6 +90,17 @@ void run(size_t size, hipStream_t stream1, hipStream_t stream2){ HIPCHECK(hipDeviceSynchronize()); HIPASSERT(Eh[10] = Ah[10] + 1.0f); HIPASSERT(Ehh[10] = Ahh[10] + 1.0f); + + HIPCHECK(hipHostFree(Ah)); + HIPCHECK(hipHostFree(Bh)); + HIPCHECK(hipHostFree(Eh)); + HIPCHECK(hipHostFree(Ahh)); + HIPCHECK(hipHostFree(Bhh)); + HIPCHECK(hipHostFree(Ehh)); + HIPCHECK(hipFree(Cd)); + HIPCHECK(hipFree(Dd)); + HIPCHECK(hipFree(Cdd)); + HIPCHECK(hipFree(Ddd)); } int main(int argc, char **argv) From d4b23da91f4b6ed58dde36505c5fa04672d2bea3 Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Tue, 18 Oct 2016 22:25:26 -0500 Subject: [PATCH 11/16] Add HIP_WAIT_MODE env var. Also weaken cases where hipSetDeviceFlags returns hipErrorInvalidValue. Change-Id: I7f113338be6fe498eaf1ab40fd0fd6b23849bb5e --- hipamd/src/hip_device.cpp | 5 +++-- hipamd/src/hip_hcc.cpp | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/hipamd/src/hip_device.cpp b/hipamd/src/hip_device.cpp index 7168ccfbe6..9d577f5313 100644 --- a/hipamd/src/hip_device.cpp +++ b/hipamd/src/hip_device.cpp @@ -285,14 +285,15 @@ hipError_t hipSetDeviceFlags( unsigned int flags) e = hipSuccess; break; default: - e = hipErrorInvalidValue; + e = hipSuccess; // TODO - should this be error? Map to Auto? + //e = hipErrorInvalidValue; break; } } unsigned supportedFlags = hipDeviceScheduleMask | hipDeviceMapHost | hipDeviceLmemResizeToMax; - if (flags & ~supportedFlags) { + if (flags & (~supportedFlags)) { e = hipErrorInvalidValue; } } else { diff --git a/hipamd/src/hip_hcc.cpp b/hipamd/src/hip_hcc.cpp index 2d46c1ead8..fd3292a6f4 100644 --- a/hipamd/src/hip_hcc.cpp +++ b/hipamd/src/hip_hcc.cpp @@ -66,7 +66,7 @@ int HIP_ATP_MARKER= 0; int HIP_DB= 0; int HIP_VISIBLE_DEVICES = 0; /* Contains a comma-separated sequence of GPU identifiers */ int HIP_NUM_KERNELS_INFLIGHT = 128; -int HIP_FORCE_BLOCKING_SYNC = 0; +int HIP_WAIT_MODE = 0; #define HIP_USE_PRODUCT_NAME 0 //#define DISABLE_COPY_EXT 1 @@ -285,8 +285,14 @@ void ihipStream_t::wait(LockedAccessor_StreamCrit_t &crit, bool assertQueueEmpty } else { assert(0); // bad wait mode. } + + if (HIP_WAIT_MODE == 1) { + waitMode = hc::hcWaitModeBlocked; + } else if (HIP_WAIT_MODE == 2) { + waitMode = hc::hcWaitModeActive; + } - crit->_av.wait(HIP_FORCE_BLOCKING_SYNC ? hc::hcWaitModeBlocked : waitMode); + crit->_av.wait(waitMode); } crit->_kernelCnt = 0; @@ -1120,7 +1126,7 @@ void ihipInit() READ_ENV_I(release, HIP_VISIBLE_DEVICES, CUDA_VISIBLE_DEVICES, "Only devices whose index is present in the secquence are visible to HIP applications and they are enumerated in the order of secquence" ); - READ_ENV_I(release, HIP_FORCE_BLOCKING_SYNC, 0, "Force blocking synchronization for stream waits. This may increase latency but is friendlier to other processes. If 0, used ."); + READ_ENV_I(release, HIP_WAIT_MODE, 0, "Force synchronization mode. 1= force yield, 2=force spin, 0=defaults specified in application"); READ_ENV_I(release, HIP_NUM_KERNELS_INFLIGHT, 128, "Max number of inflight kernels per stream before active synchronization is forced."); From 965e2e00a8f26f9df78a850648e49b8c9b4be652 Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Tue, 18 Oct 2016 22:45:45 -0500 Subject: [PATCH 12/16] Make HIP_HCC_SYS_INCLUDES_WA disable unless explicitly set =1 Change-Id: Ibfd1e049d51e420bd514b71e3652bc7029dfd4ba --- hipamd/bin/hipcc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hipamd/bin/hipcc b/hipamd/bin/hipcc index ebce3414ae..824dd8b63d 100755 --- a/hipamd/bin/hipcc +++ b/hipamd/bin/hipcc @@ -92,8 +92,8 @@ if ($HIP_PLATFORM eq "hcc") { $HIPCXXFLAGS = $HCCFLAGS; #### GCC system includes workaround #### - $WA = $ENV{'HCC_SYS_INCLUDES_WA'}; - if (!defined($WA) || ${WA} ne "0") { + $WA = $ENV{'HIP_HCC_SYS_INCLUDES_WA'}; + if (defined($WA) and ${WA} ne "0") { my $GCC_CUR_VER = `gcc -dumpversion`; my $GPP_CUR_VER = `g++ -dumpversion`; $GCC_CUR_VER =~ s/\R//g; From e1b658caff7e4a8f564d4f8b97275c02cc84b65b Mon Sep 17 00:00:00 2001 From: Maneesh Gupta Date: Wed, 19 Oct 2016 12:41:11 +0530 Subject: [PATCH 13/16] Make directed tests linking depend upon HIP_PATH/lib/* Change-Id: I35203fc655325428e241a541f64c103fb2004b6e --- hipamd/tests/hit/HIT.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hipamd/tests/hit/HIT.cmake b/hipamd/tests/hit/HIT.cmake index 206d63c77f..0c8f7d39c3 100644 --- a/hipamd/tests/hit/HIT.cmake +++ b/hipamd/tests/hit/HIT.cmake @@ -130,6 +130,7 @@ endmacro() #------------------------------------------------------------------------------- # Macro: HIT_ADD_FILES used to scan+add multiple files for testing. +file(GLOB HIP_LIB_FILES ${HIP_PATH}/lib/*) macro(HIT_ADD_FILES _dir _label) foreach (file ${ARGN}) # Build tests @@ -148,7 +149,7 @@ macro(HIT_ADD_FILES _dir _label) else() set_source_files_properties(${_sources} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1) hip_add_executable(${target} ${_sources} HIPCC_OPTIONS ${_hipcc_options} HCC_OPTIONS ${_hcc_options} NVCC_OPTIONS ${_nvcc_options}) - set_target_properties(${target} PROPERTIES OUTPUT_NAME ${_target} RUNTIME_OUTPUT_DIRECTORY ${_label}) + set_target_properties(${target} PROPERTIES OUTPUT_NAME ${_target} RUNTIME_OUTPUT_DIRECTORY ${_label} LINK_DEPENDS "${HIP_LIB_FILES}") endif() endforeach() From 5f2bf5219a7069fef2b6f3f046eb27fb5699f2a5 Mon Sep 17 00:00:00 2001 From: Maneesh Gupta Date: Wed, 19 Oct 2016 12:57:33 +0530 Subject: [PATCH 14/16] bin/hipcc: Simplify HIP_HCC_SYS_INCLUDES_WA logic Change-Id: I2d20d6edc1b5c0d065ba6ce6179441fa98712a87 --- hipamd/bin/hipcc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hipamd/bin/hipcc b/hipamd/bin/hipcc index 824dd8b63d..21453634d6 100755 --- a/hipamd/bin/hipcc +++ b/hipamd/bin/hipcc @@ -92,8 +92,8 @@ if ($HIP_PLATFORM eq "hcc") { $HIPCXXFLAGS = $HCCFLAGS; #### GCC system includes workaround #### - $WA = $ENV{'HIP_HCC_SYS_INCLUDES_WA'}; - if (defined($WA) and ${WA} ne "0") { + $WA = $ENV{'HIP_HCC_SYS_INCLUDES_WA'} // 0; + if (${WA}) { my $GCC_CUR_VER = `gcc -dumpversion`; my $GPP_CUR_VER = `g++ -dumpversion`; $GCC_CUR_VER =~ s/\R//g; From d025ed980bc2ef284e85690a4796a36f80ad038f Mon Sep 17 00:00:00 2001 From: Sandeep Kumar Date: Fri, 14 Oct 2016 18:00:26 +0530 Subject: [PATCH 15/16] Add more apps to 2_Cookbook Change-Id: Iafe462df9726a32f450bd240a2de3eaa73a10057 --- .../2_Cookbook/0_MatrixTranspose/Makefile | 2 +- .../0_MatrixTranspose/MatrixTranspose.cpp | 28 ++-- hipamd/samples/2_Cookbook/1_hipEvent/Makefile | 2 +- .../2_Cookbook/1_hipEvent/hipEvent.cpp | 27 ++-- .../2_Cookbook/2_HIP_ATP_MARKER/Makefile | 2 +- .../2_HIP_ATP_MARKER/MatrixTranspose.cpp | 27 ++-- .../2_Cookbook/3_shared_memory/Makefile | 2 +- .../2_Cookbook/3_shared_memory/Readme.md | 42 +++++ .../3_shared_memory/sharedMemory.cpp | 31 ++-- hipamd/samples/2_Cookbook/4_shfl/Makefile | 4 +- hipamd/samples/2_Cookbook/4_shfl/Readme.md | 51 ++++++ hipamd/samples/2_Cookbook/4_shfl/shfl.cpp | 23 ++- hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp | 21 ++- hipamd/samples/2_Cookbook/5_2dshfl/Makefile | 2 +- hipamd/samples/2_Cookbook/5_2dshfl/Readme.md | 51 ++++++ .../2_Cookbook/6_dynamic_shared/Makefile | 36 +++++ .../2_Cookbook/6_dynamic_shared/Readme.md | 47 ++++++ .../6_dynamic_shared/dynamic_shared.cpp | 141 +++++++++++++++++ hipamd/samples/2_Cookbook/7_streams/Makefile | 36 +++++ hipamd/samples/2_Cookbook/7_streams/Readme.md | 57 +++++++ .../samples/2_Cookbook/7_streams/stream.cpp | 148 ++++++++++++++++++ 21 files changed, 686 insertions(+), 94 deletions(-) create mode 100644 hipamd/samples/2_Cookbook/3_shared_memory/Readme.md create mode 100644 hipamd/samples/2_Cookbook/4_shfl/Readme.md create mode 100644 hipamd/samples/2_Cookbook/5_2dshfl/Readme.md create mode 100644 hipamd/samples/2_Cookbook/6_dynamic_shared/Makefile create mode 100644 hipamd/samples/2_Cookbook/6_dynamic_shared/Readme.md create mode 100644 hipamd/samples/2_Cookbook/6_dynamic_shared/dynamic_shared.cpp create mode 100644 hipamd/samples/2_Cookbook/7_streams/Makefile create mode 100644 hipamd/samples/2_Cookbook/7_streams/Readme.md create mode 100644 hipamd/samples/2_Cookbook/7_streams/stream.cpp diff --git a/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile b/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile index ffb442e443..d3630a1c19 100644 --- a/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile +++ b/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile @@ -10,7 +10,7 @@ TARGET=hcc SOURCES = MatrixTranspose.cpp OBJECTS = $(SOURCES:.cpp=.o) -EXECUTABLE=./exe +EXECUTABLE=./MatrixTranspose .PHONY: test diff --git a/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp b/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp index 91733c025a..264fcbed53 100644 --- a/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp +++ b/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp @@ -27,12 +27,12 @@ THE SOFTWARE. #define WIDTH 1024 -#define HEIGHT 1024 -#define NUM (WIDTH*HEIGHT) -#define THREADS_PER_BLOCK_X 16 -#define THREADS_PER_BLOCK_Y 16 +#define NUM (WIDTH*WIDTH) + +#define THREADS_PER_BLOCK_X 4 +#define THREADS_PER_BLOCK_Y 4 #define THREADS_PER_BLOCK_Z 1 // Device (Kernel) function, it must be void @@ -40,27 +40,25 @@ THE SOFTWARE. __global__ void matrixTranspose(hipLaunchParm lp, float *out, float *in, - const int width, - const int height) + const int width) { int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; - out[y * width + x] = in[x * height + y]; + out[y * width + x] = in[x * width + y]; } // CPU implementation of matrix transpose void matrixTransposeCPUReference( float * output, float * input, - const unsigned int width, - const unsigned int height) + const unsigned int width) { - for(unsigned int j=0; j < height; j++) + for(unsigned int j=0; j < width; j++) { for(unsigned int i=0; i < width; i++) { - output[i*height + j] = input[j*width + i]; + output[i*width + j] = input[j*width + i]; } } } @@ -100,22 +98,22 @@ int main() { // Lauching kernel from host hipLaunchKernel(matrixTranspose, - dim3(WIDTH/THREADS_PER_BLOCK_X, HEIGHT/THREADS_PER_BLOCK_Y), + dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y), dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), 0, 0, - gpuTransposeMatrix , gpuMatrix, WIDTH ,HEIGHT); + gpuTransposeMatrix , gpuMatrix, WIDTH); // Memory transfer from device to host hipMemcpy(TransposeMatrix, gpuTransposeMatrix, NUM*sizeof(float), hipMemcpyDeviceToHost); // CPU MatrixTranspose computation - matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH, HEIGHT); + matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH); // verify the results errors = 0; double eps = 1.0E-6; for (i = 0; i < NUM; i++) { - if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > 0 ) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps ) { errors++; } } diff --git a/hipamd/samples/2_Cookbook/1_hipEvent/Makefile b/hipamd/samples/2_Cookbook/1_hipEvent/Makefile index dc0f7db2e6..be4bc2169a 100644 --- a/hipamd/samples/2_Cookbook/1_hipEvent/Makefile +++ b/hipamd/samples/2_Cookbook/1_hipEvent/Makefile @@ -10,7 +10,7 @@ TARGET=hcc SOURCES = hipEvent.cpp OBJECTS = $(SOURCES:.cpp=.o) -EXECUTABLE=./exe +EXECUTABLE=./hipEvent .PHONY: test diff --git a/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp b/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp index 1abe1180da..f2aea146e4 100644 --- a/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp +++ b/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp @@ -26,12 +26,11 @@ THE SOFTWARE. #include "hip/hip_runtime.h" #define WIDTH 1024 -#define HEIGHT 1024 -#define NUM (WIDTH*HEIGHT) +#define NUM (WIDTH*WIDTH) -#define THREADS_PER_BLOCK_X 16 -#define THREADS_PER_BLOCK_Y 16 +#define THREADS_PER_BLOCK_X 4 +#define THREADS_PER_BLOCK_Y 4 #define THREADS_PER_BLOCK_Z 1 // Device (Kernel) function, it must be void @@ -39,27 +38,25 @@ THE SOFTWARE. __global__ void matrixTranspose(hipLaunchParm lp, float *out, float *in, - const int width, - const int height) + const int width) { int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; - out[y * width + x] = in[x * height + y]; + out[y * width + x] = in[x * width + y]; } // CPU implementation of matrix transpose void matrixTransposeCPUReference( float * output, float * input, - const unsigned int width, - const unsigned int height) + const unsigned int width) { - for(unsigned int j=0; j < height; j++) + for(unsigned int j=0; j < width; j++) { for(unsigned int i=0; i < width; i++) { - output[i*height + j] = input[j*width + i]; + output[i*width + j] = input[j*width + i]; } } } @@ -118,10 +115,10 @@ int main() { // Lauching kernel from host hipLaunchKernel(matrixTranspose, - dim3(WIDTH/THREADS_PER_BLOCK_X, HEIGHT/THREADS_PER_BLOCK_Y), + dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y), dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), 0, 0, - gpuTransposeMatrix , gpuMatrix, WIDTH ,HEIGHT); + gpuTransposeMatrix , gpuMatrix, WIDTH); // Record the stop event hipEventRecord(stop, NULL); @@ -146,13 +143,13 @@ int main() { printf ("hipMemcpyDeviceToHost time taken = %6.3fms\n", eventMs); // CPU MatrixTranspose computation - matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH, HEIGHT); + matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH); // verify the results errors = 0; double eps = 1.0E-6; for (i = 0; i < NUM; i++) { - if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > 0 ) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps ) { errors++; } } diff --git a/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile b/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile index ffb442e443..d3630a1c19 100644 --- a/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile +++ b/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile @@ -10,7 +10,7 @@ TARGET=hcc SOURCES = MatrixTranspose.cpp OBJECTS = $(SOURCES:.cpp=.o) -EXECUTABLE=./exe +EXECUTABLE=./MatrixTranspose .PHONY: test diff --git a/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp b/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp index 1abe1180da..f2aea146e4 100644 --- a/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp +++ b/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp @@ -26,12 +26,11 @@ THE SOFTWARE. #include "hip/hip_runtime.h" #define WIDTH 1024 -#define HEIGHT 1024 -#define NUM (WIDTH*HEIGHT) +#define NUM (WIDTH*WIDTH) -#define THREADS_PER_BLOCK_X 16 -#define THREADS_PER_BLOCK_Y 16 +#define THREADS_PER_BLOCK_X 4 +#define THREADS_PER_BLOCK_Y 4 #define THREADS_PER_BLOCK_Z 1 // Device (Kernel) function, it must be void @@ -39,27 +38,25 @@ THE SOFTWARE. __global__ void matrixTranspose(hipLaunchParm lp, float *out, float *in, - const int width, - const int height) + const int width) { int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; - out[y * width + x] = in[x * height + y]; + out[y * width + x] = in[x * width + y]; } // CPU implementation of matrix transpose void matrixTransposeCPUReference( float * output, float * input, - const unsigned int width, - const unsigned int height) + const unsigned int width) { - for(unsigned int j=0; j < height; j++) + for(unsigned int j=0; j < width; j++) { for(unsigned int i=0; i < width; i++) { - output[i*height + j] = input[j*width + i]; + output[i*width + j] = input[j*width + i]; } } } @@ -118,10 +115,10 @@ int main() { // Lauching kernel from host hipLaunchKernel(matrixTranspose, - dim3(WIDTH/THREADS_PER_BLOCK_X, HEIGHT/THREADS_PER_BLOCK_Y), + dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y), dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), 0, 0, - gpuTransposeMatrix , gpuMatrix, WIDTH ,HEIGHT); + gpuTransposeMatrix , gpuMatrix, WIDTH); // Record the stop event hipEventRecord(stop, NULL); @@ -146,13 +143,13 @@ int main() { printf ("hipMemcpyDeviceToHost time taken = %6.3fms\n", eventMs); // CPU MatrixTranspose computation - matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH, HEIGHT); + matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH); // verify the results errors = 0; double eps = 1.0E-6; for (i = 0; i < NUM; i++) { - if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > 0 ) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps ) { errors++; } } diff --git a/hipamd/samples/2_Cookbook/3_shared_memory/Makefile b/hipamd/samples/2_Cookbook/3_shared_memory/Makefile index 5e9ce47211..24aafcd152 100644 --- a/hipamd/samples/2_Cookbook/3_shared_memory/Makefile +++ b/hipamd/samples/2_Cookbook/3_shared_memory/Makefile @@ -10,7 +10,7 @@ TARGET=hcc SOURCES = sharedMemory.cpp OBJECTS = $(SOURCES:.cpp=.o) -EXECUTABLE=./exe +EXECUTABLE=./sharedMemory .PHONY: test diff --git a/hipamd/samples/2_Cookbook/3_shared_memory/Readme.md b/hipamd/samples/2_Cookbook/3_shared_memory/Readme.md new file mode 100644 index 0000000000..6b9393397c --- /dev/null +++ b/hipamd/samples/2_Cookbook/3_shared_memory/Readme.md @@ -0,0 +1,42 @@ +## Using shared memory ### + +Earlier we learned how to write our first hip program, in which we compute Matrix Transpose. In this tutorial, we'll explain how to use the shared memory to improve the performance. + +## Introduction: + +As we mentioned earlier that Memory bottlenecks is the main problem why we are not able to get the highest performance, therefore minimizing the latency for memory access plays prominent role in application optimization. In this tutorial, we'll learn how to use static shared memory and will explain the dynamic one latter. + +## Requirement: +For hardware requirement and software installation [Installation](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/INSTALL.md) + +## prerequiste knowledge: + +Programmers familiar with CUDA, OpenCL will be able to quickly learn and start coding with the HIP API. In case you are not, don't worry. You choose to start with the best one. We'll be explaining everything assuming you are completely new to gpgpu programming. + +## Simple Matrix Transpose + +We will be using the Simple Matrix Transpose application from the previous tutorial and modify it to learn how to use shared memory. + +## Shared Memory + +Shared memory is way more faster than that of global and constant memory and accessible to all the threads in the block. If the size of shared memory is known at compile time, we can specify the size and will use the static shared memory. In the same sourcecode, we will use the `__shared__` variable type qualifier as follows: + +` __shared__ float sharedMem[1024*1024];` + +Be careful while using shared memory, since all threads within the block can access the shared memory, we need to sync the operation of individual threads by using: + +` __syncthreads();` + +## How to build and run: +Use the make command and execute it using ./exe +Use hipcc to build the application, which is using hcc on AMD and nvcc on nvidia. + +## More Info: +- [HIP FAQ](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_faq.md) +- [HIP Kernel Language](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_kernel_language.md) +- [HIP Runtime API (Doxygen)](http://gpuopen-professionalcompute-tools.github.io/HIP) +- [HIP Porting Guide](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_porting_guide.md) +- [HIP Terminology](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_terms.md) (including Rosetta Stone of GPU computing terms across CUDA/HIP/HC/AMP/OpenL) +- [clang-hipify](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/clang-hipify/README.md) +- [Developer/CONTRIBUTING Info](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/CONTRIBUTING.md) +- [Release Notes](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/RELEASE.md) diff --git a/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp b/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp index 9950b8d020..9b51aba442 100644 --- a/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp +++ b/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp @@ -26,13 +26,12 @@ THE SOFTWARE. #include "hip/hip_runtime.h" -#define WIDTH 1024 -#define HEIGHT 1024 +#define WIDTH 64 -#define NUM (WIDTH*HEIGHT) +#define NUM (WIDTH*WIDTH) -#define THREADS_PER_BLOCK_X 16 -#define THREADS_PER_BLOCK_Y 16 +#define THREADS_PER_BLOCK_X 4 +#define THREADS_PER_BLOCK_Y 4 #define THREADS_PER_BLOCK_Z 1 // Device (Kernel) function, it must be void @@ -40,15 +39,14 @@ THE SOFTWARE. __global__ void matrixTranspose(hipLaunchParm lp, float *out, float *in, - const int width, - const int height) + const int width) { - __shared__ float sharedMem[16*16]; + __shared__ float sharedMem[WIDTH*WIDTH]; int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; - sharedMem[y * width + x] = in[x * height + y]; + sharedMem[y * width + x] = in[x * width + y]; __syncthreads(); @@ -59,14 +57,13 @@ __global__ void matrixTranspose(hipLaunchParm lp, void matrixTransposeCPUReference( float * output, float * input, - const unsigned int width, - const unsigned int height) + const unsigned int width) { - for(unsigned int j=0; j < height; j++) + for(unsigned int j=0; j < width; j++) { for(unsigned int i=0; i < width; i++) { - output[i*height + j] = input[j*width + i]; + output[i*width + j] = input[j*width + i]; } } } @@ -106,22 +103,22 @@ int main() { // Lauching kernel from host hipLaunchKernel(matrixTranspose, - dim3(WIDTH/THREADS_PER_BLOCK_X, HEIGHT/THREADS_PER_BLOCK_Y), + dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y), dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), 0, 0, - gpuTransposeMatrix , gpuMatrix, WIDTH ,HEIGHT); + gpuTransposeMatrix , gpuMatrix, WIDTH); // Memory transfer from device to host hipMemcpy(TransposeMatrix, gpuTransposeMatrix, NUM*sizeof(float), hipMemcpyDeviceToHost); // CPU MatrixTranspose computation - matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH, HEIGHT); + matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH); // verify the results errors = 0; double eps = 1.0E-6; for (i = 0; i < NUM; i++) { - if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > 0 ) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps ) { printf("%d cpu: %f gpu %f\n",i,cpuTransposeMatrix[i],TransposeMatrix[i]); errors++; } diff --git a/hipamd/samples/2_Cookbook/4_shfl/Makefile b/hipamd/samples/2_Cookbook/4_shfl/Makefile index 1d30c78749..3383cf2bf5 100644 --- a/hipamd/samples/2_Cookbook/4_shfl/Makefile +++ b/hipamd/samples/2_Cookbook/4_shfl/Makefile @@ -10,7 +10,7 @@ TARGET=hcc SOURCES = shfl.cpp OBJECTS = $(SOURCES:.cpp=.o) -EXECUTABLE=./exe +EXECUTABLE=./shfl .PHONY: test @@ -22,7 +22,7 @@ CXX=$(HIPCC) $(EXECUTABLE): $(OBJECTS) - $(HIPCC) $(OBJECTS) -o $@ + $(HIPCC) $(OBJECTS) -o $@ test: $(EXECUTABLE) diff --git a/hipamd/samples/2_Cookbook/4_shfl/Readme.md b/hipamd/samples/2_Cookbook/4_shfl/Readme.md new file mode 100644 index 0000000000..da62901851 --- /dev/null +++ b/hipamd/samples/2_Cookbook/4_shfl/Readme.md @@ -0,0 +1,51 @@ +## Warp shfl operations ### + +In this tutorial, we'll explain how to use the warp shfl operations to improve the performance. + +## Introduction: + +Let's talk about Warp first. The kernel code is executed in groups of fixed number of threads known as Warp. For nvidia WarpSize is 32 while for AMD, 32 for Polaris architecture and 64 for rest. Threads in a warp are referred to as lanes and are numbered from 0 to warpSize -1. With the help of shfl ops, we can directly exchange values of variable between threads without using any memory ops within a warp. There are four types of shfl ops: +` int __shfl (int var, int srcLane, int width=warpSize); ` +` float __shfl (float var, int srcLane, int width=warpSize); ` +` int __shfl_up (int var, unsigned int delta, int width=warpSize); ` +` float __shfl_up (float var, unsigned int delta, int width=warpSize); ` +` int __shfl_down (int var, unsigned int delta, int width=warpSize); ` +` float __shfl_down (float var, unsigned int delta, int width=warpSize); ` +` int __shfl_xor (int var, int laneMask, int width=warpSize) ` +` float __shfl_xor (float var, int laneMask, int width=warpSize); ` + +## Requirement: +For hardware requirement and software installation [Installation](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/INSTALL.md) + +## prerequiste knowledge: + +Programmers familiar with CUDA, OpenCL will be able to quickly learn and start coding with the HIP API. In case you are not, don't worry. You choose to start with the best one. We'll be explaining everything assuming you are completely new to gpgpu programming. + +## Simple Matrix Transpose + +We will be using the Simple Matrix Transpose application from the previous tutorial and modify it to learn how to use shared memory. + +## __shfl ops + +In this tutorial, we'll use `__shfl()` ops. In the same sourcecode, we used for MatrixTranspose. We'll add the following: + +` out[i*width + j] = __shfl(val,j*width + i);` + +Be careful while using shfl operations, since all exchanges are possible between the threads of corresponding warp only. + +## How to build and run: +Use the make command and execute it using ./exe +Use hipcc to build the application, which is using hcc on AMD and nvcc on nvidia. + +## requirement for nvidia +please make sure you have a 3.0 or higher compute capable device in order to use warp shfl operations and add `-gencode arch=compute=30, code=sm_30` nvcc flag in the Makefile while using this application. + +## More Info: +- [HIP FAQ](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_faq.md) +- [HIP Kernel Language](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_kernel_language.md) +- [HIP Runtime API (Doxygen)](http://gpuopen-professionalcompute-tools.github.io/HIP) +- [HIP Porting Guide](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_porting_guide.md) +- [HIP Terminology](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_terms.md) (including Rosetta Stone of GPU computing terms across CUDA/HIP/HC/AMP/OpenL) +- [clang-hipify](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/clang-hipify/README.md) +- [Developer/CONTRIBUTING Info](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/CONTRIBUTING.md) +- [Release Notes](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/RELEASE.md) diff --git a/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp b/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp index 07d5cd42d2..e0f4c2120d 100644 --- a/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp +++ b/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp @@ -27,9 +27,8 @@ THE SOFTWARE. #define WIDTH 4 -#define HEIGHT 4 -#define NUM (WIDTH*HEIGHT) +#define NUM (WIDTH*WIDTH) #define THREADS_PER_BLOCK_X 4 #define THREADS_PER_BLOCK_Y 4 @@ -40,17 +39,16 @@ THE SOFTWARE. __global__ void matrixTranspose(hipLaunchParm lp, float *out, float *in, - const int width, - const int height) + const int width) { int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; - //int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; + float val = in[x]; for(int i=0;i 0 ) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps ) { printf("%d cpu: %f gpu %f\n",i,cpuTransposeMatrix[i],TransposeMatrix[i]); errors++; } diff --git a/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp b/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp index 16e5c74892..1b22a0c297 100644 --- a/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp +++ b/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp @@ -27,9 +27,8 @@ THE SOFTWARE. #define WIDTH 4 -#define HEIGHT 4 -#define NUM (WIDTH*HEIGHT) +#define NUM (WIDTH*WIDTH) #define THREADS_PER_BLOCK_X 4 #define THREADS_PER_BLOCK_Y 4 @@ -40,28 +39,26 @@ THE SOFTWARE. __global__ void matrixTranspose(hipLaunchParm lp, float *out, float *in, - const int width, - const int height) + const int width) { int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; float val = in[y*width + x]; - out[x*height + y] = __shfl(val,y*width + x); + out[x*width + y] = __shfl(val,y*width + x); } // CPU implementation of matrix transpose void matrixTransposeCPUReference( float * output, float * input, - const unsigned int width, - const unsigned int height) + const unsigned int width) { - for(unsigned int j=0; j < height; j++) + for(unsigned int j=0; j < width; j++) { for(unsigned int i=0; i < width; i++) { - output[i*height + j] = input[j*width + i]; + output[i*width + j] = input[j*width + i]; } } } @@ -104,19 +101,19 @@ int main() { dim3(1), dim3(THREADS_PER_BLOCK_X , THREADS_PER_BLOCK_Y), 0, 0, - gpuTransposeMatrix , gpuMatrix, WIDTH ,HEIGHT); + gpuTransposeMatrix , gpuMatrix, WIDTH); // Memory transfer from device to host hipMemcpy(TransposeMatrix, gpuTransposeMatrix, NUM*sizeof(float), hipMemcpyDeviceToHost); // CPU MatrixTranspose computation - matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH, HEIGHT); + matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH); // verify the results errors = 0; double eps = 1.0E-6; for (i = 0; i < NUM; i++) { - if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > 0 ) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps) { printf("%d cpu: %f gpu %f\n",i,cpuTransposeMatrix[i],TransposeMatrix[i]); errors++; } diff --git a/hipamd/samples/2_Cookbook/5_2dshfl/Makefile b/hipamd/samples/2_Cookbook/5_2dshfl/Makefile index 502d2948b0..b742bbf80a 100644 --- a/hipamd/samples/2_Cookbook/5_2dshfl/Makefile +++ b/hipamd/samples/2_Cookbook/5_2dshfl/Makefile @@ -10,7 +10,7 @@ TARGET=hcc SOURCES = 2dshfl.cpp OBJECTS = $(SOURCES:.cpp=.o) -EXECUTABLE=./exe +EXECUTABLE=./2dshfl .PHONY: test diff --git a/hipamd/samples/2_Cookbook/5_2dshfl/Readme.md b/hipamd/samples/2_Cookbook/5_2dshfl/Readme.md new file mode 100644 index 0000000000..fba114152a --- /dev/null +++ b/hipamd/samples/2_Cookbook/5_2dshfl/Readme.md @@ -0,0 +1,51 @@ +## Warp shfl operations in 2D ### + +This tutorial is follow-up of the previous tutorial, where we learned how to use shfl ops. In this tutorial, we'll explain how to scale similar kind of operations to multi-dimensional space by using previous tutorial source-code. + +## Introduction: + +Let's talk about Warp first. The kernel code is executed in groups of fixed number of threads known as Warp. For nvidia WarpSize is 32 while for AMD, 32 for Polaris architecture and 64 for rest. Threads in a warp are referred to as lanes and are numbered from 0 to warpSize -1. With the help of shfl ops, we can directly exchange values of variable between threads without using any memory ops within a warp. There are four types of shfl ops: +` int __shfl (int var, int srcLane, int width=warpSize); ` +` float __shfl (float var, int srcLane, int width=warpSize); ` +` int __shfl_up (int var, unsigned int delta, int width=warpSize); ` +` float __shfl_up (float var, unsigned int delta, int width=warpSize); ` +` int __shfl_down (int var, unsigned int delta, int width=warpSize); ` +` float __shfl_down (float var, unsigned int delta, int width=warpSize); ` +` int __shfl_xor (int var, int laneMask, int width=warpSize) ` +` float __shfl_xor (float var, int laneMask, int width=warpSize); ` + +## Requirement: +For hardware requirement and software installation [Installation](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/INSTALL.md) + +## prerequiste knowledge: + +Programmers familiar with CUDA, OpenCL will be able to quickly learn and start coding with the HIP API. In case you are not, don't worry. You choose to start with the best one. We'll be explaining everything assuming you are completely new to gpgpu programming. + +## Simple Matrix Transpose + +We will be using the Simple Matrix Transpose application from the previous tutorial and modify it to learn how to use shared memory. + +## __shfl ops in 2D + +In the same sourcecode, we used for MatrixTranspose. We'll add the following: +` int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; ` +` out[x*width + y] = __shfl(val,y*width + x); ` + +With the help of this application, we can say that kernel code can be converted into multi-dimensional threads with ease. + +## How to build and run: +Use the make command and execute it using ./exe +Use hipcc to build the application, which is using hcc on AMD and nvcc on nvidia. + +## requirement for nvidia +please make sure you have a 3.0 or higher compute capable device in order to use warp shfl operations and add `-gencode arch=compute=30, code=sm_30` nvcc flag in the Makefile while using this application. + +## More Info: +- [HIP FAQ](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_faq.md) +- [HIP Kernel Language](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_kernel_language.md) +- [HIP Runtime API (Doxygen)](http://gpuopen-professionalcompute-tools.github.io/HIP) +- [HIP Porting Guide](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_porting_guide.md) +- [HIP Terminology](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_terms.md) (including Rosetta Stone of GPU computing terms across CUDA/HIP/HC/AMP/OpenL) +- [clang-hipify](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/clang-hipify/README.md) +- [Developer/CONTRIBUTING Info](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/CONTRIBUTING.md) +- [Release Notes](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/RELEASE.md) diff --git a/hipamd/samples/2_Cookbook/6_dynamic_shared/Makefile b/hipamd/samples/2_Cookbook/6_dynamic_shared/Makefile new file mode 100644 index 0000000000..5d867a58c9 --- /dev/null +++ b/hipamd/samples/2_Cookbook/6_dynamic_shared/Makefile @@ -0,0 +1,36 @@ +HIP_PATH?= $(wildcard /opt/rocm/hip) +ifeq (,$(HIP_PATH)) + HIP_PATH=../../.. +endif + +HIPCC=$(HIP_PATH)/bin/hipcc + +TARGET=hcc + +SOURCES = dynamic_shared.cpp +OBJECTS = $(SOURCES:.cpp=.o) + +EXECUTABLE=./dynamic_shared + +.PHONY: test + + +all: $(EXECUTABLE) test + +CXXFLAGS =-g +CXX=$(HIPCC) + + +$(EXECUTABLE): $(OBJECTS) + $(HIPCC) $(OBJECTS) -o $@ + + +test: $(EXECUTABLE) + $(EXECUTABLE) + + +clean: + rm -f $(EXECUTABLE) + rm -f $(OBJECTS) + rm -f $(HIP_PATH)/src/*.o + diff --git a/hipamd/samples/2_Cookbook/6_dynamic_shared/Readme.md b/hipamd/samples/2_Cookbook/6_dynamic_shared/Readme.md new file mode 100644 index 0000000000..a10fd56a95 --- /dev/null +++ b/hipamd/samples/2_Cookbook/6_dynamic_shared/Readme.md @@ -0,0 +1,47 @@ +## Using Dynamic shared memory ### + +Earlier we learned how to use static shared memory. In this tutorial, we'll explain how to use the dynamic version of shared memory to improve the performance. + +## Introduction: + +As we mentioned earlier that Memory bottlenecks is the main problem why we are not able to get the highest performance, therefore minimizing the latency for memory access plays prominent role in application optimization. In this tutorial, we'll learn how to use dynamic shared memory. + +## Requirement: +For hardware requirement and software installation [Installation](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/INSTALL.md) + +## prerequiste knowledge: + +Programmers familiar with CUDA, OpenCL will be able to quickly learn and start coding with the HIP API. In case you are not, don't worry. You choose to start with the best one. We'll be explaining everything assuming you are completely new to gpgpu programming. + +## Simple Matrix Transpose + +We will be using the Simple Matrix Transpose application from the previous tutorial and modify it to learn how to use shared memory. + +## Shared Memory + +Shared memory is way more faster than that of global and constant memory and accessible to all the threads in the block. For In the same sourcecode, we will use the `HIP_DYNAMIC_SHARED` keyword to declare dynamic shared memory as follows: + +` HIP_DYNAMIC_SHARED(float, sharedMem) ` +here the first parameter is the data type while the second one is the variable name. + +The other important change is: +` hipLaunchKernel(matrixTranspose, ` + dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y), + dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), + sizeof(float)*WIDTH*WIDTH, 0, + gpuTransposeMatrix , gpuMatrix, WIDTH); +here we replaced 4th parameter with amount of additional shared memory to allocate when launching the kernel. + +## How to build and run: +Use the make command and execute it using ./exe +Use hipcc to build the application, which is using hcc on AMD and nvcc on nvidia. + +## More Info: +- [HIP FAQ](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_faq.md) +- [HIP Kernel Language](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_kernel_language.md) +- [HIP Runtime API (Doxygen)](http://gpuopen-professionalcompute-tools.github.io/HIP) +- [HIP Porting Guide](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_porting_guide.md) +- [HIP Terminology](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/docs/markdown/hip_terms.md) (including Rosetta Stone of GPU computing terms across CUDA/HIP/HC/AMP/OpenL) +- [clang-hipify](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/clang-hipify/README.md) +- [Developer/CONTRIBUTING Info](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/CONTRIBUTING.md) +- [Release Notes](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/RELEASE.md) diff --git a/hipamd/samples/2_Cookbook/6_dynamic_shared/dynamic_shared.cpp b/hipamd/samples/2_Cookbook/6_dynamic_shared/dynamic_shared.cpp new file mode 100644 index 0000000000..22d7eb9626 --- /dev/null +++ b/hipamd/samples/2_Cookbook/6_dynamic_shared/dynamic_shared.cpp @@ -0,0 +1,141 @@ +/* +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 +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. +*/ +#include + +// hip header file +#include "hip/hip_runtime.h" + +#define WIDTH 16 + +#define NUM (WIDTH*WIDTH) + +#define THREADS_PER_BLOCK_X 4 +#define THREADS_PER_BLOCK_Y 4 +#define THREADS_PER_BLOCK_Z 1 + +// Device (Kernel) function, it must be void +// hipLaunchParm provides the execution configuration +__global__ void matrixTranspose(hipLaunchParm lp, + float *out, + float *in, + const int width) +{ + // declare dynamic shared memory + HIP_DYNAMIC_SHARED(float, sharedMem); + + int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; + int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; + + sharedMem[y * width + x] = in[x * width + y]; + + __syncthreads(); + + out[y * width + x] = sharedMem[y * width + x]; +} + +// CPU implementation of matrix transpose +void matrixTransposeCPUReference( + float * output, + float * input, + const unsigned int width) +{ + for(unsigned int j=0; j < width; j++) + { + for(unsigned int i=0; i < width; i++) + { + output[i*width + j] = input[j*width + i]; + } + } +} + +int main() { + + float* Matrix; + float* TransposeMatrix; + float* cpuTransposeMatrix; + + float* gpuMatrix; + float* gpuTransposeMatrix; + + hipDeviceProp_t devProp; + hipGetDeviceProperties(&devProp, 0); + + std::cout << "Device name " << devProp.name << std::endl; + + int i; + int errors; + + Matrix = (float*)malloc(NUM * sizeof(float)); + TransposeMatrix = (float*)malloc(NUM * sizeof(float)); + cpuTransposeMatrix = (float*)malloc(NUM * sizeof(float)); + + // initialize the input data + for (i = 0; i < NUM; i++) { + Matrix[i] = (float)i*10.0f; + } + + // allocate the memory on the device side + hipMalloc((void**)&gpuMatrix, NUM * sizeof(float)); + hipMalloc((void**)&gpuTransposeMatrix, NUM * sizeof(float)); + + // Memory transfer from host to device + hipMemcpy(gpuMatrix, Matrix, NUM*sizeof(float), hipMemcpyHostToDevice); + + // Lauching kernel from host + hipLaunchKernel(matrixTranspose, + dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y), + dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y), + sizeof(float)*WIDTH*WIDTH, 0, + gpuTransposeMatrix , gpuMatrix, WIDTH); + + // Memory transfer from device to host + hipMemcpy(TransposeMatrix, gpuTransposeMatrix, NUM*sizeof(float), hipMemcpyDeviceToHost); + + // CPU MatrixTranspose computation + matrixTransposeCPUReference(cpuTransposeMatrix, Matrix, WIDTH); + + // verify the results + errors = 0; + double eps = 1.0E-6; + for (i = 0; i < NUM; i++) { + if (std::abs(TransposeMatrix[i] - cpuTransposeMatrix[i]) > eps ) { + printf("%d cpu: %f gpu %f\n",i,cpuTransposeMatrix[i],TransposeMatrix[i]); + errors++; + } + } + if (errors!=0) { + printf("FAILED: %d errors\n",errors); + } else { + printf ("dynamic_shared PASSED!\n"); + } + + //free the resources on device side + hipFree(gpuMatrix); + hipFree(gpuTransposeMatrix); + + //free the resources on host side + free(Matrix); + free(TransposeMatrix); + free(cpuTransposeMatrix); + + return errors; +} diff --git a/hipamd/samples/2_Cookbook/7_streams/Makefile b/hipamd/samples/2_Cookbook/7_streams/Makefile new file mode 100644 index 0000000000..64b0f0e097 --- /dev/null +++ b/hipamd/samples/2_Cookbook/7_streams/Makefile @@ -0,0 +1,36 @@ +HIP_PATH?= $(wildcard /opt/rocm/hip) +ifeq (,$(HIP_PATH)) + HIP_PATH=../../.. +endif + +HIPCC=$(HIP_PATH)/bin/hipcc + +TARGET=hcc + +SOURCES = stream.cpp +OBJECTS = $(SOURCES:.cpp=.o) + +EXECUTABLE=./stream + +.PHONY: test + + +all: $(EXECUTABLE) test + +CXXFLAGS =-g +CXX=$(HIPCC) + + +$(EXECUTABLE): $(OBJECTS) + $(HIPCC) $(OBJECTS) -o $@ + + +test: $(EXECUTABLE) + $(EXECUTABLE) + + +clean: + rm -f $(EXECUTABLE) + rm -f $(OBJECTS) + rm -f $(HIP_PATH)/src/*.o + diff --git a/hipamd/samples/2_Cookbook/7_streams/Readme.md b/hipamd/samples/2_Cookbook/7_streams/Readme.md new file mode 100644 index 0000000000..a75149925e --- /dev/null +++ b/hipamd/samples/2_Cookbook/7_streams/Readme.md @@ -0,0 +1,57 @@ +## Streams ### + +In all Earlier tutorial we used single stream, In this tutorial, we'll explain how to launch multiple streams. + +## Introduction: + +The various instances of kernel to be executed on device in exact launch order defined by Host are called streams. We can launch multiple streams on a single device. We will learn how to learn two streams which can we scaled with ease. + +## Requirement: +For hardware requirement and software installation [Installation](https://github.com/GPUOpen-ProfessionalCompute-Tools/HIP/INSTALL.md) + +## prerequiste knowledge: + +Programmers familiar with CUDA, OpenCL will be able to quickly learn and start coding with the HIP API. In case you are not, don't worry. You choose to start with the best one. We'll be explaining everything assuming you are completely new to gpgpu programming. + +## Simple Matrix Transpose + +We will be using the Simple Matrix Transpose application from the previous tutorial and modify it to learn how to launch multiple streams. + +## Streams + +In this tutorial, we'll use both instances of shared memory (i.e., static and dynamic) as different streams. We declare stream as follows: +` hipStream_t streams[num_streams]; ` + +and create stream using `hipStreamCreate` as follows: +` for(int i=0;i +#include + +#define WIDTH 32 + +#define NUM (WIDTH*WIDTH) + +#define THREADS_PER_BLOCK_X 4 +#define THREADS_PER_BLOCK_Y 4 +#define THREADS_PER_BLOCK_Z 1 + +using namespace std; + +__global__ void matrixTranspose_static_shared(hipLaunchParm lp, + float *out, + float *in, + const int width) +{ + __shared__ float sharedMem[WIDTH*WIDTH]; + + int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; + int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; + + sharedMem[y * width + x] = in[x * width + y]; + + __syncthreads(); + + out[y * width + x] = sharedMem[y * width + x]; +} + +__global__ void matrixTranspose_dynamic_shared(hipLaunchParm lp, + float *out, + float *in, + const int width) +{ + // declare dynamic shared memory + HIP_DYNAMIC_SHARED(float, sharedMem) + + int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; + int y = hipBlockDim_y * hipBlockIdx_y + hipThreadIdx_y; + + sharedMem[y * width + x] = in[x * width + y]; + + __syncthreads(); + + out[y * width + x] = sharedMem[y * width + x]; +} + +void MultipleStream (float **data, float *randArray, float **gpuTransposeMatrix, float **TransposeMatrix, int width) +{ + const int num_streams = 2; + hipStream_t streams[num_streams]; + + for(int i=0;i eps ) { + printf("%d stream0: %f stream1 %f\n",i,TransposeMatrix[0][i],TransposeMatrix[1][i]); + errors++; + } + } + if (errors!=0) { + printf("FAILED: %d errors\n",errors); + } else { + printf ("stream PASSED!\n"); + } + + free(randArray); + for(int i=0;i<2;i++){ + hipFree(data[i]); + hipFree(gpuTransposeMatrix[i]); + free(TransposeMatrix[i]); + } + + hipDeviceReset(); + return 0; +} From b4702f3912dbd90993da36ec8c577ca157e409fa Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Thu, 20 Oct 2016 10:44:30 +0530 Subject: [PATCH 16/16] Quickfix for HCSWAP-60, support for hipHostMallocPortable Change-Id: I2a4fcacea9d916ef222324fc9e9d8191f6dc12d0 --- hipamd/src/hip_memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipamd/src/hip_memory.cpp b/hipamd/src/hip_memory.cpp index 9e0473f02c..01f940a408 100644 --- a/hipamd/src/hip_memory.cpp +++ b/hipamd/src/hip_memory.cpp @@ -150,7 +150,7 @@ hipError_t hipHostMalloc(void** ptr, size_t sizeBytes, unsigned int flags) if(ctx){ // am_alloc requires writeable __acc, perhaps could be refactored? auto device = ctx->getWriteableDevice(); - if(flags == hipHostMallocDefault){ + if((flags == hipHostMallocDefault)|| (flags == hipHostMallocPortable)){ *ptr = hc::am_alloc(sizeBytes, device->_acc, amHostPinned); if(sizeBytes < 1 && (*ptr == NULL)){ hip_status = hipErrorMemoryAllocation;