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
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.