Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. // myslau.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include <vector>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <omp.h>
  8. #include <time.h>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. class Matrix
  14. {
  15. public:
  16. int n;
  17. vector<vector<int> > m;
  18. Matrix(int n)
  19. {
  20. this->n = n;
  21. m.resize(n, vector<int>(n));
  22. }
  23. void rand_mtrx(int mx)
  24. {
  25. ofstream f;
  26. f.open("C:\\myfile.txt");
  27. if (f.is_open())
  28. {
  29. for (int i = 0; i < n; i++)
  30. {
  31. for (int j = 0; j < n; j++)
  32. {
  33. f << m[i][j] << ' ';
  34. }
  35. f << endl;
  36. }
  37. }
  38. for (int i = 0; i < n; i++)
  39. {
  40. for (int j = 0; j < n; j++)
  41. {
  42. m[i][j] = rand() % mx;
  43. }
  44. }
  45. }
  46. int getSize()
  47. {
  48. return n;
  49. }
  50. };
  51.  
  52. Matrix operator*(Matrix& m1, Matrix& m2)
  53. {
  54. int n = m1.m.size();
  55. Matrix m(n);
  56. time_t g_timeElapsed;
  57. omp_set_num_threads(4);
  58. #pragma omp parallel for
  59. for (int i = 0; i < n; i++)
  60. {
  61. for (int j = 0; j < n; j++)
  62. {
  63. for (int k = 0; k < n; k++)
  64. {
  65. m.m[i][j] += m1.m[i][k] * m2.m[k][j];
  66. }
  67. }
  68. }
  69. return m;
  70. }
  71.  
  72. int main()
  73. {
  74. int n;
  75. cin >> n;
  76. Matrix a(n), b(n), c(n);
  77. a.rand_mtrx(10);
  78. b.rand_mtrx(10);
  79. clock_t start, finish;
  80. start = clock();
  81. c = a * b;
  82. finish = clock();
  83. cout << "random_mtrx" << endl;
  84. for (int i = 0; i < n; i++)
  85. {
  86. for (int j = 0; j < n; j++)
  87. {
  88. cout << a.m[i][j] << ' ';
  89. }
  90. cout << endl;
  91. }
  92. cout << "random_mtrx" << endl;
  93. for (int i = 0; i < n; i++)
  94. {
  95. for (int j = 0; j < n; j++)
  96. {
  97. cout << b.m[i][j] << ' ';
  98. }
  99. cout << endl;
  100. }
  101. cout << "result" << endl;
  102. for (int i = 0; i < n; i++)
  103. {
  104. for (int j = 0; j < n; j++)
  105. {
  106. cout << c.m[i][j] << ' ';
  107. }
  108. cout << endl;
  109. }
  110. cout << endl << ((double(finish) - double(start)) / CLOCKS_PER_SEC) << " sec" << endl;
  111. return 0;
  112. }
  113.  
  114. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  115. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  116.  
  117. // Советы по началу работы
  118. // 1. В окне обозревателя решений можно добавлять файлы и управлять ими.
  119. // 2. В окне Team Explorer можно подключиться к системе управления версиями.
  120. // 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
  121. // 4. В окне "Список ошибок" можно просматривать ошибки.
  122. // 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
  123. // 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement