我有很多在CUDA项目中的类,它们大多是华丽的struct
,并通过组合依赖于彼此:
class A {
public:
typedef boost::shared_ptr<A> Ptr;
A(uint n_elements) { ... // allocate element_indices };
DeviceVector<int>::iterator get_element_indices();
private:
DeviceVector<int> element_indices;
}
class B {
public:
B(uint n_elements) {
... // initialize members
};
A::Ptr get_a();
DevicePointer<int>::iterator get_other_stuff();
private:
A::Ptr a;
DeviceVector<int> other_stuff;
}
DeviceVector
是thrust::device_vector
的一个包装器,::iterator
可以转换为原始设备指针。这是必需的,因为需要调用自定义内核并需要对设备内存进行处理。
我很关心封装性,但是:
- 必须公开数据的原始指针,以便使用
A
和B
的类可以在GPU上运行自定义内核 - 不需要默认构造函数,应自动分配设备内存-->
shared_ptr<T>
- 只需要在
A
和B
上实现非常少的方法。
因此,通过使用结构体,可以使生活变得更加简单。
struct A {
void initialize(uint n_elements);
DeviceVector<int> element_indices;
}
struct B {
void initialize(uint n_elements);
A a;
DeviceVector<int> other_stuff;
}
我想知道在封装的意义上,这是否实际上是等效的。如果是,整个概念是否存在任何问题,并可能在某些时候产生影响?
struct
是正确的。 - Drew Dormanninitialize
让我更明显地看到用户必须在创建A
或B
对象后做一些事情。 - bbtrb