¨ thesis March 11, 2013 15:54 Page 1 ©
National Technical University of Athens School of Electrical and Computer Engineering Division of Computer Science
Optimizing the Sparse Matrix-Vector Multiplication Kernel for Modern Multicore Computer Architectures
¨ ¨ © © Ph.D. esis
Vasileios K. Karakasis Electrical and Computer Engineer, Dipl.-Ing.
Athens, Greece December, 2012
¨ © ¨ thesis March 11, 2013 15:54 Page 2 ©
¨ ¨ © ©
¨ © ¨ thesis March 11, 2013 15:54 Page 3 ©
National Technical University of Athens School of Electrical and Computer Engineering Division of Computer Science
Optimizing the Sparse Matrix-Vector Multiplication Kernel for Modern Multicore Computer Architectures
Ph.D. esis
Vasileios K. Karakasis Electrical and Computer Engineer, Dipl.-Ing.
Advisory Committee: Nectarios Koziris Panayiotis Tsanakas ¨ Andreas Stafylopatis ¨ © ©
Approved by the examining committee on December 19, 2012.
Nectarios Koziris Panayiotis Tsanakas Andreas Stafylopatis Associate Prof., NTUA Prof., NTUA Prof., NTUA
Andreas Boudouvis Giorgos Stamou Dimitrios Soudris Prof., NTUA Lecturer, NTUA Assistant Prof., NTUA
Ioannis Cotronis Assistant Prof., UOA
Athens, Greece December, 2012
¨ © ¨ thesis March 11, 2013 15:54 Page 4 ©
Vasileios K. Karakasis Ph.D., National Technical University of Athens, Greece.
¨ ¨ © ©
Copyright © Vasileios K. Karakasis, 2012. Με επιφύλαξη παντός δικαιώματος. All rights reserved
Απαγορεύεται η αντιγραφή, αποθήκευση και διανομή της παρούσας εργασίας, εξ ολοκλήρου ή τμήματος αυτής, για εμπορικό σκοπό. Επιτρέπεται η ανατύπωση, αποθήκευση και διανομή για σκοπό μη κερδοσκοπικό, εκπαιδευτικής ή ερευνητικής φύσης, υπό την προϋπόθεση να αναφέρε- ται η πηγή προέλευσης και να διατηρείται το παρόν μήνυμα. Ερωτήματα που αφορούν τη χρήση της εργασίας για κερδοσκοπικό σκοπό πρέπει να απευθύνονται προς τον συγγραφέα.
Η έγκριση της διδακτορικής διατριβής από την Ανώτατη Σχολή των Ηλεκτρολόγων Μηχανικών και Μηχανικών Υπολογιστών του Ε. Μ. Πολυτεχνείου δεν υποδηλώνει αποδοχή των γνωμών του συγγραφέα (Ν. 5343/1932, άρθρο 202).
¨ © ¨ thesis March 11, 2013 15:54 Page 1 ©
¨ Στους γονείς μου, ¨ © © Κωνσταντίνο και Ειρήνη, και στα αδέλφια μου, Μάριο, Όλγα, Αλέξανδρο
¨ © ¨ thesis March 11, 2013 15:54 Page 2 ©
¨ ¨ © ©
¨ © ¨ thesis March 11, 2013 15:54 Page i ©
Abstract
is thesis focuses on the optimization of the Sparse Matrix-Vector Multipli- cation kernel (SpMV) for modern multicore architectures. We perform an in-depth performance analysis of the kernel and identi ed its major perfor- mance bottlenecks. is allows us to propose an advanced storage format for sparse matrices, the Compressed Sparse eXtended (CSX) format, which tar- gets speci cally the minimization of the memory footprint of the sparse ma- trix. is format provides signi cant improvements in the performance of the SpMV kernel in a variety of matrices and multicore architectures, main- taining a considerable performance stability. Finally, we investigate the per- formance of the SpMV kernel from an energy-efficiency perspective, in order to identify the execution con gurations that lead to optimal performance- energy tradeoffs.
Keywords: high performance computing; scienti c applications; sparse matrix-vector multiplication; multicore; data compression; energy-efficiency; SpMV; CSX; HPC
¨ ¨ © ©
¨ © ¨ thesis March 11, 2013 15:54 Page ii ©
¨ ¨ © ©
¨ © ¨ thesis March 11, 2013 15:54 Page i ©
Contents
Contents i
List of Figures v
List of Tables ix
List of Algorithms xi ¨ ¨ © Αντί Προλόγου xiii © 1 Introduction 1 1.1 Sparse linear systems ...... 1 1.2 e computational aspect of iterative methods ...... 3 1.3 Challenges of multicore architectures ...... 5 1.3.1 Energy consumption considerations ...... 7 1.3.2 e algorithmic nature of the SpMV kernel ...... 8 1.3.3 e energy efficiency of the SpMV kernel ...... 9 1.4 Contribution of this thesis ...... 10 1.4.1 In-depth performance analysis and prediction models 10 1.4.2 e CSX storage format for sparse matrices ...... 11 1.4.3 Toward an energy-efficient SpMV implementation . . 13 1.5 Outline ...... 14
2 Storing Sparse Matrices 17 2.1 Conventional storage formats ...... 17 2.2 Exploiting the density structure of the matrix ...... 20 2.2.1 Fixed size blocking ...... 21 2.2.2 Variable size blocking ...... 23 2.2.3 Other approches ...... 27 2.3 Explicit compression of the matrix indices ...... 29
i
¨ © ¨ thesis March 11, 2013 15:54 Page ii ©
Contents
2.4 Exploiting symmetry in sparse matrices ...... 31
3 e Performance of the Sparse Matrix-Vector Kernel 35 3.1 An algorithmic view ...... 35 3.2 Experimental preliminaries ...... 38 3.2.1 Matrix suite ...... 39 3.2.2 Hardware platforms ...... 39 3.2.3 Measurements procedures and policies ...... 43 3.3 A quantitative evaluation ...... 45 3.3.1 Single-threaded performance ...... 45 3.3.2 Multithreaded performance ...... 47 3.4 Related work ...... 60 3.5 Summary ...... 60
4 Optimization opportunities of blocking 63 4.1 e effect of compression ...... 63 4.2 e effect of the computations ...... 67 4.2.1 Vectorization and the block shape ...... 67 4.3 Predicting the optimal block size ...... 71 ¨ 4.3.1 e M model ...... 72 ¨ © © 4.3.2 e S model ...... 73 4.3.3 e MC model ...... 74 4.3.4 e O model ...... 74 4.3.5 Assessing the accuracy of the models ...... 75 4.3.6 Extensions ...... 79 4.4 Summary ...... 81
5 e Compressed Sparse eXtended Format 83 5.1 e need for an integrated storage format ...... 84 5.2 CSX data structures ...... 86 5.3 Detection and encoding of substructures ...... 88 5.3.1 Mining the matrix for substructures ...... 88 5.3.2 Selecting substructures for nal encoding ...... 93 5.3.3 Building the CSX data structures ...... 94 5.4 Generating the SpMV code ...... 95 5.5 Tackling the preprocessing cost ...... 99 5.6 Porting to NUMA architectures ...... 100 5.6.1 Optimizing the computations ...... 101 5.7 Evaluating the performance of CSX ...... 102 5.7.1 CSX compression potential ...... 102 5.7.2 CSX performance ...... 104
ii
¨ © ¨ thesis March 11, 2013 15:54 Page iii ©
Contents
5.7.3 CSX preprocessing cost ...... 111 5.8 Integrating CSX into multiphysics simulation soware . . . . . 113 5.9 Summary ...... 116
6 Exploiting symmetry in sparse matrices 119 6.1 e symmetric SpMV kernel ...... 119 6.2 Minimizing the reduction cost ...... 122 6.2.1 Effective ranges of local vectors ...... 122 6.2.2 Local vectors indexing ...... 123 6.2.3 Alternative methods ...... 126 6.3 CSX for symmetric matrices ...... 127 6.4 Performance evaluation ...... 129 6.4.1 Local vectors methods ...... 129 6.4.2 Symmetric CSX ...... 131 6.4.3 Reduced bandwidth matrices ...... 132 6.4.4 Impact on the CG iterative method ...... 134 6.5 Summary ...... 136
¨ 7 Energy-efficiency considerations 139 ¨ © 7.1 Fundamentals of processor power dissipation ...... 139 © 7.1.1 Sources of power dissipation ...... 139 7.1.2 Energy-Delay products ...... 142 7.1.3 Energy-efficiency from a soware perspective . . . . . 142 7.2 Performance-Energy tradeoffs in the SpMV kernel ...... 144 7.2.1 Experimental setup ...... 145 7.2.2 Characterizing the tradeoffs ...... 146 7.3 Predicting the optimal execution con gurations ...... 150 7.3.1 Clustering the matrices ...... 151 7.3.2 Constructing the cluster Pareto front ...... 152 7.3.3 Classi cation and testing ...... 153 7.3.4 Limitations ...... 157 7.4 Open issues ...... 158
8 Conclusions 159 8.1 SpMV: Victim of the memory wall ...... 159 8.2 CSX: A viable approach to a high performance SpMV . . . . . 160 8.3 Toward an energy-efficient SpMV ...... 162 8.4 Future research directions ...... 162
List of publications 165
iii
¨ © ¨ thesis March 11, 2013 15:54 Page iv ©
Contents
Bibliography 167
Index 179
Short biography 183
¨ ¨ © ©
iv
¨ © ¨ thesis March 11, 2013 15:54 Page v ©
List of Figures
1.1 Execution time breakdown for the non-preconditioned CG itera- tive method for different problem categories...... 5 1.2 e two current trends in modern multicore architectures: sym- metric shared memory and non-uniform memory access (NUMA) architectures...... 6 1.3 Demonstration of the SpMV kernel speedup in relation to the mem- ory bandwidth consumption in a two-way quad-core symmetric ¨ shared memory system...... 9 ¨ © © 2.1 e Coordinate sparse matrix storage format...... 18 2.2 e Compressed Sparse Row storage format...... 19 2.3 e Blocked Compressed Sparse Row storage format...... 21 2.4 e Row Segmented Diagonal storage format...... 23 2.5 e Variable Block Length storage format...... 24 2.6 e Variable Block Row storage format...... 26 2.7 e Ellpack-Itpack storage format, suitable for vector processors and modern GPU architectures...... 29 2.8 Run-length encoding of the matrix column indices...... 30 2.9 e Symmetric Sparse Skyline format...... 31 2.10 e RAW dependency on the output vector in a parallel execution of the symmetric SpMV kernel...... 32
3.1 e block diagrams of the multiprocessor systems used for the ex- perimental evaluations in this thesis (continues at page 42). . . . . 41 3.1 e block diagrams of the multiprocessor systems used for the ex- perimental evaluations in this thesis...... 42 3.2 Performance overheads of the SpMV kernel...... 48 3.2 Performance overheads of the SpMV kernel...... 49 3.3 Speedup of the SpMV kernel in SMP systems...... 50
v
¨ © ¨ thesis March 11, 2013 15:54 Page vi ©
List of Figures
3.4 SpMV speedup in Dunnington using the ‘share-all’ core- lling pol- icy...... 51 3.5 e contention in the memory bus of SMP architectures...... 52 3.6 Performance of the SpMV kernel in relation to the arithmetic in- tensity ( op:byte ratio)...... 52 3.7 Load imbalance in the SpMV kernel...... 53 3.8 Variation of op:byte ratio across the matrix...... 54 3.9 e bene t of matrix reordering...... 55 3.10 e speedup of the SpMV kernel in NUMA architectures...... 56 3.11 Saturation of the interconnection link in NUMA architectures. . . 57 3.12 Transparent data placement in NUMA architectures...... 59 3.13 Effect of sharing the input vector in NUMA architectures...... 59
4.1 Correlation of the compression ratio and the performance improve- ment...... 65 4.2 e speedup achieved by a variety of blocking storage formats. . . 66 4.3 e effect of compression...... 67 4.4 e effect of SpMV vectorization in SMP platforms...... 71 4.5 e effect of SpMV vectorization in NUMA platforms...... 72 ¨ ¨ 4.6 Selection accuracy of the considered performance models. . . . . 77 © © 4.7 Execution time prediction accuracy of the considered performance models...... 80
5.1 e advantage of detecting multiple substructures inside a sparse matrix...... 85 5.2 e ctl byte-array used by CSX to encode the location informa- tion of the non-zero elements of a sparse matrix...... 87 5.3 CSX matrix encoding...... 87 5.4 e variable-size integer encoding employed by CSX...... 88 5.5 Example of the run-length encoding of the column indices. . . . . 89 5.6 Detection of 2-D substructures in CSX...... 92 5.7 e substructures detected and encoded by CSX in a diverse set of sparse matrices...... 93 5.8 e just-in-time compilation framework of CSX...... 97 5.9 e compression potential of CSX...... 103 5.10 CSX speedup in SMP systems...... 105 5.11 CSX speedup in SMP systems using a ‘share-nothing’ core- lling policy...... 106 5.12 CSX speedup in NUMA architectures...... 107 5.13 Per-matrix performance in Gainestown...... 108 5.14 Performance of CSX and CSR in RCM reordered matrices. . . . . 111
vi
¨ © ¨ thesis March 11, 2013 15:54 Page vii ©
List of Figures
5.15 e preprocessing cost of CSX...... 112 5.16 e updated matrix-vector multiplication interface of Elmer. . . . 114 5.17 Execution time breakdown of the CSX library...... 115 5.18 Speedup of the Elmer multiphysics simulation soware...... 116
6.1 e RAW dependency on the output vector in a parallel execution of the symmetric SpMV kernel...... 120 6.2 Speedup of the naive symmetric SpMV implementation...... 122 6.3 Local vector methods for the reduction phase of the symmetric SpMV kernel...... 124 6.4 e density of the effective regions of local vectors...... 125 6.5 e workload overhead of the reduction phase...... 126 6.6 Encoded substructures by CSX-Sym in an 8 × 8 sparse matrix. . . 128 6.7 Symmetric SpMV speedup with different local vectors reduction methods...... 130 6.8 Symmetric SpMV execution time breakdown...... 131 6.9 Symmetric SpMV speedup with the CSX-Sym format...... 132 6.10 Per-matrix performance for the CSX-Sym format...... 132 6.11 Per-matrix performance on reordered symmetric matrices. . . . . 134 ¨ 6.12 CG execution time breakdown...... 136 ¨ © © 7.1 Performance-energy tradeoffs of the SpMV kernel...... 148 7.2 e optimal performance-energy tradeoffs for two sparse matrices. 151 7.3 Matrix clustering...... 153 7.4 Construction of the cluster Pareto front...... 154 7.5 e three rst Pareto fronts of the matrix thermal2...... 155 7.6 Optimal execution con gurations predictions...... 156
vii
¨ © ¨ thesis March 11, 2013 15:54 Page viii ©
¨ ¨ © ©
¨ © ¨ thesis March 11, 2013 15:54 Page ix ©
List of Tables
3.1 e matrix suite used for experimental evaluation...... 40 3.2 Technical characteristics of the hardware platforms used the ex- perimental evaluations...... 43 3.3 Microbenchmarks for assessing the performance problems of the SpMV kernel...... 46
4.1 Selection accuracy of the considered performance models. . . . . 76 ¨ 4.2 Execution time prediction accuracy of the considered SpMV per- ¨ © formance models...... 78 © 5.1 Key features of the most important CSR alternatives...... 84 5.2 e coordinate transformations applied by CSX...... 91 5.3 CSX performance stability...... 109 5.4 DMV vs. CSX performance...... 110 5.5 e benchmark problems used for the evaluation of the CSX inte- gration into Elmer...... 114
6.1 Compression ratio of the symmetric CSX...... 128 6.2 Symmetric SpMV performance improvement due to matrix re- ordering...... 133
7.1 Performance monitoring events used for the prediction of power dissipation...... 146 7.2 Cross-validation results...... 157 7.3 Energy-delay prediction accuracy...... 157
ix
¨ © ¨ thesis March 11, 2013 15:54 Page x ©
¨ ¨ © ©
¨ © ¨ thesis March 11, 2013 15:54 Page xi ©
List of Algorithms
1.1 High-level representation of the matrix-vector multiplication. . . . 8 2.1 Implementation of the SpMV kernel using the COO format. . . . 18 2.2 Implementation of the SpMV kernel using the CSR format. . . . . 20 2.3 Implementation of the SpMV kernel using the BCSR format. . . . 22 2.4 Implementation of the SpMV kernel using the VBL format. . . . . 25 2.5 Implementation of the SpMV kernel using the SSS symmetric stor- age format...... 32 3.1 Implementation of the SpMV kernel using the CSR format (see ¨ Chapter 2, Section 2.1 for a detailed description)...... 36 ¨ © 4.1 SIMD implementation of the BCSR kernel for 1 × 2 blocks. . . . . 69 © 4.2 SIMD implementation of the BCSR kernel for 2 × 1 blocks. . . . . 70 5.1 Run-length encoding of the column indices...... 89 5.2 Detecting substructures in CSX’s internal representation...... 90 5.3 Detection, selection and encoding of the substructures in CSX. . . 94 5.4 Construction of the CSX data structures...... 96 5.5 Helper procedure for emitting delta units...... 97 5.6 e SpMV kernel template used the CSX storage format...... 98 6.1 Parallel implementation of the symmetric SpMV kernel using the SSS storage format...... 121 6.2 e non-preconditioned Conjugate Gradient algorithm...... 135
xi
¨ © ¨ thesis March 11, 2013 15:54 Page xii ©
¨ ¨ © ©
¨ © ¨ thesis 11 Μ 2013 15:54 Page xiii ©
Αντί Προλόγου
Πολλές οι σκέψεις και τα συναισθήματα που γεμίζουν το μυαλό μου φτάνοντας στο τέλος του «αγώνα» που λέγεται διδακτορικό. Ανακούφιση και ικανοποίη- ση για την τελείωσή του, προσδοκία για το μέλλον, νοσταλγία για τις όμορφες και δύσκολες στιγμές του. Κάθε τι που με τον ένα ή τον άλλο τρόπο σημα- δεύει την πορεία σου είναι συνήθως άρρηκτα συνδεδεμένο με τον ανθρώπινο παράγοντα, άμεσα ή έμμεσα, διακριτικά ή εύγλωττα. Το διδακτορικό δεν θα μπορούσε να είναι εξαίρεση, όχι μόνο λόγω του πολυετούς του, αλλά και λό- γω του δυναμικού και του φάσματος των συναισθημάτων που συχνά-πυκνά ¨ ¨ προκαλεί κατά την διάρκειά του. Δεν πρόκειται, λοιπόν, επ’ ουδενί για ένα © © «one-man-show», αλλά αποτελεί την συνισταμένη της συνεισφοράς, υλικής, πνευματικής, ψυχολογικής, εμφανούς ή αφανούς, ενός συνόλου εξαιρετικών ανθρώπων. Κατ’ αρχήν, θα ήθελα να ευχαριστήσω τον επιβλέποντά μου, αναπληρω- τή καθηγητή Νεκτάριο Κοζύρη, για την εμπιστοσύνη που μου έδειξε στο δύ- σκολο έργο του διδακτορικού, εν πρώτοις αποδεχόμενός με στο διδακτορικό πρόγραμμα, και εν συνεχεία, φροντίζοντας για την απρόσκοπτη και επί της ου- σίας εκπόνηση της διατριβής μου, μέσω της συνεχούς επαφής ολόκληρης της ομάδας μας με την τελευταία τεχνολογία και της ενθάρρυνσης της ενεργού συμμετοχής μας στην ευρύτερη ερευνητική κοινότητα. Οφείλω, επίσης, την ευχαριστία μου και στους συνεπιβλέποντες, καθηγητή Ανδρέα-Γεώργιο Στα- φυλοπάτη, για την στενή, αρμονική και εποικοδομητική συνεργασία μας κατά τα πρώτα χρόνια του διδακτορικού, και καθηγητή Παναγιώτη Τσανάκα. Η διδακτορική αυτή διατριβή, όμως, ίσως να μην υπήρχε χωρίς την αμέ- ριστη συμπαράσταση του Λέκτορα Γιώργου Γκούμα. Η συμβολή του Γιώρ- γου ήταν καθοριστική, τόσο στο επίπεδο της οργάνωσης και της ερευνητικής ισχυροποίησης της ομάδας μας, όσο και συγκεκριμένα στην δική μου διατρι- βή. Από τα πρώτα μου βήματα στον απαιτητικό χώρο του High Performance Computing, ο Γιώργος βρισκόταν συνεχώς δίπλα μου, στις ατυχίες, που υπήρ- ξαν αρκετές, και στις επιτυχίες, που υπήρξαν σημαντικές και ελπιδοφόρες για το μέλλον.
xiii
¨ © ¨ thesis March 11, 2013 15:54 Page xiv ©
Αντί Προλόγου
Θα ήταν μεγάλη παράλειψη στο σύντομο αυτό σημείωμα να μην αναφερ- θώ στα υπόλοιπα μέλη του εργαστηρίου, παλαιότερα και νεώτερα, που με το υψηλό τους επιστημονικό και πνευματικό επίπεδο δημιουργούν ένα σημαντι- κό ρεύμα ανόδου που συμπαρασύρει και εμπνέει κάθε νεοεισερχόμενο στην ομάδα. Δύσκολο να ξεχωρίσεις συγκεκριμένα πρόσωπα χωρίς ενδεχομένως να «αδικήσεις» κάποια άλλα, καθότι το ευχάριστο κλίμα εργασίας και συνερ- γασίας δημιουργήθηκε και συντηρείται συλλογικά. Από τον Βαγγέλη Κού- κη, που απετελέσε ένα σημαντικό έναυσμα ώστε να ενταχθώ στην ομάδα του CSLab , γνωρίζοντάς τον ως προπτυχιακός φοιτητής, τον Κορνήλιο Κούρτη, με τον οποίο συνεργαστήκαμε και με ενέπνευσε σε μεγάλο βαθμό ο τρόπος εργασίας του και αντίληψης των πραγμάτων, τον Γιώργο Τσουκαλά, τον Νίκο Αναστόπουλο, τον Κωστή Νίκα, τον Τάσο Νάνο, την Γεωργία Κουβέλη, τον Τάσο Κατσιγιάννη, τον Στέφανο Γεράγγελο, με τους οποίους περάσαμε όμορ- φες στιγμές τεχνικών ή μη αναλύσεων εντός και εκτός συνόρων, τον Θοδωρή Γκούντουβα, ο οποίος συνέβαλε με την διπλωματική του εργασία στην βελ- τίωση και επέκταση καίριων σημείων της διατριβής μου, αλλά και τα υπόλοι- πα μέλη των «Παραλλήλων» και «Κατανεμημένων», όλοι μαζί συνέβαλαν και συμβάλλουν στην δημιουργία του ευχάριστου κλίματος που δρα ως κινητή- ριος δύναμη για κάθε επιτυχία. ¨ ¨ Οφείλω, όμως, να ευχαριστήσω θερμά και τους «εκτός του κάδρου» συμ- © © βάλλοντες, τους αεί φίλους μου από τα λυκειακά και προπτυχιακά χρόνια, Νί- κο, Κώστα και Σωκράτη, που απετέλεσαν συχνά την διαφυγή και διέξοδο από την τύρβη της καθημερινότητας, και την κοπέλα μου, Ευανθία, που τα τελευ- ταία χρόνια βρισκόταν συνεχώς δίπλα μου, μεταδίδοντάς μου την δύναμη και την αγάπη της, για να συνεχίσω στον δύσκολο δρόμο του διδακτορικού. Τέλος, αλλά μάλλον ως ακρογωνιαίοι παρά ακροτελεύτιοι, οι γονείς μου, Κωνσταντίνος και Ειρήνη, και τα αδέλφια μου, Μάριος, Όλγα και Αλέξανδρος, αξίζουν την αμέριστη ευγνωμοσύνη και ευχαριστία μου για όλα όσα μου έχουν προσφέρει όλα αυτά τα χρόνια, υλικά, ηθικά, πνευματικά. Τους αφιερώνω αυ- τό το πόνημα.
Με βαθειά εκτίμηση, Βασίλειος Κ. Καρακάσης