int main (int argc, char *argv[]){
	MPI_Comm comm;
	int p, my_rank;
	long long int n;
	long long int local_n;
	float ans=0;
	long long int in_circle=0;
	double start, finish, elapsed;
	float pi,err;



	MPI_Init(&argc, &argv);
	comm = MPI_COMM_WORLD;
	MPI_Comm_size(comm, &p);
	MPI_Comm_rank(comm, &my_rank);

	if (my_rank == 0){
		if (argc>1) {
			n = strtol(argv[1], NULL, 10);
		}
		else {
			n = 0;
		}
	}
	MPI_Bcast(&n, 1, MPI_LONG_LONG, 0, comm);
	if (n <= 0){
		if (my_rank == 0){
			printf("Enter the value for n: mpiexec -n <number of nodes> "); 
			printf("./mpi_monte_carlo_pi <value for n>. \nProgram will Terminate\n");
		}
		MPI_Finalize();
	}

	GET_TIME(start);
	local_n = n/p;
	in_circle = monte_carlo_pi(local_n, my_rank, in_circle);
	ans = global_calc(n, in_circle, my_rank, comm, p, ans);
	pi = pi_val();
	err = pi - ans;
	GET_TIME(finish);
	elapsed = finish-start;

	if (my_rank ==0){
		printf ("Estimated Value of Pi for %lld 'darts': %f\n",n,ans);
		printf ("Actual Value of Pi: %f\n", pi);
		printf ("Difference between Estimated and Actual: %f\n",err);
		printf ("Time: %f\n", elapsed);
	}

	MPI_Finalize();
	return 0;
}
Esempio n. 2
0
void g() {

	int procNumber;
	AcquireLock();
	procNumber = procCounter++;
	//printf("got lock\n");
	ReleaseLock();
	int size;
	unsigned int n, i;
	int in = 0;
	int d, pi, x, y;


	n = 1000000;

	//srand (time(NULL));
//	*randNum = 100;
	setSeed((int)time(NULL));
	//in = monte_carlo_pi(n);
	
	for(i = 0; i < CORES; i++) {
	  if(i == procNumber) {
	    results[i] = monte_carlo_pi(n, i);
	    AcquireLock();
	    done++;
	    ReleaseLock();
	  }
	}
	AcquireLock();
	if (procNumber == 0) {
	  while(done < CORES);
	  for (i=0; i < CORES; i++) {
	    in += results[i];
	  }
	  in *= 1000;
	  pi = 4*in/n;
	  //AcquireLock();
	  printf("Resposta in: %d\n",pi);
	  //ReleaseLock();
	} else {
	  printf("SOU O CORE %d\n", procNumber);
	}
	ReleaseLock();

}
Esempio n. 3
0
int main(void) {
	int size;
	unsigned int n, i;
	long long unsigned int in;
	double d, pi, x, y;
	
	n = 100000;

	//srand (time(NULL));
	*randNum = (int) time(NULL);

	in = monte_carlo_pi(n);

	pi = 4*in/((double)n);
	printf("%lf\n",pi);

	return 0;
}
Esempio n. 4
0
int main(int argc, char **argv) {

	unsigned long result, iterations;
	double et, included, total, pi, cpi;
	struct timeval start, end;

	name = argv[0];

	if (argc < 2) {
		usage();
	}

	iterations = atol(argv[1]);

	if (iterations <= 0) {
		usage();
	}

	gettimeofday(&start, NULL);

	result = monte_carlo_pi(*iterations);

	included = (double)(result);
	total = (double)(iterations);

	gettimeofday(&end, NULL);

	et = ((double)start.tv_sec) + ((double)start.tv_usec)/1000000.0;
	et = ((double)end.tv_sec) + ((double)end.tv_usec)/1000000.0 - et;

	pi = (4 * included) / total;

        cpi = 2 * asin(1);
	printf("Estimated π value is %.12f (error is %.12f)\n", pi, fabs(cpi - pi));
	printf("Total execution time %.6f seconds (%.2f iterations per second)\n",
		et, total/et);

}
Esempio n. 5
0
int main(void) {
	int size;
	unsigned int n, i;
	long long unsigned int in;
	double d, pi, x, y;
	long unsigned int duracao;
	struct timeval start, end;

	scanf("%d %u",&size, &n);

	srand (time(NULL));

	gettimeofday(&start, NULL);
	in = monte_carlo_pi(n);
	gettimeofday(&end, NULL);

	duracao = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));

	pi = 4*in/((double)n);
	printf("%lf\n%lu\n",pi,duracao);

	return 0;
}
double
pi (int n_epochs)
{
    
    return 4 * monte_carlo_pi(n_epochs) / (double) n_epochs;
}