Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma clang diagnostic push
- #pragma ide diagnostic ignored "openmp-use-default-none"
- #include <iostream>
- #include <omp.h>
- #include <iostream>
- #include <chrono>
- #include <vector>
- #include <random>
- #define N 100
- using namespace std;
- vector<int> randomVector(size_t size);
- void fillS(int(*a)[N]) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- a[i][j] = rand() % 10 + 1;
- }
- }
- }
- int MatrixTask() {
- vector<int> v1(N);
- vector<int> v2(N);
- v1 = randomVector(N);
- v2 = randomVector(N);
- double start = omp_get_wtime();
- std::chrono::time_point<std::chrono::high_resolution_clock> start7 = std::chrono::high_resolution_clock::now();
- #pragma omp parallel num_threads(4)
- transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), plus<int>());
- std::chrono::time_point<std::chrono::high_resolution_clock> end7 = std::chrono::high_resolution_clock::now();
- double end = omp_get_wtime();
- cout << "parallel vectors " << (end - start) << "time: "
- << chrono::duration_cast<chrono::microseconds>(end7 - start7).count() << " microsec\n";
- vector<int> v3(N);
- vector<int> v4(N);
- v3 = randomVector(N);
- v4 = randomVector(N);
- double start2 = omp_get_wtime();
- std::chrono::time_point<std::chrono::high_resolution_clock> start8 = std::chrono::high_resolution_clock::now();
- transform(v3.begin(), v3.end(), v4.begin(), v3.begin(), plus<int>());
- std::chrono::time_point<std::chrono::high_resolution_clock> end8 = std::chrono::high_resolution_clock::now();
- double end2 = omp_get_wtime();
- cout << "not parallel vectors " << (end2 - start2) << "time: "
- << chrono::duration_cast<chrono::microseconds>(end8 - start8).count() << " microsec\n";
- int matrix1[N][N];
- int matrix2[N][N];
- int matrix3[N][N];
- fillS(matrix1);
- fillS(matrix2);
- std::chrono::time_point<std::chrono::high_resolution_clock> start9 = std::chrono::high_resolution_clock::now();
- double start3 = omp_get_wtime();
- #pragma omp parallel for num_threads(4)
- for (int i = 0; i < N; i++) {
- for (int k = 0; k < N; k++) {
- for (int j = 0; j < N; j++) {
- matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- double end3 = omp_get_wtime();
- std::chrono::time_point<std::chrono::high_resolution_clock> end9 = std::chrono::high_resolution_clock::now();
- cout << "parallel matrix " << (end3 - start3) << "time: "
- << chrono::duration_cast<chrono::microseconds>(end9 - start9).count() << " microsec\n";
- //Умножение матриц
- int matrix4[N][N];
- int matrix5[N][N];
- int matrix6[N][N];
- fillS(matrix4);
- fillS(matrix5);
- double start4 = omp_get_wtime();
- std::chrono::time_point<std::chrono::high_resolution_clock> start10 = std::chrono::high_resolution_clock::now();
- for (int i = 0; i < N; i++) {
- for (int k = 0; k < N; k++) {
- for (int j = 0; j < N; j++) {
- matrix6[i][j] += matrix4[i][k] * matrix5[k][j];
- }
- }
- }
- std::chrono::time_point<std::chrono::high_resolution_clock> end10 = std::chrono::high_resolution_clock::now();
- double end4 = omp_get_wtime();
- cout << "not parallel matrix " << (end4 - start4) << "time: "
- << chrono::duration_cast<chrono::microseconds>(end10 - start10).count() << " microsec\n";
- return 0;
- }
- vector<int> randomVector(size_t size) {
- vector<int> v(size);
- generate(v.begin(), v.end(), std::rand);
- return v;
- }
- #pragma clang diagnostic pop
Advertisement
Add Comment
Please, Sign In to add comment