diff --git a/projects/clr/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile b/projects/clr/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile index ffb442e443..d3630a1c19 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/0_MatrixTranspose/Makefile +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp b/projects/clr/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp index 91733c025a..264fcbed53 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/0_MatrixTranspose/MatrixTranspose.cpp +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/1_hipEvent/Makefile b/projects/clr/hipamd/samples/2_Cookbook/1_hipEvent/Makefile index dc0f7db2e6..be4bc2169a 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/1_hipEvent/Makefile +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp b/projects/clr/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp index 1abe1180da..f2aea146e4 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/1_hipEvent/hipEvent.cpp +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile b/projects/clr/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile index ffb442e443..d3630a1c19 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/Makefile +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp b/projects/clr/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp index 1abe1180da..f2aea146e4 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/2_HIP_ATP_MARKER/MatrixTranspose.cpp +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/Makefile b/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/Makefile index 5e9ce47211..24aafcd152 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/Makefile +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/Readme.md b/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/Readme.md new file mode 100644 index 0000000000..6b9393397c --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp b/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp index 9950b8d020..9b51aba442 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/3_shared_memory/sharedMemory.cpp +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/4_shfl/Makefile b/projects/clr/hipamd/samples/2_Cookbook/4_shfl/Makefile index 1d30c78749..3383cf2bf5 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/4_shfl/Makefile +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/4_shfl/Readme.md b/projects/clr/hipamd/samples/2_Cookbook/4_shfl/Readme.md new file mode 100644 index 0000000000..da62901851 --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp b/projects/clr/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp index 07d5cd42d2..e0f4c2120d 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/4_shfl/shfl.cpp +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp b/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp index 16e5c74892..1b22a0c297 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/2dshfl.cpp +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/Makefile b/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/Makefile index 502d2948b0..b742bbf80a 100644 --- a/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/Makefile +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/Readme.md b/projects/clr/hipamd/samples/2_Cookbook/5_2dshfl/Readme.md new file mode 100644 index 0000000000..fba114152a --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/6_dynamic_shared/Makefile b/projects/clr/hipamd/samples/2_Cookbook/6_dynamic_shared/Makefile new file mode 100644 index 0000000000..5d867a58c9 --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/6_dynamic_shared/Readme.md b/projects/clr/hipamd/samples/2_Cookbook/6_dynamic_shared/Readme.md new file mode 100644 index 0000000000..a10fd56a95 --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/6_dynamic_shared/dynamic_shared.cpp b/projects/clr/hipamd/samples/2_Cookbook/6_dynamic_shared/dynamic_shared.cpp new file mode 100644 index 0000000000..22d7eb9626 --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/7_streams/Makefile b/projects/clr/hipamd/samples/2_Cookbook/7_streams/Makefile new file mode 100644 index 0000000000..64b0f0e097 --- /dev/null +++ b/projects/clr/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/projects/clr/hipamd/samples/2_Cookbook/7_streams/Readme.md b/projects/clr/hipamd/samples/2_Cookbook/7_streams/Readme.md new file mode 100644 index 0000000000..a75149925e --- /dev/null +++ b/projects/clr/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; +}