Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.24 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <mpi.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7. #define ROOT 0
  8. #define MSG_TAG 100
  9.  
  10. int S=1000;
  11. int K=20;
  12. int N=1000;
  13. int i,w,x,j;
  14.  
  15.  
  16. int main(int argc,char **argv)
  17. {
  18.     int size,tid;
  19.     double start,stop, wyp,wys,pol;
  20.  
  21.  
  22.  
  23.     MPI_Init(&argc, &argv);
  24.     start = MPI_Wtime();
  25.     int matrix [N][N] ;
  26.     char processor_name[MPI_MAX_PROCESSOR_NAME];
  27.         int name_len;
  28.         MPI_Get_processor_name(processor_name, &name_len);
  29.  
  30.     MPI_Comm_size( MPI_COMM_WORLD, &size );
  31.     MPI_Comm_rank( MPI_COMM_WORLD, &tid );
  32.     int res;
  33.     int reallycount=0;
  34.     if ( tid == 0 ) {
  35.     MPI_Status status;
  36.     int p,r;
  37.  
  38.    // srand(time(0));
  39. //int d = rand();
  40.   //  printf("RAND: %d", d);
  41.  
  42.  
  43.     int i,j;
  44.  
  45.     // wypelnianie macierzy glownej
  46.     int matrix [N][N] ;
  47.     for (i=0;i<N;i++){
  48.             for(j=0;j<N;j++){
  49.                  matrix[i][j]=2*i+j;
  50.  
  51.  
  52.             }
  53.         }
  54.     wyp = MPI_Wtime();
  55.     printf("wypelniono %f\n", wyp-start);
  56.     //zliczanie ile bedzie podmacierzy
  57.     int podmacierze=0;
  58.         for (i=0;i<N;i++){
  59.             for(j=0;j<N;j++){
  60.  
  61.  
  62.                 if (i+K<=N & j+K<=N){
  63.  
  64.  
  65.                     podmacierze++;
  66.                 }
  67.             }
  68.         }
  69.     printf("PODMACIERZE: %d\n", podmacierze);
  70.         pol = MPI_Wtime();
  71.     printf("policzono %f\n", pol-start);
  72.  
  73.     for(x=1;x<size;x++){
  74.         int ww=N*N;
  75.         MPI_Send( &matrix, ww, MPI_INT,x, MSG_TAG, MPI_COMM_WORLD );
  76.     }
  77.     int podmacierze1=0;
  78.         for (i=tid;i<N;i=i+size){
  79.             for(j=0;j<N;j++){
  80.  
  81.  
  82.                 if (i+K<=N & j+K<=N){
  83.         int suma=0;
  84.         for (w=0;w<K;w++){
  85.                         for (x=0;x<K;x++){
  86.                             suma=suma+ matrix[i+w][i+x];
  87.  
  88.  
  89.         }}         
  90.        
  91.             if(suma==S){
  92.               printf("ZNALEZIONO, SUMA: %d\n",suma);
  93.             }  
  94.  
  95.  
  96.                     podmacierze1++;
  97.                 }
  98.             }
  99.         }
  100.   //  printf("PODMACIERZE: %d\n", podmacierze1);
  101.    
  102.  
  103.     //PODZIAL PODMACIERZY NA PROCESY, WYSLANIE DO KAZDEGO PROCESU INFO, ILE DOSTANIE PODMACIERZY DO SPRAWDZENIA
  104.     int x;
  105.     for(x=1;x<size;x++){
  106.         int forsend = (podmacierze/(size-x));
  107.  
  108.         podmacierze=podmacierze-forsend;
  109.        // MPI_Send( &forsend, 1, MPI_INT, size-x, MSG_TAG, MPI_COMM_WORLD );
  110.     }
  111.  
  112.  
  113.     wys = MPI_Wtime();
  114.     printf("wyslano %f\n", wys-start);
  115.    // printf("wyslalem %d podmacierzy\n", reallycount);
  116.  
  117.     }
  118.     else {
  119.     MPI_Status status2;
  120.         int matrixs [N][N] ;
  121.  
  122.     int ile = N*N;
  123.         printf("ILE: %d\n", ile);
  124.         MPI_Recv( &matrixs, ile, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status2);
  125.  
  126.         //printf("to : %d\n", matrixs[122][324]);
  127.     int podmacierze=0;
  128.         for (i=tid;i<N;i=i+size){
  129.             for(j=0;j<N;j++){
  130.  
  131.         int suma=0;
  132.                 if (i+K<=N & j+K<=N){
  133.            
  134.                     for (w=0;w<K;w++){
  135.                         for (x=0;x<K;x++){
  136.                             suma=suma+ matrixs[i+w][i+x];
  137.  
  138.  
  139.         }}         
  140.        
  141.    
  142.             if(suma==S){
  143.               printf("ZNALEZIONO, SUMA: %d\n",suma);
  144.             }  
  145.             //printf("sumka: %d", suma);
  146.  
  147.                 }
  148.             }
  149.         }
  150.  
  151. /*    int forme=0;
  152.     MPI_Status status3;
  153.     MPI_Recv( &forme, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status3);
  154.     printf("PC: %s , jestem proces numer %d, dla mnie jest %d podmacierzy\n",processor_name, tid, forme);
  155.     int cos;
  156.     MPI_Status status2;
  157.     int l,k;
  158.  
  159.     int z;
  160.     int numberIteration=0;
  161.     int countRec=0;
  162.     int g;
  163.  
  164.    
  165.     for(g=0;g<forme;g++){
  166.     int sum=0;
  167.     int matrixs [K][K] ;
  168.     int wieviel = K*K;
  169.     MPI_Recv( &matrixs, wieviel, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status2);
  170.     for (l=0;l<K;l++){
  171.             for(k=0;k<K;k++){
  172.  
  173.  
  174.             int wait = matrixs[k][l];
  175.                   //MPI_Recv( &wait, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status2);
  176.  
  177.           sum+=wait;
  178.  
  179.             }
  180.  
  181.     }     //  printf("PC: %s, proces: %d, suma : %d\n",processor_name,tid,sum);
  182.         if(sum==S){printf("ZNALEZIONO : %d\n", sum);
  183.     }
  184.  
  185.     numberIteration++;
  186.       //   printf("proces: %d, numer iteracji : %d\n",tid,numberIteration);
  187.  
  188. }*/
  189.  
  190.  
  191.  
  192.         }
  193.  
  194.     stop = MPI_Wtime();
  195.     printf("koniec %f\n", stop-start);
  196.     MPI_Finalize();
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement