说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C14-SharedMemory/exercises.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
14.9. Exercises
- 第 14 章练习(英文交互式练习,仍在编写中)
- Chapter 14 Exercises (in progress)
附加练习
Additional Exercises
- 完整实现 scalar_multiply 程序。用
gettimeofday()函数在 1 亿个元素上为代码计时。随着线程数的增加,程序的运行时间如何变化?如果把元素个数增加到 10 亿呢?20 亿呢?
- Implement the entirety of the scalar_multiply program. Time your code using the
gettimeofday()function and 100 millions elements. How does the time of the program vary as you increase the number of threads? What if you increase the number of elements to 1 billion? 2 billion?
- 改进最初的多线程
scalar multiply函数:把所有参数放进一个struct,从 main 中传入。为这个版本的代码计时。有什么区别吗?(参考答案)
- Improve the original
scalar multiplythreaded function by placing all the arguments into astructand passing it through main. Time the performance of this version of the code. Is there any difference? (solution)
- 通过实现更好的负载均衡方法来改进多线程的
scalar_multiply函数。换句话说,实现前面旁注中所说的负载均衡方法。
- Improve the
scalar_multiplythreaded function by implementing a better load balancing procedure. In other words, implement the load balancing procedure in the note above.
- 运用所学的知识,尝试实现一个执行矩阵-向量乘法的程序。在矩阵-向量乘法中,矩阵的每一行都与某个元素向量相乘。
- Using what you have learned, try implementing a program that performs matrix vector multiplication. In matrix vector multiplication, each row in the matrix is multiplied by some vector of elements.
- 实现计数排序算法第 2 步的并行版本。为它的性能计时。
- Implement a parallel version of the Step 2 of the CountSort algorithm. Time your performance.
- 尝试把计数排序程序的第 1 步和第 2 步合并到一个程序中。为此,你需要在程序中再加一轮
pthread_create()和pthread_join()。
- Try combining Step 1 and Step 2 of the CountSort program into a single program. To do this, you will need to add another cycle of
pthread_create()andpthread_join()to your program.
- 为新的计数排序程序的整体性能计时。
- Time the total performance of the new CountSort program.
- OpenMP:
writeElems()函数假定用户输入的线程数小于MAX。有没有办法改写这段代码,使它无论线程数是多少都能正常工作?
- OpenMP: The
writeElems()function makes the assumption that the user only inputs a number of threads less thatMAX. Is there a way to rewrite this code so that it will work, regardless of the number of threads?