forked from hudhfo/OpenMP-and-PThread_Matrix-Multiplication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenMP-matrix_multiplication.cpp
More file actions
36 lines (30 loc) · 922 Bytes
/
OpenMP-matrix_multiplication.cpp
File metadata and controls
36 lines (30 loc) · 922 Bytes
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
#include <iostream>
#include <pthread.h>
#include <omp.h>
#include <sys/time.h>
#include <ctime>
using namespace std;
const int maxn = 4;
int A[maxn][maxn], B[maxn][maxn], C[maxn][maxn];
int main() {
int i, j, k;
omp_set_num_threads(omp_get_num_procs());
srand(time(NULL));
for (i = 0; i < maxn; i++)
for (j = 0; j < maxn; j++) {
A[i][j] = rand() % 10;
B[i][j] = rand() % 10;
}
#pragma omp parallel for private(i,j,k) shared(A,B,C)
for (i = 0; i < maxn; ++i)
for (j = 0; j < maxn; ++j)
for (k = 0; k < maxn; ++k){
//printf("OpenMP Test, : %d\n", omp_get_thread_num());
C[i][j] += A[i][k] * B[k][j];
}
for (i = 0; i < maxn; i++) {
for (j = 0; j < maxn; j++)
cout << C[i][j] << "\t";
cout << endl;
}
}