【问题标题】:Modifying zip iterator with eigen::Matrix gives errenous results使用 eigen::Matrix 修改 zip 迭代器会产生错误的结果
【发布时间】:2019-05-11 10:26:39
【问题描述】:

我有三组点 X、Y、Z。我打算使用 Eigen::Matrix4f 应用变换。我使用一个 zip 迭代器和一个转换运算符来做到这一点。程序编译但结果只是部分正确。这篇文章的灵感来自How to modify the contents of a zip iterator

的转变 A= [0 1 2;3 4 5;6 7 8; 1 1 1] 与 M=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] 使用 M*A 应导致: R=[28 34 40; 68 86 104; 108 138 168] 但是它给出: R=[28 34 40; 208 251 294; 2410 2905 3400]。

X 值正在正确修改。但是 Y 和 Z 值是错误的。

我的代码和cmakelists如下:

#include <thrust/iterator/zip_iterator.h>
#include <thrust/execution_policy.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>

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

typedef thrust::device_vector<float>::iterator                     FloatIterator;
typedef thrust::tuple<FloatIterator, FloatIterator, FloatIterator> FloatIteratorTuple;
typedef thrust::zip_iterator<FloatIteratorTuple>                   Float3Iterator;

typedef thrust::tuple<float,float,float> Float3;

struct modify_tuple
{
    Eigen::Matrix4f _Mat4f;
    modify_tuple(Eigen::Matrix4f Mat4f) : _Mat4f(Mat4f) { }
    __host__ __device__ Float3 operator()(Float3 a) const
    {

        Eigen::Vector4f V(thrust::get<0>(a), thrust::get<1>(a), thrust::get<2>(a), 1.0);

    V=_Mat4f*V;

    Float3  res=thrust::make_tuple( V(0,0), V(1,0), V(2,0) );

        return res;
    }
};


