如何“移动" Eigen :: VectorXd [英] How to "move" Eigen::VectorXd s

查看:141
本文介绍了如何“移动" Eigen :: VectorXd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近的一篇文章中的评论者告诉我,我需要更好地利用c ++ 11 move-semantics解决我的代码中的瓶颈.以下是需要修复的内容的简化版本.

A commenter in a recent post of mine told me I need to utilize c++11 move-semantics better to deal with a bottleneck in my code. Below is a simplified version of what needs to be fixed.

#include <iostream>
#include <Eigen/Dense>
#include <vector>

void makeCopy(std::vector<Eigen::VectorXd> &oldV){
    int n = oldV.size();
    std::vector<Eigen::VectorXd> mandatoryCopy;
    mandatoryCopy.resize(n);

    for(int i = 0; i < n; i++){
        mandatoryCopy[i] = oldV[i];
    }

    // swap the two
    oldV = mandatoryCopy;
}

int main(int argc, char **argv)
{
    // starting vector
    int len(1000);
    Eigen::VectorXd placeHolder(50);
    std::vector<Eigen::VectorXd> v(len, placeHolder);

    // copy it a bunch of times
    for(int iter = 0; iter < 1000; ++iter){
        std::cout << "iter: " << iter << "\n";
        makeCopy(v);
    }

    return 0;
}

问题:在makeCopy的for循环中,oldV[i]是一个左值,那么我该怎么做类似mandatoryCopy[i]&& = oldV[i]的事情?这是主要的瓶颈,对不对?我在想类似mandatoryCopy[i]&& = std::move(oldV[i])之类的东西,但这显然无法编译.

Question: inside the for loop of makeCopy, oldV[i]is an lvalue, so how could I do something like mandatoryCopy[i]&& = oldV[i]? This is the primary bottleneck, right? I'm thinking something like mandatoryCopy[i]&& = std::move(oldV[i]), but this obviously won't compile.

按照@vsoftco的建议,我已经尝试过

As per @vsoftco's suggestion, I have tried

std::vector<Eigen::VectorXd> makeCopy2(std::vector<Eigen::VectorXd> oldV){

    int n = oldV.size();
    std::vector<Eigen::VectorXd> mandatoryCopy;
    mandatoryCopy.resize(n);

    for(int i = 0; i < n; i++){
        mandatoryCopy[i] = oldV[i];
    }
    return mandatoryCopy;
}

但是我发现它比较慢. @vsoftco和@ggael都提到返回修改后的复制参数而不是再次复制会更快,我同意,但是我怀疑这对于我的实际代码是否可行.稍后我可以问这个问题,但这将是一个单独的问题/线程.

but I find that it is slower. Both @vsoftco and @ggael have mentioned that it would be faster to return a modified copied argument, instead of copying again, and I agree, but I doubt that this is possible for my actual code. I could ask about this later, but it would be a separate question/thread.

推荐答案

您在看的不是正确的行.如果必须复制一份,那么您将无法摆脱它.尽管如此,最好避免使用for循环和right:

You are not looking at the right line. If one copy is mandatory, then you cannot get rid of it. Nonetheless, better avoid the for loop and right:

std::vector<Eigen::VectorXd> mandatoryCopy = oldV;

另一方面,您可以通过将oldV=mandatoryCopy替换为:

On the other hand, you can omit the second copy by replacing oldV=mandatoryCopy with:

std::swap(oldV,mandatoryCopy);

将执行廉价的指针交换.您会得到:

that will perform a cheap pointer exchange. You get:

void makeCopy(std::vector<Eigen::VectorXd> &oldV){
  std::vector<Eigen::VectorXd> V = oldV;
  // do something with V
  std::swap(oldV,V);
}

对于功能样式,在第二个示例中,您必须直接使用已经是副本的参数:

For the functional style, in your second example, you must directly play with the argument which is already a copy:

std::vector<Eigen::VectorXd> makeCopy2(std::vector<Eigen::VectorXd> V){
  // do something with V
  return V;
}

并用v = makeCopy2(v);调用.

不要忘记使用-std=c++11进行编译以获得移动语义副本.

Do not forget to compile with -std=c++11 to get move semantic copies.

最后,最好将vector<VectorXd>包装在MatrixXd中.这将极大地减少内存分配的数量:

Finally, it might be better to pack your vector<VectorXd> within a MatrixXd. This will dramatically reduce the number of memory allocations:

void makeCopy3(MatrixXd &oldV){
  int n = oldV.cols();
  MatrixXd V = oldV;
  for(int i = 0; i < n; i++){
    V.col(i) *= 0.99;
  }
  oldV.swap(V); // or oldV = std::move(V); with c++11 enabled
}

这篇关于如何“移动" Eigen :: VectorXd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