cuda,pycuda - 如何编写复数 - 错误:class“cuComplex”没有成员“i” [英] cuda, pycuda -- how to write complex numbers -- errors:class "cuComplex" has no member "i"

查看:1346
本文介绍了cuda,pycuda - 如何编写复数 - 错误:class“cuComplex”没有成员“i”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难在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,..)
...
pre>

如何将它转换为使用pycuda?
我尝试sth这样(根据书cuda的例子):

  struct cuComplex {
float real;
float imag;
cuComplex(float a,float b):real(a),imag(b){}
cuComplex operator *(const cuComplex& a){
return cuComplex -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。); //而不是cmplx j(0.,1。);

__global__ void compute(float * Rs,cuComplex * M,..)//而不是RealFieldType const& Rs,ComplexFieldType const&我采取的一些错误是:



不允许使用数据成员初始化器



此声明没有存储类或类型说明符


谢谢!



-------------------- -EDIT - ----- ----------------------------------------



我使用 #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



到现在为止,在全局函数中,
i只尝试了float * M或cmplx * M我收到错误:


变量cmplx不是类型名称


如果我使用pycuda :: complex cmplx; ,然后我得到:



名称后跟by::必须是类或命名空间名称


另外:



< blockquote>


表达式必须具有指针到对象类型(但可能来自代码的另一部分)




解决方案

这真的不清楚你实际上是做什么(如果你真的知道自己)问题越来越混乱,因为编辑和评论滚动。但是为了扩展Andreas的答案,下面是一个简单的,可编译的CUDA代码,它使用了pycuda原生复杂类型:

  #include< pycuda-complex.hpp> 

template< typename T>
__global__ void kernel(const T * x,const T * y,T * z)
{
int tid = threadIdx.x + blockDim.x * blockIdx.x;

z [tid] = x [tid] + y [tid];
}


typedef pycuda :: complex< float> scmplx;
typedef pycuda :: complex< double> dcmplx;

template void kernel< float>(const float *,const float *,float *);
template void kernel< double>(const double *,const double *,double *);
template void kernel< scmplx>(const scmplx *,const scmplx *,scmplx *);
template void kernel< dcmplx>(const dcmplx *,const dcmplx *,dcmplx *);

这给出了单和双实型和复杂版本的琐碎内核,并编译与nvcc这样:

  $ nvcc -arch = sm_20 -Xptxas = -  v-I $ HOME / pycuda-2011.1.2 / src / cuda -c scmplx.cu 
ptxas信息:为'sm_20'编译条目函数'_Z6kernelIN6pycuda7complexIdEEEvPKT_S5_PS3_'
ptxas信息:使用12个寄存器,44字节cmem [0],168字节cmem [2]字节cmem [16]
ptxas信息:为'sm_20'编译条目函数'_Z6kernelIN6pycuda7complexIfEEEvPKT_S5_PS3_'
ptxas信息:使用8个寄存器,44字节cmem [0],168字节cmem [2]
ptxas info:为'sm_20'编译入口函数'_Z6kernelIdEvPKT_S2_PS0_'
ptxas info:使用8个寄存器,44个字节cmem [0],168个字节cmem [2]
ptxas info:编译入口函数'_Z6kernelIfEvPKT_S2_PS0_' for'sm_20'
ptxas info:使用4个寄存器,44个字节cmem [0],168个字节cmem [2]

也许这是回答你的问题....


I have difficulties to use complex numbers in cuda,pycuda.

I have this in C:

#include <complex>
typedef std::complex<double> cmplx;
....
cmplx j(0.,1.);   

Also,in the same code:

#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,..)
...

How can i convert this to use it with pycuda? I tried sth like this (according to the book '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
....

Some of the errors i take are:

data member initializer is not allowed

this declaration has no storage class or type specifier

Thank you!

---------------------EDIT----------------------------------------------

I did the following using #include <pycuda-complex.hpp> (relative to the above) :

pycuda::complex<float> cmplx;

cmplx j(0.,1.);

and as for typedef std::vector< boost::array<std::complex<double>,3 > > ComplexFieldType;

and ComplexFieldType const & M ,inside the global function, i tried just "float *M " or "cmplx *M".

Until now , i am getting error :

variable "cmplx" is not a type name

If i use pycuda::complex cmplx; ,then i get:

identifier "cmplx" is undefined

name followed by "::" must be a class or namespace name

Also:

expression must have pointer-to-object type (but maybe this is from another part of code)

解决方案

It really isn't clear what you are actually trying to do (if you actually know yourself), and the question is getting progressively more confused as the edits and comments roll on. But to expand Andreas's answer a little, here is a simple, compilable piece of CUDA code which uses the pycuda native complex type correctly:

#include <pycuda-complex.hpp>

template<typename T>
__global__ void kernel(const T * x, const T *y, T *z)
{
    int tid = threadIdx.x + blockDim.x * blockIdx.x;

    z[tid] = x[tid] + y[tid];
}


typedef pycuda::complex<float> scmplx;
typedef pycuda::complex<double> dcmplx;

template void kernel<float>(const float *, const float *, float *);
template void kernel<double>(const double *, const double *, double *);
template void kernel<scmplx>(const scmplx *, const scmplx *, scmplx *);
template void kernel<dcmplx>(const dcmplx *, const dcmplx *, dcmplx *);

This gives you single and double real and complex versions of the trivial kernel and compiles with nvcc something like this:

$ nvcc -arch=sm_20 -Xptxas="-v" -I$HOME/pycuda-2011.1.2/src/cuda -c scmplx.cu 
ptxas info    : Compiling entry function '_Z6kernelIN6pycuda7complexIdEEEvPKT_S5_PS3_' for 'sm_20'
ptxas info    : Used 12 registers, 44 bytes cmem[0], 168 bytes cmem[2], 4 bytes cmem[16]
ptxas info    : Compiling entry function '_Z6kernelIN6pycuda7complexIfEEEvPKT_S5_PS3_' for 'sm_20'
ptxas info    : Used 8 registers, 44 bytes cmem[0], 168 bytes cmem[2]
ptxas info    : Compiling entry function '_Z6kernelIdEvPKT_S2_PS0_' for 'sm_20'
ptxas info    : Used 8 registers, 44 bytes cmem[0], 168 bytes cmem[2]
ptxas info    : Compiling entry function '_Z6kernelIfEvPKT_S2_PS0_' for 'sm_20'
ptxas info    : Used 4 registers, 44 bytes cmem[0], 168 bytes cmem[2]

Perhaps this goes someway to answering your question....

这篇关于cuda,pycuda - 如何编写复数 - 错误:class“cuComplex”没有成员“i”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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