Using POSIX thread for multi-core and multi-thread parallel computing in Linux

Source: Internet
Author: User

The POSIX thread library encapsulates a thread processing interface based on processor, operating system, and other features. For Linux systems based on the x86 processor architecture, it usually schedules a newly created thread to a core different from the main thread for execution by default, if this can better balance the load. Therefore, when using POSIX Threads, developers cannot assert whether the currently created thread runs at the same core as the main thread, or assert that it must run at a different core than the main thread. Of course, since POSIX Threads have round robin scheduling (round-robin), even if they are at the same core as the main thread, the performance will not be significantly affected by the use of spin locks. However, it is best to use a Grand Central Dispatch mechanism similar to OS X and iOS to explicitly display the thread scheduling queue.

When using POSIX Threads in Linux, you must first add the libpthread. So dynamic library. Therefore, add-lpthread to the connector options.

The following code completes the test in Lenovo z475, amd Apu A6-3420M, 4 GB ddr3, ubuntu12.04 system.

C code:

/* ============================================================================ 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    1000000extern 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}

 

Assembly code:

.text.align 2.globl getRealtimeCyclesgetRealtimeCycles:    rdtsc    shl     $32, %rdx    or      %rdx, %rax    ret

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.