Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <mpi.h>
- #include <math.h>
- #include <stdlib.h>
- #include <time.h>
- #define PRECISION 0.000001
- //#define RANGESIZE 1
- #define DATA 0
- #define RESULT 1
- #define FINISH 2
- //#define DEBUG
- double f (double x)
- {
- return x;//sin (x) * sin (x) / x;
- }
- double SimpleIntegration (double a, double b)
- {
- return a;
- }
- int main (int argc, char **argv)
- {
- int messagecnt =0;
- int myrank, proccount;
- double a = 1, b = 100;
- int n=512;
- char input[512];
- int zarodek;
- zarodek= time(NULL);
- srand(zarodek); // za zarodek wstawiamy pobrany czas w sekundach
- int i=0;
- for(i=0;i<n;i++)
- {
- int x = rand()%3;
- char xx;
- if(x==0)
- { xx = 'a';}
- else if(x==1)
- { xx='b';}
- else
- { xx='c';}
- input[i] = xx;
- }
- double result = 0, resulttemp;
- int sentcount = 0;
- MPI_Status status;
- // Initialize MPI
- MPI_Init (&argc, &argv);
- // find out my rank
- MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
- // find out the number of processes in MPI_COMM_WORLD
- MPI_Comm_size (MPI_COMM_WORLD, &proccount);
- if (proccount < 2)
- {
- printf ("Run with at least 2 processes");
- MPI_Finalize ();
- return -1;
- }
- if(n % (proccount-1) != 0)
- {
- printf ("Can't devide task evenly, try running with 9");
- MPI_Finalize ();
- return -1;
- }
- int package_size = n/(proccount-1);
- /*
- if (((b - a) / RANGESIZE) < 2 * (proccount - 1))
- {
- printf ("More subranges needed");
- MPI_Finalize ();
- return -1;
- }*/
- // now the master will distribute the data and slave processes will perform computations
- if (myrank == 0)
- {
- int a = 0;
- int b = a + package_size-1;
- // first distribute some ranges to all slaves
- for (i = 1; i < proccount; i++)
- {
- printf ("\nMaster sending chars from %f,%f to process %d\n", a,
- b, i);
- fflush (stdout);
- int nums[2];
- nums[0] = sentcount;
- nums[1] = package_size;
- // send it to process i
- MPI_Send(nums,2,MPI_INT,i,DATA,MPI_COMM_WORLD);
- MPI_Send(&input[(i-1)*package_size], package_size, MPI_CHAR, i, DATA, MPI_COMM_WORLD);
- sentcount++;
- a = b + 1;
- b = a + package_size - 1;
- }
- do
- {
- // distribute remaining subranges to the processes which have completed their parts
- MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- int recived_result;
- int recived_data[512];
- if (status.MPI_TAG == RESULT)
- {
- MPI_Get_count(&status, MPI_INT, &recived_result);
- // Now receive the message with the allocated buffer
- MPI_Recv(recived_data, recived_result, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- printf("Master dynamically received %d numbers from %d\n", recived_result, status.MPI_SOURCE);
- fflush (stdout);
- if(recived_data[0] < -666)
- {
- printf("Slave %d didn't find anything\n",status.MPI_SOURCE);
- fflush (stdout);
- }
- else
- {
- printf("Slave %d found: ",status.MPI_SOURCE);
- for(i=0; i<recived_result; i++)
- {
- printf("%d ",recived_data[i]);
- }
- printf("\n");
- fflush(stdout);
- }
- }
- // check the sender and send some more data
- printf ("\nMaster sending chars from %f,%f to process %d", a,
- b, i);
- fflush (stdout);
- int nums[2];
- nums[0] = sentcount;
- nums[1] = package_size;
- // send it to process i
- MPI_Send(nums,2,MPI_INT,i,DATA,MPI_COMM_WORLD);
- MPI_Send(&input[(i-1)*package_size], package_size, MPI_CHAR, i, DATA, MPI_COMM_WORLD);
- sentcount++;
- a = b + 1;
- b = a + package_size - 1;
- }
- while (b + 1 < n);
- // now receive results from the processes
- for (i = 0; i < (proccount - 1); i++)
- {
- MPI_Probe (i+1, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- int recived_result;
- int recived_data[512];
- if (status.MPI_TAG == RESULT)
- {
- MPI_Get_count(&status, MPI_INT, &recived_result);
- // Now receive the message with the allocated buffer
- MPI_Recv(recived_data, recived_result, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- printf("Master dynamically received %d numbers from %d\n", recived_result, status.MPI_SOURCE);
- fflush (stdout);
- if(recived_data[0] < -666)
- {
- printf("Slave %d didn't find anything\n",status.MPI_SOURCE);
- fflush (stdout);
- }
- else
- {
- printf("Slave %d found: ",status.MPI_SOURCE);
- for(i=0; i<recived_result; i++)
- {
- printf("%d ",recived_data[i]);
- }
- printf("\n");
- fflush(stdout);
- }
- }
- }
- // shut down the slaves
- for (i = 1; i < proccount; i++)
- {
- MPI_Send (NULL, 0, MPI_DOUBLE, i, FINISH, MPI_COMM_WORLD);
- }
- // now display the result
- printf ("\nHi, I am process 0, the result is %f\n", result);
- }
- else
- {
- // slave
- // this is easy - just receive data and do the work
- do
- {
- MPI_Probe (0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- char data_recived[512];
- int numbers[2];
- int pnumber;
- int sizep;
- if (status.MPI_TAG == DATA)
- {
- if(messagecnt % 2 == 0)
- {
- MPI_Recv (numbers, package_size, MPI_CHAR, 0, DATA, MPI_COMM_WORLD, &status);
- sizep = numbers[1];
- pnumber= numbers[0];
- }
- else
- {
- MPI_Recv (data_recived, numbers[1], MPI_CHAR, 0, DATA, MPI_COMM_WORLD, &status);
- // compute my part
- int last = -1;
- int found[512];
- int howmany = 0;
- int index = 0;
- for(i=0; i<sizep; i++)
- {
- if(last == -1 && data_recived[i] == 'a')
- {
- last++;
- index = i;
- }
- else if(last == 0 && data_recived[i] == 'b')
- {
- last++;
- }
- else if(last == 1 && data_recived[i] == 'c')
- { last=-1;
- index = index + (pnumber-1)*sizep;
- found[howmany] = index;
- howmany++;
- }
- else
- { last=-1;}
- if(i==sizep-1)
- {
- if(howmany == 0)
- {found[0] = -666;}
- MPI_Send(found, howmany+1, MPI_INT,0,RESULT, MPI_COMM_WORLD);
- }
- }
- }
- messagecnt++;
- }
- // send the result back
- }
- while (status.MPI_TAG != FINISH);
- }
- // Shut down MPI
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement