Advertisement
AI_UBI

Untitled

Dec 28th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ppl.h>
  4. #include <vector>
  5. #include <chrono>
  6.  
  7. class Matrix
  8. {
  9. public:
  10.  
  11.     int rows = 32;
  12.     int cols = 32;
  13.  
  14.     std::vector<std::vector<double>> mat;
  15.  
  16.  
  17.     double drand(double fMin, double fMax)
  18.     {
  19.         double f = (double)rand() / RAND_MAX;
  20.         return fMin + f * (fMax - fMin);
  21.     }
  22.  
  23.     Matrix(int iRows, int iCols) : rows(iRows), cols(iCols)
  24.     {
  25.         mat.resize(rows);
  26.         for (int i = 0; i < rows; i++)
  27.             mat[i].resize(cols);
  28.     }
  29.  
  30.  
  31.     Matrix RandomMatrix(double dispersion)
  32.     {
  33.         for (int i = 0; i < rows; i++)
  34.             for (int j = 0; j < cols; j++)
  35.                 mat[i][j] = drand(-dispersion, dispersion);
  36.  
  37.         return *this;
  38.     }
  39.  
  40.     ~Matrix()
  41.     {
  42.     }
  43.  
  44.     void CalcMulRow(Matrix result, Matrix m1, Matrix m2, int index)
  45.     {
  46.         auto iRowA = m1.mat[index];
  47.         auto iRowC = result.mat[index];
  48.         for (int k = 0; k < m1.cols; k++)
  49.         {
  50.             auto kRowB = m2.mat[k];
  51.             auto ikA = iRowA[k];
  52.             for (int j = 0; j < result.cols; j++)
  53.                 iRowC[j] += ikA * kRowB[j];
  54.         }
  55.     }
  56.  
  57.     Matrix operator *(const Matrix &m)
  58.     {
  59.         if (this->cols != m.rows)
  60.             throw ("Wrong dimensions of matrix, m1.cols (" + std::to_string(this->cols) + ") != m2.rows (" + std::to_string(m.rows) + ")!");
  61.  
  62.         Matrix rs(this->cols, m.rows);
  63.  
  64.         Concurrency::parallel_for(size_t(0), size_t(rows), [&](size_t i)
  65.         {
  66.             for (size_t j = 0; j < cols; j++)
  67.             {
  68.                 double temp = 0;
  69.                 for (int k = 0; k < cols; k++)
  70.                 {
  71.                     temp += this->mat[i][k] * m.mat[k][j];
  72.                 }
  73.  
  74.                 rs.mat[i][j] = temp;
  75.             }
  76.         });
  77.  
  78.         return rs;
  79.     }
  80.  
  81. };
  82. typedef std::chrono::high_resolution_clock Clock;
  83.  
  84. int main()
  85. {
  86.     const int TRIALS = 10;
  87.  
  88.     Matrix A(1000, 1000);
  89.     Matrix B(1000, 1000);
  90.  
  91.     A.RandomMatrix(1.0);
  92.     B.RandomMatrix(1.0);
  93.  
  94.     std::vector<Matrix> mList;
  95.  
  96.     printf("Warming up, wait...\n");
  97.     printf("Starting trials!\n");
  98.  
  99.     //actual trials
  100.     std::chrono::time_point<std::chrono::steady_clock> before, after;
  101.  
  102.     before = Clock::now();
  103.    
  104.     for (int i = 0; i < TRIALS; i++)
  105.     {
  106.         mList.push_back(A * B);
  107.     }
  108.  
  109.     after = Clock::now();
  110.  
  111.     std::chrono::duration<double, std::milli> fp_ms = after - before;
  112.  
  113.     printf("Milliseconds: %f\n", fp_ms);
  114.     getchar();
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement