Merge branch 'amd-develop' into amd-master
Change-Id: I6b59c6f0d187685344e0444f769e19454a2c6ef0
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -91,6 +91,18 @@ if ($HIP_PLATFORM eq "hcc") {
|
||||
$HIPCC=$HCC;
|
||||
$HIPCXXFLAGS = $HCCFLAGS;
|
||||
|
||||
#### GCC system includes workaround ####
|
||||
$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;
|
||||
$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";
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
@@ -626,8 +637,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
|
||||
*
|
||||
@@ -688,6 +703,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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -10,7 +10,7 @@ TARGET=hcc
|
||||
SOURCES = MatrixTranspose.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
EXECUTABLE=./exe
|
||||
EXECUTABLE=./MatrixTranspose
|
||||
|
||||
.PHONY: test
|
||||
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ TARGET=hcc
|
||||
SOURCES = hipEvent.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
EXECUTABLE=./exe
|
||||
EXECUTABLE=./hipEvent
|
||||
|
||||
.PHONY: test
|
||||
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ TARGET=hcc
|
||||
SOURCES = MatrixTranspose.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
EXECUTABLE=./exe
|
||||
EXECUTABLE=./MatrixTranspose
|
||||
|
||||
.PHONY: test
|
||||
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ TARGET=hcc
|
||||
SOURCES = sharedMemory.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
EXECUTABLE=./exe
|
||||
EXECUTABLE=./sharedMemory
|
||||
|
||||
.PHONY: test
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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++;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
@@ -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<width;i++)
|
||||
{
|
||||
for(int j=0;j<width;j++)
|
||||
out[i*height + j] = __shfl(val,j*width + i);
|
||||
out[i*width + j] = __shfl(val,j*width + i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,14 +56,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];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,19 +105,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++;
|
||||
}
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ TARGET=hcc
|
||||
SOURCES = 2dshfl.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
EXECUTABLE=./exe
|
||||
EXECUTABLE=./2dshfl
|
||||
|
||||
.PHONY: test
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -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<iostream>
|
||||
|
||||
// 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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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<num_streams;i++) `
|
||||
` hipStreamCreate(&streams[i]); `
|
||||
|
||||
and while kernel launch, we make the following changes in 5th parameter to hipLaunchKernel(having 0 as the default stream value):
|
||||
|
||||
` hipLaunchKernel(matrixTranspose_static_shared, `
|
||||
dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y),
|
||||
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y),
|
||||
0, streams[0],
|
||||
gpuTransposeMatrix[0], data[0], width);
|
||||
|
||||
` hipLaunchKernel(matrixTranspose_dynamic_shared, `
|
||||
dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y),
|
||||
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y),
|
||||
sizeof(float)*WIDTH*WIDTH, streams[1],
|
||||
gpuTransposeMatrix[1], data[1], 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)
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
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 WARRANUMTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INUMCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNUMESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANUMY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER INUM AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR INUM CONUMECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <hip/hip_runtime.h>
|
||||
|
||||
#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<num_streams;i++)
|
||||
hipStreamCreate(&streams[i]);
|
||||
|
||||
for(int i=0;i<num_streams;i++)
|
||||
{
|
||||
hipMalloc((void**)&data[i], NUM * sizeof(float));
|
||||
hipMemcpyAsync(data[i], randArray, NUM * sizeof(float), hipMemcpyHostToDevice,streams[i]);
|
||||
}
|
||||
|
||||
hipLaunchKernel(matrixTranspose_static_shared,
|
||||
dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y),
|
||||
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y),
|
||||
0, streams[0],
|
||||
gpuTransposeMatrix[0], data[0], width);
|
||||
|
||||
hipLaunchKernel(matrixTranspose_dynamic_shared,
|
||||
dim3(WIDTH/THREADS_PER_BLOCK_X, WIDTH/THREADS_PER_BLOCK_Y),
|
||||
dim3(THREADS_PER_BLOCK_X, THREADS_PER_BLOCK_Y),
|
||||
sizeof(float)*WIDTH*WIDTH, streams[1],
|
||||
gpuTransposeMatrix[1], data[1], width);
|
||||
|
||||
for(int i=0;i<num_streams;i++)
|
||||
hipMemcpyAsync(TransposeMatrix[i], gpuTransposeMatrix[i], NUM*sizeof(float), hipMemcpyDeviceToHost, streams[i]);
|
||||
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
hipSetDevice(0);
|
||||
|
||||
float *data[2], *TransposeMatrix[2], *gpuTransposeMatrix[2], *randArray;
|
||||
|
||||
int width = WIDTH;
|
||||
|
||||
randArray = (float*)malloc(NUM * sizeof(float));
|
||||
|
||||
TransposeMatrix[0] = (float*)malloc(NUM * sizeof(float));
|
||||
TransposeMatrix[1] = (float*)malloc(NUM * sizeof(float));
|
||||
|
||||
hipMalloc((void**)&gpuTransposeMatrix[0], NUM * sizeof(float));
|
||||
hipMalloc((void**)&gpuTransposeMatrix[1], NUM * sizeof(float));
|
||||
|
||||
for(int i = 0; i < NUM; i++)
|
||||
{
|
||||
randArray[i] = (float)i*1.0f;
|
||||
}
|
||||
|
||||
MultipleStream(data, randArray, gpuTransposeMatrix, TransposeMatrix, width);
|
||||
|
||||
hipDeviceSynchronize();
|
||||
|
||||
// verify the results
|
||||
int errors = 0;
|
||||
double eps = 1.0E-6;
|
||||
for (int i = 0; i < NUM; i++) {
|
||||
if (std::abs(TransposeMatrix[0][i] - TransposeMatrix[1][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;
|
||||
}
|
||||
@@ -26,8 +26,8 @@ THE SOFTWARE.
|
||||
#include <stack>
|
||||
|
||||
#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<ihipCtx_t *> tls_ctxStack;
|
||||
|
||||
@@ -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
|
||||
@@ -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,26 @@ 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 = hipSuccess; // TODO - should this be error? Map to Auto?
|
||||
//e = hipErrorInvalidValue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned supportedFlags = hipDeviceScheduleMask | hipDeviceMapHost | hipDeviceLmemResizeToMax;
|
||||
|
||||
if (flags & (~supportedFlags)) {
|
||||
e = hipErrorInvalidValue;
|
||||
}
|
||||
} else {
|
||||
e = hipErrorInvalidDevice;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -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"
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
@@ -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) {
|
||||
@@ -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);
|
||||
|
||||
+48
-14
@@ -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"
|
||||
|
||||
|
||||
|
||||
@@ -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_WAIT_MODE = 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<int> 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,29 @@ 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.
|
||||
}
|
||||
|
||||
if (HIP_WAIT_MODE == 1) {
|
||||
waitMode = hc::hcWaitModeBlocked;
|
||||
} else if (HIP_WAIT_MODE == 2) {
|
||||
waitMode = hc::hcWaitModeActive;
|
||||
}
|
||||
|
||||
crit->_av.wait(waitMode);
|
||||
}
|
||||
|
||||
crit->_kernelCnt = 0;
|
||||
@@ -277,7 +313,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 +741,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 +752,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 +822,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;
|
||||
@@ -1093,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_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_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.");
|
||||
|
||||
@@ -1177,13 +1210,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(), "<ihipInit>");
|
||||
tprintf(DB_SYNC, "pid=%u %-30s g_numLogicalThreads=%u\n", getpid(), "<ihipInit>", g_numLogicalThreads);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ THE SOFTWARE.
|
||||
|
||||
#include <hc.hpp>
|
||||
#include <hsa/hsa.h>
|
||||
#include "hip/hcc_detail/hip_util.h"
|
||||
#include "hip_util.h"
|
||||
|
||||
|
||||
#if defined(__HCC__) && (__hcc_workweek__ < 16354)
|
||||
@@ -384,11 +384,14 @@ typedef ihipStreamCriticalBase_t<StreamMutex> ihipStreamCritical_t;
|
||||
typedef LockedAccessor<ihipStreamCritical_t> 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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ THE SOFTWARE.
|
||||
#include <hc_am.hpp>
|
||||
|
||||
#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.
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<NUM;i++) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user