Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab03.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #include "omp.h"
- void calcul_paralel(int **a , int **b ,int **c, int n) {
- #pragma omp parallel num_threads(16)
- {
- #pragma omp for
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- c[i][j] = 0;
- for (int k = 0; k < n; k++)
- c[i][j] = c[i][j] + a[i][k] * b[k][j];
- }
- }
- }
- }
- void calcul_secvential(int **a, int **b, int **c, int n) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- c[i][j] = 0;
- for (int k = 0; k < n; k++)
- c[i][j] = c[i][j] + a[i][k] * b[k][j];
- }
- }
- }
- int main()
- {
- int n;
- std::cout << "Intoduceti dimesiunea matricilor:";
- std::cout<<std::endl;;
- std::cin >> n;
- int **a = (int **)malloc(n * sizeof(int*));
- int **b = (int **)malloc(n * sizeof(int*));
- int **c = (int **)malloc(n * sizeof(int*));
- int **d = (int **)malloc(n * sizeof(int*));
- for (int i = 0; i < n; i++) {
- a[i] = (int *)malloc(n * sizeof(int));
- b[i] = (int *)malloc(n * sizeof(int));
- c[i] = (int *)malloc(n * sizeof(int));
- d[i] = (int *)malloc(n * sizeof(int));
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++)
- {
- a[i][j] = 1;
- b[i][j] = 1;
- }
- }
- double t1, t2, elapsed_seconds, t3, t4, elapsed_seconds2;
- t1 = omp_get_wtime();
- calcul_paralel(a, b, c, n);
- t2= omp_get_wtime();
- elapsed_seconds = t2 - t1;
- t3 = omp_get_wtime();
- calcul_secvential(a, b, d, n);
- t4 = omp_get_wtime();
- elapsed_seconds2 = t4 - t3;
- //for (int i = 0; i < n; i++) {
- // for (int j = 0; j < n; j++) {
- // std::cout << d[i][j] << " ";
- // }
- // std::cout << std::endl;
- //}
- std::cout << std::endl;
- std::cout << " Timp paralel :" << elapsed_seconds << std::endl;
- std::cout << " Timp secvential :" << elapsed_seconds2 << std::endl;
- std::cout << " Raport timp :" << elapsed_seconds2 / elapsed_seconds << std::endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement