Advertisement
KonaJjr

SP_LV3_ZAD1_GPT

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