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;
}