简体   繁体   English

指向设备对象成员的指针

[英]Pointer to member of device object

I know, that I should not dereference pointer to device.我知道,我不应该取消引用指向设备的指针。 But is the following an dereferencation?但以下是取消引用吗?

struct example{
 int a;
 float b;
};

void test(){
 example* p_e; 
 cudamalloc(&p_e,sizeof(example));
 float* db = &p_e->b; // is this line valid?
}

It works on my computer, but is this valid?它在我的电脑上工作,但这有效吗?

edit:编辑:

With it works, I meant, that it holds in my testrun that:有了它,我的意思是,它在我的测试运行中保持:

reinterpret_cast<void*>(&p_e->b) == reinterpret_cast<void*>(reinterpret_cast<unsigned char*>(p_e)+sizeof(int))

Regarding this:关于这一点:

float db = &p_e->b;

I can't imagine a situation where the address of an entity in C or C++ is a float value.我无法想象 C 或 C++ 中实体的地址是float值的情况。 So from a language perspective, it makes no sense.所以从语言的角度来看,这是没有意义的。 If we modify that to:如果我们将其修改为:

float *db = &p_e->b;

That is a sensible construct, although whether it is "useful" depends on how you intend to use it.这是一个明智的构造,尽管它是否“有用”取决于您打算如何使用它。 Taking the address of the location p_e->b requires taking the value of the pointer contained in p_e (a location in host memory, so no device dereferencing going on there) and adding to it the offset to the location b within the object.获取位置p_e->b的地址需要获取包含在p_e中的指针的值(主机内存中的一个位置,因此不会在那里进行设备解引用)并将对象内位置b的偏移量添加到该值中。 That offset doesn't involve any dereferencing.该偏移量不涉及任何取消引用。 It can be computed simply by inspection of the class/structure definition.它可以简单地通过检查类/结构定义来计算。 So none of that requires any dereferencing of pointers.所以这些都不需要对指针进行任何取消引用。

The pointer that is produced ( db ) is now pointing to a location in device memory, so that pointer should only be used (dereferenced) in device code.生成的指针 ( db ) 现在指向设备内存中的一个位置,因此该指针只能在设备代码中使用(取消引用)。 But you could pass that pointer by value to a CUDA kernel and make "use" of it, sensibly.但是您可以将该指针按值传递给 CUDA 内核并明智地“使用”它。

If you actually intended to populate the value db with the float contents pointed to by the pointer p_e->b , first of all your code does not do that, and second it would not generally be possible in host code to directly retrieve that data from the device, when the underlying base structure pointer ( b_e ) is allocated using cudaMalloc .如果您实际上打算用指针p_e->b指向的float内容填充值db ,首先您的代码不会这样做,其次在主机代码中通常不可能直接从设备,当使用cudaMalloc分配基础结构指针( b_e )时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM