Advertisement
KonaJjr

SP_LV3_ZAD1_OTHER

Jan 5th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include<math.h>
  2. #include<stdexcept>
  3. #include<time.h>
  4. #include<Windows.h>
  5. #include<process.h>
  6. #include<iostream>
  7.  
  8.  
  9. constexpr unsigned int MATRIX = 1032;
  10. constexpr unsigned int THREADS = 3;
  11.  
  12. float getValue(int i, int j) {
  13.     float value = 0;
  14.     for (int k = 0; k <= i; k++) {
  15.         value += k * sin(j) - j * cos(k);
  16.     }
  17.     return value;
  18. }
  19.  
  20. float getSequential() {
  21.  
  22.     int** matrix = new int* [MATRIX];
  23.     clock_t t1;
  24.     clock_t t2;
  25.     std::cout << "Sequential" << std::endl;
  26.     t1 = clock();
  27.     for (int i = 0; i < MATRIX; ++i) {
  28.         matrix[i] = new int[MATRIX];
  29.     }
  30.     for (int row = 0; row < MATRIX; row++) {
  31.         for (int col = 0; col < MATRIX; col++) {
  32.             matrix[row][col] = getValue(row, col);
  33.         }
  34.     }
  35.     t2 = clock();
  36.     std::cout << "Time: " << t2 - t1 << " ms" << std::endl;
  37.     float sum = 0;
  38.     for (int row = 0; row < MATRIX; row++) {
  39.         for (int col = 0; col < MATRIX; col++) {
  40.             sum += matrix[row][col];
  41.         }
  42.     }
  43.     std::cout << "Sum: " << sum << std::endl;
  44.     delete[] matrix;
  45.     return sum;
  46. }
  47.  
  48. class Matrix {
  49. private:
  50.     unsigned _rows;
  51.     unsigned _columns;
  52.     float* _data;
  53.  
  54. public:
  55.     Matrix(unsigned int rows, unsigned int columns) {
  56.         _rows = rows;
  57.         _columns = columns;
  58.         if (rows == 0 || columns == 0) {
  59.             throw std::invalid_argument("Rows and columns cannot be 0 or less.");
  60.         }
  61.         _data = new float[rows * columns];
  62.     }
  63.  
  64.     ~Matrix() {
  65.         delete[] _data;
  66.     }
  67.  
  68.     float& operator() (unsigned row, unsigned column) {
  69.         if (row >= _rows || column >= _columns) {
  70.             throw std::out_of_range("Index out of range.");
  71.         }
  72.         return _data[_columns * row + column];
  73.     }
  74.  
  75.     float operator() (unsigned row, unsigned col) const {
  76.         if (row >= _rows || col >= _columns) {
  77.             throw std::out_of_range("Index out of range.");
  78.         }
  79.         return _data[_columns * row + col];
  80.     }
  81. };
  82.  
  83.  
  84. struct THREAD_PARAMS {
  85.     Matrix* matrixPtr;
  86.     unsigned int startRow;
  87.     unsigned int rowCount;
  88.     unsigned int columnCount;
  89. };
  90.  
  91. unsigned __stdcall WorkerThread(PVOID param) {
  92.     THREAD_PARAMS* params = (THREAD_PARAMS*)param;
  93.     unsigned int row = params->startRow;
  94.     unsigned int column = 0;
  95.     for (row; row < params->startRow + params->rowCount; row++) {
  96.         for (column = 0; column < params->columnCount; column++) {
  97.             params->matrixPtr->operator()(row, column) = getValue(row, column);
  98.         }
  99.     }
  100.     _endthreadex(0);
  101.     return 0;
  102. }
  103.  
  104.  
  105.  
  106. int main(int argc, char* argv[]) {
  107.     //we can call getSequential() method if we want sequentially fill matrix
  108.  
  109.     clock_t t1;
  110.     clock_t t2;
  111.     t1 = clock();
  112.     float sum = getSequential();
  113.     t2 = clock();
  114.     Matrix mat = Matrix(MATRIX, MATRIX);
  115.     clock_t t3;
  116.     clock_t t4;
  117.     std::cout << "With threads" << std::endl;
  118.     t3 = clock();
  119.  
  120.     HANDLE* hWork = new HANDLE[THREADS];
  121.     THREAD_PARAMS* params = new THREAD_PARAMS[THREADS];
  122.     unsigned int threadCounter = 0;
  123.     unsigned int rowsPerThread = MATRIX / THREADS;
  124.     for (threadCounter; threadCounter < THREADS; threadCounter++) {
  125.         params[threadCounter] = THREAD_PARAMS{
  126.             &mat,
  127.             threadCounter * rowsPerThread,
  128.             rowsPerThread,
  129.             MATRIX
  130.         };
  131.         hWork[threadCounter] = (HANDLE)_beginthreadex(
  132.             NULL,
  133.             0,
  134.             WorkerThread,
  135.             &params[threadCounter],
  136.             0,
  137.             NULL
  138.         );
  139.     }
  140.  
  141.     WaitForMultipleObjects(
  142.         THREADS,
  143.         hWork,
  144.         true,
  145.         INFINITE
  146.     );
  147.  
  148.     t4 = clock();
  149.     std::cout << "Time: " << t4 - t3 << " ms" << std::endl;
  150.     std::cout << "Omjer: " << (double)(t4 - t3) / (t2 - t1) << std::endl;
  151.  
  152.  
  153.     float sum2 = 0;
  154.     int row, column = 0;
  155.     for (row = 0; row < MATRIX; row++) {
  156.         for (column = 0; column < MATRIX; column++) {
  157.             sum2 += mat(row, column);
  158.         }
  159.     }
  160.     std::cout << "Sum: " << sum2 << std::endl;
  161.     float tocnost = sum / sum2;
  162.     /*if (tocnost < 0)
  163.     {
  164.         tocnost = sum2 / sum;
  165.     }*/
  166.     std::cout << "Tocnost[%]: " << abs(tocnost * 100) << "%" << std::endl;
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement