gpt4 book ai didi

cpu-architecture - 了解内存模型

转载 作者:行者123 更新时间:2023-12-04 20:22:42 24 4
gpt4 key购买 nike

我最近一直在阅读内存模型,但我对它是如何工作的感到有些困惑。

报价 http://cis.poly.edu/muller/CS623/weakmemory.htm

if processor writes a new X then writes a new Y, all other processors that subsequently execute a read Y then a read X, will access either the new Y and new X, the old Y and the new X, or old X and the old Y: but no processor will access the new Y and the old X. This assumption of strong ordering was, at one time, reasonable. Current computer manufactures, however, recommend that programmers not rely on memory ordering. This is because newer memory management systems attempt to reorder memory access for optimization purposes. Systems that are allowed to reorder memory request are called weakly-ordered memory systems (models). To examine how a reordering might be used to improve performance, consider the following assembler code [2].


Load reg1, A                 // register1 = contents of memory A
Load reg2, B // register2 = contents of memory B
ADD reg3, reg1, reg2 // register3 = register1 + register2
Store reg3, C // contents of memory C = contents of register3

If we assume that location B is currently in cache and location A is not cached, then loading A will take longer than B. Instead of waiting for A, the CPU can fetch B from its cache, hiding B’s latency: thus the CPU can perform the addition as soon as A is available. By relaxing the strong (sequential) memory model of execution (i.e., A must load first, followed by B), greater performance is possible----but reordering may not be transparent to software. Consider the code fragment below, it is part of the code that can be used to implements a spinlock semaphore [2].



我的问题是,较弱的内存模型怎么可能导致处理器可以访问新的 Y 和旧的 X。它不是写入相同的内存(ram)还是工作方式不同?我假设如果一个进程修改了一个变量,然后另一个进程读取它,它会读取最新的值。

我不确定的另一件事是哪个组件允许内存访问重新排序,我现在的假设是允许编译器重新排序指令。但是 CPU 也可以对它们重新排序吗?

谢谢

最佳答案

您必须记住缓存位于 CPU 和内存之间。当软件写入一个值时,它肯定会进入缓存,但如果在缓存将其复制到 RAM 之前再次写入该值,它可能永远不会进入主内存(想想循环变量和局部变量)。整个讨论围绕数据实际放入 RAM 或从 RAM 读取的不同模型展开。在内核中,这并不重要,因为它们将使用来自缓存或 RAM 的最后写入的值。

关于cpu-architecture - 了解内存模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4594498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com