Created
February 26, 2016 03:22
-
-
Save tanayseven/a370339a2838c443dc94 to your computer and use it in GitHub Desktop.
MPI Codes for cloud lab 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <mpi.h> | |
#define ROOT 0 | |
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); | |
int i, n, sn, *num, *sub_num, tag = 0; | |
float avg = 0, avg_arr[size], final_avg = 0; | |
if(rank == ROOT) { | |
printf("Enter n:\n"); | |
scanf("%d",&n); | |
sn = n/size; | |
num = (int*)malloc(sizeof(int)*n); | |
printf("Input %d numbers\n",n); | |
for (i = 0 ; i < n ; i++ ) { | |
scanf("%d",&num[i]); | |
} | |
} | |
MPI_Bcast(&sn, 1, MPI_INT, ROOT, MPI_COMM_WORLD); | |
sub_num = (int*)malloc(sizeof(int)*sn); | |
MPI_Scatter(num, sn, MPI_INT, sub_num, sn, MPI_INT, ROOT, MPI_COMM_WORLD); | |
for (i = 0 ; i < sn; ++i) { | |
avg += sub_num[i]; | |
} | |
avg /= sn; | |
MPI_Gather(&avg, 1, MPI_FLOAT, avg_arr, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD); | |
if( rank == ROOT ) { | |
for(i = 0 ; i < size ; i++ ) { | |
final_avg += avg_arr[i]; | |
} | |
final_avg /= size; | |
printf("The final average is: %f\n",final_avg); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mpi.h> | |
#define ROOT 0 | |
int sum(int *arr, int len) { | |
int i, sum; | |
for (i = 0, sum = 0 ; i < len ; ++i) { | |
sum += arr[i]; | |
} | |
return sum; | |
} | |
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); | |
int i, arr[size], num, sum; | |
if(rank == ROOT) { | |
printf("Input %d numbers\n",size); | |
for(i = 0 ; i < size ; ++i) { | |
scanf("%d",&arr[i]); | |
} | |
} | |
MPI_Bcast(arr, size, MPI_INT, ROOT, MPI_COMM_WORLD); | |
num = arr[rank]*arr[rank]; | |
MPI_Reduce(&num, &sum, 1, MPI_INT, MPI_SUM, ROOT, MPI_COMM_WORLD); | |
if(rank == ROOT ) { | |
printf("The result is: %d\n",sum); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mpi.h> | |
#define ROOT 0 | |
long long int factorial(int num) { | |
static long int fact = 1; | |
fact *= num; | |
if( num <= 1) { | |
return fact; | |
} | |
factorial(num-1); | |
} | |
int main(int argc, char **argv) { | |
int rank, size; | |
long long int sum, sq; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
MPI_Comm_size(MPI_COMM_WORLD,&size); | |
if (rank == ROOT) { | |
printf("Finding sum of factorial(s) of %d number(s) from 1\n",size); | |
} | |
sq = factorial(rank+1); | |
MPI_Reduce(&sq, &sum, 1, MPI_LONG_LONG_INT,MPI_SUM, ROOT, MPI_COMM_WORLD); | |
if(rank == ROOT) { | |
printf("The result is: %lld\n",sum); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mpi.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX 337 | |
#define ROOT 0 | |
int main(int argc, char *argv[]) { | |
int arr[MAX], rank, size, tag = 0, len; char *msg; | |
//MPI_Status stat; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
MPI_Comm_size(MPI_COMM_WORLD, &size); | |
if(rank == ROOT) { | |
msg = "Hello World"; | |
len = strlen(msg); | |
} | |
MPI_Bcast(&len, 1, MPI_INT, ROOT, MPI_COMM_WORLD); | |
if(rank != ROOT) { | |
msg = (char*)malloc(sizeof(char)*len); | |
} | |
MPI_Bcast(msg, len, MPI_CHAR, ROOT, MPI_COMM_WORLD); | |
if(rank != ROOT) { | |
printf("Process %d received %s \n", rank, msg); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <mpi.h> | |
#define PROC1 0 | |
#define PROC2 1 | |
int main(int argc, char *argv[]) { | |
int size, rank, len, tag = 0; char buff[1024]; | |
MPI_Init(&argc,&argv); | |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
MPI_Comm_size(MPI_COMM_WORLD,&size); | |
MPI_Status stat; | |
if(rank == PROC1) { | |
printf("Enter a string: \n"); | |
scanf("%s",buff); | |
len = strlen(buff); | |
MPI_Send(&len, 1, MPI_INT, PROC2, tag++, MPI_COMM_WORLD); | |
MPI_Send(buff, len, MPI_CHAR, PROC2, tag++, MPI_COMM_WORLD); | |
printf("Proc %d sent string \"%s\" to Proc %d\n",PROC1, buff, PROC2); | |
MPI_Recv(&len, 1, MPI_INT, PROC2, tag++, MPI_COMM_WORLD, &stat); | |
MPI_Recv(buff, len, MPI_CHAR, PROC2, tag++, MPI_COMM_WORLD, &stat); | |
printf("Proc %d rcvd string \"%s\" to Proc %d\n",PROC1, buff, PROC2); | |
} | |
if(rank == PROC2) { | |
MPI_Recv(&len, 1, MPI_INT, PROC1, tag++, MPI_COMM_WORLD, &stat); | |
MPI_Recv(buff, len, MPI_CHAR, PROC1, tag++, MPI_COMM_WORLD, &stat); | |
printf("Proc %d rcvd string \"%s\" from %d\n", PROC2, buff, PROC1); | |
MPI_Send(&len, 1, MPI_INT, PROC1, tag++, MPI_COMM_WORLD); | |
MPI_Send(buff, len, MPI_CHAR, PROC1, tag++, MPI_COMM_WORLD); | |
printf("Proc %d sent string \"%s\" from %d\n", PROC2, buff, PROC1); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <mpi.h> | |
#define MASTER 0 | |
int main(int argc, char *argv[]) { | |
int size, rank, len, tag = 0,i; char buff[1024]; | |
MPI_Init(&argc,&argv); | |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
MPI_Comm_size(MPI_COMM_WORLD,&size); | |
MPI_Status stat; | |
if(rank == MASTER) { | |
printf("Enter a string: \n"); | |
scanf("%s",buff); | |
len = strlen(buff)+1; | |
for( i = 1 ; i < size ; i++) { | |
MPI_Send(&len, 1, MPI_INT, i, tag, MPI_COMM_WORLD); | |
MPI_Send(buff, len, MPI_CHAR, i, tag+1, MPI_COMM_WORLD); | |
} | |
} | |
if(rank != MASTER) { | |
MPI_Recv(&len, 1, MPI_INT, MASTER, tag, MPI_COMM_WORLD, &stat); | |
MPI_Recv(buff, len, MPI_CHAR, MASTER, tag+1, MPI_COMM_WORLD, &stat); | |
printf("Proc %d rcvd string \"%s\" from %d\n", rank, buff, MASTER); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <mpi.h> | |
#define MASTER 0 | |
int main(int argc, char *argv[]) { | |
int size, rank, num, tag = 0,i; | |
MPI_Init(&argc,&argv); | |
MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
MPI_Comm_size(MPI_COMM_WORLD,&size); | |
MPI_Status stat; | |
if(rank == MASTER) { | |
printf("Enter a number: \n"); | |
scanf("%d",&num); | |
for( i = 1 ; i < size ; i++) { | |
MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD); | |
} | |
} | |
if(rank != MASTER) { | |
MPI_Recv(&num, 1, MPI_INT, MASTER, tag, MPI_COMM_WORLD, &stat); | |
printf("Proc %d rcvd string \"%d\" from %d\n", rank, num, MASTER); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <mpi.h> | |
#include <time.h> | |
#define ROOT 0 | |
int main(int argc, char **argv) { | |
int rank, size; | |
MPI_Status stat; | |
MPI_Init(&argc,&argv); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
MPI_Comm_size(MPI_COMM_WORLD, &size); | |
long int npts = 1e9, n = 0, tag = 0; | |
long int i; | |
double f,sum=0, final_sum=0; | |
double xmin,xmax,x; | |
xmin = 0.0; | |
xmax = 1.0; | |
if( rank == ROOT ) { | |
int q = npts / (size-1); | |
n = npts % (size-1); | |
for(i = 1 ; i < size ; i++ ) { | |
MPI_Send(&q, 1, MPI_INT, i, tag, MPI_COMM_WORLD); | |
} | |
} | |
else { | |
MPI_Recv(&n, 1, MPI_INT, ROOT, tag++, MPI_COMM_WORLD, &stat); | |
} | |
srand(time(NULL)+rank); | |
for (i=0; i<n; i++) { | |
x = (double) rand()/RAND_MAX*(xmax-xmin) + xmin; | |
sum += 4.0/(1.0 + x*x); | |
} | |
MPI_Reduce(&sum,&final_sum, 1, MPI_DOUBLE, MPI_SUM, ROOT, MPI_COMM_WORLD); | |
if(rank == ROOT) { | |
f = final_sum/npts; | |
printf("PI calculated with %ld points = %f \n",npts,f); | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment