真的可以在OoOE处理器中对存储器进行重新排序吗? [英] Can memory store be reordered really, in an OoOE processor?

查看:52
本文介绍了真的可以在OoOE处理器中对存储器进行重新排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道, OoOE处理器可以重新排序两条指令.例如,在不同线程之间共享两个全局变量.

We know that two instructions can be reordered by an OoOE processor. For example, there are two global variables shared among different threads.

int data;
bool ready;

编写器线程产生 data 并打开标志 ready ,以允许读者使用该数据.

A writer thread produce data and turn on a flag ready to allow readers to consume that data.

data = 6;
ready = true;

现在,在OoOE处理器上,这两个指令可以重新排序(指令获取,执行).但是最后提交/回写结果呢?即商店会井然有序吗?

Now, on an OoOE processor, these two instructions can be reordered (instruction fetch, execution). But what about the final commit/write-back of the results? i.e., will the store be in-order?

据我了解,这完全取决于处理器的内存模型.例如,x86/64具有强大的内存模型,并且不允许对存储进行重新排序.相反,ARM通常具有弱模型,在这种模型中可能会发生商店重新排序(以及其他几种重新排序).

From what I learned, this totally depends on a processor's memory model. E.g., x86/64 has a strong memory model, and reorder of stores is disallowed. On the contrary, ARM typically has a weak model where store reordering can happen (along with several other reorderings).

此外,直觉告诉我我是对的,因为否则我们将不需要在典型的多线程程序中使用的那两条指令之间设置存储屏障.

Also, the gut feeling tells me that I am right because otherwise we won't need a store barrier between those two instructions as used in typical multi-threaded programs.

但是,这是我们的维基百科所说的:

But, here is what our wikipedia says:

..在上面的概述中,OoOE处理器避免了因当指令为由于缺少数据,尚未完全准备好进行处理.

.. In the outline above, the OoOE processor avoids the stall that occurs in step (2) of the in-order processor when the instruction is not completely ready to be processed due to missing data.

OoOE处理器及时用其他指令填充这些插槽"准备好了,然后在最后重新排序结果以使其显示指令已正常处理.

OoOE processors fill these "slots" in time with other instructions that are ready, then re-order the results at the end to make it appear that the instructions were processed as normal.

我很困惑.是说结果必须按顺序写回吗?真的,在OoOE处理器中,可以存储到 data ready 重新排序吗?

I'm confused. Is it saying that the results have to be written back in-order? Really, in an OoOE processor, can store to data and ready be reordered?

推荐答案

体系结构的一致性模型(或内存模型)确定可以对哪些内存操作进行重新排序.这个想法始终是从代码中获得最佳性能,同时保留程序员期望的语义.从维基百科来看,存储操作是为了程序员而出现的,即使它们可能已经重新排序.当代码为单线程时,重新排序通常是安全的,因为处理器可以轻松检测到潜在的违规行为.

The consistency model (or memory model) for the architecture determines what memory operations can be reordered. The idea is always to achieve the best performance from the code, while preserving the semantics expected by the programmer. That is the point from wikipedia, the memory operations appear in order to the programmer, even though they may have been reordered. Reordering is generally safe when the code is single-threaded, as the processor can easily detect potential violations.

在x86上,通用模型是写入不与其他写入重新排序.但是,处理器使用的是乱序执行(OoOE),因此指令不断地重新排序.通常,处理器具有一些其他硬件结构来支持OoOE,例如重排序缓冲区和负载存储队列.重排序缓冲区可确保所有指令看起来都按顺序执行,以使中断和异常中断程序中的特定点.加载存储队列的功能类似,因为它可以根据内存模型恢复内存操作的顺序.加载存储队列还可以消除地址的歧义,以便处理器可以识别何时对相同或不同的地址进行操作.

On x86, the common model is that writes are not reordered with other writes. Yet, the processor is using out of order execution (OoOE), so instructions are being reordered constantly. Generally, the processor has several additional hardware structures to support OoOE, like a reorder buffer and load-store queue. The reorder buffer ensures that all instructions appear to execute in order, such that interrupts and exceptions break a specific point in the program. The load-store queue functions similarly, in that it can restore the order of memory operations according to the memory model. The load-store queue also disambiguates addresses, so that the processor can identify when the operations are made to the same or different addresses.

回到OoOE,处理器在每个周期中执行10s至100s的指令.加载和存储正在计算其地址,等等.处理器可能会预取用于访问的高速缓存行(可能包括高速缓存一致性),但是在安全之前(根据内存模型),它实际上无法访问该行以进行读取或写入).

Back to OoOE, the processor is executing 10s to 100s of instructions in every cycle. Loads and stores are computing their addresses, etc. The processor may prefetch the cache lines for the accesses (which may include cache coherence), but it cannot actually access the line either to read or write until it is safe (according to the memory model) to do so.

插入存储屏障,内存栅栏等会告诉编译器和处理器有关对内存操作重新排序的进一步限制.编译器是实现内存模型的一部分,因为某些语言(如Java)具有特定的内存模型,而其他语言(如C)则遵循内存访问应看起来像按顺序执行一样."

Inserting store barriers, memory fences, etc tell both the compiler and processor about further restrictions to reordering the memory operations. The compiler is part of implementing the memory model, as some languages like java have specific memory model, while others like C obey the "memory accesses should appear as if they were executed in order".

最后,是的,可以在OoOE中对数据和就绪数据进行重新排序.但这取决于内存模型是否真正存在.因此,如果您需要特定的顺序,请使用障碍等提供适当的指示,以使编译器,处理器等不会选择其他顺序来获得更高的性能.

In conclusion, yes, data and ready can be reordered in an OoOE. But it depends on the memory model as to whether they actually are. So if you need a specific order, provide the appropriate indication using barriers, etc such that the compiler, processor, etc will not choose a different order for higher performance.

这篇关于真的可以在OoOE处理器中对存储器进行重新排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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