マルチスレッド最適化:ハイパースレッドとマルチコア


ハイパースレッドは、非ハードウェア方式でシングルコア上でマルチコアをシミュレートする技術であり、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つのスレッドを開き、それぞれ10000000/2の加算を計算します.main関数の開始でプロセッサ親和性を設定します.つまり、スレッドがどのprocessor上で実行できるかを指定します.
#include 
#include 
#include 
#include 
#include 

#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
"); 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
"); 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番のプロセッサは1番目のコアがハイパースレッドでシミュレーションされ、3、4番のプロセッサは2番目のコアがハイパースレッドでシミュレーションされたもので、私たちは全部で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.