#define N 10000
#define MOD 1000
struct Element { int index; int value; };
int main() {
int arr[N];
struct Element maximum;
// initialize the array with random values
for(int i = 0 ; i < N ; i ++ )
{
arr[i] = rand() % MOD;
}
// parallelize the outer for loop and reduce on maximum
#pragma omp parallel for reduction(max: maximum)
for (int i = N-1; i > 0; i-- ){
// initialize the maximum element to the current element
maximum.value = arr[i];
maximum.index = i;
// parallelize the inner loop and reduce on maximum, using an Element initialized to the maximum of omp_in and omp_out
// initializer allows correct sorting in case of negative numbers as well
#pragma omp declare reduction(max : struct Element : omp_out = omp_in.value > omp_out.value ? omp_in : omp_out) initializer(omp_priv=omp_orig)
for (int j = i - 1; j >= 0; j--)
{
// check if current value is larger than the stored maximum
if (arr[j] > maximum.value)
{
// update the value and index
maximum.value = arr[j];
maximum.index = j;
}
}
// swap
int tmp = arr[maximum.index];
arr[maximum.index] = arr[i];
arr[i] = maximum.value;
}
// print the first 50 elements
for (int i = 0; i < 50; i++)
{
}
return 0;
}