[英]accessing private members of a class in cuda kernel
我創建了一個類並將其對象傳遞給cuda內核。
內核的代碼是:
__global__ void kernel(pt *p,int n)
{
int id=blockDim.x*blockIdx.x+threadIdx.x;
if(id<n)
{
p[id]=p[id]*p[id];
}}
並且它給出以下錯誤: error: 'int pt::a' is private
問題是:我如何訪問班級的私人成員?
如果沒有私人會員,該計划可以正常運行
class pt{
int a,b;
public:
pt(){}
pt(int x,int y)
{
a=x;
b=y;
}
friend ostream& operator<<(ostream &out,pt p)
{
out<<"("<<p.a<<","<<p.b<<")\n";
return out;
}
int get_a()
{
return this->a;
}
int get_b()
{
return this->b;
}
__host__ __device__ pt operator*(pt p)
{
pt temp;
temp.a=a*p.a;
temp.b=b*p.b;
return temp;
}
pt operator[](pt p)
{
pt temp;
temp.a=p.a;
temp.b=p.b;
return temp;
}
void set_a(int p)
{
a=p;
}
void set_b(int p)
{
b=p;
}};
一個類的私有成員只能由其成員函數及其朋友訪問。
您的C ++代碼中存在一些錯誤。
這在我的機器上編譯(CUDA 4.0 Mac Osx)
#include <iostream>
class pt {
int a,b;
public:
__host__ __device__ pt(){}
__host__ __device__ pt(int x,int y) : a(x), b(y)
{
}
int get_a()
{
return this->a;
}
int get_b()
{
return this->b;
}
__host__ __device__ pt operator*(pt p)
{
pt temp;
temp.a=a*p.a;
temp.b=b*p.b;
return temp;
}
pt operator[](pt p)
{
pt temp;
temp.a=p.a;
temp.b=p.b;
return temp;
}
void set_a(int p)
{
a=p;
}
void set_b(int p)
{
b=p;
}
friend std::ostream& operator<<(std::ostream &out,pt p);
};
std::ostream& operator<<( std::ostream &out,pt p)
{
out<<"("<<p.a<<","<<p.b<<")\n";
return out;
}
__global__ void kernel(pt *p,int n)
{
int id=blockDim.x*blockIdx.x+threadIdx.x;
if(id<n)
{
p[id]=p[id]*p[id];
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.