"MaxDegreeOfParallelism" for Array.Parallel?

Is it possible to set MaxDegreeOfParallelism (this is the maximum number of threads to use) for the Array.Parallel module, since it uses Parallel.For under the hood ?

+3
source share
3 answers

According to this post , it seems that there is no way to limit the number of threads globally in the final version of Parallel Extensions. An alternative to what Brian offers would be to use PLINQ (which works with parallel sequences) instead of functions that work with arrays.

This can be done using the module PSeqfrom F # PowerPack . It provides features like PSeq.map, PSeq.filterand many others that work with parallel sequences (which can also be well composed using pipelining). For parallel sequences, you can use the WithDegreeOfParallelism extension method to indicate behavior.

You can implement a wrapper function for it:
[EDIT: it already exists!]

let withDegreeOfParallelism n (pq:ParallelQuery<_>) = 
  pq.WithDegreeOfParallelsm(n)

And then write:

let res = 
  data |> PSeq.map (fun n -> ...)
       |> PSeq.withDegreeOfParallelism ParallelOptions.MaxDegreeOfParallelism
       |> Array.ofSeq

This may have a different performance, because it is implemented differently than the functions in the module Array.Parallel, but it certainly depends on your scenario.

+3
source

No, I do not think so.

Array.Parallel, array.fs ( CTP) .

+1

Assuming that I want to say no more than 10 topics that I replace:

myArray 
|> Array.Parallel.iter (fun item -> doWork item)

with

let maxPara = 10
myArray
|> Array.splitInto maxPara
|> Array.Parallel.iter (fun items -> items |> List.iter (fun item -> doWork item))
0
source

Source: https://habr.com/ru/post/1744533/


All Articles