Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C++ Error when compiling c++ code using OpenMP

jthorntonn

New Coder
I am trying to show the difference between the sequential and parallel versions of the selection sort. My code was working before but as soon as I added the clocks in to record the times I get errors referring to "cannot convert main():: lambda5 to 'int'." I am compiling my code with g++ -std=c++11 -openmp 'filename'. Here is my code:

Code:
#include <bits/stdc++.h>
#include <omp.h>
#include <algorithm>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
 
using namespace std;

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

int main(){
int vars = 100000;

int testarr1 = [vars];
int testarr2 = [vars];

for(int i = 0; i < vars; i++){
    testarr1[i] = rand();
    testarr2[i] = rand();
}

clock_t time_req1;
time_req1 = clock();
SeqSelectionSort(testarr1, 100000);
time_req1 = clock() - time_req1;
cout << "Execution time for Sequential Selection Sort: " << (float)time_req1 / CLOCKS_PER_SEC << " seconds\n" << endl;

clock_t time_req2;
time_req2 = clock();
selectionsortomp(testarr2, 100000);
time_req2 = clock() - time_req2;
cout << "Execution time for Parallel Selection Sort: " << (float)time_req2 / CLOCKS_PER_SEC << " seconds\n" << endl;

return 0;
}
 
Also, considering the size of the arrays, I would consider to use dynamic memory allocation instead (The operator new or the container std::vector for example). There is a (relatively small) limit to the amount of static memory that can be used. If the limit is surpassed, the program will crash.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom