[英]Using a class member of type 'array'
I know this is a simple question, but I have not been able to find the answer. 我知道这是一个简单的问题,但我找不到答案。
I want a C++ class that manages a large block of memory, where the memory is regularly processed in the GPU when a certain class method is called. 我想要一个管理大块内存的C ++类,当调用某个类方法时,GPU中会定期处理内存。 The class constructor is passed the size of the array, and after construction the array size never changes.
类构造函数传递给数组的大小,构造后数组大小永远不会改变。 The method that does the parallel_for_each should not waste processor cycles or memory when its not necessary.
执行parallel_for_each的方法在不必要时不应浪费处理器周期或内存。
How do I do this? 我该怎么做呢?
I can't create a concurrency::array as a class member, because I need to know how big the array will be before it is created. 我不能创建一个concurrency :: array作为类成员,因为我需要知道数组在创建之前有多大。 I can't have a member that is a pointer to a concurrency::array (and then allocate it with 'new' in, for example, the constructor), because I can't figure out how to specify it to the parallel_for_each.
我不能有一个指向concurrency :: array的指针成员(然后用'new'分配它,例如构造函数),因为我无法弄清楚如何将它指定给parallel_for_each。
On a side note, I don't normally need to copy the array between the GPU and host, but its fine if for some reason I have to do that, as long as its not done regularly. 在旁注中,我通常不需要在GPU和主机之间复制阵列,但是如果出于某种原因我必须这样做,只要它没有定期完成。 Otherwise it would waste processor cycles and memory according to the size of the array.
否则,它会根据阵列的大小浪费处理器周期和内存。
Here's an example of something like what I want. 这是一个像我想要的东西的例子。 Of course, the reference/pointer captured by the parallel_for_each is wrong.
当然,parallel_for_each捕获的引用/指针是错误的。 (This is not checked for syntax):
(不检查语法):
class MyClass
{
int* myHostArrayPtr;
concurrency::array<int,1>* myGpuArrayPtr;
MyClass(int size)
{
myHostArrayPtr = new int(size);
memset(myHostArrayPtr,0,size * sizeof(int));
myGpuArrayPtr = new concurrency::array<int,1>(size,myHostArrayPtr);
}
void ProcessInGpu()
{
parallel_for_each(
myGpuArrayPtr->extent,
[&myGpuArrayPtr](index<1> i) restrict(amp)
{
myGpuArray[i]+=14;
}
);
}
};
I think, you need templates here: 我想,你需要这里的模板:
template <std::size_t N> class MyClass {
concurrency::array<int,N> myGpuArray;
...
}
int main () {
MyClass<10> someName;
...
}
OK, I think I figured it out. 好吧,我想我弄明白了。 One must put the parallel_for_each in a function that takes references to the array objects, and then they can be passed by reference to the parallel_for_each.
必须将parallel_for_each放在一个接受数组对象引用的函数中,然后可以通过引用传递parallel_for_each来传递它们。 To wit:
以机智:
void MyClass::Process(concurrency::array<int,1>& myGpuArray){
parallel_for_each(
myGpuArray.extent,
[&myGpuArray](index<1> i) restrict(amp)
{
myGpuArray[i]+=14;
}
);
}
This is interesting because its really a work around to a C++ shortcoming, that you can't refer to a pointed-to variable as a reference without the above function call work-around (I think?). 这很有趣,因为它真的是一个解决C ++缺点的工作,如果没有上面的函数调用解决方法,你不能将指向变量引用作为引用(我认为?)。 (That is, and not call the copy-constructor).
(也就是说,不要调用copy-constructor)。
EDIT: 编辑:
Yep, the above works. 是的,上面的工作。 I benchmarked it and its just as fast as the code that uses a local array.
我对它进行了基准测试,其速度与使用本地数组的代码一样快。 Also, I tested it by converting a pointer to a reference in the call, and that worked, too.
此外,我通过将指针转换为调用中的引用来测试它,这也是有效的。 So it will work with dynamically allocated arrays.
因此它将适用于动态分配的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.