Francesco Di Renzo GPGPU CUDA vs Open GL

  • Slides: 19
Download presentation
Francesco Di Renzo GPGPU: CUDA vs Open. GL F. Di Renzo Università di Parma

Francesco Di Renzo GPGPU: CUDA vs Open. GL F. Di Renzo Università di Parma and INFN, Parma, Italy Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL Outline Ø GPGPU General Purpose computations

Francesco Di Renzo GPGPU: CUDA vs Open. GL Outline Ø GPGPU General Purpose computations on Graphics Processing Units Ø A look at the current scenario: we have heard of hardware (an impressive growth!); what about software ? (Open. GL, CUDA. Open. CL still to come) Ø An exercise (simulation of (2 -d) XY spin model by Hybrid Monte. Carlo) will give us the chance to present two different programming solutions: § CUDA (Compute Unified Device Architecture) (on different devices) § Open. GL (GLSL) (Open Graphics Library) Ø Conclusions (actually work in progress…) Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL A growing interest for GPGPU …

Francesco Di Renzo GPGPU: CUDA vs Open. GL A growing interest for GPGPU … In the end: what are PC’s built for? what are they useful for? In recent years the more and more powerful GPU's available on the PC market have attracted attention as a cost effective solution for parallel (SIMD) computing. The birth of the GPGPU acronym is there as a witness. • At the beginning one could look at GPGPU as one of the many examples of IT-DIY • Quite soon scientific applications made their entrance • Actually, there are experts in the Lattice QCD community! Egri, Fodor, Hoebling, Katz, Nogradi, Szabo, Lattice QCD as a video game, Comput. Phys. Comm. 177 (631) 2007 • Big hw companies have been becoming quite iterested players • A good example is provided by Nvidia (CUDA hw/sw architecture, Tesla products) Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL The basic fact: GPU, a basic

Francesco Di Renzo GPGPU: CUDA vs Open. GL The basic fact: GPU, a basic SIMD architecture GPU resources allocated in a very different way with respect to CPU In the end, every computer is from a logical point of view control, data-path and storage. In the end, every computer design is an allocation of resources. Basic points: • a SIMD architecture (parallel processing of threads) is well suited for taking care of the mapping of basic graphic elements (pixels, vertices, …) • memory latencies to be hidden by computations more than by mean of a big cache The real bottom line: • they don’t run an Operating System! Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL A couple of items to be

Francesco Di Renzo GPGPU: CUDA vs Open. GL A couple of items to be kept in mind In the end, there is something GPU’s are intended for … The graphic pipeline Many steps are needed in order to get a 2 -d bitmap for a 3 -d image: many SIMD resources are devoted to them. As always in a pipeline, the output of a stage is input for the following one. Since the fragment shading stage is a very powerful one, one would like to make usage of those resources. Typical (basic) data types are textures Basically, they are (2 -d) matrices. Roughly speaking: images should be eventualy mapped to a bunch of pixels on the screen. Vector types are a natural choice: RGBA textures account for colours and alpha channel (transparency). Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL A couple of approaches There’s almost

Francesco Di Renzo GPGPU: CUDA vs Open. GL A couple of approaches There’s almost always more than one solution In the following we will report on two different implentations of the same MC simulation. They are representative of two approaches at our disposal. Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL Our lab: the (2 -d) XY

Francesco Di Renzo GPGPU: CUDA vs Open. GL Our lab: the (2 -d) XY spin model by Hybrid Monte. Carlo Your degrees of freedom are angles, i. e. orientation of the (interacting) spins in your system. There is an angle on each lattice size. You want to compute expectation values of Statistical Mechanics, which in a MC you trade for averages taken over a (stochastic) time evolution. Rule of the game: associate to each angle a (momentum) and integrate equations of motion. In practice: given the { in}, extract a set of { in}, compute You now want to go which you do by integrating (interactions! Nearest neighbors) At the end compute again And accept the new configuration { fin} with probability (Metropolis) If you think about it, some subtle points are in place … main one is synchronization! Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo (… again on … ) GPGPU: CUDA vs Open. GL XY

