bigh_29
bigh_29

Reputation: 2643

Filling a 2D AMP array row by row

Say I have a source of CPU side data that looks like this:

vector< vector<int> > cpuBlobOData;

Assume every row vector inside has the same size (a jagged 2d vector that isn't jagged).

I want to copy this to single block of GPU memory, inside an array. Is there a way to do this row by row? Something like this:

array<int, 2> gpuArray(cpuBlobOData.size(), numColumns);
for(size_t i=0; i<cpuBlobOData.size(); ++i)
{
    auto cpuRow = cpuBlobOData[i];
    concurrency::copy(cpuRow.begin(), cpuRow.end(), &gpuArray[i]);
}

I know this doesn't compile, it shows what I am trying to do. Is there something that accomplishes this? The only solution I could find was to copy the cpu vector into a 1D vector, and map that to an array_view, or copy it into an array.

That works, but it is wasteful, and for some algorithms, the continuous 1D vector may not fit into the remaining memory space.

Any advice is welcome, as I am struggling to figure out AMP.

Upvotes: 1

Views: 181

Answers (1)

Szymon Wybranski
Szymon Wybranski

Reputation: 646

Yes, you can copy it row by row. Your solution is almost there, just remove '&' from the gpuArray in the line with concurrency::copy:

array<int, 2> gpuArray(cpuBlobOData.size(), numColumns);
for(size_t i=0; i<cpuBlobOData.size(); ++i)
{
    auto cpuRow = cpuBlobOData[i];
    concurrency::copy(cpuRow.begin(), cpuRow.end(), gpuArray[i]);
}

What happens is that array<int,2> accessed with subscript operator returns an array_view<int,1> that represents a row 'i', this is called "projection". For more details please check out a blog post that covers C++ AMP projections in details.

Upvotes: 2

Related Questions