Multithreading (III): dynamic task division & amp; static task division

Source: Internet
Author: User

First, this is the definition given by the teacher --

Static task division: the problem is divided into relatively independent sub-problems equal to the number of CPUs;

Dynamic task division: when the problem has n (> Number of CPUs) relatively independent sub-problems, unhandled sub-problems are allocated to Idle threads.

The example shows how to divide static tasks. The examples in the previous articles are dynamic tasks.


[Cpp] # include <stdio. h>
# Include <pthread. h>
 
Typedef struct myTestType
{
Int threadID;
Int threadNum;
Int dataNum;
 
Int * input;
Int * output;
Int * index;
} MyTest;
 
Int calculate (int input ){
Int I;
Int output = 0;
For (I = 2; I <input/2; I ++ ){
If (input % I = 0 ){
Output = 1;
Break;
}
}
If (output = 0)
{
Sleep (1 );
}
Return output;
}
 
Void thread (myTest * pMyTest ){
Printf ("Begin threadID = % u run! \ N ", pMyTest-> threadID );
Int input, output;
Int threadID = pMyTest-> threadID;
Int threadNum = pMyTest-> threadNum;
Int dataNum = pMyTest-> dataNum;
Int split = dataNum/threadNum;
Int firstIndex = split * threadID;
Int lastIndex = firstIndex + split;
If (lastIndex> dataNum)
LastIndex = dataNum;
While (firstIndex <lastIndex ){
Input = pMyTest-> input [firstIndex];
Output = calculate (input );
Printf ("index = % 3u, input = % 8u, output = % 2u, threadID = % 2u \ n", firstIndex, input, output, threadID );
PMyTest-> output [firstIndex] = output;
FirstIndex ++;
}
Pthread_exit (NULL );
}
 
Int main (void ){
Int I, ret;
Int threadNum = 2;
MyTest * pMyTest = (myTest *) malloc (sizeof (myTest ));
PMyTest-> dataNum = 100;
PMyTest-> input = (int *) malloc (sizeof (int) * pMyTest-> dataNum );
PMyTest-> output = (int *) malloc (sizeof (int) * pMyTest-> dataNum );
For (I = 0; I <pMyTest-> dataNum; ++ I ){
If (I % 4 = 0)
PMyTest-> input [I] = (1 <(I % 30) + 1;
Else
PMyTest-> input [I] = (7 <(I % 16) + 1;
}
PMyTest-> index = (int *) calloc (1, sizeof (int ));
 
PMyTest-> threadNum = threadNum;
MyTest * inMyTest = (myTest *) malloc (sizeof (myTest) * threadNum );
For (I = 0; I <threadNum; ++ I ){
Memcpy (inMyTest + I, pMyTest, sizeof (myTest ));
(InMyTest + I)-> threadID = I;
}
Pthread_t * tid = (pthread_t *) malloc (sizeof (pthread_t) * threadNum );
Printf ("Begin create pthread. \ n ");
For (I = 0; I <threadNum; ++ I ){
Ret = pthread_create (tid + I, NULL, (void *) thread, (myTest *) (inMyTest + I ));
If (ret! = 0 ){
Printf ("Create pthread error. \ n ");
Return 0;
}
}
For (I = 0; I <threadNum; I ++)
Pthread_join (tid [I], NULL );
Free (tid );
Free (inMyTest );
Free (pMyTest-> input );
Free (pMyTest-> output );
Free (pMyTest-> index );
Free (pMyTest );
Return 0;
}

# Include <stdio. h>
# Include <pthread. h>

Typedef struct myTestType
{
Int threadID;
Int threadNum;
Int dataNum;

Int * input;
Int * output;
Int * index;
} MyTest;

Int calculate (int input ){
Int I;
Int output = 0;
For (I = 2; I <input/2; I ++ ){
If (input % I = 0 ){
Output = 1;
Break;
}
}
If (output = 0)
{
Sleep (1 );
}
Return output;
}

Void thread (myTest * pMyTest ){
Printf ("Begin threadID = % u run! \ N ", pMyTest-> threadID );
Int input, output;
Int threadID = pMyTest-> threadID;
Int threadNum = pMyTest-> threadNum;
Int dataNum = pMyTest-> dataNum;
Int split = dataNum/threadNum;
Int firstIndex = split * threadID;
Int lastIndex = firstIndex + split;
If (lastIndex> dataNum)
LastIndex = dataNum;
While (firstIndex <lastIndex ){
Input = pMyTest-> input [firstIndex];
Output = calculate (input );
Printf ("index = % 3u, input = % 8u, output = % 2u, threadID = % 2u \ n", firstIndex, input, output, threadID );
PMyTest-> output [firstIndex] = output;
FirstIndex ++;
}
Pthread_exit (NULL );
}

Int main (void ){
Int I, ret;
Int threadNum = 2;
MyTest * pMyTest = (myTest *) malloc (sizeof (myTest ));
PMyTest-> dataNum = 100;
PMyTest-> input = (int *) malloc (sizeof (int) * pMyTest-> dataNum );
PMyTest-> output = (int *) malloc (sizeof (int) * pMyTest-> dataNum );
For (I = 0; I <pMyTest-> dataNum; ++ I ){
If (I % 4 = 0)
PMyTest-> input [I] = (1 <(I % 30) + 1;
Else
PMyTest-> input [I] = (7 <(I % 16) + 1;
}
PMyTest-> index = (int *) calloc (1, sizeof (int ));

PMyTest-> threadNum = threadNum;
MyTest * inMyTest = (myTest *) malloc (sizeof (myTest) * threadNum );
For (I = 0; I <threadNum; ++ I ){
Memcpy (inMyTest + I, pMyTest, sizeof (myTest ));
(InMyTest + I)-> threadID = I;
}
Pthread_t * tid = (pthread_t *) malloc (sizeof (pthread_t) * threadNum );
Printf ("Begin create pthread. \ n ");
For (I = 0; I <threadNum; ++ I ){
Ret = pthread_create (tid + I, NULL, (void *) thread, (myTest *) (inMyTest + I ));
If (ret! = 0 ){
Printf ("Create pthread error. \ n ");
Return 0;
}
}
For (I = 0; I <threadNum; I ++)
Pthread_join (tid [I], NULL );
Free (tid );
Free (inMyTest );
Free (pMyTest-> input );
Free (pMyTest-> output );
Free (pMyTest-> index );
Free (pMyTest );
Return 0;
}


Compared with the code in the previous article, the main difference lies in the thread function. In this example, we need to determine whether there are 100 prime numbers or a total number. If dynamic task division is used, we can know which thread operates the number of integer arrays only when the result is printed. The program that uses static task division specifies that the subscript processed by Thread 0 is 0-49, and the subscript processed by thread 1 is 50-99. This is the difference between dynamic and static tasks.

They have their own advantages and disadvantages.

Dynamic task division --

Advantage: equal task end time

Disadvantage: locks and locks are required, and sometimes frequent waiting is required.

Static task division --

Advantage: simple job division, no communication between threads

Disadvantage: sometimes there is a large gap in task completion time.

 

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.