Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by dino ( 13 years ago )
#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);
}

 

Revise this Paste

Children: 65016 65018
Your Name: Code Language: