-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththird.cpp
95 lines (88 loc) · 2.92 KB
/
third.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
91
92
93
94
95
#include <omp.h>
#include <iostream>
using namespace std;
int main(){
int row1, row2, col1;
double** a, * b, * cs, *cc, *cb;
cout << "Введите количество строк первой матрицы: ";
cin >> row1;
cout << "Введите количество столбцов первой матрицы: ";
cin >> col1;
cout << "Введите количество элементов вектора: ";
cin >> row2;
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++)
{
cout << "b[" << i << "]";
cin >> b[i];
}
cs = new double[row2];
cc=new double [row2];
cb=new double[row2];
double runtime;
#pragma omp parallel for
for(int i=0;i<row2;++i){
cc[i]=0;
cb[i]=0;
cs[i]=0;
}
int count=1;
do{
runtime = omp_get_wtime()*1000;
#pragma omp parallel for shared (a,b,cs) num_threads(count)
for(int i=0;i<row1;++i){
for(int j=0;j<col1;++j){
#pragma omp atomic
cs[i]+= a[i][j]*b[j];
}
}
runtime = omp_get_wtime()*1000-runtime;
cout<<count<<" threads, lines time is "<<runtime<<endl;
runtime = omp_get_wtime()*1000;
#pragma omp parallel for shared (a,b,cc) num_threads(count)
for(int j=0;j<col1;++j){
for(int i=0;i<row2;++i){
#pragma omp atomic
cc[i]+= a[i][j]*b[j];
}
}
runtime = omp_get_wtime()*1000-runtime;
cout<<count<<" threads, columns time is "<<runtime<<endl;
runtime = omp_get_wtime()*1000;
int bh = row2 / count;
int bw = col1 / count;
#pragma omp parallel shared(a, b, cb) num_threads(count)
for(int blocks = 0; blocks < count*count; ++blocks)
{
int i = blocks / count;
int j = blocks % count;
for(int ii = i*bh; ii < (i+1) * bh; ++ii)
for(int jj = j*bw; jj < (j+1) * bw; ++jj)
#pragma omp atomic
cb[ii] += a[ii][jj] * b[jj];
}
runtime = omp_get_wtime()*1000-runtime;
cout<<count<<" threads, blocks time is "<<runtime*1000<<endl;
}while(count<omp_get_thread_num());
return 0;
}