File: test_openmp2.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (103 lines) | stat: -rw-r--r-- 1,595 bytes parent folder | download | duplicates (3)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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 */
};


void dtime(double *t);


int main(int argc, char *argv[]) 
{


  int i,id,Nthrds,Nprocs,N,k,kp;
  double sum[100],tsum;
  double stime,etime;

  dtime(&stime);

  N = 30000000; 
  k = 0;


#pragma omp parallel shared(k,sum) private(i,id,kp,tsum)
{

  id = omp_get_thread_num();
  Nthrds = omp_get_num_threads();
  Nprocs = omp_get_num_procs();

  printf("Nprocs=%2d Nthrds=%2d id=%d\n",Nprocs,Nthrds,id);

  kp = id;
  k = Nthrds-1;

  do {

    sum[kp] = 0.0;
    for (i=0; i<N; i++){
      sum[kp] += sin((double)(i+kp))+cos((double)(i+2*kp));     
    }  

    printf("id=%2d kp=%2d sum=%15.12f\n",id,kp,sum[kp]);

#pragma omp critical
    kp = ++k;

  }
  while (k<20);

#pragma omp barrier  
{
  tsum = 0.0;  
  for (i=0; i<20; i++){
    tsum += sum[i];
  }
}      
  printf("tsum=%15.12f\n",tsum);

}


  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);
  */

}