如何利用openmp改进串行编码与c并行编码

时间:2018-03-08 05:21:16

标签: c multithreading openmp

我有一个名为bubblesort的函数,这是代码:

`

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


// A function to implement bubble sort
void bubbleSort(int *ptr, int n)
{
   int i, exch0, exch1 = 1, trips = 0;
    clock_t start = clock() ;
   while(exch1)
   {
    exch0 = 0;
    exch1 = 0;
{
    for(i = 0; i < n-1; i += 2)
        {
            if(*(ptr + i) > *(ptr + i + 1))
            {
                int temp = *(ptr + i);
                *(ptr + i) = *(ptr + i + 1);
                *(ptr + i + 1) = temp;
                exch0 = 1;
            }
        }
    if(exch0 || !trips){
            for(i = 1; i < n-1; i += 2)
            {
                if(*(ptr + i) > *(ptr + i + 1))
                {
                int temp = *(ptr + i);
                    *(ptr + i) = *(ptr + i + 1);
                    *(ptr + i + 1) = temp;
                    exch1 = 1;

                }
            }
    }   
   }
    trips = 1;
}
    clock_t end = clock() ;
    double elapsed_time = (end-start)/(double)CLOCKS_PER_SEC ;
    printf("Runtime = %.2f", elapsed_time);

}

/* Function to print an array */
void printArray(int *ptr, int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", *(ptr + i));
    printf("\n");
}

// Driver program to test above functions
int main()
{
    int size, *ptr; 
    printf("Enter number of elements: ");
    scanf("%d", &size);
    ptr = (int*) malloc(size * sizeof(int));
    if(ptr == NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }
    for(int i = 0; i<size; i++){
        *(ptr + i) = rand() % (1000000 + 1 - 0) + 0;
    }
    bubbleSort(ptr, size);
    free(ptr);
    return 0;
}

` 当我想将它改进为并行代码时,即使我尝试使用大量数组(排序为200,000个数字),分类所需的时间也增加了。这是并行编程的代码:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>


// A function to implement bubble sort
void bubbleSort(int *ptr, int n)
{
   int i, exch0, exch1 = 1, trips = 0;
       double start = omp_get_wtime();

   while(exch1)
   {
    exch0 = 0;
    exch1 = 0;
    int iCPU = omp_get_num_procs();
    omp_set_num_threads(4); 

    #pragma omp parallel
{
    #pragma omp for
    for(i = 0; i < n-1; i += 2)
        {
            if(*(ptr + i) > *(ptr + i + 1))
            {
                int temp = *(ptr + i);
                *(ptr + i) = *(ptr + i + 1);
                *(ptr + i + 1) = temp;
                exch0 = 1;
            }
        }
    if(exch0 || !trips){
        #pragma omp for
            for(i = 1; i < n-1; i += 2)
            {
                if(*(ptr + i) > *(ptr + i + 1))
                {
                int temp = *(ptr + i);
                    *(ptr + i) = *(ptr + i + 1);
                    *(ptr + i + 1) = temp;
                    exch1 = 1;

                }
            }
    }   
   }
    trips = 1;
}
    double time = omp_get_wtime() - start;
    printf("%f\n",time);


}


/* Function to print an array */
void printArray(int *ptr, int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", *(ptr + i));
    printf("\n");
}

// Driver program to test above functions
int main()
{
    int size, *ptr; 
    printf("Enter number of elements: ");
    scanf("%d", &size);
    ptr = (int*) malloc(size * sizeof(int));
    if(ptr == NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }

    for(int i = 0; i<size; i++){
        *(ptr + i) = rand() % (1000000 + 1 - 0) + 0;
    }

    bubbleSort(ptr, size);


//    printf("Sorted array: \n");
//   printArray(ptr, size);
    free(ptr);
    return 0;
}

为什么并行编码比串行代码慢?问题是我使代码并行的方式或我定时代码的方式。非常感谢你,感谢您的帮助=)。

0 个答案:

没有答案