Facebook
From Bulky Teal, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 202
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <omp.h>
  4.  
  5. #define N 50000
  6.  
  7. int main(int argc, char *argv[]) {
  8.     float a[N];
  9.     float b[N];
  10.  
  11.     int ind[N];
  12.     int i, j;
  13.  
  14.     double begin_t, end_t;
  15.  
  16. //    srand(time(NULL));
  17.  
  18.     for (i=0; i<N; i++)
  19.     a[i]= (float) rand() / RAND_MAX;
  20.  
  21.     begin_t=omp_get_wtime();
  22.  
  23.     #pragma omp parallel for shared(a, b, ind) private(i,j)
  24.     for (i=0; i<N; i++) {
  25.         ind[i]=0;
  26. //    #pragma omp parallel for shared(a, b, ind, i) private(j)
  27.         for (j=0; j<N; j++)
  28.             if ((a[i]>a[j]) || ((a[i]==a[j]) && (i>j)))
  29.                 ind[i]++;
  30.         b[ind[i]]=a[i];
  31.     }
  32.  
  33.     end_t=omp_get_wtime();
  34.  
  35.     printf("a:\n");
  36.     for (i=0; i<10; i++)
  37.     printf("%10.6f", a[i]);
  38.     printf("\nb:\n");
  39.     for (i=0; i<10; i++)
  40.     printf("%10.6f", b[i]);
  41.     printf("\n");
  42.  
  43.     printf("Czas obliczen: %lf s\n",end_t-begin_t);
  44.     return 0;
  45. }