我很难在 cuda、pycuda 中使用复数。
我在 C 中有这个:
#include <complex>
typedef std::complex<double> cmplx;
....
cmplx j(0.,1.);
此外,在相同的代码中:
#include <boost/python.hpp>
#include <boost/array.hpp>
...
typedef std::vector< boost::array<std::complex<double>,3 > > ComplexFieldType;
typedef std::vector< boost::array<double,3> > RealFieldType;
...
__global__ void compute(RealFieldType const & Rs,ComplexFieldType const & M,..)
...
如何将其转换为与 pycuda 一起使用?我尝试过这样的事情(根据'cuda by an example'一书):
struct cuComplex {
float real;
float imag;
cuComplex(float a,float b): real(a),imag(b){}
cuComplex operator *(const cuComplex& a) {
return cuComplex(real*a.real -imag*a.imag ,imag*a.real +real*a.imag);
}
cuComplex operator +(const cuComplex& a) {
return cuComplex(real+a.real ,imag+a.imag);
};
cuComplex j(0.,1.); //instead of cmplx j(0.,1.);
__global__ void compute(float *Rs,cuComplex * M,..) //instead of RealFieldType const & Rs,ComplexFieldType const & M
....
我犯的一些错误是:
不允许使用数据成员初始化程序
此声明没有存储类或类型说明符
谢谢!
----------------- - 编辑- ------------- ------------------
我使用#include <pycuda-complex.hpp>
(相对于上述)做了以下操作:
pycuda::complex<float> cmplx;
cmplx j(0.,1.);
至于typedef std::vector< boost::array<std::complex<double>,3 > > ComplexFieldType;
并且ComplexFieldType const & M
,在全局函数中,我尝试了“float *M”或“cmplx *M”。
直到现在,我收到错误:
变量“cmplx”不是类型名称
如果我使用 pycuda::complex cmplx; ,然后我得到:
标识符“cmplx”未定义
后跟“::”的名称必须是类或命名空间名称
还:
表达式必须具有指向对象的类型(但也许这是来自代码的另一部分)