InitBuffer

Function Usage

TPipe is a framework used to manage global memory. You can call the InitBuffer API in TPipe to allocate memory for TQue/TBuf.

Prototype

  • Memory allocation for TQue
    1
    2
    template <class T>
    __aicore__ inline bool InitBuffer(T& que, uint8_t num, uint32_t len)
    
  • Memory allocation for TBuf
    1
    2
    template <TPosition bufPos>
    __aicore__ inline bool InitBuffer(TBuf<bufPos>& buf, uint32_t len)
    

Parameters

Table 1 bool InitBuffer(T& que, uint8_t num, uint32_t len) prototype definition parameters

Parameter

Input/Output

Meaning

que

Input

TQue object that requires memory allocation

num

Input

Number of allocated memory blocks. The double buffer function is enabled through this parameter. If num is set to 1, the double buffer function is disabled. If num is set to 2, the double buffer function is enabled.

len

Input

Size of each memory block, in bytes. If the input len does not meet the 32-byte alignment requirement, the API automatically pads up to 32-byte alignment. Non-alignment processing is involved in subsequent data movement. For details, see Non-Alignment Scenario.

Table 2 InitBuffer(TBuf<bufPos>& buf, uint32_t len) prototype definition parameters

Parameter

Input/Output

Meaning

buf

Input

TBuf object that requires memory allocation

len

Input

Size of the memory allocated to the TBuf, in bytes. If the input len does not meet the 32-byte alignment requirement, the API automatically pads up to 32-byte alignment. Non-alignment processing is involved in subsequent data movement. For details, see Non-Alignment Scenario.

Availability

Atlas Training Series Product

Precautions

The number of QUE buffers on the same TPosition varies depending on the AI processor model. Number constraint exists. This constraint must be met when a buffer is allocated.

For the Atlas Training Series Product , a maximum of four buffers can be allocated.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
AscendC::TQue<AscendC::TPosition::VECIN, 1> que0;
AscendC::TQue<AscendC::TPosition::VECIN, 1> que1;
// Not recommended:
// For example, if the operator has six inputs, six buffers are allocated.
// Respectively allocate three buffers for que0 and que1. Allocate six buffers in the VECIN position.
// The maximum number of QUE buffers on the same TPosition is 4 for Atlas training products. If the number exceeds 4, resource allocation may fail when AllocTensor or FreeTensor is used.
pipe.InitBuffer(que0, 3, len);
pipe.InitBuffer(que1, 3, len);
// You are advised to perform the following operations to solve the problem:
// If multiple buffers are used, you can combine multiple buffers into one buffer and use the buffer through offset.
pipe.InitBuffer(que0, 1, len * 3);
pipe.InitBuffer(que1, 1, len * 3);
/*
* Three local tensors are allocated. The address of local1 is the start address of the buffer in que0.
* The address of local2 is the address of local1 with offset len, and the address of local3 is the offset address of local1.
* len * 2 address
 */
int32_t offset1 = len;
int32_t offset2 = len * 2;
AscendC::LocalTensor<T> local1 = que0.AllocTensor<T>();
AscendC::LocalTensor<T> local2 = local1[offset1];
AscendC::LocalTensor<T> local3 = local1[offset2];

Returns

The buffer initialization result is returned.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Allocate memory to the TQue. The number of allocated memory blocks is 2, and the size of each memory block is 128 bytes.
AscendC::TPipe pipe; // Pipe memory management object
AscendC::TQue<AscendC::TPosition::VECOUT, 2> que; // Output the management objects of the data queue. The value of QuePosition is VECOUT.
uint8_t num = 2;
uint32_t len = 128;
pipe.InitBuffer(que, num, len);
// Allocate memory for TBuf. The allocated length is 128 bytes.
AscendC::TPipe pipe;
AscendC::TBuf<AscendC::TPosition::A1> buf; // Output data management object. The value of QuePosition is A1.
uint32_t len = 128;
pipe.InitBuffer(buf, len);