-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond.cpp
90 lines (79 loc) · 2.24 KB
/
second.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <omp.h>
#include <iostream>
using namespace std;
int main(){
int row1, row2, col1, col2;
double** a, ** b, ** c;
cout << "Введите количество строк первой матрицы: ";
cin >> row1;
cout << "Введите количество столбцов первой матрицы: ";
cin >> col1;
cout << "Введите количество строк второй матрицы: ";
cin >> row2;
cout << "Введите количество столбцов второй матрицы: ";
cin >> col2;
if (col1 != row2)
{
cout << "Умножение невозможно!";
cin.get(); cin.get();
return 0;
}
// Ввод элементов первой матрицы
a = new double* [row1];
cout << "Введите элементы первой матрицы" << endl;
for (int i = 0; i < row1; i++)
{
a[i] = new double[col1];
for (int j = 0; j < col1; j++)
{
cout << "a[" << i << "][" << j << "]= ";
cin >> a[i][j];
}
}
// Ввод элементов второй матрицы
b = new double* [row2];
cout << "Введите элементы второй матрицы" << endl;
for (int i = 0; i < row2; i++)
{
b[i] = new double[col2];
for (int j = 0; j < col2; j++)
{
cout << "b[" << i << "][" << j << "]= ";
cin >> b[i][j];
}
}
//-------------------------------------------------------
//parallel starting here
// Умножение матриц
c = new double* [row1];
for (int i = 0; i < row1; i++){
c[i] = new double[col2];
}
for (int i = 0; i < row1; i++){
for (int i = 0; i < row1; i++){
c[i] = new double[col2];
}
}
#pragma omp parallel for collapse(2) schedule(static) shared (a,b)
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
for (int k = 0; k < col1; k++)
#pragma omp critical
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
// Вывод матрицы произведения
cout << "Матрица произведения" << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
cout << c[i][j] << " ";
cout << endl;
}
cin.get(); cin.get();
return 0;
}