更多样例
====

#### 样例模板

为了方便开发者快速运行具体指令中的参考样例，本章节提供单目指令的样例模板。

开发者可以将以下样例模板作为代码框架，只需将具体指令中的样例片段拷贝替换下文代码段中的加粗内容即可。

#include "kernel\_operator.h"
  
class KernelUnary {
public:
    \_\_aicore\_\_ inline KernelUnary() {}
    \_\_aicore\_\_ inline void Init(\_\_gm\_\_ uint8\_t\* srcGm, \_\_gm\_\_ uint8\_t\* dstGm)
    {
        srcGlobal.SetGlobalBuffer((\_\_gm\_\_ half\*)srcGm);
        dstGlobal.SetGlobalBuffer((\_\_gm\_\_ half\*)dstGm);
        pipe.InitBuffer(inQueueSrc, 1, 512 \* sizeof(half));
        pipe.InitBuffer(outQueueDst, 1, 512 \* sizeof(half));
    }
    \_\_aicore\_\_ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }
private:
    \_\_aicore\_\_ inline void CopyIn()
    {
        AscendC::LocalTensor<half> srcLocal = inQueueSrc.AllocTensor<half>();
        AscendC::DataCopy(srcLocal, srcGlobal, 512);
        inQueueSrc.EnQue(srcLocal);
    }
    \_\_aicore\_\_ inline void Compute()
    {
        AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>();
        AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
  
        AscendC::Sqrt(dstLocal, srcLocal, 512);
  
        outQueueDst.EnQue<half>(dstLocal);
        inQueueSrc.FreeTensor(srcLocal);
    }
    \_\_aicore\_\_ inline void CopyOut()
    {
        AscendC::LocalTensor<half> dstLocal = outQueueDst.DeQue<half>();
        AscendC::DataCopy(dstGlobal, dstLocal, 512);
        outQueueDst.FreeTensor(dstLocal);
    }
private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueSrc;
    AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueueDst;
    AscendC::GlobalTensor<half> srcGlobal, dstGlobal;
};
extern "C" \_\_global\_\_ \_\_aicore\_\_ void unary\_simple\_kernel(\_\_gm\_\_ uint8\_t\* srcGm, \_\_gm\_\_ uint8\_t\* dstGm)
{
    KernelUnary op;
    op.Init(srcGm, dstGm);
    op.Process();
}