Skip to content

Instantly share code, notes, and snippets.

@fyse-nassar
Created September 25, 2020 05:38
Show Gist options
  • Save fyse-nassar/37fec16397d5b648a2488d7ade3f2689 to your computer and use it in GitHub Desktop.
Save fyse-nassar/37fec16397d5b648a2488d7ade3f2689 to your computer and use it in GitHub Desktop.
Parallel Program to add numbers from 1 to n using MPI
// This program adds numbers from 1 to n using MPI
// Sample Execution code : mpirun -n 4 ./a.out 100 ; Here 100 represents n
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char **argv)
{
int id, nproc;
int sum, startval, endval, partial_sum;
int num = atoi(argv[1]);
MPI_Init(&argc, &argv);
// Total number of process
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
// get id of mynode:
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Status status;
sum = 0; // zero sum for accumulation
// Spilt the numbers among different processes
startval = num * id / nproc + 1;
endval = num * (id + 1) / nproc;
for (int i = startval; i <= endval; ++i)
sum = sum + i;
printf("I am the Process %d and my partial sum is %d \n", id, sum);
if (id != 0) //the slaves sending back the partial sums
MPI_Send(&sum, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
else //id==0! the master recieves the partial sums
{
printf("\n\nI am master and my sum currently is : %d\n", sum);
for (int j = 1; j < nproc; j++)
{
MPI_Recv(&partial_sum, 1, MPI_INT, j, 1, MPI_COMM_WORLD, &status);
sum = sum + partial_sum;
printf("The sum yet is %d after adding sum %d from process %d\n", sum, partial_sum, j);
}
}
if (id == 0)
printf("The sum from 1 to %d is %d \n\n", num, sum);
MPI_Finalize();
}
// This program calculates the time required to add numbers from 1 to n using MPI
// Sample Execution code : mpirun -n 4 ./a.out 100 ; Here 100 represents n
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char **argv)
{
int id, nproc;
int sum, startval, endval, partial_sum;
int num = atoi(argv[1]);
double start_time, max_time, total_time, min_time;
MPI_Init(&argc, &argv);
// Total number of process
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
// get id of mynode:
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Status status;
sum = 0; // zero sum for accumulation
MPI_Barrier(MPI_COMM_WORLD); /*synchronize all processes*/
start_time = MPI_Wtime();
// Spilt the numbers among different processes
startval = num * id / nproc + 1;
endval = num * (id + 1) / nproc;
for (int i = startval; i <= endval; ++i)
sum = sum + i;
// printf("I am the Process %d and my partial sum is %d \n", id, sum);
if (id != 0) //the slaves sending back the partial sums
MPI_Send(&sum, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
else //id==0! the master recieves the partial sums
{
// printf("\n\nI am master and my sum currently is : %d\n", sum);
for (int j = 1; j < nproc; j++)
{
MPI_Recv(&partial_sum, 1, MPI_INT, j, 1, MPI_COMM_WORLD, &status);
sum = sum + partial_sum;
// printf("The sum yet is %d after adding sum %d from process %d\n", sum, partial_sum, j);
}
}
total_time = MPI_Wtime() - start_time;
MPI_Reduce(&total_time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce(&total_time, &min_time, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
if (id == 0)
{
// printf("The sum from 1 to %d is %d \n\n", sum, num);
printf("Minimum Time (any processor) = %f \n"
"Maximum Time (any processor) = %f \n",
min_time, max_time);
}
MPI_Finalize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment