GetSpr
【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit
产品支持情况
- 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模板参数说明。
函数原型
template <SpecialPurposeReg spr> __aicore__ inline int64_t GetSpr()参数说明
表1模板参数说明
| 参数名 | 描述 |
|---|---|
| spr | 特殊寄存器,类型为SpecialPurposeReg枚举类,具体的取值请参考表SpecialPurposeReg模板参数说明。 |
表2SpecialPurposeReg模板参数说明
| 取值 | 含义 |
|---|---|
| AR | 通常配合Squeeze,Reg矢量计算API一起使用;Squeeze,Reg矢量计算API会存储有效元素的总字节数到AR特殊寄存器。 |
数据类型
接口返回数据类型为int64_t。
返回值说明
返回int64_t类型的特殊寄存器中的数值。
约束说明
本接口只能在VF函数外调用,命名空间为AscendC,函数标记符为__aicore__。
调用示例
如下示例中Squeeze,Reg矢量计算API会存储有效元素的总字节数到AR寄存器中,宏函数结束后通过GetSpr获取AR寄存器的值(单位为字节)。
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); }结果示例如下:
输入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本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考