---
title: TopK
description: "输出源向量最大K个值及其索引。如果源向量中有相同值，则非HMPP_TOPK_RADIX算法计算这些相同值所对应的索引不排序，即非HMPP_TOPK_RADIX算法是不稳定的，TopK计算后的索引顺序与计算之前的顺序可能不同。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0175.html
sourcePath: /source/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0175.html
indexId: 008096a476847c7106af21f292b42128e6811a6ba6339ecaa9876cdf00e5051977
---
# 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
```
