超线程code例子 [英] hyperthreading code example

查看:197
本文介绍了超线程code例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些样本code,它体现了英特尔的超线程性能?它是在所有的从用户空间访问,或者这是否CPU为程序员做的所有工作透明?这是C,Linux操作系统。

Is there some sample code that exemplifies Intel's Hyperthreading performance? Is it at all accessible from user space, or does that CPU do all the work transparently for the programmer? This is for C, Linux.

推荐答案

超线程的性能取决于很多因素,是难以估计。

Hyperthreading performance depends on many factors and is difficult to estimate.

只是为了简要解释超线程:

Just to shortly explain Hyperthreading:


  • 每个核心都有一个以上的寄存器组,但没有额外的执行单元

  • 的超线程计划或多或少均匀

所以,你才真正得到更多的表现出来超线程的,如果在同一个内核上运行两个线程使用不同的执行单元和每个线程在它自己就会有太多的ADATA依赖。比如一个线程只做整数OPS,另一只浮点。然后,你可以看到额外的性能,因为你每次循环使用更多的执行单元。

So you only really get additional performance out of hyperthreads if the two threads running on the same core use different execution units and each thread on it's own would have too many adata dependencies. For example one thread only does integer ops, the other one only floating point. Then you can see extra performance because you are using more execution units per cycle.

但是,这又取决于你如何操作系统调度线程的到超线程。但从OS的点的每个超线程是逻辑的CPU。所以,这完全取决于调度放什么那里的时候。

But this in turn depends on how your OS schedules threads onto hyperthreads. From the point of view of the OS each hyperthread is a logical CPU. So it's entirely up to the scheduler what to put there and when.

在实践中超线程会给你最多10-20%的额外性能。在我们的HPC我们已经将它们关闭(发牌的原因主要是,虽然)。

In practice hyperthreads will give you at most 10-20% extra performance. On our HPC we have turned them off (for licensing reasons mainly though).

要回答您的实际问题:你不能部署code到超线程直接自己。操作系统会为你做的。您可以设置调度亲和力的用户级线程,但它仍然一切都取决于是将调度实际部署的线程。这是透明做程序员。一个好的调度程序将先均匀地部署在核心的code和只能求助于超线程,如果所有核心都很忙。

To answer your actual question: you can not deploy code onto hyperthreads directly yourself. The OS will do that for you. You can set scheduling affinities for your userland threads, but it is still all up to the scheduler to actually deploy your threads. This is done transparently to the programmer. A good scheduler will deploy your code evenly first on cores and only resort to hyperthreads if all cores are busy.

用户态coltrol 系统调用您正在寻找的的了sched_setaffinity pthread_setaffinity_np

The userland coltrol syscalls you are looking for are sched_setaffinity and pthread_setaffinity_np.

下面的例子code将部署在逻辑CPU 0和1两个线程,如果已启用超线程,这将对应于两个超线程上的第一个插槽的的第一个逻辑核心。还是它是由调度实际上把它们放在那里。如果这些超线程都忙着那么你的code将睡眠:

The following example code will deploy two threads on logical CPUs 0 and 1 which will correspond to the two hyperthreads on the first logical core of the first socket if hyperthreads are enabled. Still it is up to the scheduler to actually put them there. If those hyperthreads are busy then your code will sleep:

#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>

void * my_thread(intptr_t cput_o_run_on) {
    cpuset_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(cput_o_run_on, &cpuset);

    pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);

    // force a rescheduling
    sched_yield();

    // do something useful

    return NULL;
}

int main() {
    pthread_t thread;

    pthread_create(&thread, NULL, my_thread, 0);
    pthread_create(&thread, NULL, my_thread, 1);

    for (;;);

    return 0;
}

这篇关于超线程code例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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