-
-
Save cmrmahesh/6298546f4ab48f23a23a9e1d23e8333b to your computer and use it in GitHub Desktop.
Parallel Processing New
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 my_rank; | |
| int p; | |
| int source; | |
| int dest; | |
| int sum = 0; | |
| int tag = 0; | |
| int message; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); | |
| MPI_Comm_size(MPI_COMM_WORLD,&p); | |
| if(my_rank != 0) | |
| { | |
| message = my_rank+1; | |
| dest = 0; | |
| MPI_Send(&message,1,MPI_INT,dest,my_rank,MPI_COMM_WORLD); | |
| } | |
| else | |
| { | |
| for(source = 1;source<p;source++) | |
| { | |
| MPI_Recv(&message,1,MPI_INT,source,source,MPI_COMM_WORLD,&status); | |
| sum += message; | |
| } | |
| printf("%d",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> | |
| int compute(int a,int b) | |
| { | |
| int x= 1,y=a; | |
| while(b>0) | |
| { | |
| if(b%2) | |
| x =x*y; | |
| y = y*y; | |
| b = b>>1; | |
| } | |
| return x; | |
| } | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size; | |
| int vec[] = {1,2,4,5,6}; //1 + 2x^1 + 4X^2 .... | |
| int x = 2; | |
| int ans = 0,final_ans;; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| ans = compute(x,rank); | |
| MPI_Reduce(&ans,&final_ans,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD); | |
| if(rank == 0) | |
| { | |
| printf("%d\n",final_ans); | |
| } | |
| 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 argc,char *argv[]) | |
| { | |
| int rank,size,msg,itr,sum; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| int X[5] = {1,2,3,4,5}; | |
| int Y[5] = {1,2,3,4,5}; | |
| if(rank == 0) | |
| { | |
| sum = X[rank]*Y[rank]; | |
| for(itr = 1;itr<5;itr++) | |
| { | |
| MPI_Recv(&msg,1,MPI_INT,itr,itr,MPI_COMM_WORLD,&status); | |
| sum = sum + msg; | |
| } | |
| printf("%d",sum); | |
| } | |
| else | |
| { | |
| msg = X[rank]*Y[rank]; | |
| MPI_Send(&msg,1,MPI_INT,0,rank,MPI_COMM_WORLD); | |
| } | |
| 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<stdbool.h> | |
| #include<mpi.h> | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size; | |
| int arr[8] = {1,2,3,4,5,6,7,0}; | |
| int x = 2,s; | |
| int v; | |
| int cnt = 1; | |
| int mul = 1; | |
| bool mask = 0; | |
| arr[7] = x; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| v = arr[rank]; | |
| s = size; | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| if(rank == 7) | |
| mul = x; | |
| while(s>0) | |
| { | |
| mask = rank&cnt; | |
| MPI_Bcast(&mul,1,MPI_INT,7,MPI_COMM_WORLD); | |
| if(mask) | |
| v = v*mul; | |
| if(rank == 7) | |
| mul = v; | |
| s = s/2; | |
| cnt = cnt*2; | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| } | |
| printf("%d %d\n",rank,v); | |
| 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<string.h> | |
| #include "mpi.h" | |
| int main(int argc,char *argv[]) | |
| { | |
| int myrank; | |
| int p; | |
| int source; | |
| int dest; | |
| int num = 1,den=1; | |
| int tag = 0; | |
| int message; | |
| int X[5]; | |
| int Y[5]; | |
| int i,x = 3; | |
| double ans = 0; | |
| double final_ans = 0; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&myrank); | |
| MPI_Comm_size(MPI_COMM_WORLD,&p); | |
| if(myrank == 0) | |
| { | |
| for(i=0;i<5;i++) | |
| scanf("%d",&X[i]); | |
| for(i=0;i<5;i++) | |
| scanf("%d",&Y[i]); | |
| } | |
| MPI_Bcast(X,5,MPI_INT,0,MPI_COMM_WORLD); | |
| MPI_Bcast(Y,5,MPI_INT,0,MPI_COMM_WORLD); | |
| for(i=0;i<5;i++) | |
| { | |
| if(i!=myrank) | |
| { | |
| num = num*(x-X[i]); | |
| den = den*(X[myrank]-X[i]); | |
| } | |
| } | |
| ans = num/(double)den; | |
| ans = ans*Y[myrank]; | |
| MPI_Reduce(&ans,&final_ans,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD); | |
| if(myrank == 0) | |
| { | |
| printf("%lf\n",final_ans); | |
| } | |
| 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> | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size; | |
| int a[3][3] = {1,2,3,4,5,6,7,8,9}; | |
| int b[3][3] = {1,2,3,4,5,6,7,8,9}; | |
| int c = 0,i,j; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| int row = rank/3; | |
| int col = rank%3; | |
| printf("%d %d %d\n",rank,row,col); | |
| for(i=0;i<3;i++) | |
| { | |
| c += a[row][i]*b[i][col]; | |
| } | |
| printf("%d %d %d\n",row,col,c); | |
| 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> | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size; | |
| int mat[3][3] = {1,2,3,4,5,6,7,8,9}; | |
| int vec[3] = {3,5,6}; | |
| int val = 0,i; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| for(i=0;i<size;i++) | |
| { | |
| val = val + mat[rank][i]*vec[i]; | |
| } | |
| printf("%d %d\n",rank,val); | |
| 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> | |
| int main(int argc,char *argv[]) | |
| { | |
| int size,rank; | |
| int s; | |
| int cnt = 1; | |
| int local_val[8] = {2,3,5,3,3,2,5,8}; | |
| int l_val,t_val=0; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| l_val = local_val[rank]; | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| s = size; | |
| while(s>0) | |
| { | |
| // printf("%d %d\n",rank,s); | |
| if((rank+cnt) < size) | |
| { | |
| MPI_Send(&l_val,1,MPI_INT,rank+cnt,0,MPI_COMM_WORLD); | |
| } | |
| if((rank-cnt)>=0) | |
| { | |
| MPI_Recv(&t_val,1,MPI_INT,rank-cnt,0,MPI_COMM_WORLD,&status); | |
| } | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| l_val = l_val + t_val; | |
| t_val = 0; | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| cnt = cnt*2; | |
| s = s/2; | |
| } | |
| // printf("Out of the loop %d\n",rank); | |
| printf("%d %d\n",rank,l_val); | |
| 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> | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size; | |
| int arr[3][3] = {1,2,3,4,5,6,7,8,9}; | |
| int l_val,t_val=0,row,col; | |
| int n=3; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| row = rank/n; | |
| col = rank%n; | |
| l_val = arr[row][col]; | |
| printf("%d %d %d\n",row,col,l_val); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| if(col+1 < n) | |
| MPI_Send(&l_val,1,MPI_INT,row*3+col+1,0,MPI_COMM_WORLD); | |
| if(col>0) | |
| MPI_Recv(&t_val,1,MPI_INT,row*3+col-1,0,MPI_COMM_WORLD,&status); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| l_val = l_val + t_val; | |
| printf("%d %d %d\n",row,col,l_val); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| if(row+1 < n && col==n-1) | |
| MPI_Send(&l_val,1,MPI_INT,(row+1)*3+col,0,MPI_COMM_WORLD); | |
| if(row>0 && col == n-1) | |
| MPI_Recv(&t_val,1,MPI_INT,(row-1)*3+col,0,MPI_COMM_WORLD,&status); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| l_val = l_val + t_val; | |
| printf("%d %d %d\n",row,col,l_val); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| if(row+1 < n && col==n-1) | |
| MPI_Send(&l_val,1,MPI_INT,(row+1)*3+col-1,0,MPI_COMM_WORLD); | |
| if(row>0 && col == n-2) | |
| MPI_Recv(&t_val,1,MPI_INT,(row-1)*3+col+1,0,MPI_COMM_WORLD,&status); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| l_val = l_val + t_val; | |
| printf("%d %d %d\n",row,col,l_val); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| if(col>0) | |
| MPI_Send(&t_val,1,MPI_INT,row*3+col-1,0,MPI_COMM_WORLD); | |
| if(col < n-1) | |
| MPI_Recv(&t_val,1,MPI_INT,row*3+col+1,0,MPI_COMM_WORLD,&status); | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| l_val = l_val + t_val; | |
| MPI_Barrier(MPI_COMM_WORLD); | |
| printf("%d %d %d\n",row,col,l_val); | |
| 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> | |
| float fn(float x) | |
| { | |
| return x*x; | |
| } | |
| float compute(float a,float b,float h) | |
| { | |
| int cnt = 0; | |
| float to_return = 0.0; | |
| while(a<=b) | |
| { | |
| if(cnt%2) | |
| to_return += 4*fn(a); | |
| else | |
| to_return += 2*fn(a); | |
| cnt++; | |
| a += h; | |
| } | |
| return to_return; | |
| } | |
| int main(int argc,char *argv[]) | |
| { | |
| int size,rank; | |
| float a,b,n,h; | |
| float local,global; | |
| a = 1; | |
| b = 3; | |
| n = 10000; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| h = (b-a)/n; | |
| local = (h*compute(a+(rank*n*h)/size,a+((rank+1)*n*h)/size,h))/3.0; | |
| //printf("%f %f\n",h,local); | |
| MPI_Reduce(&local,&global,1,MPI_FLOAT,MPI_SUM,0,MPI_COMM_WORLD); | |
| if(rank == 0) | |
| { | |
| printf("%f",global); | |
| } | |
| 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> | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size; | |
| int msg,sum,i; | |
| MPI_Status status; | |
| int arr[5] = {1,2,3,4,5}; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| if(rank != 0) | |
| { | |
| MPI_Send(&arr[rank],1,MPI_INT,0,rank,MPI_COMM_WORLD); | |
| } | |
| else | |
| { | |
| sum = arr[rank]; | |
| for(i=1;i<5;i++) | |
| { | |
| MPI_Recv(&msg,1,MPI_INT,i,i,MPI_COMM_WORLD,&status); | |
| sum = sum + msg; | |
| } | |
| printf("%d",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> | |
| float fn(double x) | |
| { | |
| return x*x; | |
| } | |
| float getAns(double a,double b,double h) | |
| { | |
| float to_return = 0.0; | |
| while(a<=b) | |
| { | |
| to_return += fn(a); | |
| a = a + h; | |
| } | |
| return to_return; | |
| } | |
| int main(int argc,char *argv[]) | |
| { | |
| int rank,size,itr; | |
| float a,b,n,h; | |
| float global,local; | |
| a = 1; | |
| b = 10; | |
| n = 10000; | |
| double msg; | |
| MPI_Status status; | |
| MPI_Init(&argc,&argv); | |
| MPI_Comm_size(MPI_COMM_WORLD,&size); | |
| MPI_Comm_rank(MPI_COMM_WORLD,&rank); | |
| h = (b-a)/n; | |
| local = h*getAns(a+(rank*n*h)/size,a+((rank+1)*n*h)/size,h); | |
| MPI_Reduce(&local,&global,1,MPI_FLOAT,MPI_SUM,0,MPI_COMM_WORLD); | |
| if(rank == 0) | |
| { | |
| printf("The ans is %f",global); | |
| } | |
| MPI_Finalize(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment