1.hello World Programme in Mpi
1.hello World Programme in Mpi
// size of array
#define n 10
int a[] = { 10, 21, 32, 43, 54, 65, 76, 87, 98, 109 };
MPI_Init(&argc, &argv);
MPI_Send(&elements_per_process,
1, MPI_INT, i, 0,
MPI_COMM_WORLD);
MPI_Send(&a[index],
elements_per_process,
MPI_INT, i, 0,
MPI_COMM_WORLD);
}
// last process adds remaining elements
index = i * elements_per_process;
int elements_left = n - index;
MPI_Send(&elements_left,
1, MPI_INT,
i, 0,
MPI_COMM_WORLD);
MPI_Send(&a[index],
elements_left,
MPI_INT, i, 0,
MPI_COMM_WORLD);
}
// master process add its own sub array
int sum = 0;
for (i = 0; i < elements_per_process; i++)
sum += a[i];
sum += tmp;
}
3.BRODCSTING TO LL
1.BCST
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mpi.h>
int main(int argc, char **argv)
{
char messg[20];
int rank,size;
MPI_Status status;
int root = 0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank == root)
{
strcpy(messg,"HELLO WORLD!");
}
MPI_Bcast(messg,13,MPI_CHAR,root,MPI_COMM_WORLD);
printf("Message from process %d sent by root process %d:
%s\n",rank,root,messg);
MPI_Finalize();
return 0; }
2.BCSTI
For MPI_Ibcast() wait function is used to attain synchronization.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "mpi.h"
int main(int argc,char **argv)
{
char messg[20];
int i,rank,size;
MPI_Status status;
MPI_Request request = MPI_REQUEST_NULL;
int root = 0;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank == root)
{
strcpy(messg,"hello world!");
}
MPI_Ibcast(messg,13,MPI_CHAR,root,MPI_COMM_WORLD,&request);
MPI_Wait(&request,&status);
if(rank == root)
{
strcpy(messg,"What will happen?");
}
printf("Message from process %d sent by root process
%d:%.13s\n",rank,root,messg);
MPI_Finalize();
return 0;
}
2.Use your own function broad_cast with MPI Send and Recv.
#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
void broad_cast(void *data,int count,MPI_Datatype datatype,int root,MPI_Comm
communicator)
{
int world_rank;
MPI_Comm_rank(communicator,&world_rank);
int world_size;
MPI_Comm_size(communicator,&world_size);
if(world_rank == root)
{
for(int i = 0 ;i < world_size;i++)
{
if(i!=world_rank)
{
MPI_Send(data,count,datatype,i,0,communicator);
}
}
}
else
{
MPI_Recv(data,count,datatype,root,0,communicator,MPI_STATUS_IGNORE);
}
}
int main(int argc, char**argv)
{
MPI_Init(&argc,&argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD,&world_rank);
int data;
if(world_rank == 0)
{
data = 100;
printf("Root proces %d broadcasts data %d\n",world_rank,data);
broad_cast(&data,1,MPI_INT,0,MPI_COMM_WORLD);
}
else {
broad_cast(&data,1,MPI_INT,0,MPI_COMM_WORLD);
printf("Process %d received data %d from root
process\n",world_rank,data);
}
MPI_Finalize();
return 0;
}
Use Gather to get array 0f [30] where p1 has a1[10], p2 has a2[10] and p3 has a3[10].
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
int main(int argc, char **argv) {
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&pid);
MPI_Comm_size(MPI_COMM_WORLD,&np);
int rand_nums[10];
int enrand[30];
if(pid != 0) {
int l = sizeof(rand_nums) / sizeof(rand_nums[0]);
for(int i = 0 ; i < l ; i++ )
{
rand_nums[i] = pid * i;
}
printf("Numbers from process %d\n",pid);
for(int i = 0 ; i < l ; i++)
{
printf("%d ",rand_nums[i]);
}
printf("\n");
}
MPI_Gather(&rand_nums,10,MPI_INT,&enrand,10,MPI_INT,0,MPI_COMM_WORLD);
if (pid == 0){
MPI_Finalize();
}
int i;
if (rank == 0) {
if (rank == 0) {
printf("4. Processor %d has data: ", rank);
for (i=0; i<size; i++)
printf("%d ", globaldata[i]);
printf("\n");
}
MPI_Finalize();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
int main(int argc, char **argv) {
int pid,np;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&pid);
MPI_Comm_size(MPI_COMM_WORLD,&np);
int arr[5];
int ls;
int gs;
if(pid != 0)
{
for(int i = 0 ; i < 5 ; i++)
{
arr[i] = (pid) * 8 % ((i+1)*9);
ls+=arr[i];
}
printf("Elements in process %d\n",pid);
for(int i = 0 ; i < 5 ; i++)
{ printf("%d ",arr[i]);
}
printf("\n");
if( pid == 0)
{
printf("Total sum of 10 elements : %d\n",gs);
}
MPI_Finalize();
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
int prime_arr[100];
int s, ts;
int main(int argc, char **argv)
{
int n,i=2;
printf("Enter number of prime numbers: ");
scanf("%d", &n);
int c = 0;
while(c <= n)
{
int f = 0;
for(int j = 2 ; j <= i/2 ; j++)
{
if(i%j == 0)
{
f = 1;
break;
}
}
if(f==0)
{
prime_arr[c++] = i;
}
i++;
}
int p, s;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &p);
MPI_Comm_size(MPI_COMM_WORLD, &s);
for (int m = 0; m < n; m++)
{
ts += prime_arr[m];
}
MPI_Reduce(&ts, &s, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (p == 0)
{
for(int l = 0 ; l < n ; l++)
{
printf("%d\n",prime_arr[l]);
}
printf("Sum of %d prime numbers : %d\n", n, s);
}
return 0;
}