InitBuffer

Applicability

Product

Supported/Unsupported

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference product 's AI Core

Atlas inference product 's Vector Core

x

Atlas training products

Function Usage

Allocates memory to queues such as TQue and TBuf.

Prototype

  • Allocating memory to queues such as TQue
    1
    2
    3
    4
    5
    6
    template <class T>
    __aicore__ inline bool InitBuffer(T& que, uint8_t num, uint32_t len)
    
    // Allocate memory to queues such as TQue. You can customize the memory address information, including the start address and length.
    template <class T, class U, class V, class... Addrs>
    __aicore__ inline bool InitBuffer(T& que, const Std::tuple<U, V>& addr0, const Addrs&... addrs)
    
  • Memory allocation for TBuf
    1
    2
    template <TPosition bufPos>
    __aicore__ inline bool InitBuffer(TBuf<bufPos>& buf, uint32_t len)
    

Parameters

Table 1 Parameters in the bool InitBuffer(T& que, uint8_t num, uint32_t len) prototype definition template

Field

Meaning

T

Queue type. The value can be TQue, TQueBind.

Table 2 Parameters in the bool InitBuffer(T& que, uint8_t num, uint32_t len) prototype definition

Parameter

Input/Output

Meaning

que

Input

Objects such as TQue for which memory needs to be allocated.

num

Input

Number of allocated buffers. This parameter is used to enable the double buffer function. 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 passed len does not meet the 32-byte alignment requirement, the API automatically pads up to 32-byte aligned. Non-alignment processing is involved in subsequent data movement. For details, see Non-Alignment Scenario.

Table 3 bool InitBuffer(T& que, const Std::tuple<U, V>& addr0, const Addrs&... Parameters in the template definition of the addrs) prototype

Field

Meaning

T

Queue type. The value can be TQue or TQueBind.

U

Type of the start address. The type is integer.

V

Length type. The type is integer.

Addrs...

Address information in tuple format, including the start address and length.

Table 4 bool InitBuffer(T& que, const Std::tuple<U, V>& addr0, const Addrs&... Parameters in the template definition of the addrs) prototype

Parameter

Input/Output

Meaning

que

Input

Objects such as TQue for which memory needs to be allocated.

addr0

Input

Address information of the allocated memory block, including the start address and length.

addrs

Input

Address information list in tuple format. The number of elements in the tuple must be 2, that is, the start address and length.

Table 5 Parameters in the template definition of the InitBuffer(TBuf<bufPos>& buf, uint32_t len) prototype

Field

Meaning

bufPos

Logical position of the TBuf, which is of the TPosition type.

Table 6 Parameters in the template definition of the InitBuffer(TBuf<bufPos>& buf, uint32_t len) prototype

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 passed len does not meet the 32-byte alignment requirement, the API automatically pads up to 32-byte aligned. Non-alignment processing is involved in subsequent data movement. For details, see Non-Alignment Scenario.

Restrictions

  • The memory allocated by InitBuffer is automatically released through the destructor when the Tpipe object is destroyed.
  • If you need to reallocate the memory allocated by InitBuffer, call Reset before InitBuffer is called.
  • The total number of buffers used in a kernel cannot exceed 64.
  • You are not advised to use the InitBuffer mode for allocating custom addresses together with the mode for allocating addresses without specifying them. Otherwise, memory conflicts may occur.

Returns

Buffer initialization result.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Allocate two 128-byte memory blocks for TQue.
AscendC::TPipe pipe; // Pipe memory management object
AscendC::TQue<AscendC::TPosition::VECOUT, 2> que; // output data queue management object, where TPosition is VECOUT.
uint8_t num = 2;
uint32_t len = 128;
pipe.InitBuffer(que, num, len);

// Allocate memory for TQue. The custom memory addresses are [0, 1024], [2048, 4096], and [8192, 12288].
AscendC::TPipe pipe; // Pipe memory management object
AscendC::TQue<AscendC::TPosition::VECOUT, 1> que; // output data queue management object, where TPosition is VECOUT.
auto addr0 = Std::make_tuple(0, 1024);
auto addr1 = Std::make_tuple(2048, 2048);
auto addr2 = Std::make_tuple(8192, 4096);
pipe.InitBuffer(que, addr0, addr1, addr2);

// Allocate 128-byte memory for TBuf.
AscendC::TPipe pipe;
AscendC::TBuf<AscendC::TPosition::A1> buf; // Output data management object. The value of TPosition is A1.
uint32_t len = 128;
pipe.InitBuffer(buf, len);