Network
mpi_SR(1).c
#include "mpi.h" #include <stdio.h> int main(int argc, char *argv[]) { int rc; rc = MPI_Init(&argc, &argv); if (rc != MPI_SUCCESS) { printf("Error starting MPI program. Termiaating.\n"); MPI_Abort(MPI_COMM_WORLD, rc); } int myrank, nproc, number, numb; MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &nproc); if (myrank == 0) { printf("My rank is %d\n", myrank); number = -1; printf("Sending: P0 sends number %d to P1\n", number); MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); } else if (myrank == 1) { printf("My rank is %d\n", myrank); MPI_Recv(&numb, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Receiving: P1 receives number %d from P0\n", numb); } MPI_Finalize(); }
mpi-pi.c
#include "mpi.h" #include <stdio.h> #include <math.h> double f(a) double a; { return (4.0 / (1.0 + a*a)); } int main(int argc,char *argv[]) { int i, n, myrank, nproc, done = 0; double PI25DT = 3.141592653589793238462643; double mypi, pi, h, sum, x; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&nproc); MPI_Comm_rank(MPI_COMM_WORLD,&myrank); n = 0; while (!done) { if (myrank == 0) { printf("Enter the number of intervals: (0 quits)\n"); scanf("%d",&n); } MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); // printf("I am process %d and value n is %d\n", myid, n); if (n == 0) done = 1; else { h = 1.0 / (double) n; sum = 0.0; for (i = myrank + 1; i <= n; i += nproc) { x = h * ((double)i - 0.5); sum += f(x); } mypi = h * sum; MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); if (myrank == 0) printf("\npi is approximately %.16f, Error is %.16f\n\n", pi, fabs(pi - PI25DT)); } } MPI_Finalize(); return 0; }
mpi-ring.c
#include <mpi.h> #include <stdio.h> int main(int argc, char *argv[]) { int rc; rc = MPI_Init(&argc, &argv); if (rc != MPI_SUCCESS) { printf("Error starting MPI program. Termiaating.\n"); MPI_Abort(MPI_COMM_WORLD, rc); } int myrank, nproc, i, number; MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &nproc); if (myrank != 0) { MPI_Recv(&number, 1, MPI_INT, myrank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process %d received number %d from process %d\n", myrank, number, myrank-1); } else { number = 10; //it will be set only at P0 printf("I am process %d and number is set to %d\n", myrank, number); } MPI_Send(&number, 1, MPI_INT, (myrank+1)%nproc, 0, MPI_COMM_WORLD); if (myrank == 0) { MPI_Recv(&number, 1, MPI_INT, nproc - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process %d received number %d from process %d\n", myrank, number, nproc-1); } MPI_Finalize(); return 0; }
ITS470-HW5-Hints.docx
ITS 470
1. Complete the code that passes the array a and b from process 0 to process 1 using the algorithm shown in textbook p.246. Vary the size of array a and b from 100, 1000, and 2000 to compare and discuss the results. Explain your results from each size. For MPI_Recv function, see sample code given in course website. (20 points)
Use following code blocks for the testing.
int a[10], b[10], myrank;
MPI_Status status;
...
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
if (myrank == 0) {
MPI_Send(a, 10, MPI_INT, 1, 1, MPI_COMM_WORLD);
MPI_Send(b, 10, MPI_INT, 1, 2, MPI_COMM_WORLD);
}
else if (myrank == 1) {
MPI_Recv(b, 10, MPI_INT, 0, 2, MPI_COMM_WORLD);
MPI_Recv(a, 10, MPI_INT, 0, 1, MPI_COMM_WORLD);
}
...
2. In given code “mpi-pi.c”, the jobs are distributed in cyclic manner. Modify and write the code that distributes the job using block partition, which assigns a set of consecutive jobs to each process. For example, if there are 40 jobs and 4 processes, distribute 10 jobs each process such as 1 to 10 to process 0, 11 to 20 to process 1, 21 to 30 to process 2, and 31 to 40 to process 3. For the simplicity, assume that user inputs the number of steps that is the multiple of the number of processes. For example, if the number of processes is 4, the number of steps should be multiple of 4 such as 16, 100, 200, 10000, etc. Your code should dynamically distribute the same amount of jobs over the number of available processes. Explain your job distribution method and it should be clearly documented in your code. (20 points)
For example, if the number of interval is 1000 and number of processors is 10, then the number of jobs for each processor is simple 1000 / 10 = 100. The range at each processor will be
P0: 1 – 100 0*100 + 1 to 1*100
P1: 101 – 200 1*100 + 1 to 2*100
P0: 201 – 300 2*100 + 1 to 3*100
…
You will need to find the pattern and modify the for loop conditions.
3. Re-write pi estimation code using MPI_Send and MPI_Recv communication functions only. You can download sample code at course website called “mpi-pi.c”. Explain how you did in word here and it should be documented in your code. (Hint: Process 0 receives all partial local results and computes the estimating pi) (20 points)
See the posted matrix vector multiplication code.
Basically, broadcast can be done by sending message at processor 0 to all other processors. Once all processors finish their jobs then send back to processor 0. So, processor 0 should receive messages from all other processors.
4. Use given “mpi-pi.c” code, modify it to compute and print the elapsed time at process 0 using Wtim( ) function. Then, choose the large number of steps (i.e., n = 2,000,000,000) and run the program by use of 1, 4, 8, and 16 processes to find each corresponding elapsed time. Do this with the small number of steps (i.e., 200) to find the elapsed time at each number of processes. Plot both results to observe the relation between the number of processes and elapsed time at each number of steps. (40 points)
Use a code with different number of steps.
ITS470-HW5[1].docx
ITS 470
Homework 5
Please put Course number, your Full Name, Assignment number on top right. Submit your homework on Blackboard. Please name your file as “ITS470HW5FirstNameLastName.(doc or docx)”. You should submit all source codes, script files, and all output files from job submission. Documentation is important. The more clear and detail documentation is a good way to earn better grade.
1. Complete the code that passes the array a and b from process 0 to process 1 using the algorithm shown in textbook p.246. Vary the size of arraya and b from 100, 1000, and 2000 to compare and discuss the results. Explain your results from each size. For MPI_Recv function, see sample code given in course website. (20 points)
2. In given code “mpi-pi.c”, the jobs aredistributedin cyclic manner.Modify and write the code that distributes the job using block partition, which assignsa set of consecutive jobs to each process. For example, if there are 40 jobs and 4 processes, distribute10 jobs each process such as 1to 10 to process 0, 11 to 20 to process 1, 21 to 30 to process 2, and 31 to 40 to process 3. For the simplicity, assume that userinputsthe number of steps that is the multiple of the number of processes. For example, if the number of processes is 4, the number of steps should be multiple of 4 such as 16, 100, 200, 10000, etc.Your code should dynamically distribute the same amount of jobs over the number of available processes. Explain your job distribution method and it should be clearly documented in your code. (20 points)
3. Re-write pi estimation code using MPI_Send and MPI_Recv communication functions only. You can download sample code at course website called “mpi-pi.c”. Explain how you did in word here and it should be documented in your code. (Hint: Process 0 receives all partial local results and computes the estimating pi)(20 points)
4. Use given “mpi-pi.c” code, modify it to compute and print the elapsed time at process 0 using Wtim( ) function. Then, choose the large number of steps (i.e., n = 2,000,000,000) and run the program by use of 1, 4, 8, and 16 processes to find each corresponding elapsed time. Do this with the small number of steps (i.e., 200) to find the elapsed time at each number of processes. Plot both results to observe the relation between the number of processes and elapsed time at each number of steps. (40 points)