Exemplo n.º 1
0
int main(int argc, char* argv[]) {
   long int number_tosses, number_in_circle;
   int thread_count, i;
   double x, y, distancia;

   if (argc != 3) Usage(argv[0]);
   thread_count = strtol(argv[1], NULL, 10);
   number_tosses = strtoll(argv[2], NULL, 10);
   if (thread_count < 1 || number_tosses < 1) Usage(argv[0]);

   number_in_circle =0;
   srandom(0);
#  pragma omp parallel for num_threads(thread_count) \
      reduction(+: number_in_circle) private(x, y, distancia)
   for (i = 0; i < number_tosses; i++) {
      x = num_aleatorio(); // Gera nnumero entre -1 e 1
      y = num_aleatorio();
      distancia = x*x + y*y;

      if (distancia <= 1) {
         number_in_circle += 1;
      }
   }
   double pi = 4*number_in_circle/((double) number_tosses);
   printf("Estimacao de pi = %.14f\n", pi);
   return 0;
}  /* main */
void geraMatriz(double * a, int n, int m) {
   int i, j;
   for (i = 0; i < n; ++i) {
      for (j = 0; j < m; j++) {
         a[i*m + j] = num_aleatorio();
      }
   }
}
void geraMatriz(double * a, int m, int n) {
   int i, j;
   for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
         a[i*n + j] = num_aleatorio();
      }
   }
}
void geraMatrizTriangular(double * a, int m) {
   int i, j;
   for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
         if (j >= i) {
            a[i * m + j] = num_aleatorio();
         } else {
            a[i *m +j] = 0;
         }
      }
   }
}
int qual_situacao(int qtd_massa)
{
	int i = 0, r = 0;
	
	i = num_aleatorio(qtd_massa - 1);
	
	printf ("Aleatorio=%d ", i);
	
	r = massa[i].Risco;

	printf ("Risco=%d ", r);

	return (r);
}
void geraVetor(double * a, int m) {
   int i, j;
   for (i = 0; i < m; ++i) {
         a[i] = num_aleatorio();
   }
}