简体   繁体   English

使用类型为'array'的类成员

[英]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.

相关问题 使用派生类的初始化列表初始化超类中的类型数组的成员 - Initializing a member of type array in a superclass, using the initialization list of a derived class 使用模板化类的成员类型作为类成员变量的类型 - Using a member type of templated class as the type of a class member variable 在模板中使用第一个类型的类成员的类型 - Using the type of a class member of the first type in a template 将接口类用作另一个类中的成员类型 - Using an interface class as member type in another class 在类模板的成员函数中使用不完整类型 - Using incomplete type in a member function of a class template 使用类成员函数对象推导成员函数返回类型 - Deduce member function return type using a class member function object 为什么成员函数中的char数组类型的类成员突然变成const char数组类型? - Why is a class member of type char array suddenly of type const char array in member function? 使用成员函数对类对象数组进行排序 - Sorting array of class objects using member function 使用A类类型的shared_ptr作为B类的成员变量 - using shared_ptr of a type of Class A as a member variable of class B 将模板类型的二维数组复制到类成员 - Copying a template type 2D array to a class member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM