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 DATA 0
- #define RESULT 1
- #define FINISH 2
- #define INFO 3
- int main (int argc, char **argv)
- {
- int myid, numprocs;
- int totalresult = 0;
- int n=512;
- char input[512]; //chary w ktorych szukamy abc;
- srand(time(NULL)); //losowe litery do tablicy
- int i=0;
- for(i=0;i<n;i++)
- {
- int x = rand()%4;
- char xx;
- if(x==0)
- { xx = 'a';}
- else if(x==1)
- { xx='b';}
- else if(x==2)
- { xx='c';}
- else
- { xx = 'd';}//zeby nie było za łatwo
- input[i] = xx;
- }
- MPI_Init(&argc,&argv);
- MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
- MPI_Comm_rank(MPI_COMM_WORLD,&myid);
- printf(" Hello from process: %d Numprocs is %d\n",myid,numprocs);
- if (numprocs < 2)
- {
- printf ("Run with at least 2 processes");
- MPI_Finalize ();
- return -1;
- }
- if(n % (numprocs-1) != 0)
- {
- printf ("Can't devide task evenly, try running with -n 9");
- MPI_Finalize ();
- return -1;
- }
- if(myid == 0)
- {
- int chunksize = n / (numprocs-1);
- printf ("Master: chunksize is %d\n", chunksize);
- int slave = 1;
- MPI_Status status;
- int messagenum = 0;
- //send chunks
- for(slave = 1;slave < numprocs;slave++)
- {
- MPI_Send(&input[(slave-1)*chunksize],chunksize,MPI_CHAR,slave,DATA,MPI_COMM_WORLD);
- printf ("Master: sending some chars from index %d to process %d\n", (slave-1)*chunksize, slave);
- fflush (stdout);
- }
- int chs= 0;
- int recived[512];
- for(;;)
- {
- MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- if(status.MPI_TAG==RESULT)
- {
- messagenum++;
- printf ("Master: recived something from %d\n", status.MPI_SOURCE);
- fflush (stdout);
- MPI_Get_count(&status, MPI_INT, &chs);
- MPI_Recv(&recived, chs, MPI_INT,status.MPI_SOURCE,RESULT,MPI_COMM_WORLD,&status);
- if(recived[0] < 0)
- {
- printf ("Master: slave %d disapointed me, didn't find anything\n", status.MPI_SOURCE);
- fflush (stdout);
- }
- else
- {
- printf("Master: slave %d found abc at indexes: \n", status.MPI_SOURCE);
- for(i=0; i<chs; i++)
- { printf("%d ",recived[i]);}
- printf("\n");
- fflush(stdout);
- totalresult+=chs;
- }
- }
- if(messagenum == numprocs-1)
- {break;}
- }
- for (slave = 1; slave < numprocs; slave++)
- { MPI_Send (NULL, 0, MPI_INT, slave, FINISH, MPI_COMM_WORLD); }
- printf ("Master: abc was found in total %d times\n", totalresult);
- fflush (stdout);
- }
- else
- {
- MPI_Status status;
- int mychunksize, myindex;
- char recived[512];
- for(;;)
- {
- MPI_Probe(0,MPI_ANY_TAG,MPI_COMM_WORLD, &status);
- if(status.MPI_TAG==DATA)
- {
- printf ("%d: recived data from master! senpai notice me! \n", myid);
- fflush (stdout);
- MPI_Get_count(&status, MPI_CHAR ,&mychunksize);
- printf ("%d: data size is %d\n", myid,mychunksize);
- myindex = (myid-1)*mychunksize;
- printf ("%d: guessing my start index is %d \n", myid, myindex);
- fflush (stdout);
- MPI_Recv(&recived, mychunksize,MPI_CHAR,0,DATA,MPI_COMM_WORLD,&status);
- int i=0;
- int last = -1;
- int foundindexes[512];
- int foundnum = 0;
- int index = 0;
- for(i=0; i<mychunksize; i++)
- {
- if(last == -1 && recived[i] == 'a')
- {
- last++;
- index = i;
- }
- else if(last == 0 && recived[i] == 'b')
- {
- last++;
- }
- else if(last == 1 && recived[i] == 'c')
- {
- last=-1;
- foundindexes[foundnum] = index + myindex;
- foundnum++;
- printf ("%d: found something!\n", myid);
- }
- else
- { last=-1; }
- if(i==mychunksize-1)
- {
- int realf = foundnum;
- if(foundnum == 0)
- {foundindexes[0] = -666; foundnum++; }
- MPI_Send(&foundindexes, foundnum, MPI_INT,0,RESULT, MPI_COMM_WORLD);
- printf ("%d: sending my %d results to master\n", myid, realf);
- }
- }
- }
- else if(status.MPI_TAG==FINISH)
- {
- printf ("%d: everybody has to die someday, I just thought I still had some time..\n", myid);
- fflush (stdout);
- break;
- }
- else
- {
- printf ("%d: some werid shit is going on - sepuku time\n", myid);
- fflush (stdout);
- break;
- }
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement