Initializing C++ AMP arrays to the same value
Hi, I am Pavan Marella, an engineer on the C++ AMP team, and in my first post here I wanted to share with you a utility function that I often use.
C++ AMP has new basic types which can be used for data transfer between CPU and GPU. concurrency::array is one such data type. One way of ‘array’ object creation is by using the constructor which takes an extent object as a parameter, e.g.
extent<3> dataSize(10,1024,1024);
array<int,3> arrObj(dataSize);
The above way of ‘arrObj’ creation just creates an array object but does not initialize the content to default values. Also we cannot take for granted that the default values of the data in the newly constructed ‘arrObj’ shall be 0. Initialization of data in array object homogeneously to ‘0’ or other default value is commonly observed in samples in which the present computation depends on the previous computation values or the values in neighborhood.
Though concurrency::array has a constructor that takes an extent as well as a vector for data initialization (as shown below), I don’t like creating a vector on the CPU memory and then copying to the GPU via the array constructor just for the sake of initialization.
//Vector created just for data initialization
vector<int> intVector(10*1024*1024,0);
extent<3> dataSize(10,1024,1024);
array<int,3> arr(dataSize,initVector.begin());
Hence I have come up with a utility function that I use and am sharing with you here:
template<typename T,int Rank>
void Fill(array<T,Rank>& arr,T initValue) {
parallel_for_each(arr.extent,[&arr,initValue](index<Rank> idx)restrict(amp)
{
arr[idx] = initValue;
});
}
// Using of Fill function
extent<3> dataSize(10,1024,1024);
array<int,3> arrObj(dataSize);
Fill<int,3>(arrObj,1); // Initialising all the data in the array to '1'.
I use this approach because it avoids explicit creation of vectors/arrays on cpu, just for the sake of initialization.
The solution can be extended to other basic datatypes of concurrency like array_view etc. If you have feedback on this utility function or have others to suggest, please let me know. My next blog post will cover a sample that uses the code snippet mentioned in this blog post.
Comments
Anonymous
November 18, 2011
Has the capitalisation of the 'Concurrency' namespace changed in VS11? I've seen it written as 'concurrency' quite a lot in these blog posts.Anonymous
November 19, 2011
Yes, the lowercase concurrency namespace makes things more consistent. However the classes and typdefs are still declared in the Concurrency namespace to ensure existing code and typing habits aren't broken. The statement "namespace concurrency = Concurrency;" has been added to the end of the public header files to accomplish this.