Skip to content

Reg矢量计算编程

本文介绍如何使用基础API(C++模板接口)编写Reg矢量计算代码。基础API与Reg矢量计算编程(语言扩展C API)描述的是同一套Reg矢量硬件能力,功能语义一致,主要区别在于API命名、类型表达和可配置参数的表达方式。

Reg矢量编程的硬件原理、内存层级、VF函数执行域、流水线同步、Hardware Loop、VF融合等与API接口命名无关的内容,请参见Reg矢量计算编程(语言扩展C API)。本文只说明基础API的接口定位、模板参数、数据类型体系、API分类和典型调用方式。

基础API定位​

基础API Reg矢量计算接口位于AscendC::Reg命名空间,是语言扩展C API的C++模板化封装。基础API并不改变Reg矢量计算的编程模型:仍然需要在__aicore__域通过asc_vf_call进入__simd_vf__函数,并在VF函数内完成Load -> Compute -> Store。

基础API的主要特点如下:

  • 使用C++类型表达寄存器对象,例如RegTensor<T>、MaskReg、AddrReg、UnalignRegForLoad、UnalignRegForStore ,对应关系参考数据对象与寄存器类型。
  • 使用模板参数表达数据类型、搬运模式、PostUpdate模式等配置。
  • 多数接口可通过参数自动推导寄存器类型。
  • 搬运接口可以与LocalTensor衔接,可以支持不操作Unified Buffer(UB)地址,也可以外层__aicore__代码从LocalTensor::GetPhyAddr()获取UB物理地址,再传入VF函数内的Reg搬运接口。
  • 部分计算接口提供MaskMergeMode模板参数,用于控制未被mask选中的元素如何写入目的寄存器。

📌 提示:

  1. 基础API和C API功能一致。若需要理解“为什么需要Reg矢量编程”“寄存器与UB/GM的关系”“哪些场景需要同步”等模型性内容,请直接阅读C API版本文档。

  2. 搬运接口支持LocalTensor参数,在后续版本提供,暂未支持。

与C API的对应关系​

基础API与C API的核心差异是表达形式不同。

维度基础API(C++模板)C API(语言扩展)
命名空间/前缀AscendC::Reg::*asc_*
命名风格LoadAlign、StoreUnalignPostasc_loadalign、asc_storeunalign_post
矢量数据寄存器RegTensor<T>vector_float、vector_half、vector_int32_t等
掩码寄存器MaskRegvector_bool
地址寄存器AddrRegaddr_reg
非对齐搬入/搬出寄存器UnalignRegForLoad / UnalignRegForStorevector_load_unalign / vector_store_unalign
数据类型选择模板参数T,通常可由参数推导类型名或接口后缀,例如_b8、_b16、_b32
PostUpdatePostLiteral模板参数接口名后缀,例如_postupdate
Mask合并模式MaskMergeMode模板参数暂不支持

常见API映射如下:

功能基础APIC API
创建全量maskCreateMask<T, MaskPattern::ALL>()asc_create_mask_b*()
更新尾块maskUpdateMask<T>(count)asc_update_mask_b*()
创建地址寄存器CreateAddrReg<T>(...)asc_update_addr_reg_b*()
通用搬入/搬出Load / Storeasc_load / asc_store
对齐搬入/搬出LoadAlign / StoreAlignasc_loadalign / asc_storealign
非对齐搬入LoadUnalignPre + LoadUnalignasc_loadunalign_pre + asc_loadunalign
非对齐搬出StoreUnalign + StoreUnalignPostasc_storeunalign + asc_storeunalign_post
流水线同步LocalMemBar<src, dst>()asc_mem_bar(...)

模板参数体系​

基础API把C API中“后缀、枚举、特殊接口名”表达的能力统一收敛到模板参数中。常见模板参数如下。

模板参数作用典型取值/类型常见接口
T数据类型float、half、int32_t等RegTensor<T>、LoadAlign<T>、Add<T>
PostLiteral搬运后是否更新UB地址POST_MODE_NORMAL、POST_MODE_UPDATELoadAlign、StoreAlign、LoadUnalign、StoreUnalign
LoadDist搬入模式DIST_NORM、broadcast类取值等LoadAlign
StoreDist搬出模式正常搬出、首元素搬出、pack类取值等StoreAlign
MaskMergeModemask未选中元素处理方式ZEROING、MERGING部分计算接口,例如Add
计算模式枚举指定算法或计算变体例如ReduceType、PairReduce等规约、复合计算等

