-
-
Save cmrmahesh/f7ab620269b06be43292b9531e5e06b9 to your computer and use it in GitHub Desktop.
Parallel Processing Lab
This file contains hidden or 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" | |
| int main(int argc,char* argv){ | |
| int rnk,src,dest=0,tag=0,p; | |
| char mesg[100010]; | |
| MPI_Status stat; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rnk); | |
| MPI_Comm_size(MPI_COMM_WORLD,&p); | |
| if(rnk!=0){ | |
| sprintf(mesg,"blah blah.. %d",rnk); | |
| MPI_Send(mesg,strlen(mesg)+1,MPI_CHAR,dest,tag,MPI_COMM_WORLD); | |
| }else{ | |
| for(src=1;src<p;src++){ | |
| MPI_Recv(mesg,100,MPI_CHAR,src,tag,MPI_COMM_WORLD,&stat); | |
| printf("%s\n",mesg); | |
| } | |
| } | |
| MPI_Finalize(); | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| int main() | |
| { | |
| int tag=0,rank,p; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_size(w,&p); | |
| MPI_Comm_rank(w,&rank); | |
| printf("In the processor %i\n",rank); | |
| MPI_Finalize(); | |
| } |
This file contains hidden or 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<math.h> | |
| #include<mpi.h> | |
| #define REP(i,a,b) for(i=a;i<=b;i++) | |
| #define sf scanf | |
| #define pf printf | |
| int main(int argc,char* argv[]){ | |
| int me,p,src,dst=0,tag=0; | |
| int i,n; | |
| float xx,x[1010],y[1010],ans[1010],val,res=0; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm comm=MPI_COMM_WORLD; | |
| MPI_Status stat; | |
| MPI_Comm_size(comm,&p); | |
| MPI_Comm_rank(comm,&me); | |
| if(me==0){ | |
| sf("%d %f",&n,&xx); | |
| val=1; | |
| for(i=0;i<n;i++){ | |
| sf("%d",&x[i]); | |
| val*=(xx-x[i]); | |
| } | |
| for(i=0;i<n;i++) | |
| sf("%d",&y[i]); | |
| } | |
| MPI_Bcast(&n,1,MPI_INT,0,comm); | |
| MPI_Bcast(&xx,1,MPI_FLOAT,0,comm); | |
| MPI_Bcast(x,n,MPI_FLOAT,0,comm); | |
| MPI_Bcast(y,n,MPI_FLOAT,0,comm); | |
| MPI_Bcast(&val,1,MPI_FLOAT,0,comm); | |
| if(me>0){ | |
| val/=(xx-x[me-1]); | |
| for(i=0;i<n;i++) | |
| if(i!=me-1) | |
| val/=(x[me-1]-x[i]); | |
| val*=y[me-1]; | |
| } | |
| MPI_Gather(&val,1,MPI_FLOAT,ans,1,MPI_FLOAT,0,comm); | |
| if(me==0){ | |
| for(i=0;i<n;i++) | |
| res+=ans[i]; | |
| pf("%f\n",ans); | |
| } | |
| MPI_Finalize(); | |
| } |
This file contains hidden or 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<math.h> | |
| #define REP(i,a,b) for(i=a;i<=b;i++) | |
| #define sf scanf | |
| #define pf printf | |
| int main(int argc,char* argv[]){ | |
| int me,p,src,dest=0,tag=0; | |
| int i,n; | |
| float xx,x[1010],y[1010],ans[1010],val,res; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm comm=MPI_COMM_WORLD; | |
| MPI_Status stat; | |
| MPI_Comm_size(comm,&p); | |
| MPI_Comm_rank(comm,&me); | |
| if(me==0){ | |
| sf("%d %f",&n,&xx); | |
| val=1; | |
| for(i=0;i<n;i++){ | |
| sf("%f",&x[i]); | |
| val*=(xx-x[i]); | |
| } | |
| for(i=0;i<n;i++) | |
| sf("%f",&y[i]); | |
| } | |
| MPI_Bcast(&n,1,MPI_INT,0,comm); | |
| MPI_Bcast(&xx,1,MPI_FLOAT,0,comm); | |
| MPI_Bcast(x,n,MPI_FLOAT,0,comm); | |
| MPI_Bcast(y,n,MPI_FLOAT,0,comm); | |
| MPI_Bcast(&val,1,MPI_FLOAT,0,comm); | |
| if(me>0){ | |
| val/=(xx-x[me-1]); | |
| for(i=0;i<n;i++) | |
| if(i!=me-1) | |
| val/=(x[me-1]-x[i]); | |
| val*=y[me-1]; | |
| //pf("%f\n",val); | |
| } | |
| MPI_Gather(&val,1,MPI_FLOAT,ans,1,MPI_FLOAT,0,comm); | |
| if(me==0){ | |
| res=0; | |
| for(i=1;i<=n;i++) | |
| res+=ans[i]; | |
| pf("%f\n",res); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<math.h> | |
| #define sf scanf | |
| #define pf printf | |
| int main(int argc,char* argv[]){ | |
| int me,p,src,dest=0,tag=0; | |
| int i,j,n,m; | |
| float x[1000010],row[1010],y[1010],ans[1010],val,res; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm comm=MPI_COMM_WORLD; | |
| MPI_Status stat; | |
| MPI_Comm_size(comm,&p); | |
| MPI_Comm_rank(comm,&me); | |
| if(me==0){ | |
| sf("%d %d",&n,&m); | |
| for(i=0;i<m*n;i++){ | |
| sf("%f",&x[i]); | |
| } | |
| for(i=0;i<m;i++) | |
| sf("%f",&y[i]); | |
| } | |
| MPI_Bcast(&n,1,MPI_INT,0,comm); | |
| MPI_Bcast(&m,1,MPI_INT,0,comm); | |
| MPI_Bcast(y,m,MPI_FLOAT,0,comm); | |
| MPI_Scatter(x,m,MPI_FLOAT,row,m,MPI_FLOAT,0,comm); | |
| val=0; | |
| for(i=0;i<m;i++) | |
| val+=row[i]*y[i]; | |
| MPI_Gather(&val,1,MPI_FLOAT,ans,1,MPI_FLOAT,0,comm); | |
| if(me==0){ | |
| for(i=0;i<m;i++) | |
| pf("%f ",ans[i]); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| int main() | |
| { | |
| int tag=0,rank,p; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_size(w,&p); | |
| MPI_Comm_rank(w,&rank); | |
| printf("In the processor %i\n",rank); | |
| MPI_Finalize(); | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| #define N 50 | |
| int arr[N]; | |
| int shuffle(int x,int p) | |
| { | |
| x <<= 1; | |
| if(x >= p) | |
| { | |
| x -= p; | |
| x += 1; | |
| } | |
| return x; | |
| } | |
| int rev_shuffle(int x,int p) | |
| { | |
| if(x&1) | |
| x += p; | |
| x >>= 1; | |
| return x; | |
| } | |
| int main() | |
| { | |
| int tag=0,rank,p,a,e,x; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_size(w,&p); | |
| MPI_Comm_rank(w,&rank); | |
| if(rank == 0) | |
| { | |
| printf("Enter the coefficients\n"); | |
| for(int i=0;i<p-1;i++) | |
| scanf("%i",&arr[i]); | |
| printf("Enter x: "); | |
| scanf("%i",&arr[p-1]); | |
| } | |
| MPI_Scatter(arr,1,MPI_INT,&a,1,MPI_INT,0,w); | |
| if(rank%2 == 0) | |
| e = 0; | |
| else | |
| e = 1; | |
| if(rank == p-1) | |
| x = a; | |
| for(int i=1;i<p;i*=2) | |
| { | |
| MPI_Bcast(&x,1,MPI_INT,p-1,w); | |
| if(e == 1) | |
| a *=x; | |
| if(shuffle(rank,p) != rank) | |
| { | |
| MPI_Send(&e,1,MPI_INT,shuffle(rank,p),tag,w); | |
| MPI_Recv(&e,1,MPI_INT,rev_shuffle(rank,p),tag,w,&status); | |
| } | |
| x = x*x; | |
| } | |
| if(rank != p-1) | |
| printf("processor %i has %i\n",rank,a); | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| #define N 50 | |
| int arr[N]; | |
| int main() | |
| { | |
| int tag=0,rank,p,a,b; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_rank(w,&rank); | |
| MPI_Comm_size(w,&p); | |
| if(rank == 0) | |
| printf("%i\n",p); | |
| if(rank == 0) | |
| { | |
| for(int i=0;i<p;i++) | |
| scanf("%i",&arr[i]); | |
| } | |
| MPI_Scatter(arr,1,MPI_INT,&a,1,MPI_INT,0,w); | |
| for(int i=1;i<p;i*=2) | |
| { | |
| if(rank+i < p) | |
| MPI_Send(&a,1,MPI_INT,rank+i,tag,w); //send to rank+2^ith node | |
| if(rank-i >= 0) | |
| { | |
| MPI_Recv(&b,1,MPI_INT,rank-i,tag,w,&status); //recieve from rank-2^ith node | |
| a += b; | |
| } | |
| } | |
| printf("%i has %i\n",rank,a); | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| #define N 50 | |
| int arr[N]; | |
| int shuffle(int x,int p) | |
| { | |
| x <<= 1; | |
| if(x >= p) | |
| { | |
| x -= p; | |
| x += 1; | |
| } | |
| return x; | |
| } | |
| int rev_shuffle(int x,int p) | |
| { | |
| if(x&1) | |
| x += p; | |
| x >>= 1; | |
| return x; | |
| } | |
| int main() | |
| { | |
| int tag=0,rank,p,a,b; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_size(w,&p); | |
| MPI_Comm_rank(w,&rank); | |
| if(rank == 0) | |
| { | |
| printf("Enter the elelments\n"); | |
| for(int i=0;i<p;i++) | |
| scanf("%i",&arr[i]); | |
| } | |
| MPI_Scatter(arr,1,MPI_INT,&a,1,MPI_INT,0,w); | |
| for(int i=1;i<p;i*=2) | |
| { | |
| if(shuffle(rank,p) != rank) | |
| { | |
| MPI_Send(&a,1,MPI_INT,shuffle(rank,p),tag,w); | |
| MPI_Recv(&a,1,MPI_INT,rev_shuffle(rank,p),tag,w,&status); | |
| } | |
| //exchage step | |
| MPI_Send(&a,1,MPI_INT,rank^1,tag,w); | |
| MPI_Recv(&b,1,MPI_INT,rank^1,tag,w,&status); | |
| a += b; | |
| } | |
| if(rank == p-1) | |
| printf("sum is %i",a); | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| double func(double x) //evaluate the function | |
| { | |
| return x*x; | |
| } | |
| int main() | |
| { | |
| double x,ini = 1,fin = 8; | |
| int tag=0,rank,p; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| double partial=0; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_rank(w,&rank); | |
| MPI_Comm_size(w,&p); | |
| x = ini + (fin - ini)/(p-1) * rank; | |
| if(rank == 0 || rank == p-1) | |
| { | |
| partial = func(x); // calulate corresponding x value and evaluate | |
| } | |
| else if(rank%2 == 0) | |
| { | |
| partial = 2*func(x); // calulate corresponding x value and evaluate | |
| } | |
| else | |
| { | |
| partial = 4 * func(x); | |
| } | |
| printf("process %i partial sum %lf\n",rank,partial); | |
| if(rank != 0) | |
| { | |
| MPI_Send(&partial,1,MPI_DOUBLE,0,tag,w); | |
| } | |
| else | |
| { | |
| int i; | |
| double temp; | |
| for(i=1;i<p;i++) | |
| { | |
| MPI_Recv(&temp,1,MPI_DOUBLE,i,tag,w,&status); | |
| partial += temp; | |
| } | |
| temp = (fin - ini)*partial/(p-1); | |
| printf("the integral evaluation is %lf\n",temp/3); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| #define N 10 | |
| int arr[N]; | |
| int main() | |
| { | |
| int i,tag=0,rank,p; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_size(w,&p); | |
| MPI_Comm_rank(w,&rank); | |
| if(rank == 0) | |
| { | |
| printf("Enter the values\n"); | |
| for(i=0;i<N;i++) | |
| { | |
| scanf("%i",&arr[i]); | |
| } | |
| } | |
| MPI_Bcast(arr,N,MPI_INT,0,w); | |
| int sum=0; | |
| for(i=rank;i<N;i+=p) | |
| { | |
| sum += arr[i]; | |
| } | |
| printf("sum at %i is %i\n",rank,sum); | |
| if(rank != 0) | |
| { | |
| MPI_Send(&sum,1,MPI_INT,0,tag,w); | |
| } | |
| else | |
| { | |
| int temp; | |
| for(i=1;i<p;i++) | |
| { | |
| MPI_Recv(&temp,1,MPI_INT,i,tag,w,&status); | |
| sum += temp; | |
| } | |
| printf("The total sum is %i",sum); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<mpi.h> | |
| #include<stdio.h> | |
| double func(double x) //evaluate the function | |
| { | |
| return x*x; | |
| } | |
| int main() | |
| { | |
| double x,ini = 1,fin = 2; | |
| int tag=0,rank,p; | |
| MPI_Status status; | |
| MPI_Comm w = MPI_COMM_WORLD; | |
| double partial=0; | |
| MPI_Init(NULL,NULL); | |
| MPI_Comm_rank(w,&rank); | |
| MPI_Comm_size(w,&p); | |
| if(rank == 0 || rank == p-1) | |
| { | |
| x = ini + (fin - ini)/(p-1) * rank; | |
| printf("%i %lf\n",rank,x); | |
| partial = func(x); // calulate corresponding x value and evaluate | |
| } | |
| else | |
| { | |
| x = ini + (fin - ini)/(p-1) * rank; | |
| printf("%i %lf\n",rank,x); | |
| partial = 2*func(x); // calulate corresponding x value and evaluate | |
| } | |
| //printf("process %i partial sum %lf\n",rank,partial); | |
| if(rank != 0) | |
| { | |
| MPI_Send(&partial,1,MPI_DOUBLE,0,tag,w); | |
| } | |
| else | |
| { | |
| int i; | |
| double temp; | |
| for(i=1;i<p;i++) | |
| { | |
| MPI_Recv(&temp,1,MPI_DOUBLE,i,tag,w,&status); | |
| partial += temp; | |
| } | |
| temp = (fin - ini)*partial/(p-1); | |
| printf("the integral evaluation is %lf\n",temp/2); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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> | |
| void Read_vector(char* prompt, float local_v[], int local_n, int n,int my_rank, MPI_Comm comm); | |
| float Serial_dot(float x[], float y[], int m); | |
| float Parallel_dot(float local_x[], float local_y[], int local_n, MPI_Comm comm); | |
| int main(void) { | |
| float* local_x; | |
| float* local_y; | |
| int n; | |
| int local_n; // = n/p | |
| float dot; | |
| int p; | |
| int my_rank; | |
| MPI_Comm comm; | |
| MPI_Init(NULL, NULL); | |
| comm = MPI_COMM_WORLD; | |
| MPI_Comm_size(comm, &p); | |
| MPI_Comm_rank(comm, &my_rank); | |
| if (my_rank == 0) { | |
| printf("Enter the order of the vectors\n"); | |
| scanf("%d", &n); | |
| } | |
| MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| local_n = n/p; | |
| local_x = malloc(local_n*sizeof(float)); | |
| local_y = malloc(local_n*sizeof(float)); | |
| Read_vector("the first vector", local_x, local_n, n, my_rank, comm); | |
| Read_vector("the second vector", local_y, local_n, n, my_rank, comm); | |
| dot = Parallel_dot(local_x, local_y, local_n, comm); | |
| if (my_rank == 0) | |
| printf("The dot product is %f\n", dot); | |
| free(local_x); | |
| free(local_y); | |
| MPI_Finalize(); | |
| return 0; | |
| } | |
| void Read_vector(char* prompt,float local_v[],int local_n,int n,int my_rank,MPI_Comm comm) { | |
| int i; | |
| float* temp; | |
| if (my_rank == 0) { | |
| temp = malloc(n*sizeof(float)); | |
| printf("Enter %s\n", prompt); | |
| for (i = 0; i < n; i++) | |
| scanf("%f", &temp[i]); | |
| MPI_Scatter(temp, local_n, MPI_FLOAT, local_v, local_n, MPI_FLOAT, | |
| 0, comm); | |
| free(temp); | |
| } else { | |
| MPI_Scatter(temp, local_n, MPI_FLOAT, local_v, local_n, MPI_FLOAT, | |
| 0, comm); | |
| } | |
| } | |
| float Serial_dot(float x[],float y[],int n) { | |
| int i; | |
| float sum = 0.0; | |
| for (i = 0; i < n; i++) | |
| sum += x[i]*y[i]; | |
| return sum; | |
| } | |
| float Parallel_dot(float local_x[],float local_y[],int local_n,MPI_Comm comm) { | |
| float local_dot; | |
| float dot = 0.0; | |
| local_dot = Serial_dot(local_x, local_y, local_n); | |
| MPI_Reduce(&local_dot, &dot, 1, MPI_FLOAT, MPI_SUM, 0, comm); | |
| return dot; | |
| } |
This file contains hidden or 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<string.h> | |
| #include<mpi.h> | |
| #include<math.h> | |
| #define ARRAY_SIZE 16 | |
| int main(int argc, char *argv[]){ | |
| int me; | |
| int size; | |
| if(MPI_Init(&argc, &argv) != MPI_SUCCESS){ | |
| fprintf(stderr, "Unable to initialize MPI!\n"); | |
| return -1; | |
| } | |
| MPI_Comm_rank(MPI_COMM_WORLD, &me); | |
| MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| if(ARRAY_SIZE % size != 0 && me == 0){ | |
| fprintf(stderr,"Array size must be multiple of mpi job size.\n"); | |
| return -1; | |
| } | |
| MPI_Status status; | |
| int * array = (int *) malloc(sizeof(int) * ARRAY_SIZE); | |
| int * chunk = (int *) malloc(sizeof(int) * ARRAY_SIZE/size); | |
| int i = 0, logn=log2(size), d; | |
| int total_sum = 0; | |
| for(i = 0; i < ARRAY_SIZE; i++){ | |
| array[i] = i+1; | |
| total_sum+=array[i]; | |
| } | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| MPI_Scatter(array,ARRAY_SIZE/size,MPI_INT,chunk,ARRAY_SIZE/size,MPI_INT,0,MPI_COMM_WORLD); | |
| int sum = 0; | |
| int temp = 0; | |
| for(i = 0; i < ARRAY_SIZE/size; i++) | |
| sum += chunk[i]; | |
| for(i=logn-1;i>=0;i--){ | |
| d=pow(2,i); | |
| if(me>=d && me<2*d){ | |
| MPI_Send(&sum, 1, MPI_INT, me-d,0,MPI_COMM_WORLD); | |
| }else if(me>=0 && me<d){ | |
| MPI_Recv(&temp, 1, MPI_INT, me+d,0,MPI_COMM_WORLD,&status); | |
| sum += temp; | |
| } | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| } | |
| if(me == 0){ | |
| fprintf(stdout,"Total: %d\n",sum); | |
| fprintf(stdout,"Correct Sum: %d\n",total_sum); | |
| } | |
| free(array); | |
| free(chunk); | |
| MPI_Finalize(); | |
| return 0; | |
| } |
This file contains hidden or 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<math.h> | |
| #include<mpi.h> | |
| #include<string.h> | |
| #define f(x) sin(x) | |
| #define sf scanf | |
| #define pf printf | |
| float calc(float a,float b,int n,float h){ | |
| float res,x; | |
| int i; | |
| res=(f(a)+f(b))/2.0; | |
| x=a; | |
| for(i=1;i<n;i++){ | |
| x+=h; | |
| res+=f(x); | |
| } | |
| return res*h; | |
| } | |
| int main(int argc,char* argv[]){ | |
| int n=100,ln,p,me,src,dst=0,tag=0; | |
| float a=0,b=1.57,la,lb,h,curr,ans; | |
| MPI_Comm comm; | |
| MPI_Status stat; | |
| MPI_Init(&argc,&argv); | |
| comm=MPI_COMM_WORLD; | |
| MPI_Comm_size(comm,&p); | |
| MPI_Comm_rank(comm,&me); | |
| h=(b-a)/n; | |
| ln=n/p; | |
| la=a+me*ln*h; | |
| lb=la+ln*h; | |
| curr=calc(la,lb,ln,h); | |
| if(me==0){ | |
| ans=curr; | |
| for(src=1;src<p;src++){ | |
| MPI_Recv(&curr,1,MPI_FLOAT,src,tag,MPI_COMM_WORLD,&stat); | |
| ans+=curr; | |
| } | |
| }else{ | |
| MPI_Send(&curr,1,MPI_FLOAT,dst,tag,MPI_COMM_WORLD); | |
| } | |
| if(me==0){ | |
| pf("n=%d\nf(x)=sin(x)\na=%f\tb=%f\nIntegral=%f\n",n,a,b,ans); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment