Allocate datatype on the stack - ats

datatype is used to create a non-linear type that will be allocated on the heap and datavtype for a linear type that will be allocated on the heap.
But, is there a version of datatype that will allocate on the stack?

Related

HeapCreate with dwMaximumSize > 4GB

Is there any way to create a heap that is larger than 4 GB using the HeapCreate() function?
A heap bigger than 4 GB can be achieved by setting the dwMaximumSize parameter to zero and then asking for memory chunks. But this causes the individual parts of the heap to be split by the MEM_RESERVED chunks. I would need to create a continuous heap that has 5GB and is not divided by these MEM_RESERVED chunks.
The HeapCreate() function's dwMaximumSize parameter is of the SIZE_T type, which should allow setting this value to a higher value than 4 GB on a 64-bit system. However, when I try to allocate such a heap (larger than 4GB), HeapCreate() returns a null pointer. However, I haven't found anywhere documented that the HeapCreate() function should only allow the allocation of a heap that has dwMaximumSize parameter less than 4GB.

How deallocation of memory in vector of vector

I have a dynamic vector of vector: vector< vector <CelMap> > cels where CelMap is an object of type class,
and i need to free the memory. How can do it?
You can try shrink_to_fit - that, if granted, will reduce the allocated memory to the exact memory the vector is occupying at that moment.
Vectors will allocate more memory than they use which can be inspected with capacity and increased with reserve.
shrink_to_fit is a request to reduce the allocated memory to the actual vector size and it's granting is implementation dependent,
Requests the removal of unused capacity. It is a non-binding request
to reduce capacity() to size(). It depends on the implementation if
the request is fulfilled.

What happen if kmem_cache has no free memory for allocating?

I create a slab cache by kmem_cache_create(... size), then allocate memory from this cache by kmem_cache_alloc().
After I have allocated memory for "size" times, what happen if I call kmem_cache_alloc() to allocat size + 1th memory? Return NULL or extend cache implicitly?
The 'size' argument is not about memory reserved for anything. It is about the size of each allocation as returned by kmem_cache_alloc.
It may be there will be memory shortage in general, in which case, depending on flags pased to kmem_cache_alloc, the kernel may try to free some by e.g. shrinking caches.

Difference between return values of alloc_pages() and get_free_pages()

Why do we require alloc_pages() to return pointer to a struct page unlike other memory allocator function (get_free_pages(), kmalloc() ) ?
Please provide a use case.
Is it related to HIGHMEM Zone allocation?
alloc_pages(mask, order) allocates 2order pages and returns an instance of struct page to represent the start of the reserved block. alloc_page(mask) is a shorter notation for order = 0 if only one page is requested.
__get_free_pages(mask, order) and __get_free_page(mask) work in the same way as the
above functions but return the virtual address of the reserved memory chunk instead of a page instance.
kmalloc(size, mask) reserves a memory area of size bytes and returns a void pointer to the start of the area. If insufficient memory is available (a very improbable situation in the kernel but one that must always be catered for), a null pointer is the result.
mask specifies details about request:
• memory zone
• behavior of allocator (blocking/unblocking request, etc.)
• e.g. GFP_KERNEL, GFP_ATOMIC, GFP_DMA, etc
alloc_pages() and __get_free_pages() : allocate pages, at low level
kmalloc() : allocate physically contiguous sequence of bytes
for more information refer professional linux kernel architecture by wolfgang mauerer

Allocating more memory to an existing Global memory array

is it possible to add memory to a previously allocated array in global memory?
what i need to do is this:
//cudamalloc memory for d_A
int n=0;int N=100;
do
{
Kernel<<< , >>> (d_A,n++);
//add N memory to d_A
while(n!=5)}
does doing another cudamalloc removes the values of the previously allocated array? in my case the values of the previous allocated array should be kept...
First, cudaMalloc behaves like malloc, not realloc. This means that cudaMalloc will allocate totally new device memory at a new location. There is no realloc function in the cuda API.
Secondly, as a workaround, you can just use cudaMalloc again to allocate more more memory. Remember to free the device pointer with cudaFree before you assign a new address to d_a. The following code is functionally what you want.
int n=0;int N=100;
//set the initial memory size
size = <something>;
do
{
//allocate just enough memory
cudaMalloc((void**) &d_A, size);
Kernel<<< ... >>> (d_A,n++);
//free memory allocated for d_A
cudaFree(d_A);
//increase the memory size
size+=N;
while(n!=5)}
Thirdly, cudaMalloc can be an expensive operation, and I expect the above code will be rather slow. I think you should consider why you want to grow the array. Can you allocate memory for d_A one time with enough memory for the largest use case? There is likely no reason to allocate only 100 bytes if you know you need 1,000 bytes later on!
//calculate the max memory requirement
MAX_SIZE = <something>;
//allocate only once
cudaMalloc((void**) &d_A, MAX_SIZE);
//use for loops when they are appropriate
for(n=0; n<5; n++)
{
Kernel<<< ... >>> (d_A,n);
}
Your psuedocode does not "add memory to a previously allocated array" at all. The standard C way of increasing the size of an existing allocation is via the realloc() function, and there is no CUDA equivalent of realloc() at the time of writing.
When you do
cudaMalloc(d_A....)
// something
cudaMalloc(d_A....)
all you are doing is creating a new memory allocation and assigning it to d_A. The previous memory allocation still exists, but now you have lost the pointer value of the previous memory and have no way of accessing it. Based on this and your previous question on almost the same subject, might I suggest you spend a bit of time revising memory and pointer concepts in C before you try CUDA, because unless you have a very clear understanding of these fundamentals, you will find the distributed memory nature of CUDA to be very confusing,
I'm not sure what complications cuda adds to the mix(?) but in c you can't add memory to an already allocated array.
If you want to grow a malloc'd array, you need to malloc a new array of the size you need and copy the contents from the existing array.
If you're doing this often then it's probably worth mallocing more than you need each time to avoid costly (in terms of processing time) re-allocation operations.

Resources