我有一個很長的 vector Eigen::VectorXd X;,我想使用以下函式之一逐段更新它:
void Foo1(Eigen::Ref<Eigen::VectorXd> x) {
// Update x.
}
Eigen::VectorXd Foo2() {
Eigen::VectorXd x;
// Update x.
return x;
}
int main() {
const int LARGE_NUMBER = ...; // Approximately in the range ~[600, 1000].
const int SIZES[] = {...}; // Entries roughly in the range ~[2, 20].
Eigen::VectorXd X{LARGE_NUMBER};
int j = 0;
for (int i = 0; i < LARGE_NUMBER; i = SIZES[j]) {
// Option (1).
Foo1(X.segment(i, SIZES[j]));
// Option (2)
X.segment(i, SIZES[j]) = Foo2();
j;
}
return 0;
}
鑒于上述規格,哪個選項最有效?我會說(1)因為它會直接修改記憶體而不創建任何臨時檔案。但是,編譯器優化可能會使(2)性能更好——例如,請參閱這篇文章。
其次,考慮以下功能:
void Foo3(const Eigen::Ref<const Eigen::VectorXd>& x) {
// Use x.
}
void Foo4(const Eigen::VectorXd& x) {
// Use x.
}
是否保證Foo3使用段呼叫X始終至少Foo4與使用相同段呼叫一樣有效?也就是說,Foo3(X.segment(...))始終至少與Foo4(X.segment(...))?
uj5u.com熱心網友回復:
鑒于上述規格,哪個選項最有效?
如您所料,最有可能的選項 1。當然,這取決于更新的內容。所以你可能需要一些基準測驗。但總的來說,與分配新物件所允許的次要優化相比,分配成本是顯著的。另外,選項 2 會產生復制結果的額外成本。
是否保證使用 X 段呼叫 Foo3 始終至少與使用相同段呼叫 Foo4 一樣有效?
如果您呼叫Foo4(x.segment(...))它,則會分配一個新向量并將該段復制到其中。這是顯著更比Foo3昂貴。您唯一獲得的是矢量將正確對齊。這只是現代 CPU 的一個小好處。所以我希望 Foo3 更有效率。
請注意,有一個您沒有考慮過的選項:使用模板。
template<class Derived>
void Foo1(const Eigen::MatrixBase<Derived>& x) {
Eigen::MatrixBase<Derived>& mutable_x = const_cast<Eigen::MatrixBase<Derived>&>(x);
// Update mutable_x.
}
const-cast 令人討厭但無害。請參閱 Eigen 關于該主題的檔案。 https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
總的來說,這將允許與行內函式體大致相同的性能。不過,在您的特定情況下,它可能不會比 Foo1 的行內版本快。這是因為一般段和 Ref 物件具有基本相同的性能。
訪問 Ref 與 Vector 的效率
讓我們更詳細地看一下 an Eigen::Vector、 an Eigen::Ref<Vector>、 anEigen::Matrix和 an計算之間的性能Eigen::Ref<Matrix>。Eigen::Block(Vector.segment() 或 Matrix.block() 的回傳型別)在功能上與 Ref 相同,因此我不再贅述。
- Vector 和 Matrix 保證整個陣列與 16 位元組邊界對齊。這允許操作使用對齊的記憶體訪問(例如
movapd在這種情況下)。 - Ref 不保證對齊,因此需要未對齊的訪問(例如
movupd)。在非常舊的 CPU 上,這曾經有顯著的性能損失。這些天它不太相關。對齊很好,但它不再是矢量化的全部。參考 Agner 關于該主題的內容 [1]:
當訪問跨越高速快取線邊界的未對齊資料時,某些微處理器會受到幾個時鐘周期的懲罰。
大多數讀取或寫入 16 位元組記憶體運算元的沒有 VEX 前綴的 XMM 指令要求運算元按 16 對齊。接受未對齊的 16 位元組運算元的指令在較舊的處理器上可能非常低效。但是,AVX 和更高版本的指令集在很大程度上解除了這種限制。AVX 指令不需要對齊記憶體運算元,明確對齊的指令除外。支持 AVX 指令集的處理器通常可以非常有效地處理未對齊的記憶體運算元。
- 所有四種資料型別都保證了內部維度(向量中的唯一維度,矩陣中的單列)連續存盤。所以 Eigen 可以沿著這個維度向量化
- Ref 不保證沿外部維度的元素是連續存盤的。從一列到下一列可能存在間隙。這意味著標量運算類似于
Matrix Matrix或Matrix*Scalar可以對所有行和列中的所有元素使用單個回圈,而Ref Ref需要一個嵌套回圈,其中在所有列上有一個外回圈,在所有行上有一個內回圈。 - Ref 和 Matrix 都不保證特定列的正確對齊。因此,大多數矩陣運算(例如矩陣向量乘積)需要使用未對齊的訪問。
- 如果您在函式內創建向量或矩陣,這可能有助于轉義和別名分析。然而,在大多數情況下,Eigen 已經假定沒有別名,并且 Eigen 創建的代碼幾乎沒有給編譯器添加任何內容的空間。因此,它很少有好處。
- 呼叫約定存在差異。例如在 中
Foo(Eigen::Ref<Vector>),物件是按值傳遞的。Ref 有一個指標、一個大小,并且沒有解構式。所以它將在兩個暫存器中傳遞。這是非常有效的。對于Ref<Matrix>消耗 4 個暫存器(指標、行、列、外部步幅)的情況不太好。Foo(const Eigen::Ref<const Vector>&)將在堆疊上創建一個臨時物件并將指標傳遞給函式。Vector Foo()回傳一個具有解構式的物件。所以呼叫者在堆疊上分配空間,然后將一個隱藏的指標傳遞給函式。通常,這些差異并不顯著,但它們當然存在,并且可能與使用許多函式呼叫進行很少計算的代碼相關
With these differences in mind, let's look at the specific case at hand. You have not specified what the update method does, so I have to make some assumptions.
The computations will always be the same so we only have to look at memory allocations and accesses.
Example 1:
void Foo1(Eigen::Ref<Eigen::VectorXd> x) {
x = Eigen::VectorXd::LinSpaced(x.size(), 0., 1.);
}
Eigen::VectorXd Foo2(int n) {
return Eigen::VectorXd::LinSpaced(n, 0., 1.);
}
x.segment(..., n) = Foo2(n);
Foo1 does one unaligned memory write. Foo2 does one allocation and one aligned memory write into the temporary vector. Then it copies to the segment. That will use one aligned memory read and an unaligned memory write. Therefore Foo1 is clearly better in all circumstances.
Example 2:
void Foo3(Eigen::Ref<Eigen::VectorXd> x)
{
x = x * x.maxCoeff();
}
Eigen::VectorXd Foo4(const Eigen::Ref<Eigen::VectorXd>& x)
{
return x * x.maxCoeff();
}
Eigen::VectorXd Foo5(const Eigen::Ref<Eigen::VectorXd>& x)
{
Eigen::VectorXd rtrn = x;
rtrn = rtrn * rtrn.maxCoeff();
return rtrn;
}
Both Foo3 and 4 do two unaligned memory reads from x (one for the maxCoeff, one for the multiplication). After that, they behave the same as Foo1 and 2. Therefore Foo3 is always better than 4.
Foo5 does one unaligned memory read and one aligned memory write for the initial copy, then two aligned reads and one aligned write for the computation. After that follow the copy outside the function (same as Foo2). This is still a lot more than what Foo3 does but if you do a lot more memory accesses to the vector, it may be worthwhile at some point. I doubt it, but cases may exist.
The main take-away is this: Since you ultimately want to store the results in segments of an existing vector, you can never fully escape the unaligned memory accesses. So it is not worth worrying about them too much.
Template vs. Ref
A quick rundown of the differences:
The templated version will (if written properly) work on all data types and all memory layouts. For example if you pass a full vector or matrix, it can exploit the alignment.
在某些情況下,Ref 根本無法編譯,或者作業方式與預期不同。如上所述,Ref 保證內部維度是連續存盤的。該呼叫Foo1(Matrix.row(1))將不起作用,因為矩陣行未連續存盤在 Eigen 中。如果您使用 呼叫函式const Eigen::Ref<const Vector>&,Eigen 會將行復制到臨時向量中。
模板化版本將在這些情況下作業,但它當然不能矢量化。
Ref 版本有一些好處:
- 閱讀更清晰,意外輸入出錯的機會更少
- 你可以把它放在一個 cpp 檔案中,它會創建更少的冗余代碼。根據您的用例,更緊湊的代碼可能更有益或更合適
[1] https://www.agner.org/optimize/optimizing_assembly.pdf
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/389796.html
上一篇:C Linux最快的時間測量方法(比std::chrono快)?包括基準
下一篇:Numba中的型別串列與ND陣列
