Advertisement
Alan468

PRIR LAB 3

Dec 6th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <math.h>
  5. #include <conio.h>
  6. #include <cstdio>
  7. #include <omp.h>
  8. #include <cmath>
  9.  
  10. double f(double x)
  11. {
  12.     return sin(x);
  13. }
  14.  
  15. double CalkujSekwencyjne(double Start, double Stop, int Precision, double dx)
  16. {
  17.     double SUM = 0;
  18.  
  19.     for (double x = Start; x < Stop; x += dx)
  20.     {
  21.         SUM += f(x) * dx;
  22.     }
  23.  
  24.     return SUM;
  25. }
  26.  
  27. double CalkujRownolegleReduction(double Start, double Stop, int Precision, double dx, int ThreadsNum)
  28. {
  29.     double SUMA = 0;
  30.     double x = Start;
  31.  
  32.     int start = 0;
  33.     int i = 0;
  34.  
  35. #pragma omp parallel for shared(x,Precision,dx,start,Start) private(i) num_threads(ThreadsNum) reduction(+ : SUMA)
  36.     for (i = start; i < Precision; i++)
  37.     {
  38.         SUMA += f(Start + i*dx)*dx;
  39.         x += dx;
  40.     }
  41.     //SUMA *= dx;
  42.  
  43.     return SUMA;
  44. }
  45.  
  46. double CalkujRownolegleAtomic(double Start, double Stop, int Precision, double dx, int ThreadsNum)
  47. {
  48.     double SUM = 0;
  49.     double x = Start;
  50.  
  51.     int start = 0;
  52.     int i = 0;
  53.  
  54. #pragma omp parallel shared(x,Precision,dx,start,Start,SUM) private(i) num_threads(ThreadsNum)
  55.     {
  56.         double Result = 0;
  57. #pragma omp for
  58.         for (i = start; i < Precision; i++)
  59.         {
  60.             x += dx;
  61.             Result += f(Start + i*dx)*dx;
  62.         }
  63. #pragma omp atomic
  64.         SUM += Result;
  65.     }
  66.     //SUM *= dx;
  67.  
  68.     return SUM;
  69. }
  70.  
  71. void AtomicAndReductionFor(double Start, double Stop, int Precision, double dx, int ThreadsNum)
  72. {
  73.     double Time; // Czas pracy algorytmu
  74.     double Result; // Wynik algorytmu
  75.  
  76.     // Calkowanie rownolegle (atomic)
  77.     printf("Rownolegle atomic %d\n", ThreadsNum);
  78.  
  79.     Time = omp_get_wtime();// Pobranie aktualnego czasu
  80.     Result = CalkujRownolegleAtomic(Start, Stop, Precision, dx, ThreadsNum);// Calkowanie rownolegle
  81.  
  82.     printf("Czas: %lf\n", omp_get_wtime() - Time);// Obliczenie czasu trwania algorytmu
  83.     printf("Wynik: %.10lf\n", Result);// Wynik algorytmu
  84.  
  85.     printf("\n");
  86.  
  87.     // Calkowanie rownolegle (reduction)
  88.     printf("Rownolegle reduction %d\n", ThreadsNum);
  89.  
  90.     Time = omp_get_wtime();// Pobranie aktualnego czasu
  91.     Result = CalkujRownolegleReduction(Start, Stop, Precision, dx, ThreadsNum);// Calkowanie rownolegle
  92.  
  93.     printf("Czas: %lf\n", omp_get_wtime() - Time);// Obliczenie czasu trwania algorytmu
  94.     printf("Wynik: %.10lf\n", Result);// Wynik algorytmu
  95. }
  96.  
  97. int main()
  98. {
  99.     double Start = 0; // Startatek zakresu calkowania
  100.     double Stop = M_PI_2; // Stop zakresu calkowania
  101.     long long int Precision = 100000000; // Dokladnosc
  102.  
  103.     double Time; // Czas pracy algorytmu
  104.     double Result; // Wynik algorytmu
  105.  
  106.     // Obliczenie dx (wielkosci skoku)
  107.     double dx = (Stop - Start) / Precision;
  108.    
  109.     // Calkowanie sekwencyjne
  110.     printf("Sekwencyjnie\n");
  111.  
  112.     Time = omp_get_wtime(); // Pobranie aktualnego czasu
  113.     Result = CalkujSekwencyjne(Start, Stop, Precision, dx); // Calkowanie sekwencyjne
  114.  
  115.     printf("Czas: %lf\n", omp_get_wtime() - Time); // Obliczenie czasu trwania algorytmu
  116.     printf("Wynik: %.10lf\n", Result); // Wynik algorytmu
  117.  
  118.     printf("\n");
  119.  
  120.     AtomicAndReductionFor(Start, Stop, Precision, dx, 4);
  121.     printf("\n--------------------\n");
  122.     AtomicAndReductionFor(Start, Stop, Precision, dx, 8);
  123.     printf("\n--------------------\n");
  124.     AtomicAndReductionFor(Start, Stop, Precision, dx, 16);
  125.  
  126.     _getch();
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement