Skip to content

asc_copy_gm2ub(NPU架构版本3510)

产品支持情况​

  • Ascend 950PR&950DT系列产品:支持
  • Atlas A3系列产品:不支持
  • Atlas A2系列产品:不支持
  • Atlas 200I/500 A2推理产品:不支持
  • Atlas推理系列产品AI Core:不支持
  • Atlas推理系列产品Vector Core:不支持
  • Atlas训练系列产品:不支持

功能说明​

头文件路径为:"c_api/vector_datamove/vector_datamove.h"。

将数据从Global Memory(GM)搬运到Unified Buffer(UB)。

本接口支持连续数据搬运和高维切分数据搬运。连续数据搬运按size指定的字节数从源GM连续搬运到目的UB,如图1所示;高维切分数据搬运按burst_count指定搬运的数据块个数,每个数据块按burst_len指定的字节数搬运,并通过src_stride和dst_stride指定源操作数和目的操作数相邻数据块首地址间隔,如图2所示。

有效长度非32字节对齐时,硬件会在UB一侧补齐写入dummy假数据至32字节边界。连续搬运仅在整体末尾补齐;高维切分搬运中,dst_stride等于burst_len时仅在所有数据块的整体末尾补齐,否则每个数据块分别补齐。目的UB必须为实际补齐写入范围预留空间。

图1 连续搬运示意图

图2 高维切分搬运示意图

本接口仅在AIV上生效。

函数原型​

连续数据搬运​

C
__aicore__ inline void asc_copy_gm2ub(__ubuf__ void* dst,
                                      __gm__ void* src,
                                      uint32_t size)

高维切分数据搬运​

C
__aicore__ inline void asc_copy_gm2ub(__ubuf__ void* dst,
                                      __gm__ void* src,
                                      uint32_t burst_count,
                                      uint32_t burst_len,
                                      uint64_t src_stride,
                                      uint32_t dst_stride)

参数说明​

连续数据搬运​

表1 参数说明

参数名输入/输出描述
dst输出目的UB的起始地址。需要32字节对齐。
src输入源GM的起始地址。需要1字节对齐。
size输入搬运数据大小,单位为字节。取值范围:[1, ]。

高维切分数据搬运​

表2 参数说明

参数名输入/输出描述
dst输出目的UB的起始地址。需要32字节对齐。
src输入源GM的起始地址。需要1字节对齐。
burst_count输入待搬运的连续传输数据块个数。取值范围:[1, ]。
burst_len输入待搬运的每个连续传输数据块的长度,单位为字节。取值范围:[1, ]。
src_stride输入源操作数相邻连续数据块的距离(前面一个数据块的头与后面一个数据块的头的间隔),单位为字节。取值范围:[0, ]。
只搬运1个数据块,即burst_count设置为1时,可以将此参数设置为0。
dst_stride输入目的操作数相邻连续数据块的距离(前面一个数据块的头与后面一个数据块的头的间隔),单位为字节。取值范围:[0, ]。
只搬运1个数据块,即burst_count设置为1时,可以将此参数设置为0。

返回值说明​

无

流水类型​

PIPE_MTE2

约束说明​

通用约束​

  • 本接口在非AIV上调用直接返回。
  • 各存储单元的空间大小和对齐要求请参考存储单元说明。
  • 如果本指令与其他指令存在数据依赖,需要插入同步指令,保证多个指令串行化,防止出现异常数据。

连续数据搬运约束​

  • size需满足dtype字节对齐:dtype为b16时需为2的倍数,dtype为b32时需为4的倍数。
  • size非32字节对齐时,目的UB需要预留补齐至32字节边界后的空间。

高维切分数据搬运约束​

  • burst_len需满足dtype字节对齐:dtype为b16时需为2的倍数,dtype为b32时需为4的倍数。
  • 当dst_stride不等于burst_len时,dst_stride要求32字节对齐。
  • 目的UB容量需要按Compact整体补齐或Normal逐块补齐后的实际写入范围计算。

调用示例​

将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。

以Ascend 950PR&950DT系列产品(对应NPU架构为dav-3510)为例,编译运行命令如下:

Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"

namespace {

constexpr uint32_t INPUT_BYTES = 256;
constexpr uint32_t OUTPUT_BYTES = 256;

__global__ __vector__ void asc_copy_gm2ub_arch_3510_kernel(__gm__ uint8_t* output, __gm__ uint8_t* input)
{
    asc_init();
    __ubuf__ uint8_t local[INPUT_BYTES];
    // 选择256字节覆盖一个完整寄存器的数据量,便于逐字节核对搬运结果。
    asc_copy_gm2ub(local, input, INPUT_BYTES);
    // PIPE_MTE3等待PIPE_MTE2搬入完成后,再将UB数据搬出到GM。
    asc_sync_notify(PIPE_MTE2, PIPE_MTE3, EVENT_ID0);
    asc_sync_wait(PIPE_MTE2, PIPE_MTE3, EVENT_ID0);
    asc_copy_ub2gm_align(output, local, INPUT_BYTES);
    asc_sync();
}

void print_data(const char* name, const std::vector<uint8_t>& data)
{
    std::cout << name << ":";
    const uint32_t count = data.size() < 32 ? data.size() : 32;
    for (uint32_t i = 0; i < count; ++i) std::cout << ' ' << +data[i];
    if (data.size() > count) std::cout << " ...";
    std::cout << std::endl;
}
} // namespace

int main()
{
    std::vector<uint8_t> input(INPUT_BYTES), output(OUTPUT_BYTES, 0), golden(OUTPUT_BYTES, 0);
    for (uint32_t i = 0; i < INPUT_BYTES; ++i) input[i] = static_cast<uint8_t>(i + 1);
    for (uint32_t i = 0; i < 256; ++i) golden[i] = input[i];
    aclInit(nullptr);
    aclrtSetDevice(0);
    uint8_t *input_device = nullptr, *output_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&input_device), INPUT_BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(reinterpret_cast<void**>(&output_device), OUTPUT_BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(input_device, INPUT_BYTES, input.data(), INPUT_BYTES, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(output_device, OUTPUT_BYTES, output.data(), OUTPUT_BYTES, ACL_MEMCPY_HOST_TO_DEVICE);
    asc_copy_gm2ub_arch_3510_kernel<<<1, 0>>>(output_device, input_device);
    aclrtSynchronizeDevice();
    aclrtMemcpy(output.data(), OUTPUT_BYTES, output_device, OUTPUT_BYTES, ACL_MEMCPY_DEVICE_TO_HOST);
    print_data("Input", input);
    print_data("Output", output);
    print_data("Golden", golden);
    const bool passed = output == golden;
    std::cout << (passed ? "[Success] asc_copy_gm2ub passed." : "[Failed] asc_copy_gm2ub failed.") << std::endl;
    aclrtFree(input_device);
    aclrtFree(output_device);
    aclrtResetDevice(0);
    aclFinalize();
    return passed ? 0 : 1;
}

免责声明:本站内容由 asc-devkit 仓 master 分支自动编译生成,属于持续开发版本,可能存在缺陷,仅供预览与参考。如需稳定及商用资料,请查阅官方 昇腾社区。