aclrtMalloc
Description
Allocates linear memory of the size on the device, and returns the pointer of the allocated memory by using *devPtr. The start address of the memory is 64-byte aligned. The allocation size is the input size rounded up to the nearest multiple of 32 bytes, plus 32 bytes.
Restrictions
- The memory allocated by this API does not initialize the content. You are advised to call the aclrtMemset API to initialize the memory and clear random numbers in the memory before using the memory.
- This API does not perform implicit device synchronization or stream synchronization. The memory allocation result, either success or failure, is returned immediately.
- Memory allocated by the aclrtMalloc call needs to be freed by the aclrtFree call.
- Performance deterioration will be caused by too frequent calls to aclrtMalloc and aclrtFree. You are advised to allocate or manage memory in advance to avoid unnecessary memory allocation and freeing.
- If you want to allocate a large memory block, and divide and manage the memory, you are advised to use aclrtMallocAlign32. Compared with aclrtMalloc, aclrtMallocAlign32 only rounds the input size up to the nearest multiple of 32 bytes, but does not add an extra 32 bytes.
If you allocate a large memory block using either aclrtMalloc or aclrtMallocAlign32, and divide and manage the memory, each memory segment must meet the following requirements:
- The memory size is rounded up to the nearest multiple of 32 plus 32 bytes (m = ALIGN_UP[len,32] + 32 bytes).
- The memory start address must be 64-byte aligned (ALIGN_UP[m,64]).
len indicates the size of a memory segment. ALIGN_UP[len,k] indicates rounding up to a multiple of k bytes as in this formula: ((len – 1)/k + 1) x k.
Prototype
aclError aclrtMalloc(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
Parameters
|
Parameter |
Input/Output |
Description |
|---|---|---|
|
devPtr |
Output |
Pointer to the pointer to the allocated device memory. |
|
size |
Input |
Requested allocation size in bytes. Must not be 0. |
|
policy |
Input |
Memory allocation policy. |
Returns
The value 0 indicates success, and other values indicate failure. For details, see aclError.