2017-03-26 274 views
2

我從CSV文件轉換成float陣列讀取數據(1維/載體)和施加快速排序它:OpenMP的截斷浮子

int main() 
{ 

float floatArr[2000]; 

ReadRandomData(floatArr, "D:\RandomData.csv"); 

//#pragma omp parallel 
{ 
    QuickSort(floatArr, 0,2000); 
} 

for(int i=0; i<2000; i++) 
{ 
    cout<<floatArr[i]<<endl; 
} 

_getch(); 

} 

和輸出的東西像這樣:

enter image description here

但一旦我取消了#pragma omp parallel部分,歐tput的是這樣的(其截斷我認爲)

enter image description here

任何人都可以解釋我爲什麼會這樣使用OpenMP以及如何解決它?提前致謝!

UPDATE

這裏是快速排序部分:

void QuickSort(float* arr, int startIndex, int endIndex) 
{ 
    int pivot = arr[startIndex];     //pivot element is the leftmost element 
    int splitPoint; 

if(endIndex > startIndex)      //if they are equal, it means there is only one element and quicksort's job here is finished 
{ 
    splitPoint = Splitarr(arr, pivot, startIndex, endIndex); 
                //Splitarr() returns the position where pivot belongs to 
    arr[splitPoint] = pivot; 
    QuickSort(arr, startIndex, splitPoint-1); //Quick sort first half 
    QuickSort(arr, splitPoint+1, endIndex); //Quick sort second half 
} 

}

這裏是分裂碼:

int Splitarr(float* arr, int pivot, int startIndex, int endIndex) 
{ 
    int leftBoundary = startIndex; 
    int rightBoundary = endIndex; 

while(leftBoundary < rightBoundary)   //shuttle pivot until the boundaries meet 
{ 
    while(pivot < arr[rightBoundary]  //keep moving until a lesser element is found 
      && rightBoundary > leftBoundary)  //or until the leftBoundary is reached 
    { 
      rightBoundary--;      //move left 
    } 

      swap(arr[leftBoundary], arr[rightBoundary]); 


    while(pivot >= arr[leftBoundary]  //keep moving until a greater or equal element is found 
      && leftBoundary < rightBoundary)  //or until the rightBoundary is reached 
    { 
      leftBoundary++;      //move right 
    } 

      swap(arr[rightBoundary], arr[leftBoundary]); 

} 

return rightBoundary;        //leftBoundary is the split point because 
                //the above while loop exits only when 
                //leftBoundary and rightBoundary are equal 
} 
+0

'QuickSort'看起來像什麼? – 1201ProgramAlarm

+0

你也可以顯示你的'swap'方法嗎? – ScY

+0

@ScY默認使用C++(std)給出的那個。問題不在我認爲它正確交換和列表排序,只是最終結果被截斷。 –

回答

1

您的算法中有一件事我認爲是造成問題。

更改行:

int pivot = arr[startIndex]; 

float pivot = arr[startIndex]; 

轉換浮到INT截斷小數,如您正確設定。這可能會解決您的問題。

+0

我也試着用int pivot和not parallelized。還有一些截斷的元素。少於並行啓用,但仍然。 – ScY

+0

我認爲這是因爲'arr [splitPoint] = pivot;'但是所有的OpenMP都會被截斷讓我懷疑 –

+2

答案可能有一點,但錯過了這樣的事實,即整個並行是完全虛假/破碎的。 – Zulan