8
General General- Purpose Computation on Purpose Computation on Graphics Hardware Graphics Hardware Course Introduction Course Introduction The GPU on commodity video cards has evolved into an extremely flexible and powerful processor – Programmability – Precision – Power Motivation: Computational Power Motivation: Computational Power GPUs are fast… 3 GHz Pentium4 theoretical: 6 GFLOPS, 5.96 GB/sec peak – GeForceFX 5900 observed: 20 GFLOPs, 25.3 GB/sec peak GPUs are getting faster, faster CPUs: annual growth ; 1.5× decade growth ; 60× GPUs: annual growth > 2.0× decade growth > 1000 Courtesy Kurt Akeley, Ian Buck & Tim Purcell, GPU Gems (see course notes) Motivation: Computational Power Motivation: Computational Power Courtesy Naga Govindaraju GPU CPU An Aside: Computational Power An Aside: Computational Power Why are GPUs getting faster so fast? Arithmetic intensity: the specialized nature of GPUs makes it easier to use additional transistors for computation not cache Economics: multi-billion dollar video game market is a pressure cooker that drives innovation Motivation: Flexible and precise Motivation: Flexible and precise Modern GPUs are deeply programmable Programmable pixel, vertex, video engines Solidifying high-level language support Modern GPUs support high precision 32 bit floating point throughout the pipeline High enough for many (not all) applications

17 GPU computation

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 17 GPU computation

GeneralGeneral--Purpose Computation on Purpose Computation on Graphics HardwareGraphics Hardware

Course IntroductionCourse Introduction

• The GPU on commodity video cards has evolved into an extremely flexible and powerful processor– Programmability– Precision– Power

Motivation: Computational PowerMotivation: Computational Power• GPUs are fast…

– 3 GHz Pentium4 theoretical: 6 GFLOPS, 5.96 GB/sec peak– GeForceFX 5900 observed: 20 GFLOPs, 25.3 GB/sec peak

• GPUs are getting faster, faster– CPUs: annual growth ; 1.5× decade growth ; 60× – GPUs: annual growth > 2.0× decade growth > 1000

Courtesy Kurt Akeley,Ian Buck & Tim Purcell, GPU Gems (see course notes)

Motivation:Computational PowerMotivation:Computational Power

Courtesy Naga Govindaraju

GPU

CPU

An Aside: Computational PowerAn Aside: Computational Power• Why are GPUs getting faster so fast?

– Arithmetic intensity: the specialized nature of GPUs makes it easier to use additional transistors for computation not cache

– Economics: multi-billion dollar video game market is a pressure cooker that drives innovation

Motivation:Flexible and preciseMotivation:Flexible and precise• Modern GPUs are deeply programmable

– Programmable pixel, vertex, video engines– Solidifying high-level language support

• Modern GPUs support high precision– 32 bit floating point throughout the pipeline– High enough for many (not all) applications

Page 2: 17 GPU computation

Motivation:The Potential of GPGPUMotivation:The Potential of GPGPU• The power and flexibility of GPUs makes them an

attractive platform for general-purpose computation

• Example applications range from in-game physics simulation to conventional computational science

• Goal: make the inexpensive power of the GPU available to developers as a sort of computational coprocessor

The Problem:Difficult To UseThe Problem:Difficult To Use• GPUs designed for and driven by video games

– Programming model is unusual & tied to computer graphics– Programming environment is tightly constrained

• Underlying architectures are:– Inherently parallel– Rapidly evolving (even in basic feature set!)– Largely secret

• Can’t simply “port” code written for the CPU!

High-level Shading LanguagesHighHigh--level Shading Languageslevel Shading Languages

• Cg, HLSL, & GLSlang– Cg:

• http://www.nvidia.com/cg– HLSL:

• http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/highlevellanguageshaders.asp

– CUDA: • http://www.nvidia.com/object/cuda_home.html

float3 cPlastic = Cd * (cAmbi + cDiff) + Cs*cSpec

….MULR R0.xyz, R0.xxx, R4.xyxx;MOVR R5.xyz, -R0.xyzz;DP3R R3.x, R0.xyzz, R3.xyzz;…

CGC & FXCCGC & FXC

• HLSL and Cg are semantically 99% identical– Same language, two names

• Command line compilers– Microsoft’s FXC.exe

• Included in DX9 SDK• Compiles to DirectX vertex and pixel shader assembly only• Generates ATI preferred assemblyfxc /Tps_2_0 myshader.cg

– NVIDIA’s CGC.exe• Compiles to everything• Generates NV preferred assembly by defaultcgc -profile ps_2_0 myshader.hlsl

– Can generate very different assembly!• Typically FXC does better for ATI, CGC for NVIDIA

BabelshaderBabelshader

• Designed to convert between DirectX HLSL output, pixelshader 2.0 and OpenGL's low level shading languages

