Amdahl定律实例C# [英] Amdahl's Law Example in C#

查看:114
本文介绍了Amdahl定律实例C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与一些paralization工作,并且把我寻找到Amdahl定律。我读过一些关于这个专题的职位;

I was working with some paralization and that brought me looking into Amdahl's law. I've read a number of posts on the topic;

计算使用Amdahl定律

如何计算Amadahl定律的线程效能

http://en.wikipedia.org/wiki/Amdahl%27s_law

......但希望能找到显示在实践中一个C#示例。搜索已被证明没有结果。在理论上,应该可以使串行应用,时间parallelisable份,运行一个并行化版本,记录所花费的平行部分的长度,并比较该差(知道多少处理器正在使用),以阿姆达尔函数的结果。 ?这是正确的,是任何人都知道这种现有的例子

...but was hoping to find a C# example showing it in practice. Searching has proved no results. In theory it should be possible to make a serial application, time the parallelisable parts, run a parallelised version, recording the length it takes the parallel parts and compare the difference (knowing how many processors are being used) to the result of Amdahl's function. Is this correct and is anyone aware of such an example existing?

推荐答案

请注意:该程序的一个完整的工作可下载的版本可以在我Github上页

Note: A complete working downloadable version of the program can be found on My Github Page

因此,与Amdahl定律,我们平分工作中和必须在连续运行工作工作,可以的并行的,让我们代表了这两个工作负载列表<作用>

So with Amdahl's Law, we split the work in to "Work that must run in serial" and "Work that can be parallelized", so let's represent those two workloads as List<Action>:

var serialWorkLoad = new List<Action> { DoHeavyWork, DoHeavyWork };
var parallelizableWorkLoad = new List<Action> { DoHeavyWork, DoHeavyWork, DoHeavyWork, DoHeavyWork, DoHeavyWork, DoHeavyWork, DoHeavyWork, DoHeavyWork };



DoHeavyWork 委托出色抽象为

static void DoHeavyWork()
{
    Thread.Sleep(500);
}



正如你可以看到我所做的工作量并行有点重的乐趣并且,使这一个体面的例子。

As you can see I've made the parallelizable workload a bit heavier for fun and to make a decent example of it.

接下来我们要运行串行两种工作负载,让我们的基线:

Next we have to run both workloads in Serial to get our baseline:

var stopwatch = new Stopwatch();
stopwatch.Start();
// Run Serial-only batch of work
foreach (var serialWork in serialWorkLoad)
{
    serialWork();
}

var s1 = stopwatch.ElapsedMilliseconds;

// Run parallelizable batch of work in serial to get our baseline
foreach (var notParallelWork in parallelizableWorkLoad)
{
    notParallelWork();
}

stopwatch.Stop();
var s2 = stopwatch.ElapsedMilliseconds - s1;



在这一点上,我们有多久了每个工作负荷以串行运行。现在,让我们再次运行它,与并行的并行部分

At this point we have how long it took each workload to run in serial. Now, let's run it again, with the parallelizable portion parallelized.

stopwatch.Reset();
stopwatch.Start();
// Run Serial-only batch of work
foreach (var serialWork in serialWorkLoad)
{
    serialWork();
}

var p1 = stopwatch.ElapsedMilliseconds;

// Run parallelizable batch of work in with as many degrees of parallelism as we can
Parallel.ForEach(parallelizableWorkLoad, (workToDo) => workToDo()); // In Java this is Magic Unicorns

stopwatch.Stop();
var p2 = stopwatch.ElapsedMilliseconds - p1;

现在,我们有基线和并行化的版本,我们可以计算出加速和报告我们的发现:

Now that we have the baseline and the parallelized version, we can calculate the speedup and report our findings:

var speedup = (double)(s1 + s2) / (p1 + p2);

Console.WriteLine("Serial took  : {2}ms, {0}ms for serial work and {1}ms for parallelizable work", s1, s2, s1 + s2);
Console.WriteLine("Parallel took: {2}ms, {0}ms for serial work and {1}ms for parallelizable work", p1, p2, p1 + p2);
Console.WriteLine("Speedup was {0:F}x", speedup);

和作为Amdahl定律告诉你,那是很难与内核的#你,因为完美的扩展的串行唯一的工作。

And as Amdahl's Law tells you, it is hard to scale perfectly with the # of cores you have because of the serial-only work.

这篇关于Amdahl定律实例C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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