Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include "omp.h"
  2. #include "stdio.h"
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <process.h>
  8. using namespace std;
  9. const long long n = 100000000;
  10. static short* tablica = new short[n];
  11. volatile DWORD licznik;
  12. long long int suma;
  13.  
  14.  
  15.  
  16. CRITICAL_SECTION  g_Section;
  17.  
  18.  
  19.  
  20.  
  21. long long suma1(short* tab, long long n) {
  22.     long long sum = 0;
  23.     long long i;
  24.     for (i = 0; i < n; i++) {
  25.         sum = sum + tab[i];
  26.     }
  27.     return sum;
  28. }
  29.  
  30.  
  31. unsigned int g_Counter = 0;
  32.  
  33.  
  34. void __cdecl ThreadProc(void * Args)
  35. {
  36.     int j = n / 2;
  37.  
  38.     for (int i = 0; i < j; i++)
  39.     {
  40.         EnterCriticalSection(&g_Section);
  41.         suma = suma + tablica[i];
  42.         LeaveCriticalSection(&g_Section);
  43.     }
  44.     _endthread();
  45. }
  46.  
  47. void __cdecl ThreadProc2(void * Args)
  48. {
  49.     int j = n / 2;
  50.  
  51.     for (j; j < n; j++)
  52.     {
  53.         EnterCriticalSection(&g_Section);
  54.         suma = suma + tablica[j];
  55.         LeaveCriticalSection(&g_Section);
  56.     }
  57.  
  58.     _endthread();
  59. }
  60.  
  61.  
  62. void __cdecl ThreadProc3(void * Args)
  63. {
  64.     int j = n / 2;
  65.     long long int sum = 0;
  66.     for (int i = 0; i < j; i++)
  67.     {
  68.  
  69.         sum = sum + tablica[i];
  70.  
  71.     }
  72.     EnterCriticalSection(&g_Section);
  73.     suma = suma + sum;
  74.     LeaveCriticalSection(&g_Section);
  75.     _endthread();
  76. }
  77.  
  78. void __cdecl ThreadProc4(void * Args)
  79. {
  80.     int j = n / 2;
  81.     long long int sum = 0;
  82.     for (j; j < n; j++)
  83.     {
  84.         sum = sum + tablica[j];
  85.     }
  86.     EnterCriticalSection(&g_Section);
  87.     suma = suma + sum;
  88.     LeaveCriticalSection(&g_Section);
  89.     _endthread();
  90. }
  91.  
  92.  
  93. long long suma2(long long n) {
  94.  
  95.     long long i;
  96.     suma = 0;
  97.     HANDLE hThread = (HANDLE)_beginthread(ThreadProc, 0, NULL);
  98.     HANDLE hThread2 = (HANDLE)_beginthread(ThreadProc2, 0, NULL);
  99.     WaitForSingleObject(hThread, INFINITE);
  100.     WaitForSingleObject(hThread2, INFINITE);
  101.  
  102.     return suma;
  103.  
  104. }
  105.  
  106.  
  107. long long suma3(long long n) {
  108.  
  109.     long long i;
  110.     suma = 0;
  111.     HANDLE hThread3 = (HANDLE)_beginthread(ThreadProc3, 0, NULL);
  112.     HANDLE hThread4 = (HANDLE)_beginthread(ThreadProc4, 0, NULL);
  113.     WaitForSingleObject(hThread3, INFINITE);
  114.     WaitForSingleObject(hThread4, INFINITE);
  115.  
  116.     return suma;
  117.  
  118. }
  119.  
  120.  
  121. int main(int argc, char* argv[]) {
  122.  
  123.     InitializeCriticalSection(&g_Section);
  124.  
  125.  
  126.     cout << "ilosc dostepnych rdzeni : " << omp_get_num_procs() << endl;
  127.  
  128.  
  129.     long long i;
  130.     for (int j = 0; j < 3; j++){
  131.         licznik = GetTickCount();
  132. #pragma omp parallel for private(i) num_threads(4)
  133.         for (i = 0; i < n; i++){
  134.             tablica[i] = rand() % 10;
  135.         }
  136.  
  137.         cout << "Czas tworzenia tablicy: ";
  138.         cout << GetTickCount() - licznik << endl << endl;
  139.         long long suma;
  140.         for (int i = 0; i < 3; i++)
  141.         {
  142.             licznik = GetTickCount();
  143.             suma = suma1(tablica, n);
  144.  
  145.             cout << "wersja iteracyjna" << endl;
  146.             cout << "ilosc elementow: " << n << endl;
  147.             cout << "suma: " << suma << endl;
  148.             cout << "czas wykonania: " << GetTickCount() - licznik << endl;
  149.         }
  150.  
  151.         printf("\n\n");
  152.         for (int i = 0; i < 3; i++)
  153.         {
  154.             licznik = GetTickCount();
  155.             suma = suma2(n);
  156.             cout << "wersja na 2 watkach z sekcja krytyczna" << endl;
  157.             cout << "ilosc elementow: " << n << endl;
  158.             cout << "suma: " << suma << endl;
  159.             cout << "czas wykonania: " << GetTickCount() - licznik << endl;
  160.         }
  161.         cout << endl << endl;
  162.         for (int i = 0; i < 3; i++)
  163.         {
  164.             licznik = GetTickCount();
  165.             suma = suma3(n);
  166.             cout << "wersja na 2 watkach z zmienna lokalna i sekcja krytyczna" << endl;
  167.             cout << "ilosc elementow: " << n << endl;
  168.             cout << "suma: " << suma << endl;
  169.             cout << "czas wykonania: " << GetTickCount() - licznik << endl;
  170.         }
  171.  
  172.  
  173.         system("pause");
  174.     }
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement