Memory copy

I want to make a simple copy from the first half of a vector in the device to the second one. I tried with the sample code below, but I’m getting “Caught signal 11 (Segmentation fault: address not mapped to object at address)”

size_t offset = half_field_size;
cufftComplex *d_field_a = &d_field[0]; // d_field has size 2*half_field_size
cufftComplex *d_field_b = &d_field[offset];
CHECK(cudaStreamSynchronize(ctx.st));
CHECK(cudaMemcpyAsync(d_field_b, d_field_a, half_field_size, cudaMemcpyDeviceToDevice, ctx.st));

Any suggestion? Thanks.

I am assuming that d_field is defined as cufftComplex* d_field;

If d_field is allocated in device memory, d_field[0] and d_field[offset] cannot be accessed from the host.
You could use &d_field[offset] or d_field + offset to get the pointer to the respective element.

Sorry, in order to make the sample code ‘leaner’, I forgot to place the & in the snippet, the problematic code have the & like you described. I will fix the post.

Here comes the correct leaner code:

size_t offset = env->size_x * env->size_y;
cufftComplex *d_field_a = &d_field[0]; // d_field has size 2 * env->size_x * env->size_y * sizeof(cufftComplex)
cufftComplex *d_field_b = &d_field[offset];
CHECK(cudaStreamSynchronize(ctx.st));
CHECK(cudaMemcpyAsync(d_field_b, d_field_a, half_field_size, cudaMemcpyDeviceToDevice, ctx.st));

Where half_field_size is env->size_x * env->size_y * sizeof(cufftComplex)

Using half_field_size as both an offset in terms of elements and as a number of bytes to transfer is probably wrong.
If you need more help to find your issue, you need to provide a complete minimal example which can be compiled and executed.

My bad, again in not being precise.
I edited the last sample code.
Thanks.

Can you include the cudaMalloc at least and compile and run a working version which copies the whole array?

The problem was related to not declaring a parameter of a subroutine in the interface.
Thank you all for the help.
Best.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.