pastebin

Paste Search Dynamic
Recent pastes
struct Element
  1. #define N 10000
  2. #define MOD 1000
  3.  
  4. struct Element { int index; int value; };
  5.  
  6. int main() {
  7.        
  8.     int arr[N];
  9.     struct Element maximum;
  10.  
  11.         // initialize the array with random values
  12.     for(int i = 0 ; i < N ; i ++ )
  13.     {
  14.         arr[i] = rand() % MOD;
  15.     }
  16.  
  17.         // parallelize the outer for loop and reduce on maximum
  18.     #pragma omp parallel for reduction(max: maximum)
  19.  
  20.     for (int i = N-1; i > 0; i-- ){
  21.  
  22.                 // initialize the maximum element to the current element
  23.         maximum.value = arr[i];
  24.         maximum.index = i;
  25.  
  26.                 // parallelize the inner loop and reduce on maximum, using an Element initialized to the maximum of omp_in and omp_out
  27.                 // initializer allows correct sorting in case of negative numbers as well
  28.         #pragma omp declare reduction(max : struct Element : omp_out = omp_in.value > omp_out.value ? omp_in : omp_out) initializer(omp_priv=omp_orig)
  29.         for (int j = i - 1; j >= 0; j--)
  30.         {
  31.                         // check if current value is larger than the stored maximum
  32.             if (arr[j] > maximum.value)
  33.             {
  34.                 // update the value and index
  35.                 maximum.value = arr[j];
  36.                 maximum.index = j;
  37.             }
  38.         }
  39.        
  40.         // swap
  41.         int tmp = arr[maximum.index];
  42.         arr[maximum.index] = arr[i];
  43.         arr[i] = maximum.value;
  44.            
  45.     }
  46.    
  47.     // print the first 50 elements
  48.     for (int i = 0; i < 50; i++)  
  49.     {  
  50.         printf("%d  ", arr[i]);
  51.     }  
  52.  
  53.     return 0;
  54.  
  55. }
  56.  
Parsed in 0.012 seconds