我正在嘗試實作以下代碼片段的更快版本:
Eigen::VectorXd dTX = (( (XPSF.array() - x0).square() (ZPSF.array() - z0).square() ).sqrt() txShift)*fs/c t0*fs;
Eigen::VectorXd Zsq = ZPSF.array().square();
Eigen::MatrixXd idxt(XPSF.size(),nc);
for (int i = 0; i < nc; i ) {
idxt.col(i) = ((XPSF.array() - xe(i)).square() Zsq.array()).sqrt()*fs/c dTX.array();
idxt.col(i) = (abs(XPSF.array()-xe(i)) <= ZPSF.array()*0.5/fnumber).select(idxt.col(i),-1);
}
我現在正在使用的樣本陣列大小是:
XPSF:591*192 系數的列向量(列向量中總計 113,472 個值)
ZPSF:與 XPSF 大小相同
xe:192 個系數的 RowVector
idxt:113,472x192 大小的矩陣
當前使用 gcc 和 -msse2 和 -o3 優化運行,回圈第一行的平均時間為 ~0.08 秒,回圈第二行的平均時間為 ~0.03 秒。我知道運行時依賴于平臺,但我相信這仍然可以更快。一個商業軟體在大約兩個數量級的時間內執行我試圖在這里做的操作。另外,我懷疑我的代碼現在有點業余!
我曾嘗試通讀 Eigen 檔案以了解矢量化的作業原理、它在何處實作以及有多少代碼可能被 Eigen “隱式”并行化,但我一直在努力跟蹤細節。總的來說,我對 C 也有點陌生,但我已經看到了關于 std::thread 的檔案和其他資源,并試圖將它與此代碼結合起來,但沒有取得多大成功。
任何意見,將不勝感激。
更新:

更新 2
I would upvote Soleil's answer because it contains helpful information if I had the reputation score for it. However, I should clarify that I would like to first figure out what optimizations I can do without a GPU. I'm convinced (albeit without OpenMP) Eigen's inherent multithreading and vectorization won't speed it up any further (unless there are unnecessary temporaries being generated). How could I use something like std::thread to explicitly parellelize this? I'm struggling to combine both std::thread and Eigen to this end.
uj5u.com熱心網友回復:
開放式MP
如果您的 CPU 有足夠多的內核和執行緒,通常一個簡單快捷的第一步是通過添加編譯指示來呼叫 OpenMP:
#pragma omp parallel for
for (int i = 0; i < nc; i )
并使用/openmp(cl) 或-fopenmp(gcc) 或僅-ftree-parallelize-loops使用 gcc 編譯以自動展開回圈。
這將執行映射縮減,映射將發生在您的 CPU 可以處理的并行執行緒數上(7700HQ 為 8 個執行緒)。
通常,您還可以設定一個子句num_threads(n),其中 n 是所需的執行緒數:
#pragma omp parallel num_threads(8)
因為 7700HQ 可以處理 8 個并發執行緒,所以我使用了 8 個。
待定
您還可以使用 TBB 展開回圈:
#pragma unroll
for (int i = 0; i < nc; i )
與特征集成的執行緒
使用本征,您可以添加
OMP_NUM_THREADS=n ./my_program
omp_set_num_threads(n);
Eigen::setNbThreads(n);
帶有特征的多執行緒注釋
但是,在常見問題解答中:
目前 Eigen 僅并行化一般的矩陣-矩陣產品(作業臺),因此它本身并沒有充分利用并行硬體。”
一般來說,OpenMP 的改進并不總是在這里,因此對發布版本進行基準測驗。另一種方法是確保您使用的是矢量化指令。
再次,從常見問題/矢量化:
如何啟用矢量化?
您只需要告訴編譯器啟用相應的指令集,然后 Eigen 就會檢測到它。如果它默認啟用,那么您不需要做任何事情。在 GCC 和 clang 上,您只需傳遞 -march=native 即可讓編譯器啟用 CPU 支持的所有指令集。
在 x86 架構上,大多數編譯器默認不啟用 SSE。您需要手動啟用 SSE2(或更新版本)。例如,對于 GCC,您將傳遞 -msse2 命令列選項。
在 x86-64 架構上,SSE2 一般是默認開啟的,但是你可以開啟 AVX 和 FMA 以獲得更好的性能
在 PowerPC 上,您必須使用以下標志:-maltivec -mabi=altivec,用于 AltiVec,或 -mvsx 用于支持 VSX 的系統。
在 32 位 ARM NEON 上,以下內容: -mfpu=neon -mfloat-abi=softfp|hard,具體取決于您是否在 softfp/hardfp 系統上。當前的大多數發行版都使用硬浮點 ABI,因此選擇后者,或者保留默認值并通過 -mfpu=neon。
在 64 位 ARM 上,默認情況下啟用 SIMD,您無需執行任何額外操作。
On S390X SIMD (ZVector), you have to use a recent gcc (version >5.2.1) compiler, and add the following flags: -march=z13 -mzvector.
multithreading with cuda
Given the size of your arrays, you want to try to offload to a GPU to reach the microsecond; in that case you would have (typically) as many threads as the number of elements in your array.
For a simple start, if you have an nvidia card, you want to look at cublas, which also allows you to use the tensor registers (fused multiply add, etc) of the last generations, unlike regular kernel.
Since eigen is a header only library, it makes sense that you could use it in a cuda kernel.
您還可以使用常規內核“手動”(即,沒有本征)實作所有內容。這在工程方面是無稽之談,但卻是教育/大學專案中的常見做法,以便了解一切。
使用 OneAPI 和 Intel GPU 進行多執行緒
由于您擁有 Skylake 架構,您還可以使用 OneAPI 在 CPU 的 GPU 上展開回圈:
// Unroll loop as specified by the unroll factor.
#pragma unroll unroll_factor
for (int i = 0; i < nc; i )
(來自樣本)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341395.html
標籤:c multithreading eigen