数据类型模板参数​

基础API使用RegTensor<T>表达矢量数据寄存器。T决定寄存器内元素解释方式,寄存器宽度仍由硬件VL决定。例如在VL为256B时,RegTensor<float>可表示64个float元素,RegTensor<half>可表示128个half元素。

C++
AscendC::Reg::RegTensor<float> srcReg;
AscendC::Reg::RegTensor<float> dstReg;

AscendC::Reg::LoadAlign(srcReg, srcAddr);
AscendC::Reg::Add(dstReg, srcReg, srcReg, mask);

当接口参数已经包含RegTensor<T>时,模板参数T通常可由编译器推导;当使用特定模式、转换接口或需要消除歧义时,可以显式填写。

搬运模式模板参数​

基础API使用LoadDist、StoreDist、PostLiteral等模板参数表达搬运模式。

C++
// 对齐搬入,使用LoadDist模板参数
AscendC::Reg::LoadAlign<float, AscendC::Reg::LoadDist::DIST_BRC_B32>(srcReg, srcAddr);

// PostUpdate对齐搬入,接口执行后更新srcAddr。
AscendC::Reg::LoadAlign<float, AscendC::Reg::PostLiteral::POST_MODE_UPDATE>(srcReg, srcAddr, stride);

// 对齐搬出,使用StoreDist模板参数
AscendC::Reg::StoreAlign<float, AscendC::Reg::StoreDist::DIST_FIRST_ELEMENT_B32>(dstAddr, dstReg, mask);

MaskMergeMode模板参数​

部分Reg计算接口提供MaskMergeMode模板参数,用于描述mask未选中元素在目的寄存器中的处理方式:

  • MaskMergeMode::ZEROING:mask未选中的元素在dstReg中置零。
  • MaskMergeMode::MERGING:mask未选中的元素保留dstReg原值。
C++
AscendC::Reg::Add<T, AscendC::Reg::MaskMergeMode::ZEROING>(dstReg, src0Reg, src1Reg, mask);

AscendC::Reg::Add<T, AscendC::Reg::MaskMergeMode::MERGING>(dstReg, src0Reg, src1Reg, mask);

📌 提示:MaskMergeMode只有部分接口形态支持,由软件仿真实现。实际使用时应以单个API文档中的函数原型和参数说明为准。对于不需要保留未选中元素旧值的场景,使用默认的ZEROING语义。

数据对象与寄存器类型​

基础API使用C++类型表达Reg矢量编程中的寄存器资源。

寄存器分类基础API类型用途
矢量数据寄存器RegTensor<T, regTrait>保存VL或多VL长度的矢量数据,是计算接口的主要源/目的操作数
掩码寄存器MaskReg控制哪些元素参与计算,也可承载比较、逻辑等结果
地址寄存器AddrReg保存UB访问偏移,适合固定stride、多维索引等搬运场景
搬入非对齐寄存器UnalignRegForLoad连续非对齐搬入的临时缓存
搬出非对齐寄存器UnalignRegForStore连续非对齐搬出的临时缓存

RegTensor和MaskReg等寄存器对象只能作为整体使用,不能按C++数组方式访问其中某个元素。相关硬件含义、宽度、mask bit对应关系请参考Reg矢量计算编程(语言扩展C API)中“寄存器类型与申请”章节。

与LocalTensor衔接​

Reg矢量搬运接口实际访问的是UB地址。基于Tensor的Cpp编程中,外层通常使用LocalTensor管理UB数据。

C++
template <typename T>
__aicore__ inline void Compute()
{
    AscendC::LocalTensor<T> dst = ubAllocator.Alloc<T, TILE_LENGTH>();
    // 参数计算等
    ......
    asc_vf_call<AddVF<T>>(dst, src0, src1, oneRepeatSize, repeatTimes);
    // 尾处理
    ......
}

这种写法使基础API能够与__aicore__中LocalTensor流程衔接。

📌 提示:使用LocalTensor作为VF函数参数时,不能使用引用LocalTensor &。因为在__aicore__和__simd_vf__是两个相互隔离的执行域,无法通过跨执行域通过引用访问。

特性描述​

寄存器构造类​

Mask创建​

CreateMask用于创建固定模式的MaskReg,UpdateMask用于根据剩余元素数生成尾块mask。

