问题
Assuming a single machine with 8 cores.
In Haskell, you can compile using the threaded
option, then during runtime use +RTS -Nx
to specify the number of cores to be used. e.g.
$ myprg args // sequential run
$ myprg args +RTS -N1 // parallel run on 1..8 cores
$ myprg args +RTS -N2
$ myprg args +RTS -N4
$ myprg args +RTS -N8
From this, you get the runtimes using increasing number of cores, which you can then use to get speedups and plot a graph.
How would you do this in F#, assuming I have a parallel program e.g. using a parallel map in the code?
EDIT:
I found there are ParallelOptions e.g. MaxDegreeOfParallelism
which may be what I need but not sure about its exact behaviour, and I would have to use it programmatically which is fine as long as it behaves as expected i.e. MaxDegreeOfParallelism
= num of cores program should use, and not parallel 'tasks' or threads.
EDIT:
ProcessorAffinity
indeed limits the number of cores to use but it seems that it is not properly implemented in Mono. I checked on Windows and it seems to work. Though it is not really a good idea to use. The runtime system should be able to decide better how to manage and schedule tasks. Also, MaxDegreeOfParallelism
is about "parallelism level" which basically sets the number of tasks generated, thus could be used to vary granularity.
回答1:
F# builds a native .NET assembly. An assembly follows the rules specified for runtime (CLR) which by default has affinity on all CPU cores. You can limit CLR for a fewer number of cores by setting System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity
.
This answer seems to be incomplete for Mono environment. ProcessorAffinity
value is a bit mask, so 0
is certainly an invalid condition. I'm also wondering why the setter did not throw an exception as described in MSDN.
I would use schedutils
to check Mono affinity and also check if MONO_NO_SMP
environment flag is not set.
回答2:
To quote bytebuster's comment:
F# builds a native .NET assembly. An assembly follows the rules specified for runtime (CLR) which by default has affinity on all CPU cores. You can limit CLR for a fewer number of cores by setting System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity
.
(If you add an answer leave a comment and I will delete this)
来源:https://stackoverflow.com/questions/9678284/how-to-take-f-measurements-to-get-speedups