Hyluss

[OpenMP] pragma omp parallel

Nov 29th, 2017
532
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. // prir lab2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. #define LOOPS 50
  10.  
  11. double** StworzMacierz(int rows, int columns)
  12. {
  13.      double **macierz;
  14.      macierz = (double**)malloc(rows * sizeof(double*));
  15.      for (int i = 0; i < rows; i++)
  16.          macierz[i] = (double*)malloc(columns * sizeof(double));
  17.      return macierz;
  18. }
  19.  
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.      double **macierz;
  23.      double *v1, *v2;
  24.      long rows = 5000, columns = 5000;
  25.      time_t begin_t, end_t;
  26.      long i, j, k;
  27.  
  28.      v1 = (double*)malloc(columns * sizeof(double));
  29.      v2 = (double*)malloc(rows * sizeof(double));
  30.  
  31.      macierz = StworzMacierz(rows,columns);
  32.  
  33.      for (i = 0; i < columns; i++)
  34.      {
  35.          v1[i] = (double)(i / 1000.0f);
  36.          v2[i] = (double)(i / 1000.0f);
  37.          for ( j = 0; j < rows; j++ )
  38.              macierz[j][i] = (double)(i * j / 1000.0f);
  39.      }
  40.  
  41.      begin_t = time(NULL);
  42.  
  43.  
  44.      for (k = 0; k < LOOPS; k++)
  45.      {
  46.         #pragma omp parallel for shared(macierz, v1, v2, rows, columns) private(i, j)
  47.          for (i = 0; i < rows; i++)
  48.          {
  49.             // v2[i] = 0.0f;
  50.              for (j = 0; j < columns; j++)
  51.                  v2[i] = macierz[i][j] * v1[j];
  52.          }
  53.      }
  54.  
  55.       end_t = time(NULL);
  56.       printf("Czas obliczen: %f.\n", difftime(end_t, begin_t));
  57.  
  58.       free(v1);
  59.       free(v2);
  60.       for (i = 0; i < rows; i++)
  61.           free(macierz[i]);
  62.       free(macierz);
  63.  
  64.  
  65.     return 0;
  66. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment