David_Nightinga1e

os_lab_5

Dec 6th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <Windows.h>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8.  
  9. double result;
  10. __declspec(thread) double tempSum;
  11. const int N = 19000000;
  12. const int sizeOfBlock = 730519;
  13. double* results;
  14.  
  15. struct Info
  16. {
  17.     DWORD position;
  18.     HANDLE event;
  19.     bool calculationEnded;
  20.     int numberOfThread;
  21. };
  22.  
  23. DWORD WINAPI ThreadProc(LPVOID info)
  24. {
  25.     tempSum = 0;
  26.  
  27.     while (!static_cast<Info*>(info)->calculationEnded)
  28.     {
  29.         const int posStartOfBlock = static_cast<Info*>(info)->position;
  30.  
  31.         for (int i = posStartOfBlock; i < posStartOfBlock + sizeOfBlock && i < N; ++i)
  32.         {
  33.             const double x = (i + 0.5) / N;
  34.             tempSum += 4 / (1 + x * x);
  35.         }
  36.  
  37.         SetEvent(static_cast<Info*>(info)->event);
  38.         SuspendThread(GetCurrentThread());
  39.     }
  40.  
  41.     results[static_cast<Info*>(info)->numberOfThread] = tempSum;
  42.     return 0;
  43. }
  44.  
  45. void winApiThreading(int numberOfThreads)
  46. {
  47.     HANDLE* threads = new HANDLE[numberOfThreads];
  48.     HANDLE* events = new HANDLE[numberOfThreads];
  49.     results = new double[numberOfThreads];
  50.  
  51.     Info* infos = new Info[numberOfThreads];
  52.     for (int i = 0; i < numberOfThreads; ++i)
  53.     {
  54.         infos[i].calculationEnded = false;
  55.         infos[i].numberOfThread = i;
  56.         infos[i].event = events[i] = CreateEvent(
  57.             nullptr,
  58.             FALSE,
  59.             TRUE,
  60.             nullptr);
  61.         threads[i] = CreateThread(
  62.             nullptr,
  63.             0,
  64.             ThreadProc,
  65.             &infos[i],
  66.             CREATE_SUSPENDED,
  67.             nullptr);
  68.     }
  69.  
  70.     for (int i = 0; i < N; i += sizeOfBlock)
  71.     {
  72.         DWORD num = WaitForMultipleObjects(
  73.             numberOfThreads, events,
  74.             FALSE,
  75.             INFINITE);
  76.         infos[num].position = i;
  77.  
  78.         while (!ResumeThread(threads[num]));
  79.     }
  80.  
  81.     WaitForMultipleObjects(numberOfThreads, events, TRUE, INFINITE);
  82.  
  83.     for (int i = 0; i < numberOfThreads; ++i)
  84.     {
  85.         infos[i].calculationEnded = true;
  86.         ResumeThread(threads[i]);
  87.     }
  88.  
  89.     WaitForMultipleObjects(numberOfThreads, threads, TRUE, INFINITE);
  90.  
  91.     for (int i = 0; i < numberOfThreads; ++i)
  92.         result += results[i];
  93.  
  94.     result /= N;
  95. }
  96.  
  97. void openMpThreading(int numberOfThreads)
  98. {
  99. #pragma omp parallel for schedule(dynamic, sizeOfBlock) num_threads(numberOfThreads) reduction(+:result)
  100.     for (int i = 0; i < N; ++i)
  101.     {
  102.         const double x = (i + 0.5) / N;
  103.         result += 4 / (1 + x * x);
  104.     }
  105.     result /= N;
  106. }
  107.  
  108. int main()
  109. {
  110.     int numberOfThreads;
  111.     cout << "Number of threads\n> ";
  112.     cin >> numberOfThreads;
  113.  
  114.     int typeOfThreading;
  115.     cout << "\nSelect type of threading:\n1. WinAPI\n2. OpenMP\n> ";
  116.     cin >> typeOfThreading;
  117.  
  118.     const DWORD startTime = GetTickCount();
  119.  
  120.     if (typeOfThreading == 1)
  121.         winApiThreading(numberOfThreads);
  122.  
  123.     if (typeOfThreading == 2)
  124.         openMpThreading(numberOfThreads);
  125.  
  126.     const DWORD executionTime = GetTickCount() - startTime;
  127.  
  128.     cout << "Result: " << result << endl;
  129.     cout << "Time: " << executionTime << endl;
  130.     system("pause");
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment