cuda.bindings._v2.driver.mem_host_alloc#
- cuda.bindings._v2.driver.mem_host_alloc(size_t bytesize, unsigned int flags) intptr_t#
Allocates page-locked host memory.
Allocates
bytesizebytes of host memory that is page-locked and accessible to the device. The driver tracks the virtual memory ranges allocated with this function and automatically accelerates calls to functions such ascuMemcpyHtoD(). Since the memory can be accessed directly by the device, it can be read or written with much higher bandwidth than pageable memory obtained with functions such asmalloc(). On systems whereCU_DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLESis true,cuMemHostAllocmay not page-lock the allocated memory. Page-locking excessive amounts of memory may degrade system performance, since it reduces the amount of memory available to the system for paging. As a result, this function is best used sparingly to allocate staging areas for data exchange between host and device. Theflagsparameter enables different options to be specified that affect the allocation, as follows.CU_MEMHOSTALLOC_PORTABLE: The memory returned by this call will be considered as pinned memory by all CUDA contexts, not just the one that performed the allocation.CU_MEMHOSTALLOC_DEVICEMAP: Maps the allocation into the CUDA address space. The device pointer to the memory may be obtained by callingcuMemHostGetDevicePointer().CU_MEMHOSTALLOC_WRITECOMBINED: Allocates the memory as write-combined (WC). WC memory can be transferred across the PCI Express bus more quickly on some system configurations, but cannot be read efficiently by most CPUs. WC memory is a good option for buffers that will be written by the CPU and read by the GPU via mapped pinned memory or host->device transfers.
All of these flags are orthogonal to one another: a developer may allocate memory that is portable, mapped and/or write-combined with no restrictions. The
CU_MEMHOSTALLOC_DEVICEMAPflag may be specified on CUDA contexts for devices that do not support mapped pinned memory. The failure is deferred tocuMemHostGetDevicePointer()because the memory may be mapped into other CUDA contexts via theCU_MEMHOSTALLOC_PORTABLEflag. The memory allocated by this function must be freed withmem_free_host(). Note all host memory allocated usingmem_host_alloc()will automatically be immediately accessible to all contexts on all devices which support unified addressing (as may be queried usingCU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING). Unless the flagCU_MEMHOSTALLOC_WRITECOMBINEDis specified, the device pointer that may be used to access this host memory from those contexts is always equal to the returned host pointer*pp. If the flagCU_MEMHOSTALLOC_WRITECOMBINEDis specified, then the functioncuMemHostGetDevicePointer()must be used to query the device pointer, even if the context supports unified addressing. SeeUnified Addressingfor additional details.- Parameters:
bytesize (size_t) – Requested allocation size in bytes.
flags (unsigned int) – flags for allocation request.
- Returns:
Returned pointer to host memory.
- Return type:
intptr_t
See also
cuMemHostAlloc