0
votes

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;
}
1
Which g++ version are you using?dreamcrash
We need to know the environment you are using. (OS, Compiler). At a guess, you're doing this on Windows with the Microsoft compilers, which only support OpenMP 2.0 or so (which came out in 2000), and does not support user-defined reductions.Jim Cownie
I second Jim Cownie. You are using an old C++ compiler or a modern one with bad support for OpenMP, i.e., MSVC++.Hristo Iliev

1 Answers

0
votes

if you rename your OpenMP function like this:

void selectionsortomp(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;
    }
}

and then call in int main() your code compiles just fine for me. With g++ 7.5.0 (on Ubuntu 18.04 and OpenMP 4.5) I get no errors.