# Heap Memory Allocation in Kernel Question

**URL:** <https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031>\
**Category:** CUDA Programming and Performance\
**Created:** [February 14, 2011, 3:54pm UTC](https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031 "2011-02-14T15:54:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![svbettscheider](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@svbettscheider](https://forums.developer.nvidia.com/u/svbettscheider)\
**Post date:** [February 14, 2011, 3:54pm UTC](https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031/1 "2011-02-14T15:54:01Z")

</div>

Hi,

the document CUDA\_C\_Programming\_Guide.pdf (Site 135) describe how to allocate memory with malloc/free in kernel functions( **global** ).

Example from there is:

```auto
__global__ void mallocTest()

{ 

char* ptr = (char*)malloc(123);

printf(â€œThread %d got pointer: %p\nâ€, threadIdx.x, ptr); 

free(ptr);

}

void main()

{ 

// Set a heap size of 128 megabytes. Note that this must 

// be done before any kernel is launched. 

cudaThreadSetLimit(cudaLimitMallocHeapSize, 128*1024*1024); 

mallocTest<<<1, 5>>>();

cudaThreadSynchronize(); 

}

```

I use the same example but —\> error: calling a host function from a **device** / **global** function is not allowed

Since Toolkit Version 3.2 is it possible to allocate memory with malloc.

From Changelog Toolkit 3.2 :“Support for memory management using malloc() and free() in CUDA C compute kernels”

What am I doing wrong?

---

<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:** [February 14, 2011, 4:03pm UTC](https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031/2 "2011-02-14T16:03:57Z")

</div>

Do you have a compute capability 2.0 or 2.1 card (ie. Fermi), and are you passing a code generation option to nvcc to build for compute capability 2.0 or 2.1?

---

<div class="post-metadata">

**Author:** ![svbettscheider](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@svbettscheider](https://forums.developer.nvidia.com/u/svbettscheider)\
**Post date:** [February 14, 2011, 4:09pm UTC](https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031/3 "2011-02-14T16:09:37Z")

</div>

I have the following devices.

Device 0: “Tesla C1060”  
CUDA Capability Major/Minor version number: 1.3

Device 0: “GeForce 310M”  
CUDA Capability Major/Minor version number: 1.2

No device with Capability 2.0 or higher.

Is there an alternative for allocate memory in the kernel, for compute capability \<2.0?

//edit

The compute capability from NVIDIAÂ´s Tesla C2050 is 2.0. Which option is needed to compile the code with capability 2.0?  
sm\_20?

---

<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:** [February 14, 2011, 4:33pm UTC](https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031/4 "2011-02-14T16:33:54Z")

</div>

If you don’t have a compute 2.x device (and you don’t), memory allocation in device code is not supported.

To compile for compute 2.0, pass -arch=sm\_20 to nvcc.

---

<div class="post-metadata">

**Author:** ![svbettscheider](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@svbettscheider](https://forums.developer.nvidia.com/u/svbettscheider)\
**Post date:** [February 14, 2011, 4:39pm UTC](https://forums.developer.nvidia.com/t/heap-memory-allocation-in-kernel-question/21031/5 "2011-02-14T16:39:48Z")

</div>

Ok thanks for your Time. I will try to compile my code this week on a NVIDIAÂ´s Tesla C2050 card.
