Blame view

lab1/pi_omp.c 540 Bytes
6a98a5afa   kmazouzi   lab1
1
2
3
4
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  #include <omp.h>
adfef3110   kmazouzi   add n
5
  #define N 1000000000
6a98a5afa   kmazouzi   lab1
6
7
8
9
10
11
12
13
14
15
16
  int main (int argc, char** argv)
  {
      long long int n;
      long long int i;
  
      double l_sum, x, h;
  
      n=N;
  
      if(argc==2)
      {
adfef3110   kmazouzi   add n
17
         n=atoll(argv[1]);
6a98a5afa   kmazouzi   lab1
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
      }
  
  
      h = 1.0/n;
  
      l_sum = 0.0;
  
  #pragma omp parallel for private(i,x) reduction(+:l_sum)    
      for (i = 0; i < n; i ++)
      {
          x = (i + 0.5)*h;
          l_sum += 4.0/(1.0 + x*x);
      }
  
      l_sum *= h;
adfef3110   kmazouzi   add n
33
34
      printf("N=%lld,PI=%0.20g
  ",n, l_sum);
6a98a5afa   kmazouzi   lab1
35
36
37
  
      return 0;
  }