TopK
Outputs maximum K values of a source vector and their indexes. If same values exist in the source vector, the indexes of theses values calculated by the non-HMPP_TOPK_RADIX algorithm are not sorted. That is, the non-HMPP_TOPK_RADIX algorithm is unstable, and the index sequence after the TopK calculation may be different from that before.
The function calling process is as follows:
- Initialize the buffer by calling Init.
- Call the main function.
- Release the buffer by calling Release.
The function interface declaration is as follows:
- Initialization:
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);
- Main functions:
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);
- Memory release:
HmppResult HMPPS_TopKRelease(uint8_t *buffer);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Pointer to the source vector |
The value cannot be NULL. |
Input |
srcIndex |
Start index (index of pSrc[0]) |
[0, INT_MAX - srcLen) |
Input |
srcStride |
Stride between elements in the source vector |
(0, INT_MAX] |
Input |
srcLen |
Number of elements in the source vector |
(0, INT_MAX] |
Input |
dstValue |
Pointer to the maximum K values in the destination vector |
The value cannot be NULL. |
Output |
dstIndex |
Pointer to the indexes of maximum K values in the destination vector |
The value cannot be NULL. |
Output |
dstLen |
Number of elements in the destination vector |
(0, INT_MAX] |
Input |
hint |
Algorithm model used for the calculation |
HMPP_TOPK_AUTO, HMPP_TOPK_DIRECT, or HMPP_TOPK_RADIX |
Input |
buffer (in the Init function) |
Pointer to the pointer of the buffer required for calculation |
The value cannot be NULL. |
Output |
buffer (in the main and release functions) |
Pointer to the buffer required for calculation |
The value cannot be NULL. |
Input |
Return Value
- Success: HMPP_STS_NO_ERR
- Failure: An error code is returned.
Error Codes
Error Code |
Description |
|---|---|
HMPP_STS_NO_ERR |
No error occurs. |
HMPP_STS_NULL_PTR_ERR |
Any of the specified pointers is NULL. |
HMPP_STS_SIZE_ERR |
The value of srcLen or dstLen is less than or equal to 0. |
HMPP_STS_BAD_ARG_ERR |
The value of hint is not supported. |
HMPP_STS_OVERFLOW |
The function overflows. |
HMPP_STS_MALLOC_FAILED |
The Init function failed to allocate the memory required by the algorithm model. |
Note
Before calling this interface, you must call the Init interface to initialize the buffer memory, maximum K values, and indexes of maximum K values.
Example
#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");
}
Output:
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