Advertisement
desdemona

soww_lab2 wreszcie ładzia

Apr 7th, 2015
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define DATA 0
  8. #define RESULT 1
  9. #define FINISH 2
  10. #define INFO 3
  11.  
  12. int main (int argc, char **argv)
  13. {
  14.     int myid, numprocs;
  15.     int totalresult = 0;
  16.     int n=512;
  17.     char input[512]; //chary w ktorych szukamy abc;
  18.     srand(time(NULL));   //losowe litery do tablicy
  19.     int i=0;
  20.     for(i=0;i<n;i++)
  21.     {
  22.         int x = rand()%4;
  23.         char xx;
  24.         if(x==0)
  25.         { xx = 'a';}
  26.         else if(x==1)
  27.         { xx='b';}
  28.         else if(x==2)
  29.         { xx='c';}
  30.         else
  31.         { xx = 'd';}//zeby nie było za łatwo
  32.         input[i] = xx;
  33.     }
  34.    
  35.     MPI_Init(&argc,&argv);
  36.     MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  37.     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  38.     printf(" Hello from process: %d  Numprocs is %d\n",myid,numprocs);
  39.      
  40.     if (numprocs < 2)
  41.     {
  42.         printf ("Run with at least 2 processes");
  43.         MPI_Finalize ();
  44.         return -1;
  45.     }
  46.     if(n % (numprocs-1) != 0)
  47.     {
  48.         printf ("Can't devide task evenly, try running with -n 9");
  49.         MPI_Finalize ();
  50.         return -1;    
  51.     }
  52.    
  53.     if(myid == 0)
  54.     {
  55.         int chunksize = n / (numprocs-1);
  56.         printf ("Master: chunksize is %d\n", chunksize);
  57.         int slave = 1;
  58.         MPI_Status status;
  59.         int messagenum = 0;
  60.         //send chunks
  61.         for(slave = 1;slave < numprocs;slave++)
  62.         {
  63.             MPI_Send(&input[(slave-1)*chunksize],chunksize,MPI_CHAR,slave,DATA,MPI_COMM_WORLD);
  64.             printf ("Master: sending some chars from index %d to process %d\n", (slave-1)*chunksize, slave);
  65.             fflush (stdout);
  66.         }
  67.         int chs= 0;
  68.         int recived[512];
  69.  
  70.         for(;;)
  71.         {
  72.             MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);          
  73.             if(status.MPI_TAG==RESULT)
  74.             {
  75.                 messagenum++;
  76.                 printf ("Master: recived something from %d\n", status.MPI_SOURCE);
  77.                 fflush (stdout);
  78.                 MPI_Get_count(&status, MPI_INT, &chs); 
  79.                 MPI_Recv(&recived, chs, MPI_INT,status.MPI_SOURCE,RESULT,MPI_COMM_WORLD,&status);
  80.                 if(recived[0] < 0)
  81.                 {
  82.                     printf ("Master: slave %d disapointed me, didn't find anything\n", status.MPI_SOURCE);
  83.                     fflush (stdout);               
  84.                 }
  85.                 else
  86.                 {
  87.                     printf("Master: slave %d found abc at indexes: \n", status.MPI_SOURCE);
  88.                     for(i=0; i<chs; i++)
  89.                     { printf("%d ",recived[i]);}
  90.                     printf("\n");
  91.                     fflush(stdout);
  92.                     totalresult+=chs;                  
  93.                 }
  94.             }
  95.             if(messagenum == numprocs-1)
  96.             {break;}       
  97.         }  
  98.        
  99.         for (slave = 1; slave < numprocs; slave++)
  100.         { MPI_Send (NULL, 0, MPI_INT, slave, FINISH, MPI_COMM_WORLD); }
  101.         printf ("Master: abc was found in total %d times\n", totalresult);
  102.         fflush (stdout);   
  103.     }
  104.     else
  105.     {
  106.         MPI_Status status;
  107.         int mychunksize, myindex;
  108.         char recived[512];
  109.         for(;;)
  110.         {
  111.             MPI_Probe(0,MPI_ANY_TAG,MPI_COMM_WORLD, &status);
  112.             if(status.MPI_TAG==DATA)
  113.             {
  114.                 printf ("%d: recived data from master! senpai notice me! \n", myid);
  115.                 fflush (stdout);   
  116.                 MPI_Get_count(&status, MPI_CHAR ,&mychunksize);
  117.                 printf ("%d: data size is %d\n", myid,mychunksize);
  118.                 myindex = (myid-1)*mychunksize;
  119.                 printf ("%d: guessing my start index is %d \n", myid, myindex);
  120.                 fflush (stdout);                   
  121.                 MPI_Recv(&recived, mychunksize,MPI_CHAR,0,DATA,MPI_COMM_WORLD,&status);
  122.                 int i=0;
  123.                 int last = -1;
  124.                 int foundindexes[512];
  125.                 int foundnum = 0;
  126.                 int index = 0;
  127.                 for(i=0; i<mychunksize; i++)
  128.                 {
  129.                     if(last == -1 && recived[i] == 'a')
  130.                     {
  131.                         last++;
  132.                         index = i;
  133.                     }
  134.                     else if(last == 0 && recived[i] == 'b')
  135.                     {
  136.                         last++;
  137.                     }
  138.                     else if(last == 1 && recived[i] == 'c')
  139.                     {
  140.                         last=-1;
  141.                         foundindexes[foundnum] = index + myindex;
  142.                         foundnum++;
  143.                         printf ("%d: found something!\n", myid);
  144.                     }
  145.                     else
  146.                     { last=-1; }   
  147.                     if(i==mychunksize-1)
  148.                     {
  149.                         int realf = foundnum;
  150.                         if(foundnum == 0)
  151.                         {foundindexes[0] = -666; foundnum++; }
  152.                         MPI_Send(&foundindexes, foundnum, MPI_INT,0,RESULT, MPI_COMM_WORLD);
  153.                         printf ("%d: sending my %d results to master\n", myid, realf);
  154.                     }
  155.                 }              
  156.             }
  157.             else if(status.MPI_TAG==FINISH)
  158.             {
  159.                 printf ("%d: everybody has to die someday, I just thought I still had some time..\n", myid);
  160.                 fflush (stdout);
  161.                 break;             
  162.             }
  163.             else
  164.             {
  165.                 printf ("%d: some werid shit is going on - sepuku time\n", myid);
  166.                 fflush (stdout);
  167.                 break;             
  168.             }
  169.         }
  170.     }
  171.    
  172.    
  173.     MPI_Finalize();
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement