Skip to content

Instantly share code, notes, and snippets.

@piotrga
Created December 26, 2011 01:37
Show Gist options
  • Save piotrga/1520363 to your computer and use it in GitHub Desktop.
Save piotrga/1520363 to your computer and use it in GitHub Desktop.
Matrix multiplication with parallel collections
override def multiply(m1: Array[Array[Double]], m2: Array[Array[Double]]) : Array[Array[Double]] = {
val res = Array.ofDim[Double](m1.length, m2(0).length)
val M1_COLS = m1(0).length
val M1_ROWS = m1.length
val M2_COLS = m2(0).length
@inline def singleThreadedMultiplicationFAST(start_row:Int, end_row:Int) {
var col, i = 0
var sum = 0.0
var row = start_row
// while statements are much faster than for statements
while(row < end_row){ col = 0
while(col < M2_COLS){ i = 0; sum = 0
while(i<M1_COLS){
sum += m1(row)(i) * m2(i)(col)
i+=1
}
res(row)(col) = sum
col += 1
}; row += 1
}
}
(0 until M1_ROWS).par.foreach( i =>
singleThreadedMultiplicationFAST(i, i+1)
)
res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment