|
Posted on 2012-12-18 13:56 鑫龍 閱讀(592) 評論(0) 編輯 收藏 引用 所屬分類: linux多線程專題
實驗環境:
CPU: 雙核 Intel(R) Xeon(R) CPU 5130 @ 2.00GHz,內存:1G 系統:Red Hat Enterprise Linux ES release 4 (Nahant Update 4) 內核:2.6.9-42.ELsmp
實驗程序:
singlethread.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <pthread.h>
- #include <unistd.h>
- #define ORANGE_MAX_VALUE 1000000
- #define APPLE_MAX_VALUE 100000000
- #define MSECOND 1000000
- struct apple
- {
- unsigned long long a;
- unsigned long long b;
- };
- struct orange
- {
- int a[ORANGE_MAX_VALUE];
- int b[ORANGE_MAX_VALUE];
- };
- int main()
- {
- struct apple test;
- struct orange test1={{0},{0}};
-
- unsigned long long sum=0,index=0;
- struct timeval tpstart,tpend;
- float timeuse;
-
- test.a=0;
- test.b=0;
-
- gettimeofday(&tpstart,NULL);
-
- for(sum=0;sum<APPLE_MAX_VALUE;sum++)
- {
- test.a+=sum;
- test.b+=sum;
- }
-
- sum=0;
- for(index=0;index<ORANGE_MAX_VALUE;index++)
- {
- sum+=test1.a[index]+test1.b[index];
- }
-
- gettimeofday(&tpend,NULL);
-
- timeuse=MSECOND*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
- timeuse/=MSECOND;
- printf("main thread Used Time:%f\n",timeuse);
-
- printf("a = %llu,b = %llu,sum=%llu\n",test.a,test.b,sum);
-
- return 0;
- }
dualrhread.c- #include <sys/time.h>
- #include <pthread.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/sysinfo.h>
- #include<unistd.h>
- #define __USE_GNU
- #include<sched.h>
- #include<ctype.h>
- #include<string.h>
- #include <sys/syscall.h> /*此頭必須帶上*/
- pid_t gettid()
- {
- return syscall(SYS_gettid);
- }
- #define ORANGE_MAX_VALUE 1000000
- #define APPLE_MAX_VALUE 100000000
- #define MSECOND 1000000
- struct apple
- {
- unsigned long long a;
- unsigned long long b;
- };
- struct orange
- {
- int a[ORANGE_MAX_VALUE];
- int b[ORANGE_MAX_VALUE];
- };
- struct apple test;
- struct orange test1;
- int cpu_nums;
- cpu_set_t mask;
- //cpu_set_t get;
-
- inline int set_cpu(int i)
- {
- __CPU_ZERO(&mask);
-
- if(2 <= cpu_nums)
- {
- __CPU_SET(i,&mask);
-
- if(-1 == sched_setaffinity(gettid(),sizeof(&mask),&mask))
- {
- return -1;
- }
- }
- return 0;
- }
- void* add(void* x)
- {
- int i;
- if(-1 == set_cpu(1))
- {
- return NULL;
- }
- /*
- __CPU_ZERO(&get);
- if (sched_getaffinity(0, sizeof(get), &get) == -1)//獲取線程CPU親和力
- {
- printf("warning: cound not get thread affinity, continuing...\n");
- }
-
- for (i = 0; i < 2; i++)
- {
- if (__CPU_ISSET(i, &get))//判斷線程與哪個CPU有親和力
- {
- printf("this thread %d is running processor : %d\n", i,i);
- }
- }
- */
- unsigned long long sum=0,index=0;
- for(sum=0;sum<APPLE_MAX_VALUE;sum++)
- {
- ((struct apple *)x)->a += sum;
- ((struct apple *)x)->b += sum;
- }
-
- return NULL;
- }
-
- int main ()
- {
-
- pthread_t ThreadA;
- unsigned long long sum=0,index=0;
- struct timeval tpstart,tpend;
- float timeuse;
- int i;
-
- test.a=0;
- test.b=0;
-
- cpu_nums = sysconf(_SC_NPROCESSORS_CONF);
-
- if(-1 == set_cpu(0))
- {
- return -1;
- }
-
- gettimeofday(&tpstart,NULL);
-
- pthread_create(&ThreadA,NULL,add,&test);
-
- /*
- __CPU_ZERO(&get);
- if (sched_getaffinity(0, sizeof(get), &get) == -1)//獲取線程CPU親和力
- {
- printf("warning: cound not get thread affinity, continuing...\n");
- }
-
- for (i = 0; i < 2; i++)
- {
- if (__CPU_ISSET(i, &get))//判斷線程與哪個CPU有親和力
- {
- printf("this thread %d is running processor : %d\n", i,i);
- }
- }
- */
- for(index=0;index<ORANGE_MAX_VALUE;index++)
- {
- sum += test1.a[index]+test1.b[index];
- }
-
- pthread_join(ThreadA,NULL);
- gettimeofday(&tpend,NULL);
-
- timeuse=MSECOND*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
- timeuse/=MSECOND;
- printf("dual thread Used Time:%f\n",timeuse);
-
- printf("a = %llu,b = %llu,sum=%llu\n",test.a,test.b,sum);
-
- return 0;
- }
threethread.c- #include <sys/time.h>
- #include <pthread.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/sysinfo.h>
- #include<unistd.h>
- #define __USE_GNU
- #include<sched.h>
- #include<ctype.h>
- #include<string.h>
- #include <sys/syscall.h> /*此頭必須帶上*/
- pid_t gettid()
- {
- return syscall(SYS_gettid);
- }
- #define ORANGE_MAX_VALUE 1000000
- #define APPLE_MAX_VALUE 100000000
- #define MSECOND 1000000
- struct apple
- {
- unsigned long long a;
- //char c[128]; /*32,64,128*/
- unsigned long long b;
- //pthread_rwlock_t rwLock;
- };
- struct orange
- {
- int a[ORANGE_MAX_VALUE];
- int b[ORANGE_MAX_VALUE];
- };
- struct apple test;
- struct orange test1;
- int cpu_nums;
- cpu_set_t mask;
-
- inline int set_cpu(int i)
- {
- __CPU_ZERO(&mask);
-
- if(2 <= cpu_nums)
- {
- __CPU_SET(i,&mask);
-
- if(-1 == sched_setaffinity(gettid(),sizeof(&mask),&mask))
- {
- return -1;
- }
- }
- return 0;
- }
- void* addx(void* x)
- {
- /*
- if(-1 == set_cpu(0))
- {
- return NULL;
- }
- */
- unsigned long long sum=0,index=0;
- //pthread_rwlock_wrlock(&((struct apple *)x)->rwLock);
- for(sum=0;sum<APPLE_MAX_VALUE;sum++)
- {
- ((struct apple *)x)->a += sum;
- }
- //pthread_rwlock_unlock(&((struct apple *)x)->rwLock);
-
- return NULL;
- }
- void* addy(void* y)
- {
- /*
- if(-1 == set_cpu(1))
- {
- return NULL;
- }
- */
- unsigned long long sum=0,index=0;
- //pthread_rwlock_wrlock(&((struct apple *)y)->rwLock);
- for(sum=0;sum<APPLE_MAX_VALUE;sum++)
- {
- ((struct apple *)y)->b += sum;
- }
- //pthread_rwlock_unlock(&((struct apple *)y)->rwLock);
-
- return NULL;
- }
- int main ()
- {
- pthread_t ThreadA,ThreadB;
- unsigned long long sum=0,index=0;
- struct timeval tpstart,tpend;
- float timeuse;
- /*
- cpu_nums = sysconf(_SC_NPROCESSORS_CONF);
-
- if(-1 == set_cpu(0))
- {
- return -1;
- }
- */
- gettimeofday(&tpstart,NULL);
-
- pthread_create(&ThreadA,NULL,addx,&test);
- pthread_create(&ThreadB,NULL,addy,&test);
- for(index=0;index<ORANGE_MAX_VALUE;index++)
- {
- sum+=test1.a[index]+test1.b[index];
- }
-
- pthread_join(ThreadA,NULL);
- pthread_join(ThreadB,NULL);
-
- gettimeofday(&tpend,NULL);
-
- timeuse=MSECOND*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
- timeuse/=MSECOND;
- printf("thread thread,Used Time:%f\n",timeuse);
-
- printf("a = %llu,b = %llu,sum=%llu\n",test.a,test.b,sum);
-
- return 0;
- }
本文采用采用 gettimeofday() 來獲取系統時鐘(system clock)時間,可以精確到微秒。本文平均值算法采用的是去掉一個最大值去掉一個最小值,然后平均
一、單核 單、多線程效率比較
單核 | | 第1次 | 第2次 | 第3次 | 第4次 | 第5次 | 第6次 | 第7次 | 第8次 | 第9次 | 第10次 | 平均 | 單線程 | 0.951274 | 0.951883 | 0.951347 | 0.950893 | 0.810648 | 0.810705 | 0.951196 | 0.951343 | 0.811413 | 0.950892 | 0,916133 | 雙線程 | 0.989287 | 0.986391 | 0.986576 | 0.985043 | 0.986335 | 0.985320 | 0.986790 | 0.986837 | 0.985602 | 0.986535 | 0.986298 | 三線程加鎖 | 1.318721 | 1.318701 | 1.319036 | 1.318652 | 1.318614 | 1.318923 | 1.318636 | 1.318687 | 1.319026 | 1.318933 | 1.318785 | 三線程不加鎖 | 1.318658 | 1.318766 | 1.318570 | 1.318894 | 1.318588 | 1.318658 | 1.318958 | 1.318709 | 1.318689 | 1.319020 | 1.318740 |
 為什么多線程會比單線程更耗時呢?其原因就在于,線程啟停以及線程上下文切換都會引起額外的開銷,所以消耗的時間比單線程多。 二、雙核---單線程、多線程、優化后多線程效率比較雙核三線程不加鎖 | | 第1次 | 第2次 | 第3次 | 第4次 | 第5次 | 第6次 | 第7次 | 第8次 | 第9次 | 第10次 | Best | 單線程 | 0.947490 | 0.948153 | 0.950648 | 0.950412 | 0.950422 | 0.950407 | 0.810345 | 0.950386 | 0.947501 | 0.947513 | 0.949036 | 雙線程 | 0.983432 | 0.980912 | 0.976860 | 0.975621 | 0.976582 | 0.978240 | 0.975736 | 0.981523 | 0.979724 | 0.981337 | 0.978864 | 三線程加鎖 | 1.310762 | 1.309590 | 1.308914 | 1.309287 | 1.310924 | 1.310837 | 1.307147 | 1.311698 | 1.311861 | 1.309007 | 1.310127 | 三線程不加鎖 | 1.431863 | 1.422951 | 1.472916 | 1.482321 | 1.477823 | 1.477272 | 1.423854 | 1.423412 | 1.422267 | 1.423243 | 1.444167 | 三線程加鎖Cache32 | 1.310174 | 1.308985 | 1.311462 | 1.311574 | 1.309590 | 1.311329 | 1.311602 | 1.311154 | 1.307573 | 1.310447 | 1.310589 | 三線程不加鎖Cache32 | 0.664234 | 0.664244 | 0.664186 | 0.664264 | 0.664317 | 0.664234 | 0.662658 | 0.662176 | 0.662164 | 0.662159 | 0.663520 |
 為什么三線程不加鎖比三線程加鎖還慢呢?當兩個線程寫入同一個 cache 的不同部分時,會互相競爭該 cache 行,也就是寫后寫的問題。下文會詳細分析。 為什么三線程加鎖Cache32比三線程不加鎖Cache32比慢幾乎一倍呢?
