27
Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

Embed Size (px)

Citation preview

Page 1: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

Texture Memory

-in CUDA Perspective

TEXTURE MEMORY IN - IN CUDA PERSPECTIVE

VINAY MANCHIRAJU

Page 2: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

TEXTURE MEMORY

• Read only memory used by programs in CUDA• Used in General Purpose Computing for

Accuracy and Efficiency.• Designed for DirectX and OpenGL rendering

Pipelines.

Page 3: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

• Can cache non consecutive memory locations unlike CPU caching schemes.

• Designed to accelerate access patterns.

WHY USE TEXTURES?

Page 4: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

• Texture memory is cached on a chip.• Provides higher effective bandwidth.• Reduces memory requests to the off-chip

DRAM.• Improves performance of graphics application

where memory access patterns exhibit great deal of spatial locality.

Page 5: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

PARALLELIZING PHYSICAL SIMULATIONS

•Results are more accurate with reduced computational complexity and lesser time to solve.•Textures have a significant role in simulation problems.

Page 6: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

HEAT SIMULATION EXAMPLE

• A rectangular room consisting of a grid.• Inside the grid various heaters with fixed temperatures are scattered in the cell .

Page 7: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

FLOW OF HEAT

Warmer cells tend to cool as the heat is dissipated to cooler regions and vice versa

Page 8: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

AS A FUNCTION OF HEAT LOSS/GAIN

• Imagine that there are 4 neighbors for a given cell.

• K -> Rate of heat flow from one cell to another.• A large value of k will drive the system to a

constant temperature quickly, while a small value will allow the solution to retain large temperature gradients longer.

Page 9: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

THREE STEPS TO COMPUTE TEMPERATURE UPDATES

copy_const_kernel()Copy Heater temperatures to respective gridsEnforce a restriction that temperatures of the cells

with heaters are constant. blend_kernel():

Output temperatures are calculated based on the input temperatures of the grid using the equation.

Swap the input and output temperatures for the calculation in next step.

Page 10: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

copy_const_kernel()

Convert threadIdx and blockIdx into an x and y coordinate.Compute a linear offset into constant and input buffers.If the cell in the constant grid is nonzero copy of the heater temperature in cptr[] to the input grid in iptr[] .

Page 11: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

blend_kernel()

• 1 thread for every cell.• Offsets of the neighbors in all the 4 directions are

computed to read the temperatures of those cells.• Each thread reads its cell’s temperature, the

temperatures of its neighboring cells, perform the previous update computation, and then update its temperature with the new value.

• Calculate updated temperature adding old temperatures and scaled differences and the neighboring cell temperatures.

Page 12: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

anim_kernel()

We use DataBlock contains the constant buffer of heaters and the updated temperatures.

Arguments: pointer to a data block, number of ticks of animation that have elapsed.(not used)

We use a 16 x 16 grid and blocks of 256 threads.

Page 13: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

anim_kernel()

After the iteration we swap the input and output buffers to obtain the final temperatures.

The temperatures are converted into colors and the bitmap image is transferred from GPU to CPU.

The Program.

Page 14: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

USING TEXTURES

• Declare inputs as texture references.• Use references to floating point textures .

• Allocate GPU memory for these textures and then bind the references using cudaBindTexture()

Page 15: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

cudaBindTexture()

• Use specified buffer as a texture and texture reference as texture name.

• Please check cudaBindTexture()

Page 16: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU
Page 17: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

tex1Dfetch()

• A Compiler intrinsic function.• Used to pass texIn, texOut, texConstSrc

textures to the blend method.• This would help us to fetch the texture value

into a float point variable.

Page 18: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU
Page 19: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

copy_const_kernel()

Page 20: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

cudaUnbindTexture()

Page 21: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

USING 2D-TEXTURES

• Reference Declaration:

• Instead of using offset to calculate left, right, top and bottom we directly use x,y to access the texture.

Page 22: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

USING 2-D TEXTURES

• Bounds overflow over the grid is taken care of.• If one of x or y is less than zero, tex2D() will

return the value at zero. • If one of these values is greater than the

width, tex2D() will return the value at width 1.

Page 23: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

tex2D

Page 24: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

CudaBindTexture2d()

Page 25: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

Tradeoffs 1D vs 2D

• So from a performance standpoint, the decision between one- and two-dimensional textures is likely to be inconsequential.

• For our particular application, the code is a little simpler when using two- dimensional textures because we happen to be simulating a two-dimensional domain. But in general, since this is not always the case, we suggest you make the decision between one- and two-dimensional textures on a case-by-case basis.

Page 27: Texture Memory -in CUDA Perspective TEXTURE MEMORY IN - IN CUDA PERSPECTIVE VINAY MANCHIRAJU

THANK YOU…