-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomp_multi_matrice.cpp
More file actions
51 lines (38 loc) · 1.12 KB
/
Copy pathomp_multi_matrice.cpp
File metadata and controls
51 lines (38 loc) · 1.12 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
#include <cstdlib>
#include <ctime>
#include <random>
#include <omp.h>
const int limit = 10;
int main(int argc, char* argv[]) {
int n = strtol(argv[1], NULL, 10);
int num_thrs = strtol(argv[2], NULL, 10);
int i, j, k;
int* A = new int[n * n];
int* B = new int[n * n];
int* C = new int[n * n]();
srand(static_cast<unsigned>(time(0)));
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
A[i * n + j] = rand() % limit;
B[i * n + j] = rand() % limit;
}
}
double start, end;
start = omp_get_wtime();
# pragma omp parallel for collapse(2) default(none) shared(A, B, C, n) private(k) num_threads(num_thrs)
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
C[i * n + j] += A[i * n + k] * B[k * n + j];
}
}
}
end = omp_get_wtime();
double duration = end - start;
printf("cost time: \t%lfs\n", duration);
delete[] A;
delete[] B;
delete[] C;
return 0;
}
// ./omp_multi_matrice <the amount of data> <the number of the threads>