• Allows OpenGL programs to use Microsoft's stand alone HLSL compiler, fxc.exe to compile programs into ARB or fp30 assembly.

• Enables fair benchmarking competition between the HLSL compiler and the Cg compiler on the same platform with the same demo and driver.

http://graphics.stanford.edu/~danielrh/babelshader.html

Example Conversion Between Ps2.0 and ARB

GPGPU LanguagesGPGPU Languages• Why do we want them?

– Make programming GPUs easier!• Don’t need to know OpenGL, DirectX, or ATI/NV extensions• Simplify common operations• Focus on the algorithm, not on the implementation

• Sh: University of Waterloo– http://libsh.sourceforge.net– http://www.cgl.uwaterloo.ca

• Brook: Stanford University– http://brook.sourceforge.net– http://graphics.stanford.edu

• CUDA– Nvidia

Page 3: 17 GPU computation

Sh FeaturesSh Features• Implemented as C++ library

– Use C++ modularity, type, and scope constructs

– Use C++ to metaprogram shaders and kernels

– Use C++ to sequence stream operations• Operations can run on

– GPU in JIT compiled mode– CPU in immediate mode– CPU in JIT compiled mode

• Can be used – To define shaders– To define stream kernels

• No glue code– To set up a parameter, just declare it and

use it– To set up a texture, just declare it and use it

• Memory management– Automatically uses pbuffers and/or

uberbuffers– Textures are shadowed and act like

arrays on both the CPU and GPU– Textures can encapsulate

interpretation code– Programs can encapsulate texture

data• Program manipulation

– Introspection– Uniform/varying conversion– Program specialization– Program composition– Program concatenation– Interface adaptation

• Free and Open Source

http://libsh.sourceforge.net

Brook: general purpose streaming languageBrook: general purpose streaming language• stream programming model

– enforce data parallel computing• streams

– encourage arithmetic intensity• kernels

• C with stream extensions

• GPU = streaming coprocessor

Linear Algebra on Linear Algebra on GPUsGPUs

Jens Krüger Technische Universität München

Why LA on GPUs?Why LA on GPUs?

1. Why should we careabout Linear Algebraat all?

Use LA to solve PDEssolving PDEs can increase realism forVR, Education, Simulations, Games, …

Why Linear Algebra on GPUs?Why Linear Algebra on GPUs?

2. … and why do it on the GPU?

a) The GPU is a fast streaming processor• LA operations are easily “streamable”

b) The result of the computation is already on the GPU and ready for display

RepresentationRepresentation

Vector representation– 2D textures best we can do

• Per-fragment vs. per-vertex operations• High texture memory bandwidth• Read-write access, dependent fetches

1 N

1

N

Page 4: 17 GPU computation

Representation (cont.)Representation (cont.)

Dense Matrix representation– treat a dense matrix as a set of column vectors– again, store these vectors as 2D textures

Matrix

N

N

i N Vectors

......N

1 i N

N 2D-Textures

... ...1 i N

OperationsOperations

• Vector-Vector Operations– Reduced to 2D texture operations– Coded in vertex/fragment shaders

return tex0 OP tex1

Operations (cont.)Operations (cont.)

• Vector-Vector Operations– Reduce operation for scalar products

Reduce m x n regionin fragment shader

2 passnd

......

st1 pass

...

original Texture

...

High-Level AlgorithmsHigh-Level Algorithms

• Conjugate Gradient– Solve large linear systems

• Navier-Stokes– Fluid simulations

Simulating the worldSimulating the world

• Simulate a wide variety of phenomena on GPUs– Anything we can describe with

discrete PDEs or approximations of PDEs– Examples: boiling, reaction-diffusion,

fluids, and clouds

Example: BoilingExample: Boiling

• State = Temperature

• Three operations:– Diffusion, buoyancy, & latent heat– 7 passes in 2D, 9 per 3D slice (GeForce 4)

• Based on [Yanagita 1992]

Page 5: 17 GPU computation

Reaction-DiffusionReaction-Diffusion Fluid SimulationFluid Simulation

Cloud DynamicsCloud Dynamics

Textures: C++ Arrays (CPU)Textures: C++ Arrays (CPU)

• Creating arrays on the CPU

• One option to hold data for GPGPU calculations

Another option for rendering is to draw geometryand use that as the input data to the textures used morefor advanced rendering effects

Textures: OpenGLTextures: OpenGL• This gets complicated fast…

• Look at glTexImage2D– Texture_target (next slide)– 0: not to use any mipmap levels for this texture – Internal format (next slide)– texSize, texSize (width and height of the texture)– 0: turns off borders for our texture – Texture_format: chooses the number of channels – GL_Float : Float texture (nothing to do with the precision of the values )

– 0 or NULL : We do not want to specify texture data right now…

Page 6: 17 GPU computation

Textures: FormatsTextures: Formats• On the GPU, we use floating point textures to store the data • a variety of different so-called texture targets available

