我在C++中实现了一个算法,它作为输出返回一个庞大的元素数组。现在,我想在Rcpp中实现一个包装器,以便能够使用R调用这个函数。
我在Makevars文件中指定了以下设置:
PKG_CXXFLAGS = -std=c++11
所以我可以使用C++11版本。
// [[Rcpp::export]]
NumericMatrix compute(int width, int height)
{
vector<data_t> weights(width * height);
compute_weights(weights);
NumericMatrix mat(height, width);
copy(begin(weights), end(weights), mat.begin());
return mat;
}如果NumericMatrix在函数返回时被移动,则上述包装函数仍然有效,否则将创建一个新对象。
Rcpp是否利用移动语义?如果没有,有没有什么解决办法来避免副本的构造?
发布于 2020-05-29 06:32:56
如果NumericMatrix在函数返回时被移动,则上述包装函数仍然有效,否则将创建一个新对象。 ..。如果没有,有没有什么解决办法来避免副本的构造?
我认为复制构造函数只创建了一个浅拷贝,所以不应该有任何副本。见Rcpp: How to ensure deep copy of a NumericMatrix?和
这个例子也证实了这一点。
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector allocate_the_vec(R_xlen_t n_ele){
Rcpp::NumericVector out(n_ele);
return out;
}
/*** R
# got 16 GB ram on my laptop. 3 x 7 is an issue but 2 x 7 is not
how_large <- as.integer(7 * 10^9 / 8)
the_large_vec_1 <- allocate_the_vec(how_large)
object.size(the_large_vec_1)
the_large_vec_2 <- allocate_the_vec(how_large)
*/https://stackoverflow.com/questions/33507105
复制相似问题