Compare (Results Stored in a Register)
Function Usage
Compares the sizes of two tensors element by element. If the comparison result is true, the corresponding bit of the output result is 1. Otherwise, the bit is 0. This interface can be used when the mask parameter is required. The result is stored in a register.
The following comparison modes are supported:
- LT: lower than
- GT: greater than
- GE: greater than or equal to
- EQ: equal
- NE: not equal to
- LE: lower than or equal to
Prototype
- Bitwise mask mode
1 2
template <typename T, bool isSetMask = true> __aicore__ inline void Compare(const LocalTensor<T>& src0Local, const LocalTensor<T>& src1Local, CMPMODE cmpMode, const uint64_t mask[], const BinaryRepeatParams& repeatParams)
- Contiguous mask mode
1 2
template <typename T, bool isSetMask = true> __aicore__ inline void Compare(const LocalTensor<T>& src0Local, const LocalTensor<T>& src1Local, CMPMODE cmpMode, const uint64_t mask, const BinaryRepeatParams& repeatParams)
Parameters
|
Parameter |
Description |
|---|---|
|
T |
Data type of the source operand. The value can be half or float. |
|
isSetMask |
Indicates whether to set mask inside the API.
|
|
Parameter |
Input/Output |
Meaning |
|---|---|---|
|
src0Local and src1Local |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The start address of the LocalTensor must be 32-byte aligned. |
|
cmpMode |
Input |
Comparison mode, including EQ, NE, GE, LE, GT, and LT.
|
|
mask |
Input |
mask is used to control the elements that participate in computation in each iteration.
|
|
repeatParams |
Input |
Parameters that control the operand address strides. They are of the BinaryRepeatParams type, and contain such parameters as those that specify the address stride of the operand for the same data block between adjacent iterations and address stride of the operand between different data blocks in a single iteration. For details about the address stride of the operand between adjacent iterations, see repeatStride. For details about the address stride of the operand between different data blocks in a single iteration, see dataBlockStride. |
Returns
None
Availability
Precautions
- For details about the alignment requirements of the operand address offset, see General Restrictions.
- This instruction does not have repeat input. The default value of repeat is 1, that is, 256-byte data is calculated in an instruction.
- This command writes the result to the 128-bit cmpMask register. The data saved in the register can be obtained by using the GetCmpMask API.
Examples
In this example, the source operands src0Local and src1Local both store 64 pieces of data of the float type. The example compares the data in src0Local and src1Local element by element. If the element in src0Local is smaller than that in src1Local, the corresponding bit in the dstLocal result is set to 1. Otherwise, the bit is set to 0. The dstLocal result is stored in uint8_t format.
This example shows only part of the code used in the computation process (Compute). To run the sample code, copy the code snippet and replace some code of the Compute function in Template Sample.
- Contiguous mask mode
1 2 3 4 5
uint64_t mask = 256 / sizeof(float); // 256 indicates the number of bytes processed in each iteration. AscendC::BinaryRepeatParams repeatParams = { 1, 1, 1, 8, 8, 8 }; // dstBlkStride, src0BlkStride, src1BlkStride = 1, no gap between blocks in one repeat // dstRepStride, src0RepStride, src1RepStride = 8, no gap between repeats AscendC::Compare(src0Local, src1Local, AscendC::CMPMODE::LT, mask, repeatParams);
- Bitwise mask mode
1 2 3 4 5
uint64_t mask[2] = { UINT64_MAX, 0}; AscendC::BinaryRepeatParams repeatParams = { 1, 1, 1, 8, 8, 8 }; // srcBlkStride, = 1, no gap between blocks in one repeat // dstRepStride, srcRepStride = 8, no gap between repeats AscendC::Compare(src0Local, src1Local, AscendC::CMPMODE::LT, mask, repeatParams);
Input (src0_gm): [ 86.72287 9.413112 17.033222 -64.10005 -66.2691 -65.57659 15.898049 94.61241 -68.920685 -36.16883 15.62852 68.078514 -59.724575 -9.4302225 -64.770935 66.55523 -84.60122 57.331 60.42026 -86.78856 37.25265 8.356797 -48.544407 16.73616 15.28083 -21.889254 -67.93181 -41.01825 -68.79465 20.169441 44.11346 -27.419518 30.452742 -89.30283 -18.590672 32.45831 8.392082 -57.198048 98.76846 -81.73067 -38.274437 -83.84363 64.30617 6.028703 -20.77164 93.71867 54.190437 94.98172 -47.447758 -65.77461 82.21715 59.953922 23.599781 -77.29708 26.963976 -63.468987 79.97712 -70.47842 39.00433 52.36555 -63.94925 -65.77033 26.17237 -71.904884 ] Input (src1_gm): [ 2.2989323 51.8879 -81.49718 41.189415 6.4081917 92.566666 53.205498 -94.47063 -75.38387 36.464787 85.60772 -28.70681 42.58504 -76.15293 38.723816 10.006577 74.53035 -78.38537 71.945404 -4.060528 -14.501523 28.229202 96.87876 41.558033 -92.623215 43.318684 35.387154 -16.029816 61.544827 3.3527017 55.806778 -93.242096 22.86275 -87.506584 35.29523 8.405956 91.03445 -85.29485 34.30078 -3.8019252 93.40503 15.459968 -57.99712 -74.39948 -59.900818 -43.132637 -13.123036 41.246174 -93.01083 75.476875 -45.437893 -99.19293 13.543604 76.23386 46.192528 -39.23934 75.9787 -38.38979 9.807722 -60.610104 -23.062874 48.1669 89.913376 73.78631 ] Output (dst_gm): [122 86 237 94 150 3 226 242]
Template Sample
#include "kernel_operator.h"
template <typename T> class KernelCmpCmpmask {
public:
__aicore__ inline KernelCmpCmpmask() {}
__aicore__ inline void Init(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm,
uint32_t dataSize, AscendC::CMPMODE mode)
{
srcDataSize = dataSize;
dstDataSize = 32;
cmpMode = mode;
src0Global.SetGlobalBuffer((__gm__ T*)src0Gm);
src1Global.SetGlobalBuffer((__gm__ T*)src1Gm);
dstGlobal.SetGlobalBuffer((__gm__ uint8_t*)dstGm);
pipe.InitBuffer(inQueueSrc0, 1, srcDataSize * sizeof(T));
pipe.InitBuffer(inQueueSrc1, 1, srcDataSize * sizeof(T));
pipe.InitBuffer(outQueueDst, 1, dstDataSize * sizeof(uint8_t));
}
__aicore__ inline void Process()
{
CopyIn();
Compute();
CopyOut();
}
private:
__aicore__ inline void CopyIn()
{
AscendC::LocalTensor<T> src0Local = inQueueSrc0.AllocTensor<T>();
AscendC::LocalTensor<T> src1Local = inQueueSrc1.AllocTensor<T>();
AscendC::DataCopy(src0Local, src0Global, srcDataSize);
AscendC::DataCopy(src1Local, src1Global, srcDataSize);
inQueueSrc0.EnQue(src0Local);
inQueueSrc1.EnQue(src1Local);
}
__aicore__ inline void Compute()
{
AscendC::LocalTensor<T> src0Local = inQueueSrc0.DeQue<T>();
AscendC::LocalTensor<T> src1Local = inQueueSrc1.DeQue<T>();
AscendC::LocalTensor<uint8_t> dstLocal = outQueueDst.AllocTensor<uint8_t>();
AscendC::Duplicate(dstLocal.ReinterpretCast<float>(), static_cast<float>(0), 8);
AscendC::BinaryRepeatParams repeatParams;
uint32_t mask = 256 / sizeof(T);
AscendC::Compare(src0Local, src1Local, cmpMode, mask, repeatParams);
AscendC::PipeBarrier<PIPE_V>();
AscendC::GetCmpMask(dstLocal);
outQueueDst.EnQue<uint8_t>(dstLocal);
inQueueSrc0.FreeTensor(src0Local);
inQueueSrc1.FreeTensor(src1Local);
}
__aicore__ inline void CopyOut()
{
AscendC::LocalTensor<uint8_t> dstLocal = outQueueDst.DeQue<uint8_t>();
AscendC::DataCopy(dstGlobal, dstLocal, dstDataSize);
outQueueDst.FreeTensor(dstLocal);
}
private:
AscendC::TPipe pipe;
AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueSrc0, inQueueSrc1;
AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueueDst;
AscendC::GlobalTensor<T> src0Global, src1Global;
AscendC::GlobalTensor<uint8_t> dstGlobal;
uint32_t srcDataSize = 0;
uint32_t dstDataSize = 0;
AscendC::CMPMODE cmpMode;
};
template <typename T>
__aicore__ void main_cpu_cmp_cmpmask_demo(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm, uint32_t dataSize, AscendC::CMPMODE mode)
{
KernelCmpCmpmask<T> op;
op.Init(src0Gm, src1Gm, dstGm, dataSize, mode);
op.Process();
}
extern "C" __global__ __aicore__ void kernel_vec_compare_cmpmask_64_LT_float(GM_ADDR src0_gm, GM_ADDR src1_gm, GM_ADDR dst_gm)
{
main_cpu_cmp_cmpmask_demo<float>(src0_gm, src1_gm, dst_gm, 64, AscendC::CMPMODE::LT);
}