Shared Buffer Management
This feature manages the cross-process shared buffer and can be used together with the Shared Queue Management feature. The scope of the shared buffer is determined by the authorization of the shared queue. The scope includes the "one primary process and multiple secondary processes".
API Call Sequence

The preceding figure shows the sequence of calling key APIs, which is described as follows:
- Initialize AscendCL.
- Allocate runtime resources.
For details, see Runtime Resource Allocation and Deallocation.
- Allocate the shared buffer.
Call acltdtAllocBuf to allocate the shared buffer. Select the buffer type as required.
- Fill the shared buffer with valid data.
Call acltdtGetBufData to obtain the pointer and length of the data area of the shared buffer. Then, call aclrtMemcpy to copy user data to the shared buffer. Lastly, call acltdtSetBufDataLen to set the valid data length so that acltdtGetBufDataLen can be called to obtain the valid data length in the multi-process scenario, based on which you can obtain the data in the shared buffer.
- Set the user data of the shared buffer.
Call acltdtSetBufUserData to set the user data of the shared buffer so that it can be obtained using the acltdtGetBufUserData call in the multi-process scenario.
- Free the shared buffer.
- Deallocate runtime resources.
For the API call sequence, see Runtime Resource Allocation and Deallocation.
- Deinitialize AscendCL.
Sample Code
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include "acl/acl.h" //1. Initialize AscendCL. //The two dots (..) indicate a path relative to the directory of the executable file. //For example, if the executable file is stored in the out directory, the two dots (..) point to the parent directory of the out directory. const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); //2. Allocate runtime resources and specify the compute device. deviceId = 0 is used as an example. ret = aclrtSetDevice(0); //3. Allocate and manage mbuf. DVPP memory whose size is set to 1024U is used as an example. size_t size = 1024U; acltdtBuf buf; ret = acltdtAllocBuf(size, ACL_TDT_DVPP_MEM, &buf); //4. Fill the shared buffer with valid data. //4.1 Obtain the pointer and length of the data area of the shared buffer. void *dataPtr = nullptr; size_t dataSize = 0U; ret = acltdtGetBufData(buf, &dataPtr, &dataSize); //4.2 Copy user data to the shared buffer. size_t len = 512U; //User data size void *ptr = new (std::nothrow) char_t[len]; //The user allocates its own memory. //The user processes the allocated data. //…… ret = aclrtMemcpy(dataPtr, size, ptr, len, ACL_MEMCPY_HOST_TO_DEVICE); //5. Free the buffer. delete[] ptr; ptr = nullptr; delete[] newPtr; newPtr = nullptr; ret = acltdtFreeBuf(buf); //6. Release runtime resources. ret = aclrtResetDevice(0); //7. Deinitialize AscendCL. ret = aclFinalize(); |