Implementation of multi-threaded sequencing under Linux

Source: Internet
Author: User

For computationally intensive tasks, the computational efficiency can be greatly improved if a reasonable multithreading process is used. This blog post implements multithreading and explains some of the issues that need to be noticed.

First, say the general idea: divide the elements into n segments, and use the quick sort to process multiple threads in parallel. Finally, you need to wait for these threads to sort through the sequence, making a similar merge ordering process.

The time complexity is calculated (assuming I'm a 4-core machine) O (N+n/4log (N/4)), which is about one-fold faster than O (NLOGN). (Please bring in numerical calculation)

Let's start by introducing the Pthread_barrier series functions.

Function prototypes: #include <pthread.h>int pthread_barrier_init (pthread_barrier_t *restrict barrier, const PTHREAD_ barrierattr_t *restrict attr, unsigned count); int pthread_barrier_wait (pthread_barrier_t *barrier); int Pthread_ Barrier_destroy (pthread_barrier_t *barrier);

Parameter explanation:

pthread_barrier_t, is a count lock, the operation of the lock is contained within three functions, we do not care or direct operation. Just instantiate an object and throw it to it just fine.

pthread_barrierattr_t, set the property of the lock, set NULL to allow the function to use the default property.

Count, the number of waits you want to specify.


Popular explanations:

pthread_barrier_* actually only do and only do one thing, is to act as a railing (barrier means railing). The image is that the number of threads arriving in front of the same railing, until all threads to the line, and then remove the railing and release. 1) The INIT function is responsible for specifying the number of threads to wait, 2) the Wait () function is called by each thread, and it tells the railing "I'm in front of the starting line." Wait () Executes the end railing to check if everyone is in front of the railing, and if it is, the railing disappears. All threads continue to execute the next code; if not, all threads that have been to wait () stop at the function, leaving the thread that does not execute to wait () to continue execution ; 3) The Destroy function releases the resources for the INIT request.

Single Thread sequencing:

#include <unistd.h> #include <sys/time.h> #include <pthread.h> #include <string.h> #include <string> #include <cstdio> #include <iostream> #include <errno.h> #include <stdlib.h># Include <algorithm>using namespace std;//error checking function inline void err_exit (const string &msg,int retnum) {if (retnum!        =0) {cerr<<msg<< ":" <<strerror (retnum) <<endl;    Exit (Exit_failure);    }} #define NUMMAX 8000000Llong int Nums[nummax];int main () {Srandom (Time (NULL));    For (unsigned long i=0;i<nummax;i++) nums[i]=random ();    Timing begins gettimeofday (&start,null);    Sort (Nums,nums+nummax);    Gettimeofday (&end,null);    The calculation takes a long long startusec=start.tv_sec*1000000+start.tv_usec;    Long Long endusec=end.tv_sec*1000000+end.tv_usec;    Double elapsed= (double) (endusec-startusec)/1000000.0;    printf ("Sort took%.4f seconds\n", elapsed);    FILE *fp=fopen ("Save.txt", "w+"); For (unsigned long I=0;I&LT    nummax;i++) fprintf (FP, "%ld", Nums[i]); return 0;}
The sort time is spent as follows:


Multi-threaded sequencing:

#include <unistd.h> #include <sys/time.h> #include <pthread.h> #include <string.h> #include <string> #include <cstdio> #include <iostream> #include <errno.h> #include <climits># Include <stdlib.h> #include <algorithm>using namespace std;//error checking function inline void Err_exit (const string &        Msg,int retnum) {if (retnum!=0) {cerr<<msg<< ":" <<strerror (retnum) <<endl;    Exit (Exit_failure); }} #define Nummax 8000000l#define nthr 4#define tnum (nummax/nthr) long int nums[nummax];long int Snums[nummax];p thread_    barrier_t b;void * workthread (void *args) {long index= (long) args;    Sort (nums+index,nums+index+tnum);    Pthread_barrier_wait (&AMP;B); Pthread_exit (NULL);}    void merge () {long index[nthr];    for (long i=0;i<nthr;i++) index[i]=i*tnum;         for (long i=0;i<nummax;i++) {long min_index;         Long Min_num=long_max;           for (long j=0;j<nthr;j++) {  if ((index[j]< (j+1) *tnum) && (nums[index[j]]<min_num)) {min_num=nums[index[j]];             Min_index=j;         }} Snums[i]=nums[index[min_index]];    index[min_index]++;    }}int Main () {Srandom (Time (NULL));    For (unsigned long i=0;i<nummax;i++) nums[i]=random ();    struct Timeval start,end;    pthread_t Tid;    Timing begins gettimeofday (&start,null);    Pthread_barrier_init (&b,null,nthr+1);    For (unsigned long i=0;i<nthr;i++) pthread_create (&tid,null,workthread, (void*) (I*tnum));    Pthread_barrier_wait (&AMP;B);    Merge ();    Gettimeofday (&end,null);    The calculation takes a long long startusec=start.tv_sec*1000000+start.tv_usec;    Long Long endusec=end.tv_sec*1000000+end.tv_usec;    Double elapsed= (double) (endusec-startusec)/1000000.0;    printf ("Sort took%.4f seconds\n", elapsed);    FILE *fp=fopen ("Save.txt", "w+"); For (unsigned long i=0;i<nummax;i++) fprintf (FP, "%ld", sNums[i]); return 0;}
The results of the operation are as follows:

The number of threads is 2 o'clock:


The number of threads is 4 o'clock:


The number of threads is 8 o'clock:


Since my computer is a CPU of 4 cores, I can see that when the number of threads is 4 o'clock, it is the shortest time to achieve parallel results. When the number of threads is longer, there is an additional overhead of switching between thread.

Implementation of multi-threaded sequencing under Linux

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.