int main(void)
{
    thrust::device_vector<float> X(3);
    thrust::device_vector<float> Y(3);
    thrust::device_vector<float> Z(3);

    X[0]=0,    X[1]=1,    X[2]=2;
    Y[0]=4,    Y[1]=5,    Y[2]=6;
    Z[0]=7,    Z[1]=8,    Z[2]=9;

    std::cout << "X,Y,Z before transformation="<< std::endl;
    thrust::copy_n(X.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Y.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Z.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;


    Float3Iterator P_first = thrust::make_zip_iterator(make_tuple(X.begin(), Y.begin(), Z.begin()));
    Float3Iterator P_last  = thrust::make_zip_iterator(make_tuple(X.end(),   Y.end(),   Z.end()));


    Eigen::Matrix4f M;
    M(0,0)= 1; M(0,1)= 2;  M(0,2)= 3;  M(0,3)= 4; 
    M(1,0)= 5; M(1,1)= 6;  M(1,2)= 7;  M(1,3)= 8; 
    M(2,0)= 9; M(2,1)= 10; M(2,2)= 11; M(2,3)= 12; 
    M(3,0)= 13; M(3,1)= 14;  M(3,2)= 15;  M(3,3)= 16;

    thrust::transform(thrust::device, P_first,P_last, P_first, modify_tuple(M));

    std::cout << "X, Y, Z after transformation="<< std::endl;
    thrust::copy_n(X.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Y.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Z.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;


    return 0;
}

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

FIND_PACKAGE(CUDA REQUIRED)
INCLUDE_DIRECTORIES(${CUDA_INCLUDE_DIRS})
INCLUDE_DIRECTORIES (/usr/include/eigen3)

set(
    CUDA_NVCC_FLAGS
    ${CUDA_NVCC_FLAGS};
    -O3 -gencode arch=compute_52,code=sm_52;
    )

CUDA_ADD_EXECUTABLE(modify_zip_iterator_stackoverflow_ver2 modify_zip_iterator_stackoverflow_ver2.cu)
TARGET_LINK_LIBRARIES(modify_zip_iterator_stackoverflow_ver2 ${CUDA_LIBRARIES})

【问题讨论】:

  • 你对问题描述的向量和代码不匹配
  • 矩阵(4X4)*向量(4X1)=向量(4X1)。不清楚为什么它不起作用。

标签: cuda iterator zip tuples thrust


【解决方案1】:

可能你只需要获取最新的 Eigen。

我在 Fedora27 上使用了 CUDA 9.2,并从here 获取了最新的特征。

然后我编译并运行你的代码如下:

$ cat t21.cu
#include <thrust/iterator/zip_iterator.h>
#include <thrust/execution_policy.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>

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

typedef thrust::device_vector<float>::iterator                     FloatIterator;
typedef thrust::tuple<FloatIterator, FloatIterator, FloatIterator> FloatIteratorTuple;
typedef thrust::zip_iterator<FloatIteratorTuple>                   Float3Iterator;

typedef thrust::tuple<float,float,float> Float3;

struct modify_tuple
{
    Eigen::Matrix4f _Mat4f;
    modify_tuple(Eigen::Matrix4f Mat4f) : _Mat4f(Mat4f) { }
    __host__ __device__ Float3 operator()(Float3 a) const
    {

        Eigen::Vector4f V(thrust::get<0>(a), thrust::get<1>(a), thrust::get<2>(a), 1.0);

    V=_Mat4f*V;

    Float3  res=thrust::make_tuple( V(0,0), V(1,0), V(2,0) );

        return res;
    }
};


int main(void)
{
    thrust::device_vector<float> X(3);
    thrust::device_vector<float> Y(3);
    thrust::device_vector<float> Z(3);

    X[0]=0,    X[1]=1,    X[2]=2;
    Y[0]=4,    Y[1]=5,    Y[2]=6;
    Z[0]=7,    Z[1]=8,    Z[2]=9;

    std::cout << "X,Y,Z before transformation="<< std::endl;
    thrust::copy_n(X.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Y.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Z.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;


    Float3Iterator P_first = thrust::make_zip_iterator(make_tuple(X.begin(), Y.begin(), Z.begin()));
    Float3Iterator P_last  = thrust::make_zip_iterator(make_tuple(X.end(),   Y.end(),   Z.end()));


    Eigen::Matrix4f M;
    M(0,0)= 1; M(0,1)= 2;  M(0,2)= 3;  M(0,3)= 4;
    M(1,0)= 5; M(1,1)= 6;  M(1,2)= 7;  M(1,3)= 8;
    M(2,0)= 9; M(2,1)= 10; M(2,2)= 11; M(2,3)= 12;
    M(3,0)= 13; M(3,1)= 14;  M(3,2)= 15;  M(3,3)= 16;

    thrust::transform(thrust::device, P_first,P_last, P_first, modify_tuple(M));

    std::cout << "X, Y, Z after transformation="<< std::endl;
    thrust::copy_n(X.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Y.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Z.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;


    return 0;
}
$ nvcc -std=c++11 -I/path/to/eigen/eigen-eigen-71546f1a9f0c t21.cu -o t21 --expt-relaxed-constexpr
$ ./t21
X,Y,Z before transformation=
0,1,2,
4,5,6,
7,8,9,
X, Y, Z after transformation=
33,39,45,
81,99,117,
129,159,189,
$

输出与您对问题的期望不符,但您的期望也不正确。

解引用 zip 迭代器后提供给仿函数的第一个元组将是 (X[0],Y[0],Z[0]),即 (0,4,7)。然后您的仿函数将其转换为 (0,4,7,1) 并与您的 M 矩阵进行矩阵向量乘法。第一行内积由0*1+4*2+7*3+1*4给出,总和为33。第二行内积由0*5+4*6+7*7+1*8给出,总和为81。第三行内积由0*9+4*10+7*11+1*12给出,总和为129 . 你可以看到这个序列 33,81,129 正是上面输出的第一列。

解引用 zip 迭代器后提供给仿函数的第二个元组将是 (X[1],Y[1],Z[1]),即 (1,5,8)。然后您的仿函数将其转换为 (1,5,8,1) 并与您的 M 矩阵进行矩阵向量乘法。第一行内积由1*1+5*2+8*3+1*4 给出,总和为39。第二行内积由1*5+5*6+8*7+1*8 给出,总和为99。第三行内积由1*9+5*10+8*11+1*12 给出,总和为159。您可以看到这个序列 39,99,159 正是上面输出的第二列。

我没有对输出的第3列做相应的算术,但我认为没有错。

这是对您的代码的修改,证明结果的正确性,在 Eigen 主机代码中进行算术运算:

$ cat t21.cu
#include <thrust/iterator/zip_iterator.h>
#include <thrust/execution_policy.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>

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

typedef thrust::device_vector<float>::iterator                     FloatIterator;
typedef thrust::tuple<FloatIterator, FloatIterator, FloatIterator> FloatIteratorTuple;
typedef thrust::zip_iterator<FloatIteratorTuple>                   Float3Iterator;

typedef thrust::tuple<float,float,float> Float3;

struct modify_tuple
{
    Eigen::Matrix4f _Mat4f;
    modify_tuple(Eigen::Matrix4f Mat4f) : _Mat4f(Mat4f) { }
    __host__ __device__ Float3 operator()(Float3 a) const
    {

        Eigen::Vector4f V(thrust::get<0>(a), thrust::get<1>(a), thrust::get<2>(a), 1.0);

    V=_Mat4f*V;

    Float3  res=thrust::make_tuple( V(0,0), V(1,0), V(2,0) );

        return res;
    }
};


int main(void)
{
    thrust::device_vector<float> X(3);
    thrust::device_vector<float> Y(3);
    thrust::device_vector<float> Z(3);

    X[0]=0,    X[1]=1,    X[2]=2;
    Y[0]=4,    Y[1]=5,    Y[2]=6;
    Z[0]=7,    Z[1]=8,    Z[2]=9;
    std::cout << "X,Y,Z before transformation="<< std::endl;
    thrust::copy_n(X.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Y.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Z.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::host_vector<float> hX = X;
    thrust::host_vector<float> hY = Y;
    thrust::host_vector<float> hZ = Z;


    Float3Iterator P_first = thrust::make_zip_iterator(make_tuple(X.begin(), Y.begin(), Z.begin()));
    Float3Iterator P_last  = thrust::make_zip_iterator(make_tuple(X.end(),   Y.end(),   Z.end()));


    Eigen::Matrix4f M;
    M(0,0)= 1; M(0,1)= 2;  M(0,2)= 3;  M(0,3)= 4;
    M(1,0)= 5; M(1,1)= 6;  M(1,2)= 7;  M(1,3)= 8;
    M(2,0)= 9; M(2,1)= 10; M(2,2)= 11; M(2,3)= 12;
    M(3,0)= 13; M(3,1)= 14;  M(3,2)= 15;  M(3,3)= 16;

    thrust::transform(thrust::device, P_first,P_last, P_first, modify_tuple(M));

    std::cout << "X, Y, Z after transformation="<< std::endl;
    thrust::copy_n(X.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Y.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    thrust::copy_n(Z.begin(), 3, std::ostream_iterator<float>(std::cout, ","));
    std::cout << std::endl;
    Eigen::Vector4f hV;
    hV(0) = hX[0];
    hV(1) = hY[0];
    hV(2) = hZ[0];
    hV(3) = 1;
    hV = M*hV;
    std::cout << "column 0:" << std::endl;
    std::cout << hV;
    std::cout << std::endl;
    hV(0) = hX[1];
    hV(1) = hY[1];
    hV(2) = hZ[1];
    hV(3) = 1;
    hV = M*hV;
    std::cout << "column 1:" << std::endl;
    std::cout << hV;
    std::cout << std::endl;
    hV(0) = hX[2];
    hV(1) = hY[2];
    hV(2) = hZ[2];
    hV(3) = 1;
    hV = M*hV;
    std::cout << "column 2:" << std::endl;
    std::cout << hV;
    std::cout << std::endl;

    return 0;
}
$ nvcc -std=c++11 -I/home/bob/eigen/eigen-eigen-71546f1a9f0c t21.cu -o t21 --expt-relaxed-constexpr
$ ./t21
X,Y,Z before transformation=
0,1,2,
4,5,6,
7,8,9,
X, Y, Z after transformation=
33,39,45,
81,99,117,
129,159,189,
column 0:
 33
 81
129
177
column 1:
 39
 99
159
219
column 2:
 45
117
189
261
$

【讨论】:

  • 这确实是Eigen的问题,当我使用最新版本时消失了。
猜你喜欢
  • 2013-03-08
  • 2014-08-05
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多