# Memory copy

**URL:** <https://forums.developer.nvidia.com/t/memory-copy/309506>\
**Category:** CUDA Programming and Performance\
**Created:** [October 11, 2024, 12:55pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506 "2024-10-11T12:55:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cguerrabr](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cguerrabr](https://forums.developer.nvidia.com/u/cguerrabr)\
**Post date:** [October 11, 2024, 12:55pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/1 "2024-10-11T12:55:56Z")

</div>

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)”

```auto
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.

---

<div class="post-metadata">

**Author:** ![striker159](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@striker159](https://forums.developer.nvidia.com/u/striker159)\
**Post date:** [October 11, 2024, 1:43pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/2 "2024-10-11T13:43:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cguerrabr](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cguerrabr](https://forums.developer.nvidia.com/u/cguerrabr)\
**Post date:** [October 11, 2024, 3:48pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/3 "2024-10-11T15:48:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cguerrabr](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cguerrabr](https://forums.developer.nvidia.com/u/cguerrabr)\
**Post date:** [October 11, 2024, 3:54pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/4 "2024-10-11T15:54:04Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![striker159](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@striker159](https://forums.developer.nvidia.com/u/striker159)\
**Post date:** [October 11, 2024, 4:04pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/5 "2024-10-11T16:04:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cguerrabr](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cguerrabr](https://forums.developer.nvidia.com/u/cguerrabr)\
**Post date:** [October 11, 2024, 4:12pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/6 "2024-10-11T16:12:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [October 12, 2024, 6:25pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/7 "2024-10-12T18:25:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cguerrabr](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cguerrabr](https://forums.developer.nvidia.com/u/cguerrabr)\
**Post date:** [October 14, 2024, 4:48pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/8 "2024-10-14T16:48:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [October 28, 2024, 4:48pm UTC](https://forums.developer.nvidia.com/t/memory-copy/309506/9 "2024-10-28T16:48:42Z")

</div>

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