简体   繁体   English

与F#中的Map发生多线程冲突

[英]does multithread conflict with Map in F#

let len = 25000000
let map = Map.ofArray[|for i =1 to len do yield (i,i+1)|]
let maparr = [|map;map;map;map|]
let f1 i =   
    for i1 =1 to len do
        let l1 = maparr.[i-1].Item(i1)
        ()

let index = [|1..4|]
let _ = index |> Array.Parallel.map f1
printf "done" 

I found that only one core is working at full speed be the code above . 我发现上面的代码仅是一个全速工作的内核。 But what i except is all the four thread is working together with a high level of cpu usage. 但是我唯一的例外是所有四个线程正在一起使用较高水平的cpu。 So it seems multithread conflict with Map, am i right? 因此,似乎与Map有多线程冲突,对吗? If not, how can achieve my initial goal? 如果没有,怎么能达到我的最初目标? Thank you in advance 先感谢您

So I think you were tripping a heuristic where the library assumed when there were only a small number of tasks, it would be fastest to just use a single thread. 因此,我认为您是在尝试一种启发式方法,即库假设只有少量任务时,仅使用单个线程会更快。

This code maxes out all threads on my computer: 此代码使我的计算机上的所有线程数最大化:

let len = 1000000
let map = Map.ofArray[|for i =1 to len do yield (i,i+1)|]
let maparr = [|map;map;map;map|]
let f1 (m:Map<_,_>) =
    let mutable sum = 0
    for i1 =1 to len do
        let l1 = m.Item(i1)
        for i = 1 to 10000 do
            sum <- sum + 1
    printfn "%i" sum

let index = [|1..40|]
printfn "starting"
index |> Array.map (fun t -> maparr.[(t-1)/10]) |> Array.Parallel.iter f1
printf "done"

Important changes: 重要更改:

  1. Reduced len significantly. len大大降低。 In your code, almost all the time was spent creating the matrix. 在您的代码中,几乎所有时间都花在了创建矩阵上。
  2. Actually do work in the loop. 实际上确实在循环中工作。 In your code, it is possible that the loop was optimised to a no-op. 在您的代码中,循环有可能被优化为无操作。
  3. Run many more tasks. 运行更多任务。 This tricked the scheduler into using more threads and all is good 这欺骗了调度程序使用更多线程,一切都很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM