Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <mpi.h>
  4. #include <math.h>
  5.  
  6. #define ROOT 0
  7. #define MSG_TAG 100
  8.  
  9. int S=234;
  10. int K=3;
  11. int N=7;
  12. int i,w,x,j;
  13.  
  14.  
  15. int main(int argc,char **argv)
  16. {
  17.     int size,tid;
  18.     int R = 1;
  19.    
  20.  
  21.     MPI_Init(&argc, &argv);
  22.     int matrix [N][N] ;
  23.  
  24.     MPI_Comm_size( MPI_COMM_WORLD, &size );
  25.     MPI_Comm_rank( MPI_COMM_WORLD, &tid );
  26.     int res;
  27.     int reallycount=0;
  28.     if ( tid == 0 ) {
  29.     MPI_Status status;
  30.     int p,r;
  31.  
  32.     int i,j;
  33.     int liczba;
  34.    
  35.     // wypelnianie macierzy glownej
  36.     int matrix [N][N] ;
  37.     for (i=0;i<N;i++){
  38.             for(j=0;j<N;j++){
  39.                  matrix[i][j]=1+i*5;
  40.  
  41.                 liczba = matrix[i][j];
  42.  
  43.  
  44.             }
  45.         }
  46.     //zliczanie ile bedzie podmacierzy
  47.     int podmacierze=0;
  48.         for (i=0;i<N;i++){
  49.             for(j=0;j<N;j++){
  50.  
  51.  
  52.                 if (i+K<=N & j+K<=N){
  53.        
  54.  
  55.                     podmacierze++; 
  56.                 }
  57.             }
  58.         }
  59.     printf("PODMACIERZE: %d\n", podmacierze);
  60.  
  61.     //PODZIAL PODMACIERZY NA PROCESY, WYSLANIE DO KAZDEGO PROCESU INFO, ILE DOSTANIE PODMACIERZY DO SPRAWDZENIA
  62.     int x;
  63.     for(x=1;x<size;x++){
  64.         int forsend = (podmacierze/(size-x));
  65.  
  66.         podmacierze=podmacierze-forsend;
  67.         printf("podmacierze obecnie %d\n", podmacierze);
  68.         MPI_Send( &forsend, 1, MPI_INT, size-x, MSG_TAG, MPI_COMM_WORLD );
  69.     }
  70.  
  71. //  WYSYLANIE PODMACIERZY
  72.         int count=1;
  73.         for (i=0;i<N;i++){
  74.             for(j=0;j<N;j++){
  75.                 int matrix2[K][K];
  76.  
  77.                 if (i+K<=N & j+K<=N){
  78.                     int proces = count%size;
  79.             if(proces==0){
  80.             proces++;
  81.             count++;           
  82.             }  
  83.                     for (w=0;w<K;w++){
  84.                         for (x=0;x<K;x++){
  85.                             matrix2[w][x]= matrix[i+w][i+x];
  86.  
  87.                            int send = matrix2[w][x];
  88.    
  89.                            MPI_Send( &send, 1, MPI_INT, proces, MSG_TAG, MPI_COMM_WORLD );
  90.  
  91.                         }
  92.                     }
  93.                     count++;
  94.             reallycount++; 
  95.                 }
  96.             }
  97.         }
  98.     printf("wyslalem %d podmacierzy\n", reallycount);
  99.  
  100.     }
  101.     else {
  102.     int forme=0;
  103.     MPI_Status status3;
  104.     MPI_Recv( &forme, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status3);
  105.     printf("jestem proces numer %d, dla mnie jest %d podmacierzy\n", tid, forme);
  106.     int cos;
  107.     MPI_Status status2;
  108.     int l,k;
  109.  
  110.     int z;
  111.     int numberIteration=0;
  112.     int countRec=0;
  113.     int g;
  114.  
  115.    
  116.     for(g=0;g<forme;g++){
  117.     int sum=0;
  118.     for (l=0;l<K;l++){
  119.             for(k=0;k<K;k++){
  120.  
  121.  
  122.             int wait;      
  123.                   MPI_Recv( &wait, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status2);
  124.  
  125.           sum+=wait;
  126.  
  127.             }
  128.  
  129.     }       printf("proces: %d, suma : %d\n",tid,sum);
  130.         if(sum==S){printf("ZNALEZIONO : %d\n", sum);
  131.     }
  132.  
  133.     numberIteration++;
  134.          printf("proces: %d, numer iteracji : %d\n",tid,numberIteration);
  135.  
  136. }
  137.  
  138.  
  139.  
  140.         }
  141.  
  142.  
  143.     MPI_Finalize();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement