# cudaMemcpy Strange behaviour

**URL:** <https://forums.developer.nvidia.com/t/cudamemcpy-strange-behaviour/15986>\
**Category:** CUDA Programming and Performance\
**Created:** [April 8, 2010, 7:37am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-strange-behaviour/15986 "2010-04-08T07:37:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kng](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kng](https://forums.developer.nvidia.com/u/kng)\
**Post date:** [April 8, 2010, 7:37am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-strange-behaviour/15986/1 "2010-04-08T07:37:03Z")

</div>

so guys i am using cudaMemcpy in a loop just to test what is going on inside the kernel and i am getting very strange results

the first time i call cudaMemcpy the result is correct the second and all iteration afterwards seem to give corrupted values

```auto
gpuAssert( cudaMemcpy(temp, device_largestElements, sizeof(float), cudaMemcpyDeviceToHost) );

```

where temp is an array of float containing just one element

and device\_largestElements is an array which contains 22332 floats

and i only wish to get the first element of the array is it possible the cudaMemcpy is bugging due to the array size differences?

(btw if i remove the cudaMemcpy from outside the loop and just call it once after the loop then the result is correct)

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [April 8, 2010, 8:38am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-strange-behaviour/15986/2 "2010-04-08T08:38:34Z")

</div>

> [@](#):
>
> and i only wish to get the first element of the array is it possible the cudaMemcpy is bugging due to the array size differences?

Almost certainly not. In most of my linear algebra codes, I run my own GPU memory manager which looks after a gigantic chunk of pre-allocated device memory for the life of the application. My codes wind up doing the equivalent of

```auto
cudaMemcpy(host_chunk, device_chunk+random_offset, (size_t)random_size * sizeof(random_type), cudaMemcpyDeviceToHost)

```

all day long (literally thousands of times over hours/days) and never miss a beat, so anecdotally I would suggest you look somewhere else for your problem.

---

<div class="post-metadata">

**Author:** ![kng](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kng](https://forums.developer.nvidia.com/u/kng)\
**Post date:** [April 8, 2010, 8:49am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-strange-behaviour/15986/3 "2010-04-08T08:49:55Z")

</div>

> [@](#):
>
> Almost certainly not. In most of my linear algebra codes, I run my own GPU memory manager which looks after a gigantic chunk of pre-allocated device memory for the life of the application. My codes wind up doing the equivalent of
> 
> ```auto
> cudaMemcpy(host_chunk, device_chunk+random_offset, (size_t)random_size * sizeof(random_type), cudaMemcpyDeviceToHost)
> 
> ```
> 
> all day long (literally thousands of times over hours/days) and never miss a beat, so anecdotally I would suggest you look somewhere else for your problem.

you made me laugh with the emphasis and effort you put in to prove a point :D needed that thanks
