MrgSort

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

Atlas A2 training product/Atlas A2 inference product

Atlas 200I/500 A2 inference product

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Merges at most four sorted queues into one. The results are sorted in descending order of the score fields.

Generally, the data to be processed by MrgSort is the data processed by Sort32, or the output of Sort32. The queue structure is as follows:
  • When the data type is float, each structure occupies 8 bytes.

  • When the data type is half, each structure occupies 8 bytes, but 2 bytes in the middle are reserved.

Prototype

1
2
template <typename T>
__aicore__ inline void MrgSort(const LocalTensor<T>& dst, const MrgSortSrcList<T>& src, const MrgSort4Info& params)

Parameters

Table 1 Parameters in the template

Parameter

Description

T

For the Atlas 350 Accelerator Card, the supported data types are half and float.

For the Atlas A3 training product/Atlas A3 inference product, the supported data types are half and float.

For the Atlas A2 training product/Atlas A2 inference product, the supported data types are half and float.

For the Atlas 200I/500 A2 inference product, the supported data types are half and float.

Table 2 Parameters

Parameter

Input/Output

Meaning

dst

Output

Destination operand, which stores sorted data.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

The start address of LocalTensor must be 32-byte aligned.

src

Input

Source operand of the MrgSortSrcList structure type, which contains four sorted queues. The definition is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
template <typename T> struct MrgSortSrcList {
    __aicore__ MrgSortSrcList() {}
    __aicore__ MrgSortSrcList(const LocalTensor<T>& src1In, const LocalTensor<T>& src2In, const LocalTensor<T>& src3In,
        const LocalTensor<T>& src4In)
    {
        src1 = src1In[0];
        src2 = src2In[0];
        src3 = src3In[0];
        src4 = src4In[0];
    }
LocalTensor<T> src1; // the first queue that has been sorted
LocalTensor<T> src2; // the second queue that has been sorted
LocalTensor<T> src3; // the third queue that has been sorted
LocalTensor<T> src4; // the fourth queue that has been sorted
};

The source operand must have the same data type as the destination operand. The type of src1, src2, src3, or src4 is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The start address of the LocalTensor must be 8-byte aligned.

params

Input

Parameter required for sorting, which is of the MrgSort4Info structure type.

For details, see ${INSTALL_DIR}/include/ascendc/basic_api/interface/kernel_struct_proposal.h. Replace ${INSTALL_DIR} with the actual CANN component directory.

For details about the parameter description, see Table 3.

Table 3 MrgSort4Info parameters

Parameter

Meaning

elementLengths

Lengths of the four source queues (or numbers of the 8-byte structures), an array of the uint16_t type with a length of 4. Theoretically, the value range of each element is [0, 4095], but the value cannot exceed the storage space of the UB.

ifExhaustedSuspension

A bool specifying whether to stop the instruction when a queue is exhausted. The default value is false.

validBit

Number of valid queues. The values are as follows:
  • 3: The first two queues are valid.
  • 7: The first three queues are valid.
  • 15: All the four queues are valid.

repeatTimes

Number of iteration repeats. The total length of the four queues is skipped for the source and destination operands in each iteration. Value range: repeatTimes ∈ [1,255]

The repeatTimes parameter takes effect only when the following conditions are met:
  • src contains four queues and validBit is 15.
  • The lengths of the four source queues are the same.
  • The four source queues are stored consecutively.
  • ifExhaustedSuspension = False

Returns

None

Restrictions

  • If score [i] and score [j] are the same and i is greater than j, score [j] is selected first.
  • Data within each iteration is sorted, but data among different iterations is not sorted.
  • Note that the queues sorted by this function are not in the Region Proposal structure.
  • For details about the operand address alignment requirements, see General Address Alignment Restrictions.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// Merge the eight sorted queues. Set repeatTimes to 2, and store the data consecutively.
// Each queue contains 32 8-byte (score, index) structures.
// Output the result after the 256 elements in the score fields are sorted.
AscendC::MrgSort4Info params;
params.elementLengths[0] = 32;
params.elementLengths[1] = 32;
params.elementLengths[2] = 32;
params.elementLengths[3] = 32;
params.ifExhaustedSuspension = false;
params.validBit = 0b1111;
params.repeatTimes = 2;

AscendC::MrgSortSrcList<float> srcList;
srcList.src1 = workLocal[0];
srcList.src2 = workLocal[64]; // workLocal is of the float type. Each queue occupies 256 bytes.
srcList.src3 = workLocal[128];
srcList.src4 = workLocal[192];

AscendC::MrgSort<float>(dstLocal, srcList, params);
outQueueDst.EnQue<float>(dstLocal);
outQueueDst.FreeTensor(dstLocal);