Parallel Programming in Native Code

Parallel programming using C++ AMP, PPL and Agents libraries.

Initializing C++ AMP arrays to the same value

Initializing C++ AMP arrays to the same value

  • Comments 2

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.

Blog - Comment List MSDN TechNet
  • Loading...
Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post