Advertisement
HeroBaga

Untitled

Oct 5th, 2021
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include<iostream>
  2. #include <omp.h>
  3.  
  4. using namespace std;
  5.  
  6. float **transpose_serial(int row, int col, float **core, float **result) {
  7. int i, j;
  8. for (i = 0; i < row; i++)
  9. for (j = 0; j < col; j++) {
  10. result[j][i] = core[i][j];
  11. }
  12. return result;
  13. }
  14.  
  15. float **transpose_parall(int row, int col, float **core, float **result) {
  16. int i, j;
  17. #pragma omp parallel for shared(core, b, row, col) private(i, j) default(none)
  18. for (i = 0; i < row; i++)
  19. for (j = 0; j < col; j++) {
  20. result[j][i] = core[i][j];
  21. }
  22. return result;
  23. }
  24.  
  25. int main(int argc, char *argv[]) {
  26. int row = 100;
  27. int col = 100;
  28. int is_display = 0 ;
  29. // int row = atoi(argv[1]); // количество строк матрицы
  30. // int col = atoi(argv[2]); // количество столбцов матрицы
  31. // int is_display = atoi(argv[3]);
  32. int i, j;
  33. float **core; // матрица
  34. float **serial_res; // результат
  35. float **parall_res; // результат параллельного
  36. double t1parl, t2parl, t1serial, t2serial;
  37.  
  38. core = new float *[row];
  39. serial_res = new float *[col];
  40. parall_res = new float *[col];
  41. for (i = 0; i < row; i = i + 1) {
  42. core[i] = new float[col];
  43. }
  44. for (i = 0; i < col; i = i + 1) {
  45. serial_res[i] = new float[row];
  46. parall_res[i] = new float[row];
  47. }
  48. for (i = 0; i < row; i = i + 1) {
  49. for (j = 0; j < col; j = j + 1) {
  50. core[i][j] = i * 1.0;
  51. }
  52. }
  53. if (!is_display) {
  54. // вывод матрицы
  55. for (i = 0; i < row; i = i + 1) {
  56. for (j = 0; j < col; j = j + 1) {
  57. cout << core[i][j] << ' ';
  58. }
  59. cout << endl;
  60. }
  61. }
  62. t1serial = omp_get_wtime();
  63. serial_res = transpose_serial(row, col, core, serial_res);
  64. t2serial = omp_get_wtime();
  65.  
  66. t1parl = omp_get_wtime();
  67. parall_res = transpose_parall(row, col, core, parall_res);
  68. t2parl = omp_get_wtime();
  69. if (!is_display) {
  70. cout << endl << "Serial calculation: " << endl;
  71. for (i = 0; i < col; i++) {
  72. for (j = 0; j < row; j++) {
  73. cout << serial_res[i][j] << ' ';
  74. }
  75. cout << endl;
  76.  
  77. }
  78. cout << endl <<"Parallel calculation: " << endl;
  79. for (i = 0; i < col; i++) {
  80. for (j = 0; j < row; j++) {
  81. cout << parall_res[i][j] << ' ';
  82. }
  83. cout << endl;
  84.  
  85. }
  86. }
  87. bool flag = true;
  88. for (i = 0; i < col; i = i + 1) {
  89. for (j = 0; j < row; j = j + 1) {
  90. if (serial_res[i][j] != parall_res[i][j]) {
  91. flag = false;
  92. break;
  93. }
  94. }
  95. if (!flag)
  96. break;
  97. }
  98. if (flag)
  99. cout << "The result is the same" << endl;
  100. else
  101. cout << "The result is not the same" << endl;
  102.  
  103. cout << endl << "Parallel calculation time = " << (t2serial - t1serial) << endl;
  104. cout << "Serial calculation time = " << (t2parl - t1parl) << endl;
  105. for (i = 0; i < row; i = i + 1) {
  106. delete core[i];
  107. }
  108. delete core;
  109. for (i = 0; i < col; i = i + 1) {
  110. delete serial_res[i];
  111. delete parall_res[i];
  112. }
  113. delete serial_res;
  114. delete parall_res;
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement