java与线程绑定_memset与绑定到每个物理核心的线程并行

我的系统是一个单插槽Xeon E5-1620,它是一个Ivy Bridge处理器,有4个物理内核和8个超线程 . 我使用的是Ubuntu 14.04 LTS,Linux Kernel 3.13,GCC 4.9.0和EGLIBC 2.19 . 我用 gcc -fopenmp -O3 mem.c 编译

当我在链接中运行代码时,它默认为八个线程并给出

Touch: 11830.448 MB/s

Rewrite: 18133.428 MB/s

但是,当我绑定线程并将线程数设置为这样的物理核心数

export OMP_NUM_THREADS=4

export OMP_PROC_BIND=true

我明白了

Touch: 22167.854 MB/s

Rewrite: 18291.134 MB/s

触控率增加了一倍!绑定后运行几次总是比重写更快 . 我不明白这一点 . Why is touch faster than rewrite after binding the threads and setting them to the number of physical cores? Why has the touch rate doubled?

这是我使用的代码,没有修改Hristo Iliev的答案 .

#include

#include

#include

void zero(char *buf, size_t size)

{

size_t my_start, my_size;

if (omp_in_parallel())

{

int id = omp_get_thread_num();

int num = omp_get_num_threads();

my_start = (id*size)/num;

my_size = ((id+1)*size)/num - my_start;

}

else

{

my_start = 0;

my_size = size;

}

memset(buf + my_start, 0, my_size);

}

int main (void)

{

char *buf;

size_t size = 1L << 31; // 2 GiB

double tmr;

buf = malloc(size);

// Touch

tmr = -omp_get_wtime();

#pragma omp parallel

{

zero(buf, size);

}

tmr += omp_get_wtime();

printf("Touch: %.3f MB/s\n", size/(1.e+6*tmr));

// Rewrite

tmr = -omp_get_wtime();

#pragma omp parallel

{

zero(buf, size);

}

tmr += omp_get_wtime();

printf("Rewrite: %.3f MB/s\n", size/(1.e+6*tmr));

free(buf);

return 0;

}

编辑:没有胎面装订,但在这里使用四个线程的结果是运行八次 .

Touch: 14723.115 MB/s, Rewrite: 16382.292 MB/s

Touch: 14433.322 MB/s, Rewrite: 16475.091 MB/s

Touch: 14354.741 MB/s, Rewrite: 16451.255 MB/s

Touch: 21681.973 MB/s, Rewrite: 18212.101 MB/s

Touch: 21004.233 MB/s, Rewrite: 17819.072 MB/s

Touch: 20889.179 MB/s, Rewrite: 18111.317 MB/s

Touch: 14528.656 MB/s, Rewrite: 16495.861 MB/s

Touch: 20958.696 MB/s, Rewrite: 18153.072 MB/s

编辑:

我在其他两个系统上测试了这个代码,我无法重现它们的问题

i5-4250U(Haswell) - 2个物理内核,4个超线程

4 threads unbound

Touch: 5959.721 MB/s, Rewrite: 9524.160 MB/s

2 threads bound to each physical core

Touch: 7263.175 MB/s, Rewrite: 9246.911 MB/s

四个插座E7- 4850 - 10个物理内核,每个插槽20个超线程

80 threads unbound

Touch: 10177.932 MB/s, Rewrite: 25883.520 MB/s

40 threads bound

Touch: 10254.678 MB/s, Rewrite: 30665.935 MB/s

这表明将线程绑定到物理内核确实改善了触摸和重写,但触摸比在这两个系统上重写要慢 .

我还测试了memset的三种不同变体: my_memset , my_memset_stream 和 A_memset . 函数 my_memset 和 my_memset_stream 定义如下 . 函数 A_memset 来自Agner Fog的asmlib .

my_memset结果:

Touch: 22463.186 MB/s

Rewrite: 18797.297 MB/s

我认为这表明问题不在EGLIBC的memset函数中 .

A_memset结果:

Touch: 18235.732 MB/s

Rewrite: 44848.717 MB/s

my_memset_stream:

Touch: 18678.841 MB/s

Rewrite: 44627.270 MB/s

看一下asmlib的源代码,我看到了用于编写非时间存储的大块内存 . 这就是为什么 my_memset_stream 得到's about the same bandwidth as Agner Fog'的asmlib . maximum throughput of this system is 51.2 GB/s . 所以这表明 A_memset 和 my_memset_stream 获得了大约85%的最大吞吐量 .

void my_memset(int *s, int c, size_t n) {

int i;

for(i=0; i

s[i] = c;

}

}

void my_memset_stream(int *s, int c, size_t n) {

int i;

__m128i v = _mm_set1_epi32(c);

for(i=0; i

_mm_stream_si128((__m128i*)&s[i], v);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值