Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.1 kB
4
Indexable
Never
#include "utility.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <mpi.h>

#define IS_NOT_PRIME_TAG 5

int main(int argc,char **argv) {
  Args ins__args;
  parseArgs(&ins__args, &argc, argv);

  //program input argument
  long inputArgument = ins__args.arg;
  int isPrime, isPrimeFinal, received=1; 
  //int summaryIsPrime = 1;
  long mine;
  MPI_Request request;
  int ready;

  struct timeval ins__tstart, ins__tstop;

  int myrank,nproc;
  
  MPI_Init(&argc,&argv);

  // obtain my rank
  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
  // and the number of processes
  MPI_Comm_size(MPI_COMM_WORLD,&nproc);

  if(!myrank)
      gettimeofday(&ins__tstart, NULL);
      
     


  // run your computations here (including MPI communication)

  
  //odtad czekam na jakiegos receiva
   MPI_Irecv(&received, 1, MPI_INT, MPI_ANY_SOURCE , IS_NOT_PRIME_TAG, MPI_COMM_WORLD, &request);
  
  isPrime = 1;
  for(mine = myrank+2; mine * mine<inputArgument; mine+=nproc)
  {
  	MPI_Test(&request, &ready, IS_NOT_PRIME_TAG);
  	if(ready){
  		printf("\nmy rank is %d\n", myrank);
  		break;
  		
  	}
  	if(!(inputArgument % mine))
  	{
  		isPrime = 0;
  		break;
  	}
  }
  
  //wysylam do wszystkich wyniki
  if (isPrime == 0) {
  	//summaryIsPrime = 0;
  	printf("\nI found proof: %d\n", myrank);
  	for(int i=0; i<nproc; i++) {
	  	if (i != myrank){
	  	  	MPI_Isend(&isPrime, 1, MPI_INT, i, IS_NOT_PRIME_TAG, MPI_COMM_WORLD, &request);
	  	}
  	}
  	//czekam na zakonczenie receiva
   	//MPI_Wait(&request, IS_NOT_PRIME_TAG);
   	printf("\nNie jest pierwsza");
  }
  
  /*if (summaryIsPrime == 1 ) {
  	printf("\nJest pierwsza");
  }*/
  
  
  // synchronize/finalize your computations
  MPI_Reduce (&isPrime, &isPrimeFinal, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  //if(!myrank)
  //{
  //	if(isPrimeFinal == nproc)
  //	{
  //		printf("\nJest pierwsza: %d", isPrimeFinal);
  //	} else {
  //		printf("\nNie jest pierwsza: %d", isPrimeFinal);
  //	}
  //}




  if (!myrank) {
    gettimeofday(&ins__tstop, NULL);
    ins__printtime(&ins__tstart, &ins__tstop, ins__args.marker);
  }
  
  MPI_Finalize();

}