簡體   English   中英

O3優化標志使並行處理中的加速變差

[英]O3 optimization flag making speed-ups worse in parallel processing

我正在使用OpenMP測試C中並行程序的加速。 使用-O3標志用gcc編譯代碼,執行時間似乎要小得多。 但是,與沒有優化標志編譯的代碼相比,我對不同的線程數(2,4,8,16,24)的速度一直變慢。 這怎么可能?

這里有更多關於我到目前為止所發現的信息。 我正在編寫一個代碼,用於根據EratosthenesSieve查找素數,並嘗試使用OpenMP的並行版本對其進行優化。 這是代碼

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

// ind2num: returns the integer (3<=odd<=numMax)
//      represented by index i at prime_numbers (0<=i<=maxInd)
#define ind2num(i)  (2*(i)+3)
// num2ind: retorns the index (0<=i<=maxInd) at prime_numbers
//      which represents the number (3<=odd<=numMax)
#define num2ind(i)  (((i)-3)/2)

// Sieve: find all prime numbers until ind2num(maxInd)
void Sieve(int *prime_numbers, long maxInd) {
    long maxSqrt;
    long baseInd;
    long base;
    long i;

    // square root of the largest integer (largest possible prime factor)
    maxSqrt = (long) sqrt((long) ind2num(maxInd));

    // first base
    baseInd=0;
    base=3;

    do {
        // marks as non-prime all multiples of base starting at base^2
        #pragma omp parallel for schedule (static)
        for (i=num2ind(base*base); i<=maxInd; i+=base) {
            prime_numbers[i]=0;
        }

        // updates base to next prime number
        for (baseInd=baseInd+1; baseInd<=maxInd; baseInd++)
            if (primos[baseInd]) {
                base = ind2num(baseInd);
                break;
            }
    }
    while (baseInd <= maxInd && base <= maxSqrt);

}

例如,如果我執行它以查找小於1000000000(10 ^ 9)的所有素數,那么對於不同數量的線程(1,2,4,8,16,24),我最終會得到以下執行時間:

沒有-O3 | 56.31s | 28.87s | 21.77s | 11.19s | 6.13s | 4.50s |

使用-O3 .... | 10.10s | 5.23s | 3.74s | 2.81s | 2.62s | 2.52s |

以下是相應的加速:

沒有-O3 | 1 | 1.95 | 2.59 | 5.03 | 9.19 | 12.51 |

使用-O3 .... | 1 | 1.93 | 2.70 | 3.59 | 3.85 | 4.01 |

為什么我用-O3標志繼續降低速度?

算法的執行需要一定量的內存帶寬。 代碼越不優化,內部CPU機制就越多地占據運行時間。 代碼越優化,內存速度越大,運行時間就越大。

由於未經優化的代碼效率較低,因此在系統內存帶寬飽和之前,可以運行更多內核。 由於優化的代碼效率更高,因此可以更快地完成內存訪問,從而對系統內存帶寬造成更大的負擔。 這使得它不太可並行化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM