您的位置:首页 > 其它

多线程优化——超线程与多核

2014-06-13 00:01 127 查看
超线程是利用非硬件方式在单核上模拟多核的技术,摘录wiki.answer.com的解释:

Hyper-threading is using one processor but logically dividing it into two so that it gives the user the
benefit of two processors with only using the resources equivalent to almost one. This is achieved by
sharing, partitioning and duplicating the various resources almost into two processors. Used by the
latest Pentium processors, which are HT enabled, in layman's terms, it allows you to use more than two
applications at the same time without slowing down processing speed. Multi-threading is when various
processes are time sliced such that it gives the user the impression that all the programs are being run at
the same time. This is what happens on your computer regularly. Super-threading allows threads from
different processes to be executed at the same time unlike Multi-threading where every process has a
time slot during which, thread from only one process will be executed. But every time, if for example,
there are four instructions issued to the processor. They will all be from the same process. Hyper-
threading takes it a step further. It allows threads from different processes to be issued at the same time,
in turn utilizing the waste cycles of the processor(利用处理器流水线指令空闲周期,底层!!). You
can go to any Intel site for further info.


下面通过一个例子测试超线程与真正多核在性能上的差异,该例子中开2个线程,分别计算10000000000/2次加法,我们在main函数的开始设置处理器亲和,即指定线程可以运行在哪个processor上:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>

#define ITERATION 10000000000
#define THREAD_NUMBER 2

void* func(void* thread_param_ptr);

int main() {
	unsigned long mask = 3;
	if (sched_setaffinity(0,sizeof(mask),&mask)<0)
		exit(1);

	//used to store handle of each thread
	pthread_t* thread_array = (pthread_t*)malloc(sizeof(pthread_t)*THREAD_NUMBER);

	int i = 0;
	int res = 0;

	//create thread
	for (i = 0;i< THREAD_NUMBER;i++) {
		res = pthread_create(&thread_array[i], NULL, func, NULL);
		if (res != 0) {
			perror("Thread creation failed\n");
			exit(EXIT_FAILURE);
		}
	}
	//join thread to get return value
	for (i = 0;i< THREAD_NUMBER;i++) {
		res = pthread_join(thread_array[i], NULL);
                if (res != 0) {
                        perror("Thread join failed\n");
                        exit(EXIT_FAILURE);
                }
        }

	free(thread_array);
}

void* func(void* thread_param_ptr) {
	unsigned long i = 0;
	unsigned long iteration = ITERATION/THREAD_NUMBER;
	while (i < iteration)
		i++;
}


我的机器是双核的,再加上超线程的支持,一共有4个虚拟processor,可以通过more /proc/cpuinfo查看处理器信息。其中1、2号处理器是第一个核通过超线程模拟出来的,3、4号处理器是第二个核通过超线程模拟出来的,我们一共做6组测试,12、13、14、23、24、34,结果如下:

test_p12

real	0m15.445s
user	0m29.874s
sys	0m0.476s

test_p13

real	0m12.903s
user	0m24.926s
sys	0m0.376s

test_p14

real	0m13.212s
user	0m24.798s
sys	0m0.428s

test_p23

real	0m12.461s
user	0m24.762s
sys	0m0.000s

test_p24

real	0m12.440s
user	0m24.718s
sys	0m0.000s

test_p34

real	0m15.170s
user	0m30.078s
sys	0m0.000s


可以看出,超线程模拟的多核与真正的多核相比是有一定的性能差距的。

关于超线程,有一个比喻特别贴切:

2 cores with hyper threading appear as 4 logical cores in your system, but they are not the same
as a true quad core which has 4 physical cores. hyper threading allows a core to do 2 task at the same time as they have 2 threades on a single core, but those 2 threads share the core memory, it is faster than a dual core w/o hyper threading of course specially
in applications or softwares that can run multiple threads, but its not as fast as a true quad core that has 4 physical cores, because those threads dont share memory, they have their own core memory, so they can do task independently. its like comparing to
a person, a dual core w/o HT are 2 person each carry a sack of rice , while dual core w/HT are 2 person that each carry one and a half sack of rice for a total of 3 sacks of rice. while a true quad core with 4 physical cores are 4 persons.. so you know who
wins.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: