c - 虚假共享和 pthreads

标签 c pthreads false-sharing

我有以下任务来演示虚假共享并编写了一个简单的程序:

#include <sys/times.h>
#include <time.h>
#include <stdio.h> 
#include <pthread.h> 

long long int tmsBegin1,tmsEnd1,tmsBegin2,tmsEnd2,tmsBegin3,tmsEnd3;

int array[100];

void *heavy_loop(void *param) { 
  int   index = *((int*)param);
  int   i;
  for (i = 0; i < 100000000; i++)
    array[index]+=3;
} 

int main(int argc, char *argv[]) { 
  int       first_elem  = 0;
  int       bad_elem    = 1;
  int       good_elem   = 32;
  long long time1;
  long long time2;
  long long time3;
  pthread_t     thread_1;
  pthread_t     thread_2;

  tmsBegin3 = clock();
  heavy_loop((void*)&first_elem);
  heavy_loop((void*)&bad_elem);
  tmsEnd3 = clock();

  tmsBegin1 = clock();
  pthread_create(&thread_1, NULL, heavy_loop, (void*)&first_elem);
  pthread_create(&thread_2, NULL, heavy_loop, (void*)&bad_elem);
  pthread_join(thread_1, NULL);
  pthread_join(thread_2, NULL);
  tmsEnd1 = clock(); 

  tmsBegin2 = clock();
  pthread_create(&thread_1, NULL, heavy_loop, (void*)&first_elem);
  pthread_create(&thread_2, NULL, heavy_loop, (void*)&good_elem);
  pthread_join(thread_1, NULL);
  pthread_join(thread_2, NULL);
  tmsEnd2 = clock();

  printf("%d %d %d\n", array[first_elem],array[bad_elem],array[good_elem]);
  time1 = (tmsEnd1-tmsBegin1)*1000/CLOCKS_PER_SEC;
  time2 = (tmsEnd2-tmsBegin2)*1000/CLOCKS_PER_SEC;
  time3 = (tmsEnd3-tmsBegin3)*1000/CLOCKS_PER_SEC;
  printf("%lld ms\n", time1);
  printf("%lld ms\n", time2);
  printf("%lld ms\n", time3);

  return 0; 
} 

当我看到结果时,我感到非常惊讶(我在我的 i5-430M 处理器上运行它)。

  • 在虚假分享的情况下,为 1020 毫秒。
  • 在没有错误共享的情况下,它是 710 毫秒,只快了 30% 而不是 300%(有些网站上写的会比 300-400% 快)。
  • 在不使用 pthreads 的情况下,它是 580 毫秒。

请告诉我我的错误或解释为什么会这样。

最佳答案

错误共享是具有独立缓存的多个内核访问同一物理内存区域的结果(尽管不是同一个地址——那将是真正的共享)。

要了解虚假共享,您需要了解缓存。在大多数处理器中,每个内核都有自己的 L1 缓存,用于保存最近访问的数据。缓存按“行”组织,这些行是对齐的数据 block ,通常长度为 32 或 64 字节(取决于您的处理器)。当您从不在缓存中的地址读取时,整行从主内存(或 L2 缓存)读取到 L1。当您写入缓存中的地址时,包含该地址的行被标记为“脏”。

这就是共享方面的用武之地。如果多个核心正在从同一行读取,则它们每个都可以在 L1 中拥有该行的副本。但是,如果一个副本被标记为脏,它会使其他缓存中的行无效。如果这没有发生,那么在一个核心上进行的写入可能要等到很久以后才会对其他核心可见。所以下一次另一个核心去读取该行时,缓存未命中,它必须再次获取该行。

False 共享发生在核心读取和写入同一行上的不同地址时。尽管它们不共享数据,但缓存的行为就像它们一样,因为它们非常接近。

此效果在很大程度上取决于处理器的架构。如果你有一个单核处理器,你根本看不到效果,因为没有共享。如果您的缓存行更长,您会在“坏”和“好”情况下看到效果,因为它们仍然靠得很近。如果您的核心不共享 L2 缓存(我猜他们这样做),您可能会看到 300-400% 的差异,因为它们必须在缓存未命中时一直转到主内存。

您可能还想知道每个线程都在进行读写操作(+= 而不是 =)这一点很重要。某些处理器具有直写 高速缓存,这意味着如果内核写入不在高速缓存中的地址,它不会错过并从内存中获取该行。将此与回写 缓存进行对比,后者确实会在写入时丢失。

关于c - 虚假共享和 pthreads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8331255/

相关文章:

c - 指向字符串指针的空指针

pthreads 程序的竞争条件会使操作系统或 X 完全崩溃吗?

c++ - 多线程效率不高 : Debugging False Sharing?

c - ptr、*ptr 和 &ptr 之间的区别

c - 在 C 程序中读取命令行参数

android - 了解 rootadb 如何在 ELF 二进制文件中查找方法调用

c - 如何使用pthread_mutex_trylock?

PHP:线程代理共享一个公共(public)对象

c - OpenMP - 没有错误共享的数组插入