gpt4 book ai didi

C++ Armadillo 和 OpenMp : Parallelization of summation of outer products - define reduction for Armadillo matrix

转载 作者:行者123 更新时间:2023-11-30 02:24:57 26 4
gpt4 key购买 nike

我正在尝试使用 OpenMP 并行化一个 for 循环,它对 Armadillo 矩阵求和。我有以下代码:

#include <armadillo>
#include <omp.h>

int main()
{

arma::mat A = arma::randu<arma::mat>(1000,700);
arma::mat X = arma::zeros(700,700);
arma::rowvec point = A.row(0);

# pragma omp parallel for shared(A) reduction(+:X)
for(unsigned int i = 0; i < A.n_rows; i++){
arma::rowvec diff = point - A.row(i);
X += diff.t() * diff; // Adding the matrices to X here
}

}

我收到这个错误:

[Legendre@localhost ~]$ g++ test2.cpp -o test2 -O2 -larmadillo -fopenmp
test2.cpp: In function ‘int main()’:
test2.cpp:11:52: error: user defined reduction not found for ‘X’

我阅读了有关定义缩减的内容,但我还没有找到使用 Armadillo 矩阵的示例。就我而言,定义 Armadillo 矩阵缩减的最佳方法是什么?

最佳答案

这些缩减仅适用于内置类型(doubleint 等)。因此,您必须自己进行还原,这很简单。只需将每个线程的结果累积到一个线程局部变量中,然后将其添加到临界区内的全局结果。

#include <armadillo>
#include <omp.h>

int main()
{

arma::mat A = arma::randu<arma::mat>(1000,700);
arma::mat X = arma::zeros(700,700);
arma::rowvec point = A.row(0);

#pragma omp parallel shared(A)
{
arma::mat X_local = arma::zeros(700,700);

#pragma omp for
for(unsigned int i = 0; i < A.n_rows; i++)
{
arma::rowvec diff = point - A.row(i);
X_local += diff.t() * diff; // Adding the matrices to X here
}

#pragma omp critical
X += X_local;
}
}

使用更新的 OpenMP(我认为是 4.5?),您还可以为您的类型声明用户定义的缩减。

#include <armadillo>
#include <omp.h>

#pragma omp declare reduction( + : arma::mat : omp_out += omp_in ) \
initializer( omp_priv = omp_orig )

int main()
{

arma::mat A = arma::randu<arma::mat>(1000,700);
arma::mat X = arma::zeros(700,700);
arma::rowvec point = A.row(0);

#pragma omp parallel shared(A) reduction(+:X)
for(unsigned int i = 0; i < A.n_rows; i++)
{
arma::rowvec diff = point - A.row(i);
X += diff.t() * diff; // Adding the matrices to X here
}
}

关于C++ Armadillo 和 OpenMp : Parallelization of summation of outer products - define reduction for Armadillo matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44993310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com