[英]Using omp atomic operation in OpenMP for class members with Eigen Array
[英]Eigen: Split Array for OpenMP
我想按列在OpenMP線程上均勻划分Eigen動態大小數組。
thread 0 | thread 1 | thread 2
[[0, 1, 2], [[0], | [[1], | [[2],
[3, 4, 5], becomes: [3], | [4], | [5],
[6, 7, 8]] [6]] | [7]] | [8]]
我可以使用block
方法來做到這一點,但是我不確定Eigen是否會識別每個線程的子數組占用連續內存。
當我閱讀塊類型的文檔時,具有一個InnerPanel
模板參數,其內容如下:
如果塊映射到行主矩陣的一組行或列主矩陣的一組列(可選),則
InnerPanel
為true。 該參數允許在編譯時確定是否可以對塊表達式進行對齊訪問。
Eigen是否知道每個OpenMP線程都可以對子數組進行矢量化處理,因為每個子數組實際上都占用連續內存?
如果沒有,如何讓Eigen知道這一點?
程序:
#include <Eigen/Eigen>
#include <iostream>
int main() {
// The dimensions of the matrix is not necessary 8 x 8.
// The dimension is only known at run time.
Eigen::MatrixXi x(8,8);
x.fill(0);
int n_parts = 3;
#pragma omp parallel for
for (int i = 0; i < n_parts; ++i) {
int st = i * x.cols() / n_parts;
int en = (i + 1) * x.cols() / n_parts;
x.block(0, st, x.rows(), en - st).fill(i);
}
std::cout << x << "\n";
}
結果( g++ test.cpp -I<path to eigen includes> -fopenmp -lgomp
):
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
0 0 1 1 1 2 2 2
為確保塊表達式確實占用了連續內存,請改用middleCols
(或leftCols
或rightCols
):
#include <Eigen/Core>
template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
void inspectBlock(const Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>& block)
{
std::cout << __PRETTY_FUNCTION__ << '\n';
}
int main() {
Eigen::MatrixXi x(8,8);
inspectBlock(x.block(0, 1, x.rows(), 2));
inspectBlock(x.middleCols(1, 2));
}
結果:
void inspectBlock(const Eigen::Block<ArgType, BlockRows, BlockCols, InnerPanel>&) [with XprType = Eigen::Matrix<int, -1, -1>; int BlockRows = -1; int BlockCols = -1; bool InnerPanel = false] void inspectBlock(const Eigen::Block<ArgType, BlockRows, BlockCols, InnerPanel>&) [with XprType = Eigen::Matrix<int, -1, -1>; int BlockRows = -1; int BlockCols = -1; bool InnerPanel = true]
注意: -1
是Eigen::Dynamic
的值,即在編譯時不固定。
當然,如果矩陣是行專業的,則可以拆分int topRows
, middleRows
或bottomRows
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.