Skip to content

GetSpr

产品支持情况

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

功能说明

头文件路径为:"basic_api/kernel_operator_sys_var_intf.h"

获取指定特殊寄存器的值。当前支持表SpecialPurposeReg模板参数说明

函数原型

C++
template <SpecialPurposeReg spr>
__aicore__ inline int64_t GetSpr()

参数说明

表1 模板参数说明

参数名描述
spr特殊寄存器,类型为SpecialPurposeReg枚举类,具体的取值请参考表SpecialPurposeReg模板参数说明

表2 SpecialPurposeReg模板参数说明

取值含义
AR通常配合Squeeze,Reg矢量计算API一起使用;Squeeze,Reg矢量计算API会存储有效元素的总字节数到AR特殊寄存器。

数据类型

接口返回数据类型为int64_t。

返回值说明

返回int64_t类型的特殊寄存器中的数值。

约束说明

本接口只能在VF函数外调用,命名空间为AscendC,函数标记符为__aicore__。

调用示例

如下示例中Squeeze,Reg矢量计算API会存储有效元素的总字节数到AR寄存器中,宏函数结束后通过GetSpr获取AR寄存器的值(单位为字节)。

C++
template <typename T>
__simd_vf__ inline void SqueezeVF(__ubuf__ T* xAddr, __ubuf__ T* yAddr, uint32_t repeatTimes, uint32_t oneRepeatSize)
{
    AscendC::Reg::MaskReg mask = AscendC::Reg::CreateMask<T, AscendC::Reg::MaskPattern::M4>();
    AscendC::Reg::RegTensor<T> xReg;
    AscendC::Reg::RegTensor<T> yReg;
    AscendC::Reg::UnalignRegForStore ureg;
    AscendC::Reg::ClearSpr<AscendC::SpecialPurposeReg::AR>();
    for (uint16_t i = 0; i < repeatTimes; ++i) {
        AscendC::Reg::LoadAlign<T, AscendC::Reg::PostLiteral::POST_MODE_UPDATE>(xReg, xAddr, oneRepeatSize);
        AscendC::Reg::Squeeze<T, AscendC::Reg::GatherMaskMode::STORE_REG>(yReg, xReg, mask);
        AscendC::Reg::StoreUnAlign<T, AscendC::Reg::PostLiteral::POST_MODE_UPDATE>(yAddr, yReg, ureg);
    }
    AscendC::Reg::StoreUnAlignPost(yAddr, ureg);
}

__aicore__ inline void Process()
{
    AscendC::LocalMemAllocator<AscendC::Hardware::UB> ubAllocator;
    AscendC::LocalTensor<float> xLocal = ubAllocator.Alloc<float, 256>();
    AscendC::LocalTensor<float> yLocal = ubAllocator.Alloc<float, 64>();

    AscendC::DataCopy(xLocal, xGm, 256);
    AscendC::SetFlag<AscendC::HardEvent::MTE2_V>(EVENT_ID0);
    AscendC::WaitFlag<AscendC::HardEvent::MTE2_V>(EVENT_ID0);

    __ubuf__ float* xAddr = reinterpret_cast<__ubuf__ float*>(xLocal.GetPhyAddr());
    __ubuf__ float* yAddr = reinterpret_cast<__ubuf__ float*>(yLocal.GetPhyAddr());
    constexpr uint32_t oneRepeatSize = AscendC::GetVecLen() / sizeof(float);
    uint16_t repeatTimes = DivCeil(256, oneRepeatSize);

    asc_vf_call<SqueezeVF<float>>(xAddr, yAddr, repeatTimes, oneRepeatSize);
    AscendC::SetFlag<AscendC::HardEvent::V_S>(EVENT_ID0);
    AscendC::WaitFlag<AscendC::HardEvent::V_S>(EVENT_ID0);
    int64_t arNum = AscendC::GetSpr<AscendC::SpecialPurposeReg::AR>();
    // 可通过printf打印
    AscendC::printf("arNum的值为:%lld\n", arNum);

    AscendC::SetFlag<AscendC::HardEvent::S_MTE3>(EVENT_ID0);
    AscendC::WaitFlag<AscendC::HardEvent::S_MTE3>(EVENT_ID0);
    AscendC::DataCopy(yGm, yLocal, 64);
}

结果示例如下:

C++
输入256个float的数据(xLocal): [1.0 1.0 1.0 ... ] // 数据为全1.0的数
输入64个float的数据(yLocal): [1.0 1.0 1.0 ... ] // 数据为全1.0的数
arNum的值为:256

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