Problem of unbalanced loads, Operating System

Present your own fully documented and tested programming example illustrating the problem of unbalanced loads. Describe the use of OpenMP's scheduler as a means of mitigating this problem.

The below example shows a number of tasks that all update a global counter. Since threads share the same memory space, they indeed see and update the same memory location. The code returns a false result because updating the variable is  much quicker than creating the thread as on a multicore processor the chance of errors will greatly increase. If we artificially increase the time for the update, we will no longer get the right result. All threads read out the value of sum, wait a while (presumably calculating something) and then update.

#include

#include

#include "pthread.h"

int sum=0;

void adder() {

int sum = 0;

int t = sum; sleep(1); sum = t+1;

return;

}

#define NTHREADS 50

int main() {

int i;

pthread_t threads[NTHREADS];

printf("forking\n");

for (i=0; i

if (pthread_create(threads+i,NULL,&adder,NULL)!=0) return i+1;

printf("joining\n");

for (i=0; i

{

if (pthread_join(threads[i],NULL)!=0) return NTHREADS+i+1;

printf("Sum computed: %d\n",sum);

}

return 0;

}

The use of OpenMP is the parallel loop. Here, all iterations can be executed independently and in any order. The pragma CPP directive then conveys this fact to the compiler. A sequential code can be easily parallelized this way.

#include 
#include 
#include "pthread.h"
int sum=0;
void adder() {
int sum = 0;
int t = sum; sleep(1); sum = t+1;
return;
}
#define NTHREADS 50
int main() {
int i;
pthread_t threads[NTHREADS];
printf("forking\n");
#pragma omp for
for (i=0; i
if (pthread_create(threads+i,NULL,&adder,NULL)!=0) return i+1;
}
printf("joining\n");
for (i=0; i
{
if (pthread_join(threads[i],NULL)!=0) return NTHREADS+i+1;
printf("Sum computed: %d\n",sum);
}
return 0;
}
Posted Date: 6/29/2012 2:17:42 AM | Location : United States







Related Discussions:- Problem of unbalanced loads, Assignment Help, Ask Question on Problem of unbalanced loads, Get Answer, Expert's Help, Problem of unbalanced loads Discussions

Write discussion on Problem of unbalanced loads
Your posts are moderated
Related Questions
Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

scattered resposibi;ity and authority -necessity of operation research

Explain the Sleep (ms) Function  This call places the current thread in a suspended state for the number of milliseconds passed as the parameter (ms). After that Windows NT wil

What is a semaphore? A semaphore 'S' is a synchronization tool which is  an integer value that, apart from initialization, is accessed only by two standard atomic operations; w

For the first assignment you are required to construct a logic circuit onto a copper clad circuit board (vero board). This construction will require the use of planning the positio

What is co-operating scheduling? CPU scheduling decisions may possibly place under the following four circumstances: 1.      When process switches from the running state to

Acyclic-graph directories An acyclic graph permits directories to have shared subdirectories and files. The similar file or subdirectory may be in two dissimilar directories. A

Q. Once more considering the RPC mechanism consider the exactly once semantic. Does the algorithm for implementing this semantic implement correctly even if the ACK message back to

Front page We need to continue or customization and documentation of our system. Currently our system boots into runlevel 5 and we need it to boot into runlevel 3. For future r

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4