如何通过F#测量来获得加速 [英] How to take F# measurements to get speedups

查看:158
本文介绍了如何通过F#测量来获得加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一台8核的单机。
在Haskell中,可以使用线程选项进行编译,然后在运行时使用 + RTS -Nx 指定要使用的核心数量。例如

  $ myprg args //顺序运行
$ myprg args + RTS -N1 //并行运行于1。 .8核心
$ myprg args + RTS -N2
$ myprg args + RTS -N4
$ myprg args + RTS -N8

由此,您可以使用越来越多的核心获得运行时间,然后您可以使用它来获得加速并绘制图表。



假设我有一个并行程序,例如,你会如何在F#中做这件事在代码中使用平行映射?



编辑:
我发现有 ParallelOptions 例如 MaxDegreeOfParallelism 其中可能是我需要的,但不确定它的确切行为,我必须以编程方式使用它,只要它像预期的那样运行,即 MaxDegreeOfParallelism =程序应该使用的核心数量,而不是并行的'任务'或线程。



编辑:
ProcessorAffinity 确实限制了要使用的内核数量,但似乎并未在Mono中正确实施。我检查了Windows,它似乎工作。虽然使用它并不是一个好主意。运行时系统应该能够更好地决定如何管理和调度任务。另外, MaxDegreeOfParallelism 是关于并行性级别,它基本上设置了生成的任务数量,因此可以用来改变粒度。

解决方案

F#构建一个本地.NET程序集。程序集遵循为运行时指定的规则(CLR),默认情况下,该规则对所有CPU核心具有亲和力。通过设置 System.Diagnostics.Process.GetCurrentProcess()。ProcessorAffinity

$ b,您可以限制CLR的核心数量少于
$ b

对于Mono环境,这个答案似乎不完整。 ProcessorAffinity 是一个位掩码,因此 0 当然是无效的条件。我也想知道为什么setter没有像MSDN中描述的那样抛出异常。



我会使用 schedutils 检查Mono亲和力,并检查是否未设置 MONO_NO_SMP 环境标志。


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.

解决方案

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.

这篇关于如何通过F#测量来获得加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