the bubble sorting program i wrote in openMP is slower than the serial version of this, why is this, i cant understand whats the problem.
On serial version it takes around 0.07 and on parallel it takes around 0.9 for an array of 5000 elements
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
void swap(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main (int argc, char *argv[]) {
int SIZE =5000;
int A[SIZE];
for(int i=0;i<SIZE;i++)
{
A[i]=rand()%SIZE;
}
int N = SIZE;
int i=0, j=0;
int first;
double start,end;
start=omp_get_wtime();
#pragma omp parallel reduction( +:first )
for( i = 0; i < N; i++ )
{
first = i % 2;
#pragma omp parallel for default(none),shared(A,first,N)
for( j = first; j < N-1; j += 2 )
{
if( A[ j ] > A[ j+1 ] )
{
swap( &A[ j ], &A[ j+1 ] );
}
}
}
end=omp_get_wtime();
for(i=0;i<N;i++)
{
printf(" %d",A[i]);
}
printf("\n-------------------------\n Time Parallel= %f",(end-start));
}
You just have to delete the line
#pragma omp parallel reduction( +:first )
This line is a mistake as it runs the algorithm several times, but if you skip this line the algorithm runs correctly in parallel and will be faster using openMP, please check these slides .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.