Intel® Integrated Performance Primitives Developer Guide and Reference

ID 790148
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

TopK

Returns maximum K values of an array.

Syntax

IppStatus ippsTopK_32f(const Ipp32f* pSrc, Ipp64s srcIndex, Ipp64s srcStride, Ipp64s srcLen, Ipp32f* pDstValue, Ipp64s* pDstIndex, Ipp64s dstLen, IppTopKMode hint, Ipp8u* pBuffer);

IppStatus ippsTopK_32s(const Ipp32s* pSrc, Ipp64s srcIndex, Ipp64s srcStride, Ipp64s srcLen, Ipp32s* pDstValue, Ipp64s* pDstIndex, Ipp64s dstLen, IppTopKMode hint, Ipp8u* pBuffer);

Include Files

ipps.h

Domain Dependencies

Headers: ippcore.h, ippvm.h

Libraries: ippcore.lib, ippvm.lib

Parameters

pSrc

Pointer to the source vector.

srcIndex

Index of the pSrc[0] element, index of pSrc[n] is equal to srcIndex+n.

srcStride

The stride between elements in a source array. The unit of srcStride is Byte.

srcLen

Number of elements in the source vector.

pDstValue

Maximum values found by the function.

dstIndex

Indexes of maximum values.

dstLen

Number of K values to be returned, the function returns min(K, dstLen) elements.

hint

Parameter to choose the optimization that is most suitable for the srcLen+dstlen(K) combination, supported values: ippTopKAuto/ ippTopKDirect/ ippTopKRadix.

pBuffer

Pointer to the work buffer.

Description

This function searches for dstLen maximum values and their indexes in an input vector. The function is designed to process large input vectors by small blocks, it takes into account results of previous blocks processing getting maximum values of pSrc and pDstValue and then combining the final results into pDstValue.

The srcIndex parameter stores the index of the first element pSrc[0] of each new block, thus supporting the continuous numbering of elements. Before calling the ippsTopK function, compute the required buffer size and initialize pDstValue and pDstIndex using the ippsTopKGetBufferSize and ippsTopKInit_32s functions, respectively.

Return Values

ippStsNoErr

Indicates no error.

ippStsNullPtrErr

Indicates an error when at least one of the pointers is NULL.

ippStsSizeErr

Indicates an error when at least one of the srcLen or dstLen values is less than, or equal to zero.

ippStsBadArgErr

Indicates an error when the hint value is not supported.

Example

/*******************************************************************************
* Copyright (C) 2023 Intel Corporation.
*
* This software and the related documents are Intel copyrighted materials, and your use of them is governed by
* the express license under which they were provided to you ('License'). Unless the License provides otherwise,
* you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
* documents without Intel's prior written permission.
* This software and the related documents are provided as is, with no express or implied warranties, other than
* those that are expressly stated in the License.
*
*******************************************************************************/

#include <stdio.h>
#include "ipp.h"

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN exitLine:                                  /* Label for Exit */
#define check_sts(st) if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) Integrated Primitives (Intel(R) IPP) function returned status different from ippStsNoErr */

/* Results of ippMalloc() are not validated because Intel(R) IPP functions perform bad arguments check and will return an appropriate status  */

int main()
{
    Ipp32f src[10] = { 2.5, 1.0, 3.2, 4.7, 0.8, 5.6, 6.1, 3.9, 2.3, 4.0 };
    Ipp32f dst[5];
    Ipp64s idx[5];
    IppStatus status;
    int i;

    printf("\nSource vector\n");
    for (i = 0; i < 10; i++) printf("%f ", src[i]);

    Ipp64s buf_size;
    ippsTopKGetBufferSize(10, 5, ipp32f, ippTopKAuto, &buf_size);

    Ipp8u buf[buf_size];
    ippsTopKInit_32f(dst, idx, 5);

    check_sts(status = ippsTopK_32f(src, 0, 4, 10, dst, idx, 5, ippTopKAuto, buf));

    printf("\nTop k values:\n");
    for (i = 0; i < 5; i++) printf("%f ", dst[i]);

EXIT_MAIN
    printf("\nExit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}

See Also