如何计算螺纹效率的阿玛达尔定律 [英] How to calculate Amadahl's Law for threading effectiveness

查看:71
本文介绍了如何计算螺纹效率的阿玛达尔定律的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很容易找到和理解阿玛达尔定律的函数定义,但我能找到的所有工作示例要么太模糊,要么太学术/大脑,我的小豌豆大脑无法理解.

Its easy to find and understand the function definition for Amadahl's Law, but all of the working examples I was able to find were either too vague or too academic/cerebreal for my tiny pea brain to understand.

Amadahl 定律采用以下参数:F,无法通过多线程改进的任务的百分比,以及 N,要使用的线程数.

Amadahl's Law takes to parameters: F, the % of a task that cannot be improved via multi-threading, and N, the number of threads to use.

如何以任何程度的准确度计算 F?

你如何看待一段代码并确定它是否会被多线程改进?

How do you look at a piece of code and determine whether that will be improved by multi-threading?

推荐答案

很容易说代码的哪些部分肯定不会从多线程中受益:顺序部分.如果您必须按顺序执行一系列小步骤,多线程将无济于事,因为您总是需要等待一个步骤完成才能开始下一个步骤.从这个意义上说,许多常见任务(必然)不是顺序的:例如,在列表中搜索多个项目.如果要从列表中提取每个红色项,可以在多个线程之间共享列表的一部分,并将每个部分中的所有红色项收集到最终结果列表中.并发编程的难点在于找到针对实际问题执行此操作的有效方法.

It's relatively easy to say which parts of your code certainly won't benefit from multi-threading: sequential parts. If you have to carry out a series of small steps in order, muli-threading won't help because you always need to wait for one step to be done before starting the next. Many common tasks aren't (necessarily) sequential in this sense: for example, searching a list for a number of items. If you want to extract every red item from a list, you can share parts of the list among several threads and collect all the red items from each part into a final result list. The difficulty in concurrent programming lies in finding efficient ways of doing this for real problems.

在较低级别,您可以谈论数据依赖性:如果特定指令或块使用该块自身的计算结果,则该指令或块依赖于前一个块.所以(伪代码):

At a lower level you can talk about data dependency: a particular instruction or block depends on a previous block if it uses the results of that block's calculations in its own. So (pseudocode):

Block one:
load r1 into r2
add r1 to r3 into r4


Block two:
load r4 into r1
add 3 to r4 into r4

块二依赖块一:它们必须按顺序执行.这里:

block two depends on block one: they must be executed in order. Here:

Block one:
load r1 into r2
add r1 to r3 into r4

Block two:
load r1 into r3
add 3 to r1 into r1

事实并非如此.这对并发没有直接用处,但希望它能更具体地说明这一点.它还说明了处理并发的另一个问题:作为抽象块功能,这两个功能可以并行运行,但在此处给出的具体示例中,它们正在读取/写入一些相同的寄存器,因此编译器/流水线/任何东西都必须做更多的工作,让它们一起运行.这一切都非常复杂,但在 http://www.amazon.com/Computer-Architecture-Quantitative-Approach-Edition/dp/1558605967.

that isn't the case. This isn't directly useful for concurrency, but hopefully it illustrates the point more concretely. It also illustrates another problem in handling concurrency: as abstract blocks functionality these two can be run in parallel, but in the concrete example given here they're reading/writing some of the same registers, so a compiler/pipeliner/whatever would have to do more work to make them run together. This is all very complex, but is covered beautifully in http://www.amazon.com/Computer-Architecture-Quantitative-Approach-Edition/dp/1558605967.

哪些其他部分不能从多线程中受益取决于您的编程环境和机器架构.

Which other parts don't benefit from multi-threading depends on your programming environment and machine architecture.

至于如何获得百分比,在实际案例中可能涉及到一些问题 - 我怀疑您永远无法获得准确的数字.如果您将代码划分为功能单元并在每个单元中分析执行时间,那么这将为您提供一个大致合适的权重.那么如果一个占用90%执行时间的部分可以用多线程来改进,你说你的任务"的90%都可以改进到如此地步. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

As for how to get a percentage, there's probably some hand-waving involved in a practical case - I doubt you'll ever get a precise number. If you divide your code up into functional units and profile the execution time in each, that would give you a roughly appropriate weighting. Then if one part that takes up 90% of the execution time can be improved with multi-threading, you say that 90% of your 'task' can be so improved.

这篇关于如何计算螺纹效率的阿玛达尔定律的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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