• Internal texture format. GPUs allow for the simultaneous processing of scalars, tupels, tripels or four-tupels of data

• Precision of data: GL_FLOAT_R32_NV, GL_R, GL_R16, GL_RGB, GL_RGB16, GL_RGBA …

– ATI warning … here is where you need to specify ATI extensions

Mapping texturesMapping textures• Later we update our data stored in textures by a rendering operation.

• To be able to control exactly which data elements we compute or access from texture memory, we will need to choose a special projection that maps from the 3D world (world or model coordinate space) to the 2D screen (screen or display coordinate space), and additionally a 1:1 mapping between pixels (which we want to render to) and texels (which we access data from).

• The key to success here is to choose an orthogonal projection and a proper viewport that will enable a one to one mapping between geometry coordinates

• (add this to your reshape, init, and initFBO methods)

Using Textures as Render TargetsUsing Textures as Render Targets• the traditional end point of every rendering operation is the frame buffer, a special chunk

of graphics memory from which the image that appears on the display is read

• Problem! : the data will always be clamped to the range of [0/255; 255/255] once it reaches the framebuffer. What to do?

• cumbersome arithmetic that maps the sign-mantissa-exponent data format of an IEEE 32-bit floating point value into the four 8-bit channels ???

• OpenGL extension called EXT_framebuffer_object allows us to use an offscreen bufferas the target for rendering operations such as our vector calculations, providing full precision and removing all the unwanted clamping issues. The commonly used abbreviation is FBO, short for framebuffer object.

Frame Buffer Objects: (FBO)Frame Buffer Objects: (FBO)

• The framebuffer object extension provides a very narrow interface to render to a texture. To use a texture as render target, we have to attach the texture to the FBO

• drawback is: Textures are either read-only or write-only (important later)

To use this extension and to turn off the traditional framebuffer and use an offscreen buffer (surface) for our calculations, a few lines of code suffice. Note that binding FBO number 0 will restore the window-system specific framebuffer at any time.

Transferring data from CPU arrays to GPU texturesTransferring data from CPU arrays to GPU textures

• To transfer data (like the two vectors dataX and dataY we created previously) to a texture, we have to bind the texture toa texture target and schedule the data for transfer with an OpenGL (note: NVIDIA Code)

• Again not only method, if you rather do rendering rather then GPGPU computations draw geometry to the buffer directly as follows:

Transferring data from GPU textures to CPU arraysTransferring data from GPU textures to CPU arrays

• Many times you want the actual values that you calculated back, there are 2 ways to do this

Page 7: 17 GPU computation

Transferring data from GPU textures to QUADSTransferring data from GPU textures to QUADS

• Other time you really just want to see the mess you created on the screen

• To do this you have to render a QUAD

Preparing the computational kernel setting up input textures/arraysPreparing the computational kernel setting up input textures/arrays

Setting output arrays / texturesSetting output arrays / textures

Defining the output array (the left side of the equation) is essentially the same operation like the one we discussed to transfer data to a texture already attached to our FBO. Simple pointer manipulationby means of GL calls is all we need. In other words, we simply redirect the output: If we did not do so yet, we attach the target texture to our FBO and use standard GL calls to use it as the render target:

Performing a computationPerforming a computation

• Let us briefly recall what we did so far.

• We enabled a 1:1 mapping between the target pixels, the texture coordinates and the geometry we are about to draw.

• We also prepared a fragment shader we want to execute for each fragment.

• All that remains to be done is: Render a "suitable geometry" that ensures that our fragment shader is executed for each data element we stored in the target texture.

• In other words, we make sure that each data item is transformed uniquely into a fragment.

• Given our projection and viewport settings, this is embarrassingly easy: All we need is a filled quad

Multiple rendering passesMultiple rendering passes

• In a proper application, the result is typically used as input for a subsequent computation.

• On the GPU, this means we perform another rendering passand bind different input and output textures, eventually a different kernel etc.

• The most important ingredient for this kind of multipassrendering is the ping pong technique.

The ping pong techniqueThe ping pong technique• Ping pong is a technique to alternately use the output of a given rendering pass as

input in the next one.

• Lets look at this operation: (y_new = y_old + alpha * x)

• this means that we swap the role of the two textures y_new and y_old, since we do not need the values in y_old any more once the new values have been computed.

• There are three possible ways to implement this kind of data reuse (take a look at Simon Green's FBO slides for additional material on this): http://download.nvidia.com/developer/presentations/2005/GDC/OpenGL_Day/OpenGL_FrameBuffer_Object.pdf

Page 8: 17 GPU computation

The ping pong techniqueThe ping pong technique

• During the computation, all we need to do now is to pass the correct value from these two tupels to the corresponding OpenGL calls, and to swap the two index variables after each pass:

Isn’t that crazy?????Isn’t that crazy?????