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

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

问题描述

今天,我与并行编程课程的教授有了不同的理解,即什么是虚假共享".我的教授说的话毫无意义,所以我立即指出了这一点.她认为虚假共享"会导致程序结果错误.

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.尽可能使用私人数据.

    a. Use private data as much as possible.

    b.有时,您可以使用填充来对齐数据,以确保没有其他变量会驻留在共享数据所在的缓存中.

    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.

    欢迎进行任何更正或添加.

    Any correction or addition is welcome.

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

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