Advertisement
Guest User

Untitled

a guest
May 10th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.33 KB | None | 0 0
  1. module MatrixMult
  2. open System
  3. open System.Threading
  4.  
  5. let calcEl(l, r , max, str1, stol1, long, m1:int[,], m2:int[,], resMatrix:int[,]) =
  6.  if (r<max) then do
  7.   let mutable res = 0
  8.   for x=l to r do
  9.    res <- 0
  10.    let str = x / stol1
  11.    let stol = x%stol1
  12.    for i=0 to long-1 do
  13.     res <- res + (m1.[str,i] * m2.[i, stol])
  14.     resMatrix.[str,stol] <- res
  15.   resMatrix  
  16.    
  17.  
  18. let multiply (m1: int[,] , m2: int[,] , threadNumber:int) =
  19.  let l11 = m1.GetLength 0
  20.  let l12 = m1.GetLength 1
  21.  
  22.  let l21 = m2.GetLength 0
  23.  let l22 = m2.GetLength 1
  24.  let resLength = l11*l22
  25.  let resMatrix = (Array2D.zeroCreate l11 l22)
  26.  
  27.  let step = resLength / threadNumber
  28.  let threadArray = Array.init (threadNumber) (fun i -> new Thread(ThreadStart(fun _ ->
  29.     calcEl (i*step , (i+1)*step, resLength, l11, l22, l12, m1, m2, resMatrix)
  30.     )))
  31.  calcEl ((threadNumber-1)*step, resLength-1, resLength, l11,l22,l12,m1,m2,resMatrix)
  32.  
  33.  for t in threadArray do
  34.   t.Start()
  35.  for t in threadArray do
  36.   t.Join()
  37.  
  38.  resMatrix
  39.  
  40.  
  41.  
  42.    
  43. [<EntryPoint>]
  44. let main argv =
  45.  0
  46.  
  47. //module MatrixMult
  48. //open Programs
  49. //open NUnit.Framework
  50. //[<TestCase ([|[|5;1|]; [|2;6|]|], [| [|7;-1|]; [|-1;5|] |], 2, Result = [| [|34;0|]; [|8;28|] |])>]
  51. //let ``Test`` (m1 : int[,], m2: int[,], threadNumber : int) =
  52.  //multiply (m1, m2, threadNumber)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement