目录

Common Data Movement

Function Description

Common data movement API: moves continuous or discontinuous data.

Function Prototype

Parameters

Table 4 Parameters of common data movement APIs

Parameter Input/Output Description
dstLocal and dstGlobal Output Destination operand, which is of the LocalTensor or GlobalTensor type. When dstLocal is located in C2, the start address must be 64-byte aligned. When dstLocal is located in C2PIPE2GM, the start address must be 128-byte aligned. In other cases, the start address must be 32-byte aligned.
srcLocal and srcGlobal Input Source operand, which is of the LocalTensor or GlobalTensor type.
repeatParams Input Movement parameter, DataCopyParams type. The definition is described below and the parameter details are provided in Table 5.
calCount Input

Number of elements involved in the movement.

Note: The size of data moved by DataCopy must be a multiple of 32 bytes. Therefore, when the common data movement API (continuous data movement, including the calCount parameter) is used, calCount x sizeof(T) must be 32-byte aligned. Otherwise, the size of moved data is rounded down to the nearest multiple of 32 bytes.

Table 5 Parameters in the DataCopyParams structure

Parameter Description
blockCount Number of data chunks in the command. Value range: blockCount ∈ [1, 4095].
blockLen

Length of each data chunk. The unit is data block (32 bytes). Value range: blockLen ∈ [1, 65535].

Particularly, when dstLocal is located in C2PIPE2GM, the unit is 128 bytes; when dstLocal is located in C2, the unit is 64 bytes.

srcStride Interval between adjacent data chunks of the source operand (the interval between the tail of one data chunk and the head of the next). The unit is a data block (32 bytes). The data type is uint16_t. The value of srcStride cannot exceed the value range of this data type.
dstStride

Interval between adjacent data chunks of the destination operand (the interval between the tail of the previous data chunk and the head of the next data chunk). The unit is data block (32 bytes). The data type is uint16_t. The value of dstStride cannot exceed the value range of this data type.

Particularly, when dstLocal is located in C2PIPE2GM, the unit is 128 bytes; when dstLocal is located in C2, the unit is 64 bytes.

The following example shows how to use the DataCopyParams structure. In the example, two consecutive data chunks are moved, and each data chunk contains eight data blocks. There is no stride between adjacent data chunks of the source operand, while there is a stride of one data block between the tail and head of the adjacent data chunks of the destination operand.

Supported Models

Kirin 9020 series processors

Kirin 9030 series processors

Kirin X90 series processors

Precautions

Calling Example

#include "kernel_operator.h"
class KernelDataCopy {
public:
    __aicore__ inline KernelDataCopy() {}
    __aicore__ inline void Init(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm)
    {
        src0Global.SetGlobalBuffer((__gm__ half*)src0Gm);
        src1Global.SetGlobalBuffer((__gm__ half*)src1Gm);
        dstGlobal.SetGlobalBuffer((__gm__ half*)dstGm);
        pipe.InitBuffer(inQueueSrc0, 1, 512 * sizeof(half));
        pipe.InitBuffer(inQueueSrc1, 1, 512 * sizeof(half));
        pipe.InitBuffer(outQueueDst, 1, 512 * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }
private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<half> src0Local = inQueueSrc0.AllocTensor<half>();
        AscendC::LocalTensor<half> src1Local = inQueueSrc1.AllocTensor<half>();
        AscendC::DataCopy(src0Local, src0Global, 512);
        AscendC::DataCopy(src1Local, src1Global, 512);
        inQueueSrc0.EnQue(src0Local);
        inQueueSrc1.EnQue(src1Local);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<half> src0Local = inQueueSrc0.DeQue<half>();
        AscendC::LocalTensor<half> src1Local = inQueueSrc1.DeQue<half>();
        AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
        AscendC::Add(dstLocal, src0Local, src1Local, 512);
        outQueueDst.EnQue<half>(dstLocal);
        inQueueSrc0.FreeTensor(src0Local);
        inQueueSrc1.FreeTensor(src1Local);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<half> dstLocal = outQueueDst.DeQue<half>();
        AscendC::DataCopy(dstGlobal, dstLocal, 512);
        outQueueDst.FreeTensor(dstLocal);
    }
private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueSrc0, inQueueSrc1;
    AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueueDst;
    AscendC::GlobalTensor<half> src0Global, src1Global, dstGlobal;
};
extern "C" __global__ __aicore__ void data_copy_kernel(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm)
{
    KernelDataCopy op;
    op.Init(src0Gm, src1Gm, dstGm);
    op.Process();
}

Result example:

Input (src0Global): [1 2 3 ... 512]
Input (src1Global): [1 2 3 ... 512]
Output (dstGlobal):[2 4 6 ... 1024]