简体   繁体   English

原子 RMW 操作和函数调用的比较成本是多少?

[英]What's the compative cost of an atomic RMW operation and a function call?

My understanding is that atomic machine instructions may be up to two orders of magnitude slower than a non-atomic operation.我的理解是原子机器指令可能比非原子操作慢两个数量级。 For example, given例如,给定

int x;
x++

and

std::atomic<int> y;
y++;

my understanding is that x++ typically runs much faster than y++ .我的理解是x++通常比y++运行得快得多。 (I'm assuming that the increment operation maps to an underlying machine instruction. I'm sure the exact comparative cost varies from architecture to architecture, but I'm talking about a rule of thumb.) (我假设增量操作映射到底层机器指令。我确信确切的比较成本因架构而异,但我说的是经验法则。)

I'm interested in the relative cost of an atomic RMW operation and a non-inline function call, again, as a general rule of thumb.作为一般经验法则,我再次对原子 RMW 操作和非内联函数调用的相对成本感兴趣。 For example, given this non-inline function,例如,给定这个非内联函数,

void f(){}

what can we generally say about the cost of y++ (ie, the atomic increment) compared to the cost of executing a non-inline call to f ?与执行非内联调用f的成本相比,我们通常可以说y++的成本(即原子增量)?

My motivation is to try to put the common claim that "atomic operations are much more expensive than non-atomic operations" in perspective.我的动机是试图正确看待“原子操作比非原子操作昂贵得多”的普遍主张。 One way to do that is to try to get some idea how expensive an atomic RMW operation is compared to calling and returning from a non-inline function.一种方法是尝试了解与调用和从非内联函数返回相比,原子 RMW 操作的开销有多大。

Please don't reply with "the only way to know is to measure."请不要回复“唯一知道的方法就是测量”。 I'm not asking about an atomic RMW operation versus a function call in a particular context on a particular architecture.我不是在询问原子 RMW 操作与特定架构上特定上下文中的函数调用。 I'm asking about a general rule of thumb that could be used as the basis of discussion for people who might think, "We can never use atomic instructions, because they're too expensive," yet who wouldn't think twice about making function calls.我问的是一个一般的经验法则,可以用作讨论基础的人,他们可能会认为,“我们永远不能使用原子指令,因为它们太贵了”,但他们不会三思而后行函数调用。

The question as asked has issues.问的问题有问题。

One is that you use pseudo code syntax that doesn't have a clear storage class and that seems to operate on local objects.一是您使用的伪代码语法没有明确的存储类并且似乎对本地对象进行操作。 A local atomic object is meaningless.局部原子对象是没有意义的。 Atomic operations are for objects shared by different threads.原子操作是针对不同线程共享的对象。

A compiler could well notice that a non volatile local variable is used only in a function and not generate any special atomic operation (I don't know of any compiler that does presently does that).编译器很可能会注意到非易失性局部变量仅在函数中使用,并且不会生成任何特殊的原子操作(我不知道目前有任何编译器会这样做)。

We have to assume that the object is not local (or is volatile).我们必须假设对象不是本地的(或者是易失的)。

The cost of any memory operation depends a lot on caching.任何存储器操作的成本取决于缓存很多 If the location is not in our cache the operation will be much more costly.如果该位置不在我们的缓存中,则操作成本会高得多。

All the end of stack (the most recent part) is almost always in our cache.堆栈的所有末端(最近的部分)几乎总是在我们的缓存中。

By definition the value of shared objects must travel between caches (they are modified or read by multiple threads).根据定义,共享对象的值必须在缓存之间传输(它们被多个线程修改或读取)。

So what are you really comparing here?那么你在这里真正比较的是什么? Until you say precisely, the question can't be answered.除非你说得准确,否则这个问题是无法回答的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM