-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.cpp
More file actions
91 lines (67 loc) · 1.94 KB
/
Lab2.cpp
File metadata and controls
91 lines (67 loc) · 1.94 KB
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
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <omp.h>
void multiply(int n, int A[2000][2000], int B[2000][2000], int C[2000][2000])
{
int i, j, k;
#pragma omp parallel for
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
C[i][j] = 0;
for (k = 0; k < n; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
int main()
{
int n;
int threads;
std::cout << "Enter matrix size: ";
std::cin >> n;
std::cout << "Enter number of threads: ";
std::cin >> threads;
omp_set_num_threads(threads);
std::cout << "Max available threads: " << omp_get_max_threads() << "\n";
static int A[2000][2000];
static int B[2000][2000];
static int C[2000][2000];
std::ofstream Afile("A_matrix.txt");
std::ofstream Bfile("B_matrix.txt");
srand(time(0));
Afile << n << "\n";
Bfile << n << "\n";
int i, j;
// ãåíåðàöèÿ è çàïèñü
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
A[i][j] = rand() % 10;
B[i][j] = rand() % 10;
Afile << A[i][j] << " ";
Bfile << B[i][j] << " ";
}
Afile << "\n";
Bfile << "\n";
}
Afile.close();
Bfile.close();
double start = omp_get_wtime();
multiply(n, A, B, C);
double end = omp_get_wtime();
std::ofstream Cfile("Result.txt");
Cfile << n << "\n";
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
Cfile << C[i][j] << " ";
Cfile << "\n";
}
Cfile.close();
double t = end - start;
std::cout << "\n RESULT \n";
std::cout << "Matrix size: " << n << " x " << n << "\n";
std::cout << "Threads: " << threads << "\n";
std::cout << "Time: " << t << " sec\n";
std::cout << "Operations: " << (long long)n * n * n << "\n";
return 0;
}