歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下使用POSIX Thread作多核多線程並行計算

Linux下使用POSIX Thread作多核多線程並行計算

日期:2017/3/1 9:20:14   编辑:Linux編程

POSIX線程庫根據處理器、操作系統等特性封裝了一台線程處理的接口。對於目前基於x86處理器架構的Linux系統來說,它往往會默認地將新創建的一個線程調度到與主線程不同的核中執行,如果這樣能更好地平衡負荷的話。因此,在使用POSIX線程時,開發者不能斷言當前創建的線程是否運行在與主線程相同的核心下,也不能斷言一定運行在與主線程不同的核心下。當然,由於POSIX線程具有時間片輪詢調度(Round-Robin),因此即便與主線程處於一個核心,使用旋鎖的話,性能影響也不會太大。不過多核多線程其實最好還是使用類似於OS X以及iOS中的Grand Central Dispatch機制,顯式給出線程的調度隊列。

在Linux下使用POSIX線程時應當先得加上libpthread.so動態庫,因此在連接器選項中加上-lpthread。

以下代碼在聯想Z475,AMD APU A6-3420M,4GB DDR3,Ubuntu12.04系統下完成測試。

C代碼:

/*

============================================================================

Name : ThreadTest.c

Author : Zenny Chen

Version :

Copyright : Your copyright notice

Description : Hello World in C, Ansi-style

============================================================================

*/

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#define TOTAL_CALC_ITEMS 1000000

extern unsigned long getRealtimeCycles(void);

static volatile _Bool isFinished = 0;

static volatile int sum4Core2 = 0;

static void* threadProc(void *param)

{

int sum = 0;

int *pSrc = (int*)param;

for(int i = TOTAL_CALC_ITEMS / 2; i < TOTAL_CALC_ITEMS; i++)

sum += pSrc[i];

sum4Core2 = sum;

isFinished = 1;

return NULL;

}

static int sourceArray[TOTAL_CALC_ITEMS];

static void testOpt(void)

{

// Initialize the array

for(int i = 0; i < TOTAL_CALC_ITEMS; i++)

sourceArray[i] = i + 1;

pthread_t threadHandle;

pthread_attr_t attr;

if(pthread_attr_init(&attr) != 0)

{

puts("Attribute failed to create!");

return;

}

pthread_attr_setschedpolicy(&attr, SCHED_OTHER);

if(pthread_create(&threadHandle, &attr, &threadProc, sourceArray) != 0)

{

puts("Thread failed to create!");

return;

}

int polacy = -1;

pthread_attr_getschedpolicy(&attr, &polacy);

printf("Current sched polacy is: %d\n", polacy);

int sum = 0;

unsigned long ticks = getRealtimeCycles();

for(int i = 0; i < TOTAL_CALC_ITEMS / 2; i++)

sum += sourceArray[i];

while(!isFinished)

__asm__("pause");

sum += sum4Core2;

ticks = getRealtimeCycles() - ticks;

printf("The number of cycles is: %lu\n", ticks);

printf("The sum is: %d\n", sum);

pthread_attr_destroy(&attr);

}

static void testNaive(void)

{

// Initialize the array

for(int i = 0; i < TOTAL_CALC_ITEMS; i++)

sourceArray[i] = i + 1;

int sum = 0;

unsigned long ticks = getRealtimeCycles();

for(int i = 0; i < TOTAL_CALC_ITEMS; i++)

sum += sourceArray[i];

ticks = getRealtimeCycles() - ticks;

printf("The number of cycles is: %lu\n", ticks);

printf("The sum is: %d\n", sum);

}

int main(void)

{

testOpt();

testNaive();

testOpt();

testNaive();

return EXIT_SUCCESS; // 1784293664

}


匯編代碼:

.text
.align 2
.globl getRealtimeCycles

getRealtimeCycles:

rdtsc
shl $32, %rdx
or %rdx, %rax
ret

Copyright © Linux教程網 All Rights Reserved