Created
May 7, 2020 05:49
-
-
Save rchoudhary/3c8c05b58b7d902940abbd05a26ed11c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <cstdio> | |
| #include <ctime> | |
| #include <omp.h> | |
| #include <cstdlib> | |
| #include <chrono> | |
| #include <random> | |
| using namespace std; | |
| int main() { | |
| const int n = 10000; | |
| const float m = 1000; | |
| float** a = new float*[n]; | |
| for (int i = 0; i < n; i++) a[i] = new float[n]; | |
| float* b = new float[n]; | |
| float* c = new float[n]; | |
| default_random_engine eng(2319); | |
| uniform_real_distribution<float> dis(-m, m); | |
| auto c_start = clock(); | |
| auto t_start = chrono::high_resolution_clock::now(); | |
| #pragma omp parallel | |
| { | |
| for (int i = 0; i < n; ++i) { | |
| #pragma omp for | |
| for (int j = 0; j < n; ++j) { | |
| a[i][j] = dis(eng); | |
| } | |
| } | |
| } | |
| #pragma omp parallel | |
| { | |
| #pragma omp for | |
| for (int i = 0; i < n; ++i) { | |
| b[i] = dis(eng); | |
| } | |
| } | |
| #pragma omp parallel | |
| { | |
| #pragma omp for | |
| for (int i = 0; i < n; ++i) { | |
| c[i] = 0; | |
| } | |
| } | |
| #pragma omp parallel | |
| { | |
| int c_private[n]; | |
| #pragma omp for | |
| for (int i = 0; i < n; i++) c_private[i] = 0; | |
| for (int i = 0; i < n; i++) { | |
| #pragma omp for | |
| for (int j = 0; j < n; j++) { | |
| c_private[i] += a[i][j] * b[j]; | |
| } | |
| } | |
| #pragma omp critical | |
| { | |
| for(int i = 0; i < n; i++) c[i] += c_private[i]; | |
| } | |
| } | |
| auto c_stop = clock(); | |
| auto t_stop = chrono::high_resolution_clock::now(); | |
| auto cpu_elapsed = (double)(c_stop - c_start) / CLOCKS_PER_SEC; | |
| auto wall_elapsed = chrono::duration<double>(t_stop - t_start).count(); | |
| printf("CPU time used: %.2fs\n", cpu_elapsed); | |
| printf("Wall clock time passed: %.2fs\n", wall_elapsed); | |
| for (int i = 0; i < n; i++) delete[] a[i]; | |
| delete[] a; | |
| delete[] b; | |
| delete[] c; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment