Ask Computer Engineering Expert

// Lab for Multicore topic on shared memory programming and message passing programming
//
// This program is designed to simulate the effect of non-uniform memory access (NUMA).
// Local memory access is simulated by a sharded counter variable between two threads.
// The access to the shared variable is guarded by a spin lock.
//
// Remote memory access is simulated by message paasing between the requesting process
// and the remote process. The requesting process sends a request to the remote process
// and the remote process responds with the value requested.
//
// Threads are implemented using POSIX Threads library and message passing is implemented
// uaing MPI.
//
// Complete the program by inserting codes at commented places. Specifically, prepare codes
// for spin lock checking and releasing at the beginning and the end of each critical 
// section; and prepare MPI routines for the requesting process (the process with rank 0)
// to send a request to each remote process and receive the requested values from each
// remote process.
//
// The syntax of Pthread mutex lock and unlock routines are:
//
// int pthread_mutex_lock(pthread_mutex_t *mutex); 
// int pthread_mutex_unlock(pthread_mutex_t *mutex); 
//
// The syntax of MPI_Send and MPI_Recv routines are:
//
// int MPI_Send(void* buffer /* in */,
// int count /* in */,
// MPI_Datatype datatype /* in */,
// int destination /* in */,
// int tag /* in */,
// MPI_Comm communicator /* in */)
//
// int MPI_Recv(void* buffer /* in */,
// int count /* in */,
// MPI_Datatype datatype /* in */,
// int source /* in */,
// int tag /* in */,
// MPI_Comm communicator /* in */,
// MPI_Status* status /* out */)
//
// To compile the program on Grid.uhd.edu, you can either use the web portal or SSH shell.
// If you use SSH shell, type:
//
// /opt/openmpi/bin/mpicc -lpthread -o TheNameOfYourExecutable YourSource.c
//
// To run your program in SSH shell:
//
// /opt/openmpi/bin/mpirun -np n ./TheNameOfYourExecutable
//
// where n is the number of processes. For this lab, it has to be greater or equal to 2.

#include
#include
#include
#include "mpi.h"

const int TIMES = 1000;

pthread_mutex_t mutex;
int rank, num_proc;

int local_count = 0, distributed_count = 0;
double local_total = 0.0, local_avg, distributed_total = 0.0, distributed_avg;

void *local_count_reader();
void *local_count_preparer();
void *distributed_count_reader();
void *distributed_count_preparer();

int main(int argc, char** argv)
{
pthread_t local_reader, local_preparer, distributed_reader, distributed_preparer;

MPI_Init(&argc, &argv); //initiate MPI
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* find rank */
MPI_Comm_size(MPI_COMM_WORLD, &num_proc);

if (rank == 0)
{
pthread_create(&local_reader, NULL, local_count_reader, NULL);
pthread_create(&local_preparer, NULL, local_count_preparer, NULL);
pthread_create(&distributed_reader, NULL, distributed_count_reader, NULL);
}
else
pthread_create(&distributed_preparer, NULL, distributed_count_preparer, NULL);

if (rank == 0)
{
pthread_join(local_reader, NULL);
pthread_join(local_preparer, NULL);
pthread_join(distributed_reader, NULL);
}
else
{
pthread_join(distributed_preparer, NULL);
}

if (rank == 0)
{ printf("The average time used in reading the local shared counter is: %f microseconds.n", local_avg);
printf("The average time used in reading the distributed counter is: %f microseconds.n", distributed_avg);
}
MPI_Finalize();

return(0);
}

void *local_count_reader()
{
int i, copy;
double start, end;

for (i = 0; i < TIMES; i++)
{
start = MPI_Wtime();

// Use lock to guard the critical section
// prepare code here

copy = local_count; 

// The end of critical section
// Insert code here

end = MPI_Wtime();
local_total += end - start;
}

local_avg = 1000000.0 * local_total / TIMES;
}

void *local_count_preparer()
{
int i;

for (i = 0; i < TIMES; i++)
{

// Use lock to guard the critical section
// prepare code here

local_count++; 

// The end of critical section
// Insert code here
}
}

void *distributed_count_reader()
{
int i, copy, req = 0, dest;
double start, end;
MPI_Status *status;

for (i = 0; i < TIMES; i++)
{
for (dest = 1; dest < num_proc; dest++)
{
start = MPI_Wtime();

// The master process (p0) send a request to the process with rank "dest"
// requesting the value of the distributed_count
// use MPI_Send to send the request.
// insert code here.

// The master process (p0) receive the requested value from each of the 
// process with rank "dest"
// use MPI_Recv to receive the value.
// insert code here.

end = MPI_Wtime();

distributed_total += end - start;
}
MPI_Barrier(MPI_COMM_WORLD);
}

distributed_avg = 1000000.0 * distributed_total / (TIMES * (num_proc-1));
pthread_exit(NULL);
}

void *distributed_count_preparer()
{
int i, req;
MPI_Status *status;

for (i = 0; i < TIMES; i++)
{
// The calling process (remote process) receives a request from the master process
// use MPI_Recv to receive the request
// insert code here

distributed_count++;

// The calling process (remote process) sends the value of variable "distributed_count"
// to the master process
// use MPI_Send to send the value
// insert code here

MPI_Barrier(MPI_COMM_WORLD);
}

pthread_exit(NULL);
return;

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M980862

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As