CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live...

12
CIS 330 C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS)

Transcript of CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live...

Page 1: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

CIS 330 C++ and UnixLecture 8

Memory and Pointers II (with Graphs and BFS)

Page 2: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

Live Coding � Pointers and memory

Page 3: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

Matrix Multiplication

Page 4: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

Matrix Multiplication ci,n =∑"#$% &', ) ∗ +), ,

M

M

NN

c3,3 = a3,1 * b1,3 + a3,2 * b2,3

Page 5: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

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

Page 6: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

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

Page 7: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

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

Page 8: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

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

Page 9: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

Example Sparse Matrices

sparsity = 0.33%

Page 10: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

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

Page 11: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

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

Page 12: CIS 330 C++ and Unix · C++ and Unix Lecture 8 Memory and Pointers II (with Graphs and BFS) Live Coding Pointers and memory. Matrix Multiplication. ... 04_Debugging_Graphs_BFS Created

SpMV� The concept is simple when you understand it, but initially difficult

to grasp the indexing

� Questions?