EN
注册
我要评分
文档获取效率
文档正确性
内容完整性
文档易理解
在线提单
论坛求助

TopK

输出源向量最大K个值及其索引。如果源向量中有相同值,则非HMPP_TOPK_RADIX算法计算这些相同值所对应的索引不排序,即非HMPP_TOPK_RADIX算法是不稳定的,TopK计算后的索引顺序与计算之前的顺序可能不同。

该函数调用流程如下:

  1. 调用Init初始化缓冲区buffer内存。
  2. 调用主函数。
  3. 最后调用Release释放缓冲区buffer内存。

函数接口声明如下:

  • 初始化操作:

    HmppResult HMPPS_TopKInit_32s(int32_t srcLen, int32_t *dstValue, int32_t *dstIndex, int32_t dstLen, HmppTopKMode hint, uint8_t **buffer);

    HmppResult HMPPS_TopKInit_32f(int32_t srcLen, float *dstValue, int32_t *dstIndex, int32_t dstLen, HmppTopKMode hint, uint8_t **buffer);

  • 主函数操作:

    HmppResult HMPPS_TopK_32s(const int32_t *src, int32_t srcIndex, int32_t srcStride, int32_t srcLen, int32_t *dstValue, int32_t *dstIndex, int32_t dstLen, HmppTopKMode hint, uint8_t *buffer);

    HmppResult HMPPS_TopK_32f(const float *src, int32_t srcIndex, int32_t srcStride, int32_t srcLen, float *dstValue, int32_t *dstIndex, int32_t dstLen, HmppTopKMode hint, uint8_t *buffer);

  • 释放内存操作

    HmppResult HMPPS_TopKRelease(uint8_t *buffer);

参数

参数名

描述

取值范围

输入/输出

src

指向源向量的指针。

非空

输入

srcIndex

起始索引(pSrc[0]的索引)。

[0, INT_MAX - srcLen)

输入

srcStride

源向量中元素之间的跨度

(0, INT_MAX]

输入

srcLen

源向量中的元素长度。

(0, INT_MAX]

输入

dstValue

指向值目标向量的指针。

非空

输出

dstIndex

指向值索引目标向量的指针。

非空

输出

dstLen

目标向量中的元素长度。

(0, INT_MAX]

输入

hint

计算使用的算法模型。

HMPP_TOPK_AUTO、HMPP_TOPK_DIRECT或HMPP_TOPK_RADIX

输入

buffer

(init函数中)

指向计算所需缓冲区的指针的指针。

非空

输出

buffer(主函数中和release函数中)

指向计算所需缓冲区的指针。

非空

输入

返回值

  • 成功:返回HMPP_STS_NO_ERR
  • 失败:返回错误码。

错误码

错误码

描述

HMPP_STS_NO_ERR

表示没有错误。

HMPP_STS_NULL_PTR_ERR

当任何指定的指针为空时指示错误。

HMPP_STS_SIZE_ERR

当srcLen或者dstLen小于或等于0时指示错误。

HMPP_STS_BAD_ARG_ERR

指示hint数值不支持时错误。

HMPP_STS_OVERFLOW

指示计算溢出时错误。

HMPP_STS_MALLOC_FAILED

Init函数中进行算法模型所需内存申请失败。

注意

调用该接口计算之前,必须调用Init接口初始化缓冲区buffer内存和值目标向量、值索引目标向量。

示例

#define SRC_LEN_S 7
void TopKExample(void){
    int32_t srcLen = SRC_LEN_S;
    int32_t srcIndex = 0;
    int32_t srcStride = 4;
    int32_t dstLen = SRC_LEN_S;
    HmppTopKMode hint = HMPP_TOPK_AUTO;
    int32_t src[SRC_LEN_S] = { 7, 1, 11, 0, 2, 5, 16 };
    int32_t dstValue[SRC_LEN_S] = { 0 };
    int32_t dstIndex[SRC_LEN_S] = { 0 };
    uint8_t *buffer = NULL;
    HmppResult result;
    result = HMPPS_TopKInit_32s(srcLen, dstValue, dstIndex, dstLen, hint, &buffer);
    printf("HMPPS_TopKInit_32s result = %d\n", result);
    if (result != HMPP_STS_NO_ERR) {
        return;
    }
    result = HMPPS_TopK_32s(src, srcIndex, srcStride, srcLen, dstValue, dstIndex, dstLen, hint, buffer);
    printf("HMPPS_TopK_32s result = %d\n", result);
    HMPPS_TopKRelease(buffer);
    if (result != HMPP_STS_NO_ERR) {
        return;
    }
    int32_t i;
    printf("dstLen = %d\ndstValue =", dstLen);
    for(i = 0; i < dstLen; ++i) {
        printf(" %d", dstValue[i]);
    }
    printf("\ndstIndex =");
    for(i = 0; i < dstLen; ++i){
        printf(" %d", dstIndex[i]);
    }
    printf("\n");
}

运行结果:

HMPPS_TopKInit_32s result = 0
HMPPS_TopK_32s result = 0
dstLen = 7
dstValue = 16 11 7 5 2 1 0
dstIndex = 6 2 0 5 4 1 3