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
Compare between the one and two-dimensional memory organizations in terms of the  memory structure,  advantages, and disadvantages.  Which approach would better support the needs o

Explain the Spawnlp Functions Used in the NetWare Spawnlp(flags, execName, arg0,...)  The Spawnlp function is used to load  a NetWare executable file (NLM) specified by exec

Q. How does DMA raise system concurrency? How does it make difficult hardware design? Answer: DMA increases system concurrency by allowing the CPU to perform tasks while the

State the advantage of multiprocessor system? The advantages of multiprocessor system are:- a)      Enhanced throughput. b)      Economy of scale. c)       Enhanced re

When a computer is started on, the program that executed first is named the ''operating system.'' It controls pretty much all applications in the computer. This adds who logs in, h

List the task regions in the Spiral model.  Customer communication - In this region it is suggested to set up customer communication.  Planning - All planning activities

Explain about file allocation methods? The main problem in direct-access nature is how to assign space to these files so that disk space if utilized effectively and files can b

The field that consists of a segment index or an internal index is known as Target datum is field that consists of a segment index or an internal index.

LRU page replacement In least lately used page replacement algorithm we will replace that page that hasn't been used for the longest period of time. This is similar as an opti

For the heat conduction problem, investigate the effects on the numerical solution of the following aspects: 1. non-uniform meshes with re?nement at both ends versus a uniform m