C++
AscendC::Reg::MaskReg fullMask = AscendC::Reg::CreateMask<T, AscendC::Reg::MaskPattern::ALL>();

AscendC::Reg::MaskReg mask = AscendC::Reg::UpdateMask<T>(remainCount);

UpdateMask<T>(remainCount)适合放在VF循环中进行尾块处理。其语义与C API中的asc_update_mask_b*一致。

地址寄存器创建​

CreateAddrReg用于创建保存UB偏移量的AddrReg,最多支持四层循环跳转。

C++
AscendC::Reg::AddrReg aReg;
for(uint16_t i = 0;i < extent1; i++){
    for(uint16_t j = 0;j < extent2; j++){
        for(uint16_t k = 0;k < extent3; k++){
            for(uint16_t m = 0;m < extent4; m++){
                aReg = AscendC::Reg::CreateAddrReg(i, const1, j, const2, k, const3, m, const4);
                AscendC::Reg::LoadAlign(srcReg, srcAddr, aReg);
            }
        }
    }
}

数据搬运类​

通用搬运​

Load / Store不要求用户区分UB地址是否32B对齐,适合希望统一处理对齐和非对齐地址的场景。

C++
AscendC::Reg::Load(srcReg, srcAddr);
AscendC::Reg::Store(dstAddr, dstReg);
AscendC::Reg::Store(dstAddr, dstReg, count);

通用搬运是软件封装,性能敏感且地址满足对齐条件时,优先使用对齐搬运。

对齐搬运​

LoadAlign / StoreAlign适用于UB地址满足对齐要求的连续搬入/搬出,是Reg矢量编程中最常见的搬运接口。在搬运时可以按照内置的功能随路进行数据排布等操作,具体功能以对应API章节为准。

C++
AscendC::Reg::LoadAlign(srcReg, srcAddr);
AscendC::Reg::StoreAlign(dstAddr, dstReg, mask);

非对齐搬运​

连续非对齐搬入由LoadUnalignPre和LoadUnalign组合使用;连续非对齐搬出由StoreUnalign和StoreUnalignPost组合使用。

C++
AscendC::Reg::UnalignRegForLoad loadUreg;
AscendC::Reg::UnalignRegForStore storeUreg;

AscendC::Reg::LoadUnalignPre(loadUreg, srcAddr);
for (uint16_t i = 0; i < repeatTimes; ++i) {
    AscendC::Reg::LoadUnalign(srcReg, loadUreg, srcAddr, postUpdateStride);
    // Reg计算
    ...
    AscendC::Reg::StoreUnalign(dstAddr, dstReg, storeUreg, postUpdateStride);
}
AscendC::Reg::StoreUnalignPost(dstAddr, storeUreg, 0);

矢量计算类​

矢量计算接口直接操作RegTensor<T>或MaskReg,通常带有MaskReg mask控制有效元素。

RegTensor<T>与RegTensor<T>计算​

基础算术接口完成寄存器之间的逐元素计算,标量算术接口完成寄存器与标量的逐元素计算。

C++
AscendC::Reg::Add(dstReg, src0Reg, src1Reg, mask);
AscendC::Reg::Mul(dstReg, src0Reg, src1Reg, mask);
AscendC::Reg::Adds(dstReg, srcReg, scalarValue, mask);
AscendC::Reg::Muls(dstReg, srcReg, scalarValue, mask);

MaskReg计算​

逻辑接口可操作RegTensor或MaskReg;比较接口通常输出比较结果,选择接口按mask在两个源操作数之间选择。

C++
AscendC::Reg::And(dstMaskReg, src0MaskReg, src1MaskReg, mask);
AscendC::Reg::Compare(cmpMaskReg, src0Reg, src1Reg, mask);
AscendC::Reg::Select(dstReg, src0Reg, src1Reg, cmpMaskReg, mask);

这些接口往往有更多模板参数或限制条件,建议在具体使用时查看对应API页面。

同步控制类​

基础API使用LocalMemBar表达VF内部对UB访问流水线的同步约束。

C++
AscendC::Reg::Store(dstAddr, dstReg);
AscendC::Reg::LocalMemBar<AscendC::Reg::MemType::VEC_STORE,
                          AscendC::Reg::MemType::VEC_LOAD>();
AscendC::Reg::Load(srcReg, dstAddr);

