在 F# 中并行化两个矩阵的元素乘法
我正在尝试并行化 F# 中两个矩阵的逐元素乘法。我实在想不通。我一直尝试创建任务,但它永远不想编译。我的非工作混乱代码如下:
let myBigElemMultiply (m:matrix) (n:matrix) =
let AddTwoRows (row:int) (destination:matrix) (source1:matrix) (source2:matrix) =
for i in 0 .. destination.NumCols
destination.[row, i] <- source1.[row,i] + source2.[row,i]
destination
let result = Matrix.zero(m.NumRows)
let operations = [ for i in 0 .. m.NumRows -> AddTwoRows i result m n ]
let parallelTasks = Async.Parallel operations
Async.RunSynchronously parallelTasks
result
I'm trying to parallelize the element by element multiplication of two matrices in F#. I can't quite figure it out thought. I keep trying to create tasks but it never wants to compile. My non-working messy code is the following:
let myBigElemMultiply (m:matrix) (n:matrix) =
let AddTwoRows (row:int) (destination:matrix) (source1:matrix) (source2:matrix) =
for i in 0 .. destination.NumCols
destination.[row, i] <- source1.[row,i] + source2.[row,i]
destination
let result = Matrix.zero(m.NumRows)
let operations = [ for i in 0 .. m.NumRows -> AddTwoRows i result m n ]
let parallelTasks = Async.Parallel operations
Async.RunSynchronously parallelTasks
result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你犯了几个小错误,例如,你没有弄清楚如何进行矩阵乘法。
需要注意的一件事是,此代码的性能会非常糟糕,因为
m.[i,j]
是访问矩阵中元素的低效方法。你最好使用二维数组:测试:
一些计时:
检查 这里使用ParallelFor,它应该比异步有更好的性能。
You have made several small mistakes, e.g., you haven't figured how to do matrix multiplication.
One thing to notice is that this code would perform very badly because
m.[i,j]
is an inefficient way to access elements in a matrix. You'd better use a 2D array:testing:
some timing:
Check here by using ParallelFor, which should have a better performance than async.
这里至少有一些可以编译的代码,也许这会让您朝着正确的方向前进?
Here's at least some code that compiles, perhaps this will get you headed in the right direction?
没有意义。一对矩阵的异地逐元素乘法与复制没什么区别,此时单个核心将愉快地最大化机器的整个内存带宽,并且添加更多核心不会提高性能。所以这几乎肯定是浪费时间。
There's no point. Out-of-place element-wise multiplication of a pair of matrices is little more that copying at which point a single core will happily max out the entire memory bandwidth of your machine and adding more cores will not improve performance. So it is almost certainly a waste of time.