其原因也很簡單,那把讀寫鎖就是罪魁禍首。實際情況并不是并行執行,反而成了串行執行。 三、針對 Cache 的優化 在串行程序設計過程中,為了節約帶寬或者存儲空間,比較直接的方法,就是對數據結構做一些針對性的設計,將數據壓縮 (pack) 的更緊湊,減少數據的移動,以此來提高程序的性能。但在多核多線程程序中,這種方法往往有時會適得其反。 數據不僅在執行核和存儲器之間移動,還會在執行核之間傳輸。根據數據相關性,其中有兩種讀寫模式會涉及到數據的移動:寫后讀和寫后寫 ,因為這兩種模式會引發數據的競爭,表面上是并行執行,但實際只能串行執行,進而影響到性能。 處理器交換的最小單元是 cache 行,或稱 cache 塊。在多核體系中,對于不共享 cache 的架構來說,兩個獨立的 cache 在需要讀取同一 cache 行時,會共享該 cache 行,如果在其中一個 cache 中,該 cache 行被寫入,而在另一個 cache 中該 cache 行被讀取,那么即使讀寫的地址不相交,也需要在這兩個 cache 之間移動數據,這就被稱為 cache 偽共享,導致執行核必須在存儲總線上來回傳遞這個 cache 行,這種現象被稱為“乒乓效應”。 同樣地,當兩個線程寫入同一個 cache 的不同部分時,也會互相競爭該 cache 行,也就是寫后寫的問題。上文曾提到,不加鎖的方案反而比加鎖的方案更慢,就是互相競爭 cache 的原因。 在 X86 機器上,某些處理器的一個 cache 行是64字節,具體可以參看 Intel 的參考手冊。 既然不加鎖三線程方案的瓶頸在于 cache,那么讓 apple 的兩個成員 a 和 b 位于不同的 cache 行中,效率會有所提高嗎? 修改后的代碼片斷如下:
- struct apple
- {
- unsigned long long a;
- char c[128]; /*32,64,128*/
- unsigned long long b;
- };
|