何时需要同步与API命名无关,判断规则请参考Reg矢量计算编程(语言扩展C API)中的“流水线同步”章节。

编程示例​

下面示例展示基础API的典型调用方式。外层__aicore__函数仍负责LocalTensor管理,VF函数内使用Reg基础API完成搬入、计算和搬出。

C++
template <typename T>
__simd_vf__ inline void AddVFv1(AscendC::LocalTensor<T> dst,
                              AscendC::LocalTensor<T> src0,
                              AscendC::LocalTensor<T> src1,
                              uint32_t oneRepeatSize,
                              uint16_t repeatTimes)
{
    AscendC::Reg::RegTensor<T> src0Reg;
    AscendC::Reg::RegTensor<T> src1Reg;
    AscendC::Reg::RegTensor<T> dstReg;
    AscendC::Reg::MaskReg mask =
        AscendC::Reg::CreateMask<T, AscendC::Reg::MaskPattern::ALL>();

    for (uint16_t i = 0; i < repeatTimes; ++i) {
        uint32_t offset = i * oneRepeatSize;
        AscendC::Reg::LoadAlign(src0Reg, src0 + offset);
        AscendC::Reg::LoadAlign(src1Reg, src1 + offset);
        AscendC::Reg::Add(dstReg, src0Reg, src1Reg, mask);
        AscendC::Reg::StoreAlign(dst + offset, dstReg, mask);
    }
}

template <typename T>
__simd_vf__ inline void AddVFv2(__ubuf__ float* dstAddr,
                                __ubuf__ float* src0Addr,
                                __ubuf__ float* src1Addr,
                                uint32_t oneRepeatSize,
                                uint16_t repeatTimes)
{
    AscendC::Reg::RegTensor<T> src0Reg;
    AscendC::Reg::RegTensor<T> src1Reg;
    AscendC::Reg::RegTensor<T> dstReg;
    AscendC::Reg::MaskReg mask =
        AscendC::Reg::CreateMask<T, AscendC::Reg::MaskPattern::ALL>();

    for (uint16_t i = 0; i < repeatTimes; ++i) {
        uint32_t offset = i * oneRepeatSize;
        AscendC::Reg::LoadAlign(src0Reg, src0Addr + offset);
        AscendC::Reg::LoadAlign(src1Reg, src1Addr + offset);
        AscendC::Reg::Add(dstReg, src0Reg, src1Reg, mask);
        AscendC::Reg::StoreAlign(dstAddr + offset, dstReg, mask);
    }
}

template <typename T>
__aicore__ inline void Compute()
{
    AscendC::LocalTensor<T> dst = ubAllocator.Alloc<T, TILE_LENGTH>();

    constexpr uint32_t oneRepeatSize = 256 / sizeof(T);
    uint16_t repeatTimes = TILE_LENGTH / oneRepeatSize;
	// 使用LocalTensor作为参数
    asc_vf_call<AddVFv1<T>>(dst, src0, src1, oneRepeatSize, repeatTimes);
    
    __ubuf__ T* dstAddr = (__ubuf__ T*)dst.GetPhyAddr();
    __ubuf__ T* src0Addr = (__ubuf__ T*)src0.GetPhyAddr();
    __ubuf__ T* src1Addr = (__ubuf__ T*)src1.GetPhyAddr();
    // 使用UB地址作为参数
    asc_vf_call<AddVFv2<T>>(dstAddr, src0Addr, src1Addr, oneRepeatSize, repeatTimes);
}

如果需要处理尾块,可以在VF循环中使用UpdateMask<T>:

C++
uint32_t remainCount = count;
for (uint16_t i = 0; i < repeatTimes; ++i) {
    AscendC::Reg::MaskReg mask = AscendC::Reg::UpdateMask<T>(remainCount);
    // Load / Compute / Store
}

小结​

基础API与C API描述的是同一套Reg矢量计算能力,两者的编程模型和硬件语义保持一致。基础API的差异主要体现在接口表达方式上:它通过AscendC::Reg::*命名空间组织接口,使用C++寄存器类型描述VF内部的数据对象,并通过模板参数承载数据类型、搬运模式、计算模式等配置。与LocalTensor衔接时,可以在__aicore__函数中直接将LocalTensor作为参数传入VF函数,也可以通过LocalTensor::GetPhyAddr()取得UB地址后传入VF函数,再在VF函数内部使用Reg搬运接口完成访问。

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