线程并行高效 [英] Thread Parallel Efficient
本文介绍了线程并行高效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编写一个并行函数,该函数将用可变线程数将两个n x n矩阵相乘.
通过选择线程数和每个线程的块数来提高效率.
在此先感谢!
Write a parallel function that will multiply two n by n matrices with a variable number of threads.
This should be an efficient by choosing the number of threads and number of blocks for each thread.
Thanks in advance!
推荐答案
- 计算出乘积兼容性,(m x n)可以乘以(n x o)个矩阵,这将产生一个(m x o)个矩阵.此处并不需要,因为我们只查看nxn的平方矩阵,因此具有正确的乘法尺寸.
- 选择块大小: b
- 创建尺寸为(mxo)的新矩阵.
- 按行(或逐列)方式遍历新矩阵,以批量创建 b 的元组.
b = 2且m = n = o = 3且基于一个的数组索引的示例:
1.(1,1)(1,2)
2.(1,3)(2,1)
3.(2,2)(2,3)
4.(3,1)(3,2)
5.(3,3)
- 为每个批次启动一个线程.使用
Thread.Start
,您将把元组列表传递给线程,并以左手,右手的顺序传递对新创建的mxo矩阵以及两个原始矩阵的引用. - 在线程工作方法中,您将对传递给线程的列表的每个元组(x,y),将x行乘以y列.生成的标量将放置在新矩阵中的位置(x,y).
- 在主程序中,等待所有线程完成(
Join
).乘法结果现在位于第3步中新创建的矩阵中.
- Work out multiplication compatibility, (m x n) can multiply (n x o) matrices which will yield a (m x o) matrix. That is not really needed here since we are only looking at square matrices which are n x n and thus have the correct dimensions for multiplication.
- Choose block size: b
- Create a new matrix with the dimensions (m x o).
- Traversing the new matrix in a row by row (or column by column) fashion create tuples in batches size b.
Example with b = 2 and m = n = o = 3 and array indices based on one:
1. (1,1) (1,2)
2. (1,3) (2,1)
3. (2,2) (2,3)
4. (3,1) (3,2)
5. (3,3)
- Start a thread for each of the batches. Using
Thread.Start
you will pass the list of tuples to the thread as well as a reference to the the newly created m x o matrix as well as both original matrices in the order left hand side, right hand side. - In the threads work method you''ll do the multiplication of row x by column y for each tuple (x, y) of the list you passed to the thread. The resulting scalar is placed into the new matrix at position (x,y).
- In your main program wait for all threads to finish (
Join
). The multiplication result is now in the newly created matrix from step 3.
这就是全部.行与列或矩阵乘法 [
That is all there is to it really. The multiplication of a row by a column or matrix multiplication[^] is straight forward enough so I won''t go into that.
Regards,
Manfred
这篇关于线程并行高效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文