CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live...
Transcript of CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live...
CIS 330 C++ and UnixLecture 8
Memory and Pointers II (with Graphs and BFS)
Live Coding � Pointers and memory
Matrix Multiplication
Matrix Multiplication ci,n =∑"#$% &', ) ∗ +), ,
M
M
NN
c3,3 = a3,1 * b1,3 + a3,2 * b2,3
Example
� Sparse matrix – matrix with only a small number of values that are not zeros
� SpMV – Sparse Matrix Vector Multiply� Systems of equations � Graph processing
SpMV0 1 2 0 0
3 0 1 0 0
0 0 0 0 0
1 1 0 0 6
0 0 1 9 0
1
3
2
5
1
7
5
0
10
47
SpMV0 1 2 0 0
3 0 1 0 0
0 0 0 0 0
1 1 0 0 6
0 0 1 9 0
1
3
2
5
1
7
5
0
10
47
SpMV in COO
0 1 2 0 0
3 0 1 0 0
0 0 0 0 0
1 1 0 0 6
0 0 1 9 0
row col val
1 2 1
1 3 2
2 1 3
2 3 1
4 1 1
4 2 1
4 5 6
5 3 1
5 4 9
Example Sparse Matrices
sparsity = 0.33%
Compressed Sparse Row
row col val
1 2 1
1 3 2
2 1 3
2 3 1
4 1 1
4 2 1
4 5 6
5 3 1
5 4 9
2 3 1 3 1 2 5 3 4
1 2 3 1 1 1 6 1 9
col
val
1 3 5 5 8 10 row_ptr
Algorithm
� Input: A ∈ℝI×J, x ∈ℝJ
� Output: Ax = y ∈ℝI
� for i = 1 to I /* number of rows */
� get begin and end index into col and val for row I
� for j = begin to end
� y[i] += val[j] * x[ col[j] ]
� end
� end
SpMV� The concept is simple when you understand it, but initially difficult
to grasp the indexing
� Questions?