Advertisement
KonaJjr

SP_LV3_ZAD2_GPT

Jan 5th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <cmath>
  4. #include <vector>
  5. #include <chrono>
  6. #include <numeric> // For sum calculations
  7. #include <mutex>
  8.  
  9. // Matrix dimensions and number of threads
  10. const int N = 1000; // Matrix size (NxN)
  11. const int NTHRD = 4; // Number of threads
  12. float matrix[N][N]; // Global matrix
  13. float sequentialSum = 0; // Sum from sequential calculation
  14. float multithreadedSum = 0; // Sum from multithreaded calculation
  15. std::mutex columnMutex; // Mutex for column synchronization
  16.  
  17. // Thread data structure
  18. struct ThreadData {
  19.     int startRow;
  20.     int endRow;
  21.     int column;
  22.     float partialSum; // To store the partial sum calculated by each thread
  23. };
  24.  
  25. // Function to calculate arithmetic mean of the previous column
  26. float calculateASPS(int column) {
  27.     if (column == 0) return 0.0f;
  28.  
  29.     float sum = 0.0f;
  30.     for (int i = 0; i < N; ++i) {
  31.         sum += matrix[i][column - 1];
  32.     }
  33.     return sum / N;
  34. }
  35.  
  36. // Function to fill part of the matrix and calculate partial sum
  37. DWORD WINAPI fillMatrixPart(LPVOID param) {
  38.     ThreadData* data = (ThreadData*)param;
  39.     data->partialSum = 0;
  40.     for (int i = data->startRow; i < data->endRow; ++i) {
  41.         float asps = calculateASPS(data->column); // Calculate arithmetic mean of the previous column
  42.         float sum = 0;
  43.         for (int k = 0; k <= i; ++k) {
  44.             sum += k * sin(data->column) - data->column * cos(k);
  45.         }
  46.         matrix[i][data->column] = asps + sum;
  47.         data->partialSum += matrix[i][data->column];
  48.     }
  49.     return 0;
  50. }
  51.  
  52. // Sequential implementation
  53. void fillMatrixSequential() {
  54.     sequentialSum = 0;
  55.     for (int j = 0; j < N; ++j) {
  56.         float asps = calculateASPS(j);
  57.         for (int i = 0; i < N; ++i) {
  58.             float sum = 0;
  59.             for (int k = 0; k <= i; ++k) {
  60.                 sum += k * sin(j) - j * cos(k);
  61.             }
  62.             matrix[i][j] = asps + sum;
  63.             sequentialSum += matrix[i][j];
  64.         }
  65.     }
  66. }
  67.  
  68. int main() {
  69.     // Measure time for sequential execution
  70.     auto start = std::chrono::high_resolution_clock::now();
  71.     fillMatrixSequential();
  72.     auto end = std::chrono::high_resolution_clock::now();
  73.     auto durationSequential = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  74.     std::cout << "Sequential execution time: " << durationSequential << " ms" << std::endl;
  75.     std::cout << "Sequential sum: " << sequentialSum << std::endl;
  76.  
  77.     // Measure time for multithreaded execution
  78.     HANDLE threads[NTHRD];
  79.     ThreadData threadData[NTHRD];
  80.  
  81.     multithreadedSum = 0;
  82.     start = std::chrono::high_resolution_clock::now();
  83.  
  84.     for (int j = 0; j < N; ++j) {
  85.         float asps = calculateASPS(j); // Calculate arithmetic mean for the column
  86.  
  87.         int rowsPerThread = N / NTHRD;
  88.         for (int i = 0; i < NTHRD; ++i) {
  89.             threadData[i].startRow = i * rowsPerThread;
  90.             threadData[i].endRow = (i == NTHRD - 1) ? N : (i + 1) * rowsPerThread;
  91.             threadData[i].column = j;
  92.             threadData[i].partialSum = 0;
  93.             threads[i] = CreateThread(NULL, 0, fillMatrixPart, &threadData[i], 0, NULL);
  94.         }
  95.  
  96.         // Wait for all threads to finish
  97.         WaitForMultipleObjects(NTHRD, threads, TRUE, INFINITE);
  98.  
  99.         // Aggregate partial sums
  100.         for (int i = 0; i < NTHRD; ++i) {
  101.             multithreadedSum += threadData[i].partialSum;
  102.             CloseHandle(threads[i]);
  103.         }
  104.     }
  105.  
  106.     end = std::chrono::high_resolution_clock::now();
  107.     auto durationMultithreaded = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  108.     std::cout << "Multithreaded execution time: " << durationMultithreaded << " ms" << std::endl;
  109.     std::cout << "Multithreaded sum: " << multithreadedSum << std::endl;
  110.  
  111.     // Calculate speedup
  112.     std::cout << "Speedup: " << (float)durationSequential / durationMultithreaded << "x" << std::endl;
  113.  
  114.     // Calculate sum accuracy and percentage accuracy
  115.     float sumDifference = fabs(sequentialSum - multithreadedSum);
  116.     float accuracyPercentage = ((1.0f - (sumDifference / fabs(sequentialSum))) * 100.0f);
  117.  
  118.     std::cout << "Sum Difference: " << sumDifference << std::endl;
  119.     std::cout << "Accuracy [%]: " << accuracyPercentage << "%" << std::endl;
  120.  
  121.     return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement