I am taking selection sort and showing the difference between the sequential version and the parallel version. I am using OpenMP and when I compile my code I get this error:
main.cpp:34:44: error: expected '+', '*', '-', '&', '^', '|', '&&', '||', 'min', or 'max' before 'maximum' #pragma omp parallel for reduction(maximum:max)
Here is my code:
#include <bits/stdc++.h>
#include <algorithm>
#include <time.h>
#include <omp.h>
#include <stdio.h>
using namespace std;
void selectionsort(int* arr, int size)
{
for (int i = size - 1; i > 0; --i)
{
int max = i;
for (int j = i - 1; j >= 0; --j)
{
if (arr[j] > arr[max])
{
max = j;
}
}
swap(arr[i], arr[max]);
}
}
struct Compare { int val; int index; };
#pragma omp declare reduction(maximum : struct Compare : omp_out = omp_in.val > omp_out.val ? omp_in : omp_out)
void selectionsort(int* arr, int size)
{
for (int i = size - 1; i > 0; --i)
{
struct Compare max;
max.val = arr[i];
max.index = i;
#pragma omp parallel for reduction(maximum:max)
for (int j = i - 1; j >= 0; --j)
{
if (arr[j] > max.val)
{
max.val = arr[j];
max.index = j;
}
}
int tmp = arr[i];
arr[i] = max.val;
arr[max.index] = tmp;
}
}
int main(){
int x[10] = {8,7,9,1,2,5,4,3,0,6};
selectionsort(x, 10);
for (int i = 0; i < 10; i++)
printf("%d\n", x[i]);
return 0;
}