Francesco Di Renzo (… again on … ) GPGPU: CUDA vs Open. GL XY spin model by Hybrid Monte. Carlo A disclaimer: preliminary investigations, so that improvements are possible. Having said that, let’s pin down some common features of both implementation: - As for generation of momenta: flat random numbers were generated on CPU, conversion to gaussian performed on GPU. - Some operations are critical with respect to single/double precision: global sums of the energy were performed on CPU (as well as Metropolis step). - The core of the parallel computation is the leap-frog integration of equations of motion. - Results were cross-checked with series expansions in the high temperature regime (we are friends of P. Butera, after all) and with a reference HMC (this was done also for acceptance). - The reference serial code was run on an Intel Conroe and was also taken as a reference (at fixed HMC parameters) for performance evaluation. Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL The CUDA environment Nvidia’s entrance to

Francesco Di Renzo GPGPU: CUDA vs Open. GL The CUDA environment Nvidia’s entrance to parallel computing Nvidia calls CUDA a hw/sw architecture which is intended to enable parallel programming on GPU. In the end, they distribute a driver to access the device (list of compatible GPU models extends to last three generations) and a toolkit enabling a programming environment which is basically a (thread-oriented) extension to C. Users are provided with • A nvcc C compiler (code on the device). • Debugging and profiling tools. • FFT and BLAS CUDA libraries. • Documentation. • A collection of worked-out examples. Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL A look at a recent GPU

Francesco Di Renzo GPGPU: CUDA vs Open. GL A look at a recent GPU architecture Nvidia wants you to be aware of the basic hw … Basically, Nvidia wants you to be aware that a recent GPU is collection of multiprocessors, each made of several processors. - In particular, one should notice the hierarchy of memories at our disposal. - Basic CUDA tool is a driver enabling the access the device in a natural way. In particular, the language enables you to upload/download data to/from the device memories. - You are then entitled to make your choice on where you want your data to reside. - In the end, resources are limited and all the game goes back to their allocation. Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL CUDA processes organization Quite a natural

