多线程优化——超线程与多核
2014-06-13 00:01
127 查看
超线程是利用非硬件方式在单核上模拟多核的技术,摘录wiki.answer.com的解释:
下面通过一个例子测试超线程与真正多核在性能上的差异,该例子中开2个线程,分别计算10000000000/2次加法,我们在main函数的开始设置处理器亲和,即指定线程可以运行在哪个processor上:
我的机器是双核的,再加上超线程的支持,一共有4个虚拟processor,可以通过more /proc/cpuinfo查看处理器信息。其中1、2号处理器是第一个核通过超线程模拟出来的,3、4号处理器是第二个核通过超线程模拟出来的,我们一共做6组测试,12、13、14、23、24、34,结果如下:
可以看出,超线程模拟的多核与真正的多核相比是有一定的性能差距的。
关于超线程,有一个比喻特别贴切:
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.
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.
相关文章推荐
- Android 应用性能优化(三) 多核下的多线程计算
- 多线程多核编码优化思路_转载
- java使用volatile以及双重检查加锁优化单例在多线程下的性能
- 多线程和多核下“锁”的应用
- 使用Task的一些知识优化了一下同事的多线程协作取消的一串代码
- 【总结】ITOO在线编辑性能优化——多线程
- Android 性能优化:多线程
- 并发和并行,多线程和多进程,单核和多核,同步和异步的关系
- 多线程并发 synchronized对象锁的控制与优化
- 案例分析:Radiosity 光能传递渲染器的多核优化
- 【超线程多核心下Java多线程编程分析】
- 并行硬件技术概览:多处理器、超线程、双核、多核与FPGA
- boost 对于多线程应用的优化
- sqlite3 多线程和锁 ,优化插入速度及性能优化
- Linux系统下,CPU信息详解(cpuinfo,多核,多线程)
- TensorFlow学习系列(五):如何使用队列和多线程优化输入管道
- 性能优化、多线程、handler、数字签名
- Android多线程任务优化1:探讨AsyncTask的缺陷
- 多线程占cup100%问题优化
- Java多线程优化之偏向锁原理分析