GPU Directives
Total Page:16
File Type:pdf, Size:1020Kb
OPENACC® DIRECTIVES FOR ACCELERATORS NVIDIA GPUs Reaching Broader Set of Developers 1,000,000’s CAE CFD Finance Rendering Universities Data Analytics Supercomputing Centers Life Sciences 100,000’s Oil & Gas Defense Weather Research Climate Early Adopters Plasma Physics 2004 Present Time 2 3 Ways to Accelerate Applications with GPUs Applications Programming Libraries Directives Languages “Drop-in” Quickly Accelerate Maximum Acceleration Existing Applications Performance 3 Directives: Add A Few Lines of Code OpenMP OpenACC CPU CPU GPU main() { main() { double pi = 0.0; long i; double pi = 0.0; long i; #pragma acc parallel #pragma omp parallel for reduction(+:pi) for (i=0; i<N; i++) for (i=0; i<N; i++) { { double t = (double)((i+0.05)/N); double t = (double)((i+0.05)/N); pi += 4.0/(1.0+t*t); pi += 4.0/(1.0+t*t); } } printf(“pi = %f\n”, pi/N); printf(“pi = %f\n”, pi/N); } } OpenACC: Open Programming Standard for Parallel Computing “ OpenACC will enable programmers to easily develop portable applications that maximize the performance and power efficiency benefits of the hybrid CPU/GPU architecture of Titan. ” Buddy Bland Titan Project Director Easy, Fast, Portable Oak Ridge National Lab “ OpenACC is a technically impressive initiative brought together by members of the OpenMP Working Group on Accelerators, as well as many others. We look forward to releasing a version of this proposal in the next release of OpenMP. ” Michael Wong CEO, OpenMP http://www.openacc-standard.org/ Directives Board OpenACC Compiler directives to specify parallel regions in C, C++, Fortran OpenACC compilers offload parallel regions from host to accelerator Portable across OSes, host CPUs, accelerators, and compilers Create high-level heterogeneous programs Without explicit accelerator initialization, Without explicit data or program transfers between host and accelerator Programming model allows programmers to start simple Enhance with additional guidance for compiler on loop mappings, data location, and other performance details OpenACC Specification and Website Full OpenACC 1.0 Specification available online http://www.openacc-standard.org Quick reference card also available First implementations to appear mid-2012 Current PGI, Cray, and CAPS compilers all have accelerator directives (precursors to OpenACC) Small Effort. Real Impact. Large Oil Company Univ. of Houston Uni. Of Melbourne Ufa State Aviation GAMESS-UK Prof. M.A. Kayali Prof. Kerry Black Prof. Arthur Yuldashev Dr. Wilkinson, Prof. Naidoo 3x in 7 days 20x in 2 days 65x in 2 days 7x in 4 Weeks 10x Solving billions of Studying magnetic Better understand complex Generating stochastic Used for various fields equations iteratively for oil systems for innovations in reasons by lifecycles of geological models of such as investigating production at world’s magnetic storage media snapper fish in Port Phillip oilfield reservoirs with biofuel production and largest petroleum and memory, field sensors, Bay borehole data molecular sensors. reservoirs and biomagnetism * Achieved using the PGI Accelerator Compiler Focus on Exposing Parallelism With Directives, tuning work focuses on exposing parallelism, which makes codes inherently better Example: Application tuning work using directives for new Titan system at ORNL CAM-SE S3D Answer questions about specific Research more efficient climate change adaptation and combustion with next- mitigation scenarios generation fuels • Tuning top 3 kernels (90% of runtime) • Tuning top key kernel (50% of runtime) • 3 to 6x faster on CPU+GPU vs. CPU+CPU • 6.5x faster on CPU+GPU vs. CPU+CPU • But also improved all-CPU version by 50% • Improved performance of CPU version by 100% A simple example (Jacobi relaxation) Iterate until while ( error > tol && iter < iter_max ) { converged error=0.f; Iterate across for( int j = 1; j < n-1; j++) { elements of matrix for(int i = 1; i < m-1; i++) { Anew[j][i] = 0.25f * (A[j][i+1] + A[j][i-1] + A[j-1][i] + A[j+1][i]); error = max(error, abs(Anew[j][i] - A[j][i]); } } Calculate new value for( int j = 1; j < n-1; j++) { from neighbours for( int i = 1; i < m-1; i++ ) { A[j][i] = Anew[j][i]; }} iter++; } OpenMP CPU Implementation while ( error > tol && iter < iter_max ) { error=0.f; Parallelise loops #pragma omp parallel for shared(m, n, Anew, A) across CPU cores for( int j = 1; j < n-1; j++) { for(int i = 1; i < m-1; i++) { Anew[j][i] = 0.25f * (A[j][i+1] + A[j][i-1] + A[j-1][i] + A[j+1][i]); error = max(error, abs(Anew[j][i] - A[j][i]); } } #pragma omp parallel for shared(m, n, Anew, A) for( int j = 1; j < n-1; j++) { for( int i = 1; i < m-1; i++ ) { A[j][i] = Anew[j][i]; }} iter++; } OpenACC GPU Implementation #pragma acc data copy(A, Anew) Copy arrays into while ( error > tol && iter < iter_max ) { GPU within region error=0.f; #pragma acc parallel for( int j = 1; j < n-1; j++) { Parallelize loops for(int i = 1; i < m-1; i++) { with GPU kernels Anew[j][i] = 0.25f * (A[j][i+1] + A[j][i-1] + A[j-1][i] + A[j+1][i]); error = max(error, abs(Anew[j][i] - A[j][i]); } } #pragma acc parallel Data automatically for( int j = 1; j < n-1; j++) { copied back to at for( int i = 1; i < m-1; i++ ) { end of region A[j][i] = Anew[j][i]; }} iter++; } Speed Up Implementation Device Time (s) Speedup Single-threaded CPU Xeon X5550 33.42 -- Multi-threaded CPU (8 threads) Xeon X5550 17.81 1.88x GPU accelerated Tesla M2090 8.27 2.15x Over 2x speed compared to 8-Core OpenMP code with just 3 directives. Note: tested with PGI Accelerator directives Jacobi Relaxation (Fortran) Iterate until converged iter = 0 do while ( err .gt tol .and. iter .gt. iter_max ) iter = iter + 1 err = 0.0 Iterate across Calculate new value do j=1,m elements of matrix from neighbours do i=1,n Anew(i,j) = 0.25 * (A(i+1,j) + A(i-1,j) + A(i,j-1) + A(i, j+1) err = max( err, abs(Anew(i,j)-A(i,j)) ) end do end do if( mod(iter,100).eq.0 .or. iter.eq.1 ) print*, iter, err A = Anew end do © NVIDIA Corporation 2012 OpenMP CPU Implementation (Fortran) iter = 0 do while ( err .gt tol .and. iter .gt. iter_max ) Parallelise code iter = iter + 1 inside region err = 0.0 !$omp parallel do shared(m,n,Anew,A) reduction(max:err) do j=1,m do i=1,n Anew(i,j) = 0.25 * (A(i+1,j) + A(i-1,j) + A(i,j-1) + A(i, j+1) err = max( err, abs(Anew(i,j)-A(i,j)) ) end do end do Close off region !$omp end parallel do if( mod(iter,100).eq.0 ) print*, iter, err A = Anew end do © NVIDIA Corporation 2012 OpenACC GPU Implementation (Fortran) !$acc data copy(A,Anew) Copy arrays into GPU iter = 0 memory within region do while ( err .gt tol .and. iter .gt. iter_max ) iter = iter + 1 err = 0.0 !$acc parallel reduction( max:err ) Parallelise code do j=1,m inside region do i=1,n Anew(i,j) = 0.25 * (A(i+1,j) + A(i-1,j) + A(i,j-1) + A(i,j+1) err = max( err, abs(Anew(i,j)-A(i,j)) ) end do Close off parallel end do region !$acc end parallel if( mod(iter,100).eq.0 ) print*, iter, err !$acc parallel A = Anew !$acc end parallel end do Close off data region, !$acc end data copy data back © NVIDIA Corporation 2012 Finding Parallelism in your code (Nested) for loops are best for parallelization Large loop counts needed to offset GPU/memcpy overhead Iterations of loops must be independent of each other Compiler must be able to figure out sizes of data regions Can use directives to explicitly control sizes Pointer arithmetic should be avoided if possible Use subscripted arrays, rather than pointer-indexed arrays. Function calls within accelerated region must be inlineable. Tips and Tricks (PGI) Use time option to learn where time is being spent -ta=nvidia,time Eliminate pointer arithmetic Inline function calls in directives regions Use contiguous memory for multi-dimensional arrays Use data regions to avoid excessive memory transfers Conditional compilation with _OPENACC macro OPENACC CONCEPTS Basic Concepts CPU Memory Transfer data GPU Memory PCI Bus CPU Offload computation GPU For efficiency, decouple data movement and compute off-load Directive Syntax Fortran !$acc directive [clause [,] clause] …] Often paired with a matching end directive surrounding a structured code block !$acc end directive C #pragma acc directive [clause [,] clause] …] Often followed by a structured code block DATA MANAGEMENT Data Construct Fortran C !$acc data [clause …] #pragma acc data [clause …] structured block { structured block } !$acc end data General Clauses if( condition ) async( expression ) Manage data movement. Data regions may be nested. Data Clauses copy ( list ) Allocates memory on GPU and copies data from host to GPU when entering region and copies data to the host when exiting region. copyin ( list ) Allocates memory on GPU and copies data from host to GPU when entering region. copyout ( list ) Allocates memory on GPU and copies data to the host when exiting region. create ( list ) Allocates memory on GPU but does not copy. present ( list ) Data is already present on GPU from another containing data region. and present_or_copy[in|out], present_or_create, deviceptr. Improved OpenACC GPU Implementation #pragma acc data copyin(A), create(Anew) Reduced data while ( error > tol && iter < iter_max ) { movement error=0.f; #pragma acc parallel for( int j = 1; j < n-1; j++) { for(int i = 1; i < m-1; i++) { Anew[j][i] = 0.25f * (A[j][i+1] + A[j][i-1] + A[j-1][i] + A[j+1][i]); error = max(error, abs(Anew[j][i] - A[j][i]); } } #pragma acc parallel for( int j = 1; j < n-1; j++) { for( int i = 1; i < m-1; i++ ) { A[j][i] = Anew[j][i]; }} iter++; } Improved OpenACC GPU Implementation Reduced data !$acc data copyin(A), create(Anew) movement iter = 0 do while ( err .gt tol .and.