0% found this document useful (0 votes)
81 views

#Include #Include Int Main (Int Argc, Char Argv ) (MPI - Init (&argc,&argv) Printf ("Hello World/n") MPI - Finalize Return 0 )

The document contains 6 code snippets using MPI (Message Passing Interface) to perform parallel computations across multiple processors. The snippets demonstrate: 1) Printing "Hello World" from multiple processes; 2) Distributing arithmetic operations across processes; 3) Alternating "Hello" and "World" prints across processes; 4) Distributing prime number checking across processes; 5) Printing from a single process; and 6) Distributing array element addition across processes.

Uploaded by

priyanku123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

#Include #Include Int Main (Int Argc, Char Argv ) (MPI - Init (&argc,&argv) Printf ("Hello World/n") MPI - Finalize Return 0 )

The document contains 6 code snippets using MPI (Message Passing Interface) to perform parallel computations across multiple processors. The snippets demonstrate: 1) Printing "Hello World" from multiple processes; 2) Distributing arithmetic operations across processes; 3) Alternating "Hello" and "World" prints across processes; 4) Distributing prime number checking across processes; 5) Printing from a single process; and 6) Distributing array element addition across processes.

Uploaded by

priyanku123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <mpi.

h>
#include <stdio.h>

int main (int argc, char * argv[]){


MPI_Init(&argc,&argv);
printf("Hello World\n");
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>

int main(int argc, char * argv[]){


int a=2,b=3;
// printf("Enter numbers: \n");
// scanf("%d,%d",&a,&b);
int rank;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
switch(rank){
case 0: printf("For processor %d a+b %d\n",rank,a+b);
break;
case 1: printf("For processor %d a-b %d\n",rank,a-b);
break;
case 2: printf("For processor %d a*b %d\n",rank,a*b);
break;
case 3: printf("For processor %d a/b %d\n",rank,a/b);
}
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>

int main(int argc, char * argv[]){

int rank;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if (rank%2)
printf("%d --> World\n",rank);
else
printf("%d --> Hello\n",rank);
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>
//#include <math.h>

int isPrime(int x)
{
if (x<2)
return 0;
if (x==2||x==3)
return 1;
int n = x/2;
int i;
for(i=2;i<=n;i++)
if (x%i==0)
return 0;
return 1;
}

int main(int argc, char * argv[]){

int rank,i;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);

for (i=rank*50+1;i<=50*(rank+1);i++){
// printf("Processor %d says %d is ",rank,i);
// if(isPrime(i))
// printf("prime\n");
// else
// printf("not prime\n");
if(isPrime(i))
printf("%d: %d\n",rank,i);
}

MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>

int main (int argc, char * argv[]){


int rank,size;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank==0)
printf("Hello world from process %d",rank);
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>

int main(int argc, char * argv[]){

int rank;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int a[6]={1,2,3,4,5,6};
int b[6]={1,1,1,1,1,1};
int c[6]={};

int i;

for(i=rank*3+0;i<(rank+1)*3;i++){
c[i]=a[i]+b[i];
printf("%d: c[%d]=%d\n",rank,i,c[i]);
}

MPI_Finalize();
return 0;
}

You might also like