OpenMP parallel quicksort

前端 未结 3 978
天命终不由人
天命终不由人 2021-01-21 08:17

I try to use OpenMP to parallel quicksort in partition part and quicksort part. My C code is as follows:

#include \"stdlib.h\"
#include \"stdio.h\"
#include \"om         


        
3条回答
  •  情歌与酒
    2021-01-21 08:35

    I feel sorry for my first comment.It does not matter with your problem.I have not found the true problem of your question(Maybe your move element has the problem).According to your opinion, I wrote a similar program, it works fine.(I am also new on OpenMP).

    #include 
    #include 
    
    
    int partition(int * a, int p, int r)
    {
        int lt[r-p];
        int gt[r-p];
        int i;
        int j;
        int key = a[r];
        int lt_n = 0;
        int gt_n = 0;
    
    #pragma omp parallel for
        for(i = p; i < r; i++){
            if(a[i] < a[r]){
                lt[lt_n++] = a[i];
            }else{
                gt[gt_n++] = a[i];
            }   
        }   
    
        for(i = 0; i < lt_n; i++){
            a[p + i] = lt[i];
        }   
    
        a[p + lt_n] = key;
    
        for(j = 0; j < gt_n; j++){
            a[p + lt_n + j + 1] = gt[j];
        }   
    
        return p + lt_n;
    }
    
    void quicksort(int * a, int p, int r)
    {
        int div;
    
        if(p < r){ 
            div = partition(a, p, r); 
    #pragma omp parallel sections
            {   
    #pragma omp section
                quicksort(a, p, div - 1); 
    #pragma omp section
                quicksort(a, div + 1, r); 
    
            }
        }
    }
    
    int main(void)
    {
        int a[10] = {5, 3, 8, 4, 0, 9, 2, 1, 7, 6};
        int i;
    
        quicksort(a, 0, 9);
    
        for(i = 0;i < 10; i++){
            printf("%d\t", a[i]);
        }
        printf("\n");
        return 0;
    }
    

提交回复
热议问题