我想在OpenMP线程上按列平均分割一个Eigen动态大小的数组。本征:用于OpenMP的分割阵列
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
方法来做到这一点,但我不知道,如果将本征承认每个线程的子阵列占据的连续内存。
当我读块类型的documentation,具有与以下描述的InnerPanel
模板参数:
InnerPanel
为真,如果块映射到一组一排主要 矩阵或行的至列主矩阵的列(可选)。参数 允许在编译时确定在块表达式上对齐访问 是否可能。
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
SO必须睡觉。 –