



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Techniques for performing matrix operations, specifically inner products and matrix-matrix multiplications, on gpus. Two methods: one using a small amount of memory and more passes, and another using more working memory and fewer passes. The document also mentions the use of 2d textures to store matrices and techniques for optimizing matrix multiplication.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




There are three basic matrix operations that would be part of any GPU matrix toolkit:
A number of problems can be solved once one has these basic operations (especially in physical simulations). This is one of the most studied problems on the GPU.
Consider the inner product c = a · b, which we rewrite as c =
∑n i=1 aibi.
Each vector is stored in a 1D texture. In the ith^ rendering pass, we render a single point at coordinates (0,0), having a single texture coordinate i. The fragment program uses i to index into the two textures and returns the value s + ai + bi, where s is the running sum maintained over the previous i − 1
passes. Note that since we cannot read and write the location where s is stored in a single pass, we use a ping-pong trick to maintain s. This procedure takes n passes, and requires only a fixed number of texture locations (excluding the storage for a and b).
The second technique uses more working memory (n units), but requires fewer passes. We write a and b as 2D textures (2D textures allow for more storage, since the dimension of a texture is typically bounded, and are better optimized by the rasterizer). We now multiply the contents of the textures, storing the result in a third texture c. This can be done with a simple fragment program that takes the fragment coordinates and looks up the a and b textures, returning their product. We render a single quad in order to activate the fragment program.
c =
a 0 b 0 a 1 b 1 ... ... aibi ... ... ... anbn
Finally, all the numbers in c must be summed together. This can be done in log n passes, using a standard reduce operation. This procedure takes only log n passes, but requires 3n units of texture memory.
3 Matrix-Matrix operations
We can store matrices as 2D textures. Addition is trivial.
”Fast matrix multiplies using graphics hardware” by Larsen and McAllister”[2] Express multiplication of two matrices as dot product of vectors of matrix rows and columns. That is to compute some cell cij of matrix C, we take the dot product of row i of matrix A with column j of matrix B:
output ← c + ax 2 ∗ b 2 y pass k output ← c + axk ∗ bky
Cxy = Σnk=1axkbky 1)uses n passes 2)space N = n^2
”Dense Matrix Multiplication” by ´Ad´am Morav´anszky To make it faster: Instead of making one computation per pass, compute multiple additions per pass in fragment program:
Pass 1 becomes: output ← ax 1 b 1 y + ax 2 b 2 y + ax 3 b 3 y + ax 4 b 4 y Must consider that there is a tradeoff between the length of fragment program vs. the number of passes.
”Cache and Bandwidth Aware Matrix Multiplication on the GPU”, by Hall, Carr and Hart”[1].
[ A 11 A 12 A 21 A 22
We have been using only the red component, propose storing across all colors:
divide into 2:
ArrbbBrgrg + AggaaBbaba Basically a swizzle operation to speed things up. Closing thought: The basic idea here is using the inner product calculation in parellel.
References
[1] Hall, J., Carr, N., and Hart, J. C. Cache and bandwidth aware matrix multiplication on the gpu. Tech. Rep. UIUCDCS-R-2003-2328-1, UIUC, 2003.
[2] Larsen, E., and McAllister, D. Fast matrix multiplies using graph- ics hardware. In Supercomputing ’01: Proceedings of the 2001 ACM/IEEE conference on Supercomputing (CDROM) (2001), ACM Press, pp. 55–55.