簡體   English   中英

如何使用 GCC 編譯器優化創建 static 庫?

[英]How do I create a static library with GCC compiler optimization?

我有一個 function 來計算 C++ 中的點積。 我想用 -O3 編譯器優化來編譯這個 function。 我的代碼庫中的代碼 Rest 是用 -O0 編譯的。 為此,我創建了一個包含 function 的 static 庫,並使用 -O3 編譯了該庫。 然后我將庫鏈接到我的代碼。 但我沒有從我的庫中得到優化。

測試.cpp

#include "config.h"
int multiply(uint128 *X1, uint128 *Y1, uint128 &ans, int input_length)
{
    int i=0;
    ans = 0;
    if (input_length > 4)
    {
        for (; i < input_length - 4; i += 4)
        {
            ans += X1[i] * Y1[i];
            ans += X1[i + 1] * Y1[i + 1];
            ans += X1[i + 2] * Y1[i + 2];
            ans += X1[i + 3] * Y1[i + 3];
        }
    }
    for (; i < input_length; i++)
    {
        ans += X1[i] * Y1[i];
    }    
    return 0;
}
int main()
{
    int len = 500, wrapper = 50;
    uint128 a[len], b[len], ans;
    auto start = time_now, end = time_now;
    long long ctr = 0;
    for(int t = 0; t < wrapper; t++)
    {
        for(int i =0; i < len; i++)
        {
            a[i] = rand();
            b[i] = rand();
        }
        start = time_now;
        multiply(a, b, ans, len);
        end = time_now;
        ctr += std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
    }
    cout<<"time taken: "<<ctr<<endl;
}
Compilation:
g++ -O3 test.cpp -std=c++11
./a.out
time taken: 1372

優化的.hpp

#ifndef OPTIMIZED_HPP
#define OPTIMIZED_HPP
#include <bits/stdc++.h>
using namespace std;
typedef __uint128_t uint128;


int multiply(uint128 *X1, uint128 *Y1, uint128 &ans, int input_length);
#endif

主文件

#include "optimized.hpp"
typedef __uint128_t uint128;

#define time_now std::chrono::high_resolution_clock::now()

int main()
{

    int len = 500, wrapper = 50;
    uint128 a[len], b[len], ans;

    auto start = time_now, end = time_now;
    long long ctr = 0;

    for(int t = 0; t < wrapper; t++)
    {
        for(int i =0; i < len; i++)
        {
            a[i] = rand();
            b[i] = rand();
        }

        start = time_now;
        multiply(a, b, ans, len);
        end = time_now;
        ctr += std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
    }
    cout<<"time taken: "<<ctr<<endl;

    return 0;
}
Compilation:
(the name of the library file is optimized.cpp)
g++ -O3 -g -std=c++11 -c optimized.cpp
ar rcs libfast.a optimized.o
g++ main.cpp libfast.a -std=c++11 -O3
./a.out 
time taken: 36140

恐怕你犯了 class 腳射:我優化了 function 我試圖計時 我們都做過一次。

您已經優化了test.cpp程序-O3 編譯器觀察到程序忽略了multiply的返回值。 它還具有multiply的定義,並觀察到主體沒有外部影響,但忽略了返回值(在任何情況下都是常量0 )和忽略參考參數ans 所以這一行:

multiply(a, b, ans, len);

也可能是:

(void)0;

優化器剔除它。

您可以通過修改程序來糾正此問題,以便它使用multiply的外部影響,以影響程序的 output 並且不能僅通過了解 function 的定義來預測。

但這還不夠。 -O3優化test.cpp當編譯器可以看到multiply的定義時,與main.cpp的相同優化相比,它仍然具有主要的信息優勢,其中multiply只是對黑盒的外部引用。

要有意義地測量使用-O3優化的multiply速度與使用-O0優化的速度相比,您必須在其他條件相同的情況下測量每種情況。 所以至少這樣做:

(無意中我的test.cpp是你的main.cpp

$ g++ -Wall -Wextra -pedantic -c -O0 -o mult_O0.o optimized.cpp
$ g++ -Wall -Wextra -pedantic -c -O3 -o mult_O3.o optimized.cpp
$ g++ -Wall -Wextra -pedantic -c -O3 -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13:13: warning: ISO C++ forbids variable length array ‘a’ [-Wvla]
   13 |     uint128 a[len], b[len], ans;
      |             ^
test.cpp:13:21: warning: ISO C++ forbids variable length array ‘b’ [-Wvla]
   13 |     uint128 a[len], b[len], ans;
      |                     ^

(您可能會關心這些警告)

$ g++ -o tmult_O0 test.o mult_O0.o
$ g++ -o tmult_O3 test.o mult_O3.o
$ ./tmult_O0
time taken: 228461
$ ./tmult_O3
time taken: 99092

這表明-O3正在做它的事情。

如果你這樣做,你不需要確保在test.cpp中使用multiply的效果,因為現在編譯器知道它無法知道黑盒multiply(a, b, ans, len)並且不能剔除它。

暫無
暫無

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

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