当算法被放置在循环内时,它会产生不同的结果,C++

我在 Rcpp 中创建了以下算法并在 R 中编译它。

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>

// [[Rcpp::export]]

arma::colvec Demo(arma::mat n, int K){
  
  arma::colvec N(K);
  
  for(int j=0; j<K; ++j){
    for(int i=0; i<(K-j); ++i){
      N[j] += accu(n.submat(i,0,i,j));
    }
  } 
  return N;
}

/***R
K = 4
n = cbind(c(1008, 5112, 1026, 25, 0), 0, 0, 0, 0)
Demo(n,K)

for(i in 1:3){
 print(Demo(n,K))
 print(K)
 print(n)
}
*/

但是,当我在循环中运行它时,会发生一些非常奇怪的事情。

例如,如果我有

> K = 4
> n
     [,1] [,2] [,3] [,4] [,5]
[1,] 1008    0    0    0    0
[2,] 5112    0    0    0    0
[3,] 1026    0    0    0    0
[4,]   25    0    0    0    0
[5,]    0    0    0    0    0

然后,如果我运行该算法Demo一次,我会收到正确的结果

> Demo(n,K)
     [,1]
[1,] 7171
[2,] 7146
[3,] 6120
[4,] 1008

但是,如果我在循环中多次运行它,它就会开始表现得很奇怪

for(i in 1:3){
 print(Demo(n,K))
 print(K)
 print(n)
}
    [,1]
[1,] 7171
[2,] 7146
[3,] 6120
[4,] 1008
[1] 4
     [,1] [,2] [,3] [,4] [,5]
[1,] 1008    0    0    0    0
[2,] 5112    0    0    0    0
[3,] 1026    0    0    0    0
[4,]   25    0    0    0    0
[5,]    0    0    0    0    0
      [,1]
[1,] 14342
[2,] 14292
[3,] 12240
[4,]  2016
[1] 4
     [,1] [,2] [,3] [,4] [,5]
[1,] 1008    0    0    0    0
[2,] 5112    0    0    0    0
[3,] 1026    0    0    0    0
[4,]   25    0    0    0    0
[5,]    0    0    0    0    0
      [,1]
[1,] 21513
[2,] 21438
[3,] 18360
[4,]  3024
[1] 4
     [,1] [,2] [,3] [,4] [,5]
[1,] 1008    0    0    0    0
[2,] 5112    0    0    0    0
[3,] 1026    0    0    0    0
[4,]   25    0    0    0    0
[5,]    0    0    0    0    0

在第一次运行中,它计算正确,然后在第二次运行中,它给出了乘以 2 的正确输出,在第三次运行中,它给出了乘以 3 的正确输出。但是根据算法步骤,我看不到产生这种行为的明显步骤。

正确的输出应该是

for(i in 1:3){
 print(Demo(n,K))
}
     [,1]
[1,] 7171
[2,] 7146
[3,] 6120
[4,] 1008
     [,1]
[1,] 7171
[2,] 7146
[3,] 6120
[4,] 1008
     [,1]
[1,] 7171
[2,] 7146
[3,] 6120
[4,] 1008

回答

您正在N通过+=.

您的函数无法确保它初始化为零。 Rcpp默认情况下倾向于这样做(因为我认为这是谨慎的)-但是如果您知道自己在做什么,则可以抑制速度。

代码的最小修复版本(带有正确的标头和对 的调用.fill(0))如下。

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::colvec Demo(arma::mat n, int K){
    arma::colvec N(K);
    N.fill(0);   // important, or construct as N(k, arma::fill::zeros)
    for(int j=0; j<K; ++j){
        for(int i=0; i<(K-j); ++i){
            N[j] += accu(n.submat(i,0,i,j));
        }
    }
    return N;
}

/***R
K = 4
n = cbind(c(1008, 5112, 1026, 25, 0), 0, 0, 0, 0)
Demo(n,K)

for(i in 1:3) {
 print(Demo(n,K))
 print(K)
 print(n)
}
*/

您还可以调用.zeros()(一旦构建)或使用zeros(k)(构建)或...部署多种不同的方式来确保您的内容在添加之前被清除

检查文档后,最短的可能是arma::colvec(N, arma::fill::zeros).

  • It's all undetermined behaviour as _he is adding to uncleared memory_. Just how _undetermined_ pans out appears to vary in your setups. But that is irrelevant as the underlying code was faulty.

以上是当算法被放置在循环内时,它会产生不同的结果,C++的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>