# array of structure problem

**URL:** <https://forums.developer.nvidia.com/t/array-of-structure-problem/12311>\
**Category:** CUDA Programming and Performance\
**Created:** [September 22, 2009, 11:18am UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311 "2009-09-22T11:18:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philipp82](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Philipp82](https://forums.developer.nvidia.com/u/Philipp82)\
**Post date:** [September 22, 2009, 11:18am UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/1 "2009-09-22T11:18:05Z")

</div>

Hi,

I have problems with an array of structures. Maybe it’s because of alignment problems, but I don’t understand the CUDA manual about that… I hope some clever people can help me. I have a structure:

[codebox]struct DBDStruct

{

```
double	Temperature;

double DensN;

double	DensN2A;	

```

};

[/codebox]

and fill the structure on CPU memory with some initial values, copy that to GPU memory, calculate on GPU and copy it back. The whole code runs with “float” values, but with doubles I get problems. I added the “-arch=sm\_13” to the compiler flags ( i have a GTX 285).

My memory allocation on CPU is:

[codebox] struct DBDStruct\* oldGridValues = (struct DBDStruct\*) malloc(Number\_of\_gridpoints\_x_Number\_of\_gridpoints\_y_sizeof(

DBDStruct));[/codebox]

On GPU it is:

[codebox]struct DBDStruct\* cuda\_oldGridValues;

CUDA\_SAFE\_CALL(cudaMalloc((void\*\*)&cuda\_oldGridValues, Number\_of\_gridpoints\_x\*Number\_of\_gridpoints\_y \* sizeof(DBDStruct)));[/codebox]

and I copy the stuff to the GPU with:

[codebox]CUDA\_SAFE\_CALL(cudaMemcpy(cuda\_oldGridValues, oldGridValues, Number\_of\_gridpoints\_x\*Number\_of\_gridpoints\_y \* sizeof(DBDStruct), cudaMemcpyHostToDevice));[/codebox]

copy back to CPU mem with:

[codebox] CUDA\_SAFE\_CALL(cudaMemcpy(newGridValues, cuda\_newGridValues, Number\_of\_gridpoints\_x\*Number\_of\_gridpoints\_y \* sizeof(DBDStruct), cudaMemcpyDeviceToHost));[/codebox]

What is the easiest way to get it running with doubles?

Thx for any help!!!

Philipp.

---

<div class="post-metadata">

**Author:** ![mickey\_mouse](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mickey\_mouse](https://forums.developer.nvidia.com/u/mickey_mouse)\
**Post date:** [September 22, 2009, 11:38am UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/2 "2009-09-22T11:38:36Z")

</div>

> [@](#):
>
> The whole code runs with “float” values, but with doubles I get problems.

What kind of “problems” do you have? ;)

---

<div class="post-metadata">

**Author:** ![Philipp82](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Philipp82](https://forums.developer.nvidia.com/u/Philipp82)\
**Post date:** [September 22, 2009, 11:41am UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/3 "2009-09-22T11:41:28Z")

</div>

> [@](#):
>
> What kind of “problems” do you have? ;)

Ohh, well :rolleyes: (sorry) I get garbage results, some points are not initialized in GPU mem and some points seem to appear at two places when copied back to CPU mem. Thats why I thought it is a problem of allignment…

---

<div class="post-metadata">

**Author:** ![mickey\_mouse](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mickey\_mouse](https://forums.developer.nvidia.com/u/mickey_mouse)\
**Post date:** [September 22, 2009, 11:44am UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/4 "2009-09-22T11:44:52Z")

</div>

> [@](#):
>
> Ohh, well :rolleyes: (sorry) I get garbage results, some points are not initialized in GPU mem and some points seem to appear at two places when copied back to CPU mem. Thats why I thought it is a problem of allignment…

No prob.

Have you tried to debug the application in device emulation mode, so you can enter in kernel lines and watch how structs are copied?

---

<div class="post-metadata">

**Author:** ![Philipp82](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Philipp82](https://forums.developer.nvidia.com/u/Philipp82)\
**Post date:** [September 22, 2009, 11:50am UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/5 "2009-09-22T11:50:40Z")

</div>

No, I didn’t. I never tryed that and don’t really know how to do… But don’t I get problems with structs larger than 16 bytes anyways, when I use the abovementioned lines for memory allocation ?, because on the device there are some kind of “spaces” in the arrays ? I really don’t understand the manual in that point…

I had the same problem in another simulation, when I had a structure with more than 4 floats…, with 4 floats or less (=16Byte) it runs pretty well…

---

<div class="post-metadata">

**Author:** ![mickey\_mouse](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mickey\_mouse](https://forums.developer.nvidia.com/u/mickey_mouse)\
**Post date:** [September 22, 2009, 12:15pm UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/6 "2009-09-22T12:15:22Z")

</div>

> [@](#):
>
> No, I didn’t. I never tryed that and don’t really know how to do… But don’t I get problems with structs larger than 16 bytes anyways, when I use the abovementioned lines for memory allocation ?, because on the device there are some kind of “spaces” in the arrays ? I really don’t understand the manual in that point…
> 
> I had the same problem in another simulation, when I had a structure with more than 4 floats…, with 4 floats or less (=16Byte) it runs pretty well…

Ok, I’m in accord with you, maybe there is an alignment problem.

I’m sorry but I’ve never used array of structures and so I’ve never used alignment in mem copy. The only solution that I can suggest is to try using cudaMallocArray() and cudaMemcpyArrayToArray() to solve your problem, but I’ve never used them so I cannot help you in understanding the paramenters you need to pass at these functions. :"\>

EDIT: Look at CUDA reference manual for functions signatures ;)

---

<div class="post-metadata">

**Author:** ![Philipp82](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Philipp82](https://forums.developer.nvidia.com/u/Philipp82)\
**Post date:** [September 22, 2009, 12:17pm UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/7 "2009-09-22T12:17:29Z")

</div>

Anyone who knows what the problem is? :(

---

<div class="post-metadata">

**Author:** ![Demq](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Demq](https://forums.developer.nvidia.com/u/Demq)\
**Post date:** [September 22, 2009, 3:32pm UTC](https://forums.developer.nvidia.com/t/array-of-structure-problem/12311/8 "2009-09-22T15:32:02Z")

</div>

> [@](#):
>
> Anyone who knows what the problem is? :(

Did you try to lower the dimensions of your blocks? It might not be a problem with copying your arrays back and forth, but doing the operations with them. What happens when you simply assign a constant to the output array and copy it back (commenting out everything else).

I had a similar problem when I used too many registers in a block with doubles, then I got garbage in the results. Making the block smaller somehow helped. As far as the alignment goes, it is useful for faster copies in and out of global memory, but shouldn’t matter otherwise.

Cheers
