Reputation: 2724
I have to extract sections of an array and set the chunk to another array.
For instance, I have a 2d array (in 1d format) like A[32 X 32]; there is another array B[64 X 64] and I would want to copy an 8X8 chunk of B, starting from (0,8) of B and place it in (8,8) of A.
At present, I would probably use something like the kernel below, for getting a portion of data when offsets are passed. A similar one could also be used to setting chunks to a larger array.
__global__ void get_chunk (double *data, double *sub, int xstart, int ystart, int rows, int cols, int subset)
{
int i,j;
i = blockIdx.x * blockDim.x + threadIdx.x;
for (j = 0; j < subset; j++)
sub[i*subset+j] = data[i*cols + (xstart*cols + ystart)+j];
}
I think the same could be done using a variant of cudamemCpy* (perhaps cudamemCpyArray(...)), but I am not sure how to do it. I need some code samples, or some directions on how it could be done.
PS I had the exact same question in nvidia forums, got no reply so trying here. http://forums.nvidia.com/index.php?showtopic=223386
Thank you.
Upvotes: 1
Views: 3595
Reputation: 5554
There is no need for a kernel if you just want to copy data from one array to another on the device.
If you have your device pointers with your source data and your allocated target pointer in host code:
Pseudocode:
//source and target device pointers
double * source_d, target_d;
//get offseted source pointer
double * offTarget_d + offset * sizeof(double);
//copy n elements from offseted source data to target device pointer
cudaMemcpy(offTarget_d, source_d, n * sizeof(double), cudaMemcpyDeviceToDevice);
It was not clear if you just want to copy a range of a 1D array or if you want to copy a range of each row in a 2D array into the target row of another 2D array
Upvotes: 3