# \_\_device\_\_ variables and arrays

**URL:** <https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131>\
**Category:** CUDA Programming and Performance\
**Created:** [July 17, 2013, 11:30am UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131 "2013-07-17T11:30:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![a\_goryachih](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@a\_goryachih](https://forums.developer.nvidia.com/u/a_goryachih)\
**Post date:** [July 17, 2013, 11:30am UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/1 "2013-07-17T11:30:11Z")

</div>

Hello,  
I need to use variables and arrays during all program in different device functions, so i decided to initial it with **device**.  
But I got some error when i tried to use **device** array in global function, for example

float\* host\_a;  
**device** float\* dev\_a;

global change ()  
{  
dev[threadIdx.x] \*= -1;  
}

main()  
{  
cudaError\_t cuerr;

```
host_a = (int*) malloc(sizeof(int)*10);
for(int i =0; i<size; i++)
	host_a [i] = 0;

cuerr= cudaMalloc(&dev_a,10*sizeof(float));

cudaMemcpy(dev_a,host_a,(10*sizeof(float)),cudaMemcpyHostToDevice);

change<<<1,host_size>>();

cuerr=cudaMemcpy(host_a,dev_a,(10*sizeof(float)),cudaMemcpyDeviceToHost);

if(cuerr!=cudaSuccess)
	printf(,cudaGetErrorString(cuerr));

```

}  
after this i got “unknowm error”

---

<div class="post-metadata">

**Author:** ![cmaster.matso](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cmaster.matso/32/12153_2.png) [@cmaster.matso](https://forums.developer.nvidia.com/u/cmaster.matso)\
**Post date:** [July 17, 2013, 12:40pm UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/2 "2013-07-17T12:40:25Z")

</div>

As for You code sample ‘dev\_a’ is a device pointer to float. You cannot access it form host code.  
Tried passing ‘dev\_a’ as kernel call parameter instead? Define it locally (in main() function for example), allocate it with cudaMalloc and then pass as kernel parameter, like so:

```auto
__global__ void change(float *ptr) {
...
}

int main() {
...
float *dev_a;
cuerr= cudaMalloc(&dev_a,10*sizeof(float));
...
change<<<1,host_size>>>(dev_a);
...
}

```

Cheers,  
MK

---

<div class="post-metadata">

**Author:** ![pasoleatis](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@pasoleatis](https://forums.developer.nvidia.com/u/pasoleatis)\
**Post date:** [July 17, 2013, 4:11pm UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/3 "2013-07-17T16:11:42Z")

</div>

I think that if you define it outside of main() you need to run a command find out the address of the pointer.

---

<div class="post-metadata">

**Author:** ![a\_goryachih](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@a\_goryachih](https://forums.developer.nvidia.com/u/a_goryachih)\
**Post date:** [July 18, 2013, 4:22am UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/4 "2013-07-18T04:22:54Z")

</div>

Thanks, cmaster.matso!  
But I have more than 20 arrays and variables. Using it like kernel call parameter is not so comfortable).  
And what about constant memory? How can i define and use arrays constant memory? I mean something like that:  
float\* host\_a;  
**constant** float\* const\_a;

global change (float \*array)  
{  
array[threadIdx.x] = const\_a[threadIdx.x];  
}

main()  
{  
cudaError\_t cuerr;

float\* dev\_a;

host\_a = (int\*) malloc(sizeof(int)\*10);  
for(int i =0; i\<size; i++)  
host\_a [i] = 0;

cuerr= cudaMalloc(&dev\_a,10_sizeof(float));  
cudaMemcpy(dev\_a,host\_a,(10_sizeof(float)),cudaMemcpyHostToDevice);

/\* CUDA MAGIC  
copy data from host\_a to const\_a  
_/  
change\<\<\<1,host\_size\>\>(dev\_a);  
сuerr=cudaMemcpy(host\_a,dev\_a,(10_sizeof(float)),cudaMemcpyDeviceToHost);  
}

---

<div class="post-metadata">

**Author:** ![cmaster.matso](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cmaster.matso/32/12153_2.png) [@cmaster.matso](https://forums.developer.nvidia.com/u/cmaster.matso)\
**Post date:** [July 18, 2013, 3:16pm UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/5 "2013-07-18T15:16:41Z")

</div>

Say You have a variable ‘array’ allocated by ‘cudaMalloc’, in the host code. That means You have a pointer to a device memory block. What You need to do is to find a symbol (can be **constant** ) in Your device code, using ‘cudaGetSymbolAddress’ (say ‘dev\_a’). Next copy the address of the allocated memory block (‘array’) to the address of the symbol You got with ‘cudaGetSymbolAddress’ call, i.e. copy it into the memory place where the ‘dev\_a’ pointer is. In serial C code it would be like so:

```auto
...
float *array = malloc(N*sizeof(float));
float *dev_a;
dev_a = array; // Don't copy the content to the array but set both pointers point the same memory block
...

```

To sum-up:

 [\*] 'array' points to some device memory block, [\*] 'dev\_a' is a device-side pointer, not initialized by default (pointing somewhere unknown), [\*] one need to set up 'dev\_a' to point to memory block pointed by 'array', thus copy of the address itself.

Cheers,  
MK

---

<div class="post-metadata">

**Author:** ![a\_goryachih](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@a\_goryachih](https://forums.developer.nvidia.com/u/a_goryachih)\
**Post date:** [July 23, 2013, 7:41am UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/6 "2013-07-23T07:41:37Z")

</div>

Thanks!  
Can you give me example of cuda code? Because all of it is not easy for me) I tried to use cudaGetSymbolAddress, and got error “invalid device symbol”.

---

<div class="post-metadata">

**Author:** ![cmaster.matso](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cmaster.matso/32/12153_2.png) [@cmaster.matso](https://forums.developer.nvidia.com/u/cmaster.matso)\
**Post date:** [August 1, 2013, 12:11pm UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/7 "2013-08-01T12:11:49Z")

</div>

I use driver API functions, ‘cuModuleGetGlobal’ (to get address of a named, device-side variable), ‘cuMemGetAddressRange’ (to get address of allocated buffer) and ‘cuMemcpyHtoD’ (to finally copy the address).

---

<div class="post-metadata">

**Author:** ![cudafan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cudafan](https://forums.developer.nvidia.com/u/cudafan)\
**Post date:** [August 16, 2014, 12:42pm UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/8 "2014-08-16T12:42:16Z")

</div>

I’m having the same issue: too many global device arrays to pass them as parameters to a kernel. Could not find any example how to do it. Help!

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 16, 2014, 2:45pm UTC](https://forums.developer.nvidia.com/t/device-variables-and-arrays/30131/9 "2014-08-16T14:45:29Z")

</div>

1. Flatten all your arrays and concatenate them. Then pass a pointer to the single array and a pointer to the array that gives the offset to the start of each sub-array, in the concateneated array. Maybe also a pointer to an array of lengths of each sub-array.

2. Pass a pointer to an array of pointers, each of which is a pointer to a sub-array. You probably also need to pass an integral value that is the length of the array of pointers. Maybe also an array of the lengths of each sub-array.

cublasSgetri/cublasSgetrf uses the second method, for example, for the array of pointers to device matrices to be inverted.
