Main Content

cudaMemoryManager

Query memory usage by shared GPU memory manager for MEX functions

Since R2024a

Description

The gpucoder.MemoryManager object contains properties that describe the memory usage by the shared GPU memory manager for MEX functions. To create a gpucoder.MemoryManager object, use the cudaMemoryManager function. To free unused GPU memory allocated by the memory manager, use the object function freeUnusedMemory.

Creation

Description

memoryManager = cudaMemoryManager returns an object that describes the memory usage by the shared GPU memory manager for MEX functions.

The GPU memory manager performs efficient allocation and deallocation of GPU memory. The memory manager returned by the cudaMemoryManager function is shared across all MEX functions that are generated with the memory manager enabled.

example

Properties

expand all

This property is read-only.

Total amount of GPU memory reserved by the memory manager, specified as a uint64 value. This includes memory that is currently in use by MEX functions and memory that is not in use. The memory manager reserves large chunks of memory, referred to as memory pools, to cater to the needs of multiple MEX functions.

Data Types: uint64

This property is read-only.

Amount of GPU memory currently being used by MEX functions, specified as a uint64 value.

Data Types: uint64

This property is read-only.

Amount of GPU memory that is reserved but not currently in use, specified as a uint64 value.

Data Types: uint64

Object Functions

Use the object function freeUnusedMemory to free GPU memory allocated by the memory manager that is not used by any MEX function. This function accepts a gpucoder.MemoryManager object as input, frees unused GPU memory, and returns another gpucoder.MemoryManager object that represents the new state of the memory manager.

Examples

collapse all

In this example, you use the cudaMemoryManager function to inspect GPU memory usage during MEX execution. You then use the freeUnusedMemory object function to free the memory that is currently not in use.

Create a MATLAB® entry-point function simpleAdd.

function c = simpleAdd(a, b)

coder.gpu.kernelfun;
c = coder.nullcopy(zeros(size(a)));

for i = 1:numel(a)
    c(i) = a(i) + b(i);
end

end

During code generation, the GPU memory manager is enabled by default. To generate CUDA® MEX function for simpleAdd, run this command in the MATLAB Command Window:

codegen -args {ones(2048, 1), ones(2048, 1)} simpleAdd -gpuprofile

Run the generated CUDA MEX.

simpleAdd_mex(ones(2048, 1),ones(2048, 1));

Create a gpucoder.MemoryManager object by using the cudaMemoryManager function and display its properties.

memMgr = cudaMemoryManager
memMgr = 

  MemoryManager with properties:

    TotalReservedMemory: 8388608 (8.00 MB)
            MemoryInUse: 0
         MemoryNotInUse: 8388608 (8.00 MB)

Use the freeUnusedMemory function to free the GPU memory that is not in use. Display the new gpucoder.MemoryManager object.

freeUnusedMemory(memMgr);
memMgr
memMgr = 

  MemoryManager with properties:

    TotalReservedMemory: 0
            MemoryInUse: 0
         MemoryNotInUse: 0

Version History

Introduced in R2024a