Advertisement
desdemona

soww_lab2 2

Mar 24th, 2015
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.24 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <mpi.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. #define PRECISION 0.000001
  9. //#define RANGESIZE 1
  10. #define DATA 0
  11. #define RESULT 1
  12. #define FINISH 2
  13.  
  14. //#define DEBUG
  15. double f (double x)
  16. {
  17.   return x;//sin (x) * sin (x) / x;
  18. }
  19.  
  20. double SimpleIntegration (double a, double b)
  21. {
  22.   return a;
  23. }
  24.  
  25. int main (int argc, char **argv)
  26. {
  27.   int messagecnt =0;
  28.   int myrank, proccount;
  29.   double a = 1, b = 100;
  30.   int n=512;
  31.  
  32.   char input[512];
  33.   int zarodek;
  34.   zarodek= time(NULL);
  35.   srand(zarodek);   // za zarodek wstawiamy pobrany czas w sekundach
  36.   int i=0;
  37.   for(i=0;i<n;i++)
  38.   {
  39.     int x = rand()%3;
  40.     char xx;
  41.     if(x==0)
  42.     { xx = 'a';}
  43.     else if(x==1)
  44.     { xx='b';}
  45.     else
  46.     { xx='c';}
  47.     input[i] = xx;
  48.   }
  49.  
  50.   double result = 0, resulttemp;
  51.   int sentcount = 0;
  52.   MPI_Status status;
  53.  
  54.   // Initialize MPI
  55.   MPI_Init (&argc, &argv);
  56. // find out my rank
  57.   MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
  58. // find out the number of processes in MPI_COMM_WORLD
  59.   MPI_Comm_size (MPI_COMM_WORLD, &proccount);
  60.  
  61.   if (proccount < 2)
  62.     {
  63.       printf ("Run with at least 2 processes");
  64.       MPI_Finalize ();
  65.       return -1;
  66.  
  67.     }
  68.    
  69.     if(n % (proccount-1) != 0)
  70.     {
  71.       printf ("Can't devide task evenly, try running with 9");
  72.       MPI_Finalize ();
  73.       return -1;    
  74.     }
  75.    
  76.     int package_size = n/(proccount-1);
  77.     /*
  78.   if (((b - a) / RANGESIZE) < 2 * (proccount - 1))
  79.     {
  80.       printf ("More subranges needed");
  81.       MPI_Finalize ();
  82.       return -1;
  83.     }*/
  84.    
  85. // now the master will distribute the data and slave processes will perform computations
  86.   if (myrank == 0)
  87.     {
  88.       int a = 0;
  89.       int b = a + package_size-1;
  90.  
  91. // first distribute some ranges to all slaves
  92.       for (i = 1; i < proccount; i++)
  93.     {
  94.       printf ("\nMaster sending chars from %f,%f to process %d\n", a,
  95.           b, i);
  96.       fflush (stdout);
  97.      
  98.       int nums[2];
  99.       nums[0] = sentcount;
  100.       nums[1] = package_size;
  101. // send it to process i
  102.       MPI_Send(nums,2,MPI_INT,i,DATA,MPI_COMM_WORLD);
  103.       MPI_Send(&input[(i-1)*package_size], package_size, MPI_CHAR, i, DATA, MPI_COMM_WORLD);
  104.       sentcount++;
  105.       a = b + 1;
  106.       b = a + package_size - 1;
  107.     }
  108.  
  109.       do
  110.     {
  111. // distribute remaining subranges to the processes which have completed their parts
  112.       MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  113.       int recived_result;
  114.       int recived_data[512];
  115.       if (status.MPI_TAG == RESULT)
  116.       {
  117.          MPI_Get_count(&status, MPI_INT, &recived_result);
  118.          // Now receive the message with the allocated buffer
  119.           MPI_Recv(recived_data, recived_result, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  120.               printf("Master dynamically received %d numbers from %d\n", recived_result, status.MPI_SOURCE);
  121.           fflush (stdout);     
  122.           if(recived_data[0] < -666)
  123.           {
  124.         printf("Slave %d didn't find anything\n",status.MPI_SOURCE);
  125.         fflush (stdout);       
  126.           }
  127.           else
  128.           {
  129.         printf("Slave %d found: ",status.MPI_SOURCE);
  130.         for(i=0; i<recived_result; i++)
  131.         {
  132.            printf("%d ",recived_data[i]);
  133.         }
  134.         pritnf("\n");
  135.         fflush(stdout);
  136.           }
  137.       }
  138.  
  139.  
  140.       // check the sender and send some more data
  141.       printf ("\nMaster sending chars from %f,%f to process %d", a,
  142.           b, i);
  143.       fflush (stdout);
  144.       int nums[2];
  145.       nums[0] = sentcount;
  146.       nums[1] = package_size;
  147. // send it to process i
  148.       MPI_Send(nums,2,MPI_INT,i,DATA,MPI_COMM_WORLD);
  149.       MPI_Send(&input[(i-1)*package_size], package_size, MPI_CHAR, i, DATA, MPI_COMM_WORLD);
  150.       sentcount++;
  151.       a = b + 1;
  152.       b = a + package_size - 1;
  153.     }
  154.       while (b + 1 < n);
  155.       // now receive results from the processes
  156.       for (i = 0; i < (proccount - 1); i++)
  157.     {
  158.       MPI_Probe (i+1, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  159.       int recived_result;
  160.       int recived_data[512];
  161.       if (status.MPI_TAG == RESULT)
  162.       {
  163.          MPI_Get_count(&status, MPI_INT, &recived_result);
  164.          // Now receive the message with the allocated buffer
  165.           MPI_Recv(recived_data, recived_result, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  166.               printf("Master dynamically received %d numbers from %d\n", recived_result, status.MPI_SOURCE);
  167.           fflush (stdout);     
  168.           if(recived_data[0] < -666)
  169.           {
  170.         printf("Slave %d didn't find anything\n",status.MPI_SOURCE);
  171.         fflush (stdout);       
  172.           }
  173.           else
  174.           {
  175.         printf("Slave %d found: ",status.MPI_SOURCE);
  176.         for(i=0; i<recived_result; i++)
  177.         {
  178.            printf("%d ",recived_data[i]);
  179.         }
  180.         pritnf("\n");
  181.         fflush(stdout);
  182.           }
  183.       }
  184.     }
  185. // shut down the slaves
  186.       for (i = 1; i < proccount; i++)
  187.     {
  188.       MPI_Send (NULL, 0, MPI_DOUBLE, i, FINISH, MPI_COMM_WORLD);
  189.     }
  190.  
  191.       // now display the result
  192.       printf ("\nHi, I am process 0, the result is %f\n", result);
  193.     }
  194.   else
  195.     {
  196.       // slave
  197.       // this is easy - just receive data and do the work
  198.       do
  199.     {
  200.      
  201.       MPI_Probe (0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  202.       char data_recived[512];
  203.       int numbers[2];
  204.       int pnumber;
  205.       int sizep;
  206.       if (status.MPI_TAG == DATA)
  207.         {
  208.           if(messagecnt % 2 == 0)
  209.           {
  210.         MPI_Recv (numbers, package_size, MPI_CHAR, 0, DATA, MPI_COMM_WORLD,
  211.               &status);
  212.         sizep = numbers[1];
  213.         pnumber= numbers[0];
  214.           }
  215.           else
  216.           {
  217.           MPI_Recv (data_recived, numbers[1], MPI_CHAR, 0, DATA, MPI_COMM_WORLD,
  218.               &status);    
  219.  
  220.   // compute my part
  221.         int last = -1;
  222.         int found[512];
  223.         int howmany = 0;
  224.         int index = 0;
  225.         for(i=0; i<sizep; i++)
  226.         {
  227.           if(last == -1 && data_recived[i] == 'a')
  228.           {
  229.             last++;
  230.             index = i;
  231.           }
  232.           else if(last == 0 && data_recived[i] == 'b')
  233.           {
  234.             last++;
  235.           }
  236.           else if(last == 1 && data_recived[i] == 'c')
  237.           { last=-1;
  238.             index = index + (pnumber-1)*sizep;
  239.             found[howmany] = index;
  240.             howmany++;
  241.           }
  242.           else
  243.           { last=-1;}
  244.          
  245.           if(i==sizep-1)
  246.           {
  247.             if(howmany == 0)
  248.             {found[0] = -666;}
  249.             MPI_Send(found, howmany+1, MPI_INT,0,RESULT, MPI_COMM_WORLD);
  250.           }
  251.         }
  252.           }
  253.           messagecnt++;
  254.           }
  255. // send the result back
  256.  
  257.         }
  258.         while (status.MPI_TAG != FINISH);
  259.        
  260.     }
  261.    
  262.  
  263.     }
  264.    
  265.    
  266. // Shut down MPI
  267.   MPI_Finalize();
  268.   return 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement