Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <ppl.h>
- #include <chrono>
- #include <vector>
- class Matrix
- {
- double drand(double fMin, double fMax)
- {
- double f = (double)rand() / RAND_MAX;
- return fMin + f * (fMax - fMin);
- }
- public:
- int rows = 0;
- int cols = 0;
- std::vector<std::vector<double>> mat;
- Matrix(int iRows, int iCols) : rows(iRows), cols(iCols)
- {
- mat.resize(rows);
- for (int i = 0; i < rows; i++)
- mat[i].resize(cols);
- }
- void RandomMatrix(double dispersion)
- {
- for (int i = 0; i < rows; i++)
- for (int j = 0; j < cols; j++)
- mat[i][j] = drand(-dispersion, dispersion);
- }
- ~Matrix()
- {
- }
- Matrix * operator *(const Matrix *m) const
- {
- Matrix *rs = new Matrix(cols, m->rows);
- concurrency::parallel_for(size_t(0), size_t(rs->rows), [&](size_t i)
- {
- for (int k = 0; k < this->cols; k++)
- {
- for (int j = 0; j < rs->cols; j++)
- rs->mat[i][j] += mat[i][k] * m->mat[k][j];
- }
- });
- return rs;
- }
- };
- typedef std::chrono::high_resolution_clock Clock;
- int main()
- {
- const int TRIALS = 10;
- Matrix A(2000, 2000);
- Matrix B(2000, 2000);
- A.RandomMatrix(1.0);
- B.RandomMatrix(1.0);
- std::vector<Matrix *> mList;
- printf("Starting trials!\n");
- //actual trials
- std::chrono::time_point<std::chrono::steady_clock> before, after;
- before = Clock::now();
- for (int i = 0; i < TRIALS; i++)
- {
- mList.push_back(A * &B);
- }
- after = Clock::now();
- std::chrono::duration<double, std::milli> fp_ms = after - before;
- printf("Milliseconds: %f\n", fp_ms);
- getchar();
- return 0;
- }
Add Comment
Please, Sign In to add comment