什么是“虚假分享"?如何重现/避免它? [英] What is "false sharing"? How to reproduce / avoid it?

查看:41
本文介绍了什么是“虚假分享"?如何重现/避免它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我和我的教授在并行编程课上有了不同的理解,关于什么是虚假共享".我的教授说的没有意义,所以我立即指出.她认为虚假分享"会导致节目结果出错.

Today I got a different understand with my professor on the Parallel Programming class, about what is "false sharing". What my professor said makes little sense so I pointed it out immediately. She thought "false sharing" will cause a mistake in the program's result.

我说过,当不同的内存地址分配给同一个缓存行时,就会发生错误共享",将数据写入其中一个会导致另一个被踢出缓存.如果处理器在两个伪共享地址之间写转转,两者都不能停留在缓存上,所有的操作都会导致DRAM的访问.

I said, "false sharing" happens when different memory address are assigned to the same cache line, writing data to one of it will cause another being kicked out of the cache. If the processors write between the two false sharing address turn and turn about, both of them could not stay on the cache so all operations will result in the access of DRAMs.

这是我目前的看法.事实上,我也不确定我说了什么......如果我有误解,请指出.

That's my opinion so far. In fact I'm not definitely sure about what I said either... If I got a misunderstanding just point it out please.

所以有一些问题.缓存假定为 64 字节对齐,4 路组关联.

So there are some questions. The cache is assumed 64 bytes aligned, 4-way set-associative.

  1. 两个地址间隔超过 64 字节的地址是否有可能是虚假共享"?
  2. 单线程程序是否有可能遇到错误共享"问题?
  3. 重现虚假共享"的最佳代码示例是什么?
  4. 一般来说,避免程序员虚假共享"应该注意什么?

推荐答案

我会就你的问题分享我的观点.

I'll share my point of view on your questions.

  1. 由比块大小更多的字节分隔的两个地址不会驻留在完全相同的缓存行上.因此,如果一个内核在其缓存中具有第一个地址,而另一个内核请求第二个地址,则第一个内核不会因为该请求而从缓存中删除.因此不会发生错误的共享未命中.

  1. Two addresses that are separated by more bytes than block's size, won't reside on the exact same cache line. Thus, if a core has the first address in its cache, and another core requests the second address, the first won't be removed from cache because of that request. So a false sharing miss won't occur.

我无法想象在完全没有并发的情况下会如何发生错误共享,因为除了单线程之外没有其他任何人来竞争缓存行.

I can't imagine how false sharing would occur when there's no concurrency at all, as there won't be anyone else but the single thread to compete for the cache line.

取自 此处,使用 OpenMP,重现虚假共享的简单示例是:

Taken from here, using OpenMP, a simple example to reproduce false sharing would be:

double sum=0.0, sum_local[NUM_THREADS];

#pragma omp parallel num_threads(NUM_THREADS)
{
    int me = omp_get_thread_num();
    sum_local[me] = 0.0;

    #pragma omp for
    for (i = 0; i < N; i++)
        sum_local[me] += x[i] * y[i];

    #pragma omp atomic
    sum += sum_local[me];
}

  • 一些一般注意事项,我可以想到避免错误共享:

  • Some general notes that I can think of to avoid false sharing would be:

    一个.尽可能使用私人数据.

    a. Use private data as much as possible.

    B.有时您可以使用 padding 来对齐数据,以确保没有其他变量驻留在共享数据所在的同一个缓存中.

    b. Sometimes you can use padding in order to align data, to make sure that no other variables will reside in the same cache that shared data reside.

    欢迎任何更正或补充.

    这篇关于什么是“虚假分享"?如何重现/避免它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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