# Passing array

**URL:** <https://forums.developer.nvidia.com/t/passing-array/13648>\
**Category:** CUDA Programming and Performance\
**Created:** [December 6, 2009, 6:55pm UTC](https://forums.developer.nvidia.com/t/passing-array/13648 "2009-12-06T18:55:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julek](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Julek](https://forums.developer.nvidia.com/u/Julek)\
**Post date:** [December 6, 2009, 6:55pm UTC](https://forums.developer.nvidia.com/t/passing-array/13648/1 "2009-12-06T18:55:41Z")

</div>

My problem is, that I have have a kernel with 64 blocks, and each block of threads needs to know some index value (each thread in n-th block has to know n-th index). Can I pass the array of indexes by a kernel function parameter? These indexes will be in shared or local memory?

---

<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:** [December 6, 2009, 7:37pm UTC](https://forums.developer.nvidia.com/t/passing-array/13648/2 "2009-12-06T19:37:34Z")

</div>

You can only pass pointers to global memory as kernel arguments. If these indices are constant over the life of one kernel, then the best approach is probably to store them in constant memory. For 64 integers, constant memory (which has cache and a broadcast mechanism) should be the fastest way for every thread in a given block to get the value they need.

---

<div class="post-metadata">

**Author:** ![Cygnus\_X1](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cygnus_x1/32/9784_2.png) [@Cygnus\_X1](https://forums.developer.nvidia.com/u/Cygnus_X1)\
**Post date:** [December 7, 2009, 12:28am UTC](https://forums.developer.nvidia.com/t/passing-array/13648/3 "2009-12-07T00:28:59Z")

</div>

Yes, constant memory is probably best suited for your problem. For example you can try the following:

At file scope:

```auto
__constant__ int lookupArray[64];

```

In your host function:

```auto
int hostArray[64];

setTheHostArray(...);

cudaMemcpyToSymbol(lookupArray,(void*)hostArray,64*sizeof(int),0);

callKernel<<<X,Y>>>();

```

and in the kernel name “lookupArray” will be visible to you as if it was a global read-only constant.

Search for cudaMemcpyToSymbol in the Reference Manual if you want details on what cudaMemcpyToSymbol does.

---

<div class="post-metadata">

**Author:** ![cbuchner1](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cbuchner1/32/14168_2.png) [@cbuchner1](https://forums.developer.nvidia.com/u/cbuchner1)\
**Post date:** [December 7, 2009, 2:02am UTC](https://forums.developer.nvidia.com/t/passing-array/13648/4 "2009-12-07T02:02:42Z")

</div>

> [@](#):
>
> My problem is, that I have have a kernel with 64 blocks, and each block of threads needs to know some index value (each thread in n-th block has to know n-th index). Can I pass the array of indexes by a kernel function parameter? These indexes will be in shared or local memory?

I tried passing an array by value to a CUDA kernel and it crashed. Apparently the compiler tried to be smart and wanted to pass this as a pointer reference - which of course did not work with a host memory pointer. Duh ;)

Christian

---

<div class="post-metadata">

**Author:** ![Cygnus\_X1](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cygnus_x1/32/9784_2.png) [@Cygnus\_X1](https://forums.developer.nvidia.com/u/Cygnus_X1)\
**Post date:** [December 7, 2009, 9:14am UTC](https://forums.developer.nvidia.com/t/passing-array/13648/5 "2009-12-07T09:14:10Z")

</div>

> [@](#):
>
> I tried passing an array by value to a CUDA kernel and it crashed.

If you really need to do that, pack your array into a struct. In C, C++ you cannot pass an array explicitly (you always pass a pointer) and in case of CUDA you would pass a host pointer to a device kernel - hence the crash.
