Created
November 16, 2014 13:27
-
-
Save soltys/43d423ca59177f78f1a2 to your computer and use it in GitHub Desktop.
matrix_mul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* matrix-omp.cpp */ | |
const int size = 1000; | |
float a[size][size]; | |
float b[size][size]; | |
float c[size][size]; | |
int main() | |
{ | |
// Initialize buffers. | |
for (int i = 0; i < size; ++i) { | |
for (int j = 0; j < size; ++j) { | |
a[i][j] = (float)i + j; | |
b[i][j] = (float)i - j; | |
c[i][j] = 0.0f; | |
} | |
} | |
// Compute matrix multiplication. | |
// C <- C + A x B | |
#pragma omp parallel for default(none) shared(a,b,c) | |
for (int i = 0; i < size; ++i) { | |
for (int j = 0; j < size; ++j) { | |
for (int k = 0; k < size; ++k) { | |
c[i][j] += a[i][k] * b[k][j]; | |
} | |
} | |
} | |
return 0; | |
} | |
/* | |
$ g++ -O2 matrix.cpp -o matrix | |
$ g++ -O2 -fopenmp matrix-omp.cpp -o matrix-omp | |
$ time ./matrix | |
real 0m12.976s | |
user 0m12.460s | |
sys 0m0.059s | |
$ time ./matrix-omp | |
real 0m6.952s | |
user 0m12.556s | |
sys 0m0.050s | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment