【问题标题】:Trouble using thrust complex(double) vector with sinus function使用带正弦函数的推力复数(双)向量时遇到问题
【发布时间】:2016-09-15 11:23:22
【问题描述】:

我在设备和主机上的 Thrust 复数双向量上使用 sin 函数时遇到了一些问题:似乎计算是在浮点数中完成的。

使用thrust::device_vector< thrust::complex<double> >thrust::host_vector< thrust::complex<double> >,我得到:

sin( 1+0i ) == (0.8414709568023682,0)

std::complex<double>:

sin( 1+0i ) == (0.8414709848078965,0)

std::complex<float>

sin( 1+0i ) == (0.8414709568023682,0)

我在代码中犯了什么错误?在我使用的编译过程中

nvcc test.cu -o test

这里是完整的代码:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/complex.h>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>

template <typename Vector> 
void Print(Vector &V){
  for (int i=0;i<V.size();i++)
    std::cout <<  V[i] << "  ";
  std::cout << "\n";
}

template <typename T>
struct sin_functor : public thrust::unary_function< T , T >
{
  __host__ __device__
  T operator()( T x) const
  {
    return sin( x );
  }
};

template <typename Vector> 
void ThrustComputation(){
  typedef typename Vector::value_type Tvec;
  Vector A(2);
  A[0]=Tvec(1.,0.);A[1]=Tvec(1.,1.);

  std::cout << "A: " << std::endl;
  std::cout << "  ";Print<Vector>(A);

  Vector B(A.size());

  thrust::transform(A.begin(),A.end(),B.begin(), sin_functor<Tvec>());
  std::cout << "B =sin(A): " << std::endl;
  std::cout << "  ";Print<Vector>(B);
}

template <typename T> 
void stdComputation(){
  std::complex<T> sA[2];
  sA[0]=std::complex<T>(1.,0.);
  sA[1]=std::complex<T>(1.,1.);

  std::cout << "sA: " << std::endl;
  std::cout << "  " << sA[0] << "  " << sA[1] << std::endl;
  std::cout << "sin(sA): " << std::endl;
  std::cout << "  " << sin(sA[0]) << "  " << sin(sA[1]) << std::endl;
}


int main(int argc, char **argv)
{  
  std::cout << std::setprecision(16); 
  std::cout << "Thrust: Computation on GPU device (double)\n";
  ThrustComputation<thrust::device_vector< thrust::complex<double> > >();
  std::cout << "Thrust: Computation on host (double)\n";
  ThrustComputation<thrust::host_vector< thrust::complex<double> > >();
  std::cout << "std: Computation (double)\n";
  stdComputation<double>();
  std::cout << "std: Computation (float)\n";
  stdComputation<float>();
  return 0;
}

我的电脑(Ubuntu 14.04 LTS,cuda 7.5)上的输出是:

Thrust: Computation on GPU device (double)
A: 
  (1,0)  (1,1)  
B =sin(A): 
  (0.8414709568023682,0)  (1.298457622528076,0.6349639296531677)  
Thrust: Computation on host (double)
A: 
  (1,0)  (1,1)  
B =sin(A): 
  (0.8414709568023682,0)  (1.298457622528076,0.6349639296531677)  
std: Computation (double)
sA: 
  (1,0)  (1,1)
sin(sA): 
  (0.8414709848078965,0)  (1.298457581415977,0.6349639147847361)
std: Computation (float)
sA: 
  (1,0)  (1,1)
sin(sA): 
  (0.8414709568023682,0)  (1.298457503318787,0.6349638700485229)

【问题讨论】:

  • 您的实际问题是什么?类似“为什么std::complexthrust::complex 产生的输出不完全相同?
  • 对我来说,使用推力::device_vector > sin(1+0i) 的计算是错误的:精确解的错误是 1e-8!跨度>
  • 你可以在这里提出错误报告:github.com/thrust/thrust/issues/new

标签: cuda thrust


【解决方案1】:

这似乎是推力库中的一个真正的错误。快速扫描 github 上的代码,我找到了this,这可能是罪魁祸首。似乎复杂的sin 所依赖的推力的双精度csinh 函数有一个意外的中间转换为浮动,这可能导致您观察到的精度损失。按照 cmets 中的建议,您应该将此报告为错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多