Insertion Sort with OpenMP

The OpenMP ARB supports various international events to help educate and inform the OpenMP community. Notices about upcoming conferences and events will be posted here.

Insertion Sort with OpenMP

Postby mushthielv » Mon Aug 29, 2016 11:54 am

I'm trying to parallelize an insertion sort algorithm in the following way:

Code: Select all
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void insertionsort(int vetor[], int tam)
{
    int i, j, tmp;
    omp_set_num_threads(4);

    #pragma omp parallel for shared (vetor, tam, tmp) private (i,j)


    for(i = 1; i < tam; i++)
    {
        j = i;

     
        while(j > 0 && vetor[j-1] > vetor[j])
        {

            tmp = vetor[j];
            vetor[j] = vetor[j-1];
            vetor[j-1] = tmp;
            j--;
        }
    }

}


But doesn't return the correct ordering. Can someone help me?
mushthielv
 
Posts: 1
Joined: Mon Aug 29, 2016 8:41 am

Re: Insertion Sort with OpenMP

Postby MarkB » Tue Aug 30, 2016 4:36 am

You can't parallelise insertion sort like this: each iteration of the loop must be executed after the previous one.

(Please post questions like this in the Using OpenMP forum viewforum.php?f=3 ).
MarkB
 
Posts: 782
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Conferences and Events

Who is online

Users browsing this forum: No registered users and 1 guest