通过多线程将值分配给稀疏矩阵时出现段错误

问题描述 投票:0回答:1

我打算用从一系列步骤中得出的值来填充一个稀疏矩阵,以使其效率更高。OpenMP用于加速这些进程,我发现在使用1个线程时它可以正常工作,但是对于多线程捕获了segfault线程,我准备了一个简单的演示代码来重现该错误,衷心希望有人能帮我一个忙。

#include <RcppArmadillo.h>
#include <omp.h>

// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(bigmemory, BH)]]

using namespace std;
using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
arma::sp_mat test(arma::vec x, int n, int threads = 1){
    omp_set_num_threads(threads);
    arma::sp_mat m(n, n);
    #pragma omp parallel for schedule(dynamic) 
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            m(i, j) = x[i * n + j];
        }
    }
    return m;
}

# run 
a<-test(sample(c(0,1,2),100*100,rep=T), n=100, threads=1)
a<-test(sample(c(0,1,2),100*100,rep=T), n=100, threads=10)
r rcpp rcpparmadillo
1个回答
1
投票

tl; dr:

© www.soinside.com 2019 - 2024. All rights reserved.