没有合适的资源?快使用搜索试试~ 我知道了~
首页Is Parallel Programming Hard, And, If So, What Can You.pd
Is Parallel Programming Hard, And, If So, What Can You.pd
需积分: 9 9 下载量 85 浏览量
更新于2023-03-16
评论
收藏 6.97MB PDF 举报
Is Parallel Programming Hard, And, If So, What Can You.pdf
资源详情
资源评论
资源推荐
ii
Legal Statement
This work represents the views of the editor and the authors and does not necessarily represent the view of their
respective employers.
Trademarks:
•
IBM, zSeries, and PowerPC are trademarks or registered trademarks of International Business Machines Corpora-
tion in the United States, other countries, or both.
• Linux is a registered trademark of Linus Torvalds.
• i386 is a trademark of Intel Corporation or its subsidiaries in the United States, other countries, or both.
• Other company, product, and service names may be trademarks or service marks of such companies.
The non-source-code text and images in this document are provided under the terms of the Creative Commons
Attribution-Share Alike 3.0 United States license.
1
In brief, you may use the contents of this document for any purpose,
personal, commercial, or otherwise, so long as attribution to the authors is maintained. Likewise, the document may
be modified, and derivative works and translations made available, so long as such modifications and derivations are
offered to the public on equal terms as the non-source-code text and images in the original document.
Source code is covered by various versions of the GPL.
2
Some of this code is GPLv2-only, as it derives from the
Linux kernel, while other code is GPLv2-or-later. See the comment headers of the individual source files within the
CodeSamples directory in the git archive
3
for the exact licenses. If you are unsure of the license for a given code
fragment, you should assume GPLv2-only.
Combined work © 2005-2014 by Paul E. McKenney.
1
http://creativecommons.org/licenses/by-sa/3.0/us/
2
http://www.gnu.org/licenses/gpl-2.0.html
3
git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git
Contents
1 How To Use This Book 1
1.1 Roadmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Quick Quizzes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Alternatives to This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Sample Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Whose Book Is This? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Introduction 5
2.1 Historic Parallel Programming Difficulties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Parallel Programming Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.2 Productivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.3 Generality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 Alternatives to Parallel Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.1 Multiple Instances of a Sequential Application . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.2 Use Existing Parallel Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.3 Performance Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 What Makes Parallel Programming Hard? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.4.1 Work Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.4.2 Parallel Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.3 Resource Partitioning and Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.4 Interacting With Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.5 Composite Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.6 How Do Languages and Environments Assist With These Tasks? . . . . . . . . . . . . . . . . 13
2.5 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Hardware and its Habits 15
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.1 Pipelined CPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.2 Memory References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.1.3 Atomic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.4 Memory Barriers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.5 Cache Misses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.1.6 I/O Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2 Overheads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2.1 Hardware System Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2.2 Costs of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Hardware Free Lunch? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
iii
iv CONTENTS
3.3.1 3D Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.2 Novel Materials and Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.3 Light, Not Electrons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.4 Special-Purpose Accelerators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.5 Existing Parallel Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4 Software Design Implications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4 Tools of the Trade 25
4.1 Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2 POSIX Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.2.1 POSIX Process Creation and Destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.2.2 POSIX Thread Creation and Destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.3 POSIX Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.4 POSIX Reader-Writer Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.3 Atomic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.4 Linux-Kernel Equivalents to POSIX Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.5 The Right Tool for the Job: How to Choose? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5 Counting 35
5.1 Why Isn’t Concurrent Counting Trivial? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.2 Statistical Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.2.1 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.2.2 Array-Based Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.2.3 Eventually Consistent Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.2.4 Per-Thread-Variable-Based Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.2.5 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.3 Approximate Limit Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.3.1 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.3.2 Simple Limit Counter Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.3.3 Simple Limit Counter Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.3.4 Approximate Limit Counter Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.3.5 Approximate Limit Counter Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.4 Exact Limit Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.4.1 Atomic Limit Counter Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.4.2 Atomic Limit Counter Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.4.3 Signal-Theft Limit Counter Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.4.4 Signal-Theft Limit Counter Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.4.5 Signal-Theft Limit Counter Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.5 Applying Specialized Parallel Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.6 Parallel Counting Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6 Partitioning and Synchronization Design 59
6.1 Partitioning Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1 Dining Philosophers Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.2 Double-Ended Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.1.3 Partitioning Example Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.2 Design Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.3 Synchronization Granularity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.3.1 Sequential Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.3.2 Code Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
CONTENTS v
6.3.3 Data Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.3.4 Data Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.3.5 Locking Granularity and Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.4 Parallel Fastpath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.4.1 Reader/Writer Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.4.2 Hierarchical Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
6.4.3 Resource Allocator Caches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
6.5 Beyond Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6.5.1 Work-Queue Parallel Maze Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.5.2 Alternative Parallel Maze Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.5.3 Performance Comparison I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
6.5.4 Alternative Sequential Maze Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.5.5 Performance Comparison II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.5.6 Future Directions and Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.6 Partitioning, Parallelism, and Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7 Locking 87
7.1 Staying Alive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1.1 Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1.2 Livelock and Starvation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7.1.3 Unfairness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.1.4 Inefficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.2 Types of Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.2.1 Exclusive Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.2.2 Reader-Writer Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.2.3 Beyond Reader-Writer Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.2.4 Scoped Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.3 Locking Implementation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.3.1 Sample Exclusive-Locking Implementation Based on Atomic Exchange . . . . . . . . . . . . 98
7.3.2 Other Exclusive-Locking Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.4 Lock-Based Existence Guarantees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.5 Locking: Hero or Villain? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.5.1 Locking For Applications: Hero! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.5.2 Locking For Parallel Libraries: Just Another Tool . . . . . . . . . . . . . . . . . . . . . . . . 101
7.5.3 Locking For Parallelizing Sequential Libraries: Villain! . . . . . . . . . . . . . . . . . . . . . 104
7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
8 Data Ownership 107
8.1 Multiple Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
8.2 Partial Data Ownership and pthreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
8.3 Function Shipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
8.4 Designated Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
8.5 Privatization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
8.6 Other Uses of Data Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
9 Deferred Processing 111
9.1 Reference Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
9.1.1 Implementation of Reference-Counting Categories . . . . . . . . . . . . . . . . . . . . . . . 112
9.1.2 Hazard Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
9.1.3 Linux Primitives Supporting Reference Counting . . . . . . . . . . . . . . . . . . . . . . . . 116
剩余520页未读,继续阅读
phenix_lord
- 粉丝: 23
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 2023年中国辣条食品行业创新及消费需求洞察报告.pptx
- 2023年半导体行业20强品牌.pptx
- 2023年全球电力行业评论.pptx
- 2023年全球网络安全现状-劳动力资源和网络运营的全球发展新态势.pptx
- 毕业设计-基于单片机的液体密度检测系统设计.doc
- 家用清扫机器人设计.doc
- 基于VB+数据库SQL的教师信息管理系统设计与实现 计算机专业设计范文模板参考资料.pdf
- 官塘驿林场林防火(资源监管)“空天地人”四位一体监测系统方案.doc
- 基于专利语义表征的技术预见方法及其应用.docx
- 浅谈电子商务的现状及发展趋势学习总结.doc
- 基于单片机的智能仓库温湿度控制系统 (2).pdf
- 基于SSM框架知识产权管理系统 (2).pdf
- 9年终工作总结新年计划PPT模板.pptx
- Hytera海能达CH04L01 说明书.pdf
- 数据中心运维操作标准及流程.pdf
- 报告模板 -成本分析与报告培训之三.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0