超线程代码示例 [英] hyperthreading code example

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

问题描述

是否有一些示例代码可以说明英特尔的超线程性能?它是否可以从用户空间访问,或者该 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.

简单解释一下超线程:

  • 每个内核有多个寄存器组,但没有额外的执行单元
  • 超线程的调度或多或少是均匀的

因此,如果运行在同一核心上的两个线程使用不同的执行单元,并且每个线程本身具有太多的数据依赖项,那么您只能从超线程中获得额外的性能.例如,一个线程仅执行整数操作,另一个线程仅执行浮点操作.然后您可以看到额外的性能,因为您每个周期使用更多的执行单元.

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.

但这反过来又取决于您的操作系统如何将线程调度到超线程上.从操作系统的角度来看,每个超线程都是一个逻辑 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).

回答您的实际问题:您不能自己直接将代码部署到超线程上.操作系统会为你做这件事.您可以为用户态线程设置调度关联,但实际部署线程仍然完全取决于调度程序.这对程序员是透明的.一个好的调度器会首先在内核上均匀地部署您的代码,并且只有在所有内核都忙时才使用超线程.

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

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

以下示例代码将在逻辑 CPU 0 和 1 上部署两个线程,这将对应于第一个套接字的第一个逻辑内核上的两个超线程如果超线程已启用.仍然由调度程序实际将它们放在那里.如果这些超线程很忙,那么您的代码将休眠:

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;
}

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

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