Francesco Di Renzo GPGPU: CUDA vs Open. GL CUDA processes organization Quite a natural threads environment A process running on CPU (host) can start (several) kernels on GPU (device). - Basic organization is blocks of threads which come in a grid of blocks. - You have commands to upload/download data to/from the device memory. - To execute a kernel your call will be My_kernel<<< dim. G, , dim. B>>>(my_arg_1, My_kernel<<<dim. G dim. B>>>(my_arg_1, …, my_arg_n) - Threads within a block can be synchronized (they are assigned by the system to the same multiprocessor) and they typically access shared memory. - There are limitations to the number of threads within a block and of blocks within a grid. Then there are limitations imposed by shared memory dimension. Actual allocation of resources is up to the system (blocks sliced into warps). Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL Lattice allocation First candidate is a

Francesco Di Renzo GPGPU: CUDA vs Open. GL Lattice allocation First candidate is a quite a natural one. Beware! Now we don’t plot the entire lattice, but a sublattice straightforward: map sites to threads, organize - Allocating the lattice can be really blocks (i. e. sublattices), take care of the sublattices borders and try to take advantage of the shared memories. - Nearest neighbors are accessed in a very natural way once you get a border. - We have to keep your own balance optimizing the usage of resources: mainly, dimensions of grid and blocks (well, first implementation is easy and fast; after that you try to do better!) Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL A code sample #define DIM_GH 256

Francesco Di Renzo GPGPU: CUDA vs Open. GL A code sample #define DIM_GH 256 #define DIM_GW 16 #define DIM_BH 2 #define DIM_BW 32. . . float *h_QA, float *d_QA, *d_QB; . . . CUT_DEVICE_INIT(argc, argv); CUT_SAFE_CALL( cut. Create. Timer(&h. Timer) ); . . . h_QA = (float *)malloc(DATA_SZ); . . . // printf(". . . allocating GPU memory. n"); CUDA_SAFE_CALL( cuda. Malloc((void **)&d_QA, DATA_SZ) ); CUDA_SAFE_CALL( cuda. Malloc((void **)&d_QB, DATA_SZ) ); . . // printf(". . . copying input data to GPU mem. n"); CUDA_SAFE_CALL( cuda. Memcpy(d_QA, h_QA, DATA_SZ, cuda. Memcpy. Host. To. Device) ); CUDA_SAFE_CALL( cuda. Memcpy(d_QB, h_QA, DATA_SZ, cuda. Memcpy. Host. To. Device) ); . . Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo // Esecuzione del Kernel GPGPU: CUDA vs Open. GL (…) A

Francesco Di Renzo // Esecuzione del Kernel GPGPU: CUDA vs Open. GL (…) A code sample dim 3 dim. Grid( DIM_GW, DIM_GH ); dim 3 dim. Block( DIM_BW, DIM_BH ); printf("Executing GPU kernel. . . n"); CUT_SAFE_CALL( cut. Reset. Timer(h. Timer) ); CUT_SAFE_CALL( cut. Start. Timer(h. Timer) ); for( jj = 0; jj < sweeps; jj++ ) {. . . genera. PRANDGPU<<<dim. Grid, dim. Block 2>>>(d_PA); dinamica. PGPU<<<dim. Grid, dim. Block>>>(d_QA, d_QB, d_PA, d_E 0, d_EP, . . . ); . . . . __global__ void dinamica. QGPU( float *d_QA, float *d_QB, float *d_PA, float delta. Tau, int flg 2) { // Block index int bx = block. Idx. x; int by = block. Idx. y; // Thread index int tx = thread. Idx. x; int ty = thread. Idx. y; . . . // Evoluzione delle Q. . . d_QA[a. Begin+DIM_BW*ty+tx] = d_QB[a. Begin+DIM_BW*ty+tx] +delta. Tau*d_PA[a. Begin+DIM_BW*ty+tx]; Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL It is easy to gain with

Francesco Di Renzo GPGPU: CUDA vs Open. GL It is easy to gain with respect to the serial code But maybe too easy … optimal codes needs as usual extra effort … Let’s compare different devices: Nvidia Ge. Force 9600 M GT (on my Mac. Book. Pro), Nvidia Ge. Force 8800 GTX (a used-to-be high performance desktop graphics card), Nvidia Tesla C 1060 (a really dedicated system). Number of multiprocessors: 4, 16, 30. We plot gain with respect to the serial code (TCPU / TCUDA) on a given lattice size, with a given choice of dim. G, dim. B. Beware! Number of processors is not the only feature! (memory …) Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL Let’s now talk Open. GL The

Francesco Di Renzo GPGPU: CUDA vs Open. GL Let’s now talk Open. GL The first, tricky approach to GPGPU which has been used GLSL (Shading Language): another (less direct) extension to C, providing an environment for access to GPU in the Open. GL framework. - Vector variables are a natural choice (vec 2, vec 3, vec 4). vec 4 are an obvious choice for RGBA textures. - Special output variables (e. g. vec 4 gl_Frag. Color): always keep in mind you are supposed to process images. - math library available! Your code will be essentially devoted to enter the graphic pipeline! - GLSL is initialized runtime, while your kernel has to be “prepared” and then enabled to enter the rendering pipeline. - Input basically boils down to binding textures to texture units, while output is attaching the target texture to a FBO. Finally, remember that we need a filled quad in order to “draw” … - In a pipeline output of one stage is regarded as input for next one. This restricts RW access: PING-PONG technique! Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL Lattice allocation Things can be much

Francesco Di Renzo GPGPU: CUDA vs Open. GL Lattice allocation Things can be much more natural than they appear (2 -d …) - Texels can be your spins. - Texels make a texture like spins make a lattice. - A RGBA texture easily accomodates 4 independent replicas of a lattice (maybe at different temperatures). You only need to be careful on Metropolis acceptance step (easily done: Y = z Ynew + (1 -z) Yold ). - Nearest neighbors are also easily accomodated: in 2 -d they are 4, so they fit in a RGBA texture as well. 2 -d systems are actually a gold plated application … Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL It was even easier to gain!

Francesco Di Renzo GPGPU: CUDA vs Open. GL It was even easier to gain! The more you can fit the better you do … - Red points are CUDA results, blue ones are th Open. GL results. Again, gain factors vs lattice sizes, on the same device (Ge. Force 8800 GTX). Workshop CCR 2009 Palau - May 12, 2009

Francesco Di Renzo GPGPU: CUDA vs Open. GL Conclusions • • • Work was

Francesco Di Renzo GPGPU: CUDA vs Open. GL Conclusions • • • Work was intended as a benchmark exercise (XY model was really only a lab; it was even implemented as an almost worst-case scenario…). It might well be that one can do better than this. New solutions will be soon available (Open. CL). CUDA environment is really friendly: it is easy to get (cost-effective!) fairly good performances. Still, scaling with the number of resources is (as always) not trivial and memory issues are important! Workshop CCR 2009 Palau - May 12, 2009