InitBufPool
功能说明
通过Tpipe::InitBufPool接口可划分出整块资源,整块TbufPool资源可以继续通过TBufPool::InitBufPool接口划分成小块资源。
函数原型
template <class T> __aicore__ inline bool InitBufPool(T& bufPool, uint32_t len); template <class T, class U> __aicore__ inline bool InitBufPool(T& bufPool, uint32_t len, U& shareBuf);
参数说明
参数名称 |
输入/输出 |
含义 |
|---|---|---|
bufPool |
输入 |
新划分的资源池,类型为TBufPool |
len |
输入 |
新划分资源池长度,单位为Byte,非32Bytes对齐会自动向上补齐至32Bytes对齐 |
参数名称 |
输入/输出 |
含义 |
|---|---|---|
bufPool |
输入 |
新划分的资源池,类型为TBufPool |
len |
输入 |
新划分资源池长度,单位为Byte,非32Bytes对齐会自动向上补齐至32Bytes对齐 |
shareBuf |
输入 |
被复用资源池,类型为TBufPool,新划分资源池与被复用资源池共享起始地址及长度 |
支持的型号
Atlas 训练系列产品
Atlas推理系列产品AI Core
Atlas A2训练系列产品/Atlas 800I A2推理产品
注意事项
- 新划分的资源池与被复用资源池的物理内存需要一致,两者共享起始地址及长度;
- 输入长度需要小于等于被复用资源池长度;
- 其他泛用约束参考TBufPool;
返回值
无
调用示例
数据量较大且内存有限时,无法一次完成所有数据搬运,需要拆分成多个阶段计算,每次计算使用其中的一部分数据,可以通过TBufPool资源池进行内存地址复用。本例中,从Tpipe划分出资源池tbufPool0,tbufPool0为src0Gm分配空间后,继续分配了资源池tbufPool1,指定tbufPool1与tbufPool2复用并分别运用于第一、二轮计算,此时tbufPool1及tbufPool2共享起始地址及长度。
namespace AscendC {
class ResetApi {
public:
__aicore__ inline ResetApi() {}
__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.InitBufPool(tbufPool0, 131072);
tbufPool0.InitBuffer(srcQue0, 1, 65536); // Total src0
tbufPool0.InitBufPool(tbufPool1, 65536);
tbufPool0.InitBufPool(tbufPool2, 65536, tbufPool1);
}
__aicore__ inline void Process()
{
tbufPool1.InitBuffer(srcQue1, 1, 32768);
tbufPool1.InitBuffer(dstQue0, 1, 32768);
CopyIn();
Compute();
CopyOut();
tbufPool1.Reset();
tbufPool2.InitBuffer(srcQue2, 1, 32768);
tbufPool2.InitBuffer(dstQue1, 1, 32768);
CopyIn1();
Compute1();
CopyOut1();
tbufPool2.Reset();
tbufPool0.Reset();
pipe.Reset();
}
private:
__aicore__ inline void CopyIn()
{
LocalTensor<half> src0Local = srcQue0.AllocTensor<half>();
LocalTensor<half> src1Local = srcQue1.AllocTensor<half>();
DataCopy(src0Local, src0Global, 16384);
DataCopy(src1Local, src1Global, 16384);
srcQue0.EnQue(src0Local);
srcQue1.EnQue(src1Local);
}
__aicore__ inline void Compute()
{
LocalTensor<half> src0Local = srcQue0.DeQue<half>();
LocalTensor<half> src1Local = srcQue1.DeQue<half>();
LocalTensor<half> dstLocal = dstQue0.AllocTensor<half>();
Add(dstLocal, src0Local, src1Local, 16384);
dstQue0.EnQue<half>(dstLocal);
srcQue0.FreeTensor(src0Local);
srcQue1.FreeTensor(src1Local);
}
__aicore__ inline void CopyOut()
{
LocalTensor<half> dstLocal = dstQue0.DeQue<half>();
DataCopy(dstGlobal, dstLocal, 16384);
dstQue0.FreeTensor(dstLocal);
}
__aicore__ inline void CopyIn1()
{
LocalTensor<half> src0Local = srcQue0.AllocTensor<half>();
LocalTensor<half> src1Local = srcQue2.AllocTensor<half>();
DataCopy(src0Local, src0Global[16384], 16384);
DataCopy(src1Local, src1Global[16384], 16384);
srcQue0.EnQue(src0Local);
srcQue2.EnQue(src1Local);
}
__aicore__ inline void Compute1()
{
LocalTensor<half> src0Local = srcQue0.DeQue<half>();
LocalTensor<half> src1Local = srcQue2.DeQue<half>();
LocalTensor<half> dstLocal = dstQue1.AllocTensor<half>();
Add(dstLocal, src0Local, src1Local, 16384);
dstQue1.EnQue<half>(dstLocal);
srcQue0.FreeTensor(src0Local);
srcQue2.FreeTensor(src1Local);
}
__aicore__ inline void CopyOut1()
{
LocalTensor<half> dstLocal = dstQue1.DeQue<half>();
DataCopy(dstGlobal[16384], dstLocal, 16384);
dstQue1.FreeTensor(dstLocal);
}
private:
TPipe pipe;
TBufPool<TPosition::VECCALC> tbufPool0, tbufPool1, tbufPool2;
TQue<QuePosition::VECIN, 1> srcQue0, srcQue1, srcQue2;
TQue<QuePosition::VECOUT, 1> dstQue0, dstQue1;
GlobalTensor<half> src0Global, src1Global, dstGlobal;
};
} // namespace AscendC
extern "C" __global__ __aicore__ void tbufpool_kernel(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm)
{
AscendC::ResetApi op;
op.Init(src0Gm, src1Gm, dstGm);
op.Process();
}
父主题: TBufPool