1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <omp.h>
struct timeval2 {
long tv_sec; /* second */
long tv_usec; /* microsecond */
};
#define asize1 10
#define asize2 8
#define asize3 4
void dtime(double *t);
int main(int argc, char *argv[])
{
int i,is,ie,id,N,NT;
double sum;
double stime,etime;
dtime(&stime);
N = 300000000;
NT = 4;
#pragma omp parallel private(i,id,is,ie,sum)
{
id = omp_get_thread_num();
is = id*N/NT;
ie = (id+1)*N/NT;
printf("%d is=%2d ie=%2d\n",id,is,ie);
sum = 0.0;
for (i=is; i<ie; i++){
sum += sin((double)i)+cos((double)i);
}
printf("%15.12f\n",sum);
}
dtime(&etime);
printf("Elapsed time (s)=%15.12f\n",etime-stime);
return 0;
}
void dtime(double *t)
{
/* real time */
struct timeval timev;
gettimeofday(&timev, NULL);
*t = timev.tv_sec + (double)timev.tv_usec*1e-6;
/* user time + system time */
/*
float tarray[2];
clock_t times(), wall;
struct tms tbuf;
wall = times(&tbuf);
tarray[0] = (float) (tbuf.tms_utime / (float)CLK_TCK);
tarray[1] = (float) (tbuf.tms_stime / (float)CLK_TCK);
*t = (double) (tarray[0]+tarray[1]);
printf("dtime: %lf\n",*t);
*/
}
|