13

今天我和我的教授在并行编程课上对什么是“虚假共享”有了不同的理解。我的教授说的没有道理,所以我立即指出。她认为“虚假分享”会导致节目结果出现错误。

我说过,当不同的内存地址分配给同一个缓存行时,会发生“错误共享”,将数据写入其中一个会导致另一个被踢出缓存。如果处理器在两个虚假共享地址之间来回写入,它们都不能留在缓存中,因此所有操作都会导致对DRAM的访问。

到目前为止,这是我的看法。事实上,我也不确定我所说的......如果我有误解,请指出。

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

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

1 回答 1

6

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

  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.

  2. 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.

  3. 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];
    }
    
  4. Some general notes that I can think of to avoid false sharing would be:

    a. Use private data as much as possible.

    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.

于 2014-11-19T16:29:19.013 回答