#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <omp.h>
#define N 100000000
#define u 2.0
#define nt 5
void somaParcialR(double *soma, int tnum){
int i,ci,pi;
double passo,x;
passo = (u-1)/(double)N;
ci = (int) tnum*(N/nt);
pi = (int) (tnum + 1)*(N/nt);
for(i=ci; i<pi; i++){//parĂ¢metros inicial e final "dependem da thread"
x = 1 + i*passo;
*soma = *soma + 0.5*(1/x + 1/(x+passo));
}
}
void main(){
double soma=0.0, ti,tf, passo=(u-1)/(double)N;
int i,tnum;
struct timeval tempo_inicio,tempo_fim;
//marca tempo inicial
gettimeofday(&tempo;_inicio,NULL);
#pragma omp parallel num_threads(nt) reduction(+:soma)
{
tnum = omp_get_thread_num();
somaParcialR(&soma;,tnum);
}
//marca o tempo final
gettimeofday(&tempo;_fim,NULL);
//calcula o tempo gasto
tf = (double)tempo_fim.tv_usec/1000000.0 + tempo_fim.tv_sec;
ti = (double)tempo_inicio.tv_usec/1000000.0 + tempo_inicio.tv_sec;
printf("NĂºmero de threads: %d -> Tempo gasto: %.2f milissegundos \n",nt,(tf-ti)*1000000);
//printf("\nln %.1f = %f\n", u, passo*soma);
}Add a code snippet to your website: www.paste.org