How to use Intel IPP

IPP?

  • Intel Integrated Performance Primitives (IPP)
  • 이미지 처리 및 신호 처리
  • C/C++ 언어와 함께 사용
  • https://www.intel.com/content/www/us/en/developer/tools/oneapi/ipp.html#gs.3aq3sv

IPP 라이브러리 설치하기:

Option 1. Intel oneAPI Base Toolkit

Option 2. 독립형 Intel IPP

chmod +x l_ipp_oneapi_p_2021.10.1.16_offline.sh # 권한부여
./l_ipp_oneapi_p_2021.10.1.16_offline.sh # 설치
  • 설치 UI 팝업 (따라서 할 것)
  • 설치된 경로 예시 /home/ko/intel/oneapi

Option 3. 클라우드 기반

  • 시도하지 않음. 나중에

프로젝트 설정:

IPP를 사용하는 C/C++ 프로젝트를 설정해야 합니다. 프로젝트 설정에는 IPP 라이브러리의 헤더 파일 및 라이브러리 파일을 포함하고, 컴파일러 및 링커 플래그를 설정해야 합니다.

  • include headerfiles
    • 전처리 지시문을 이용해 포함시킴.
    • 소스코드 파일에서 다른 헤더 파일을 포함시킴.
    • 함수 선언, 매크로, 데이터 구조, 클래스 정의 등을 포함
    • 코드의 재사용과 구조화에 사용
#include <stdio.h>
#include <ipp.h>
  • library
    • 컴파일된 코드 및 함수의 모음. 프로그램에서 재사용될 수 있는 기능을 제공
    • 정적 라이브러리 .a, .lib - 컴파일 시에 프로그램에 포함
    • 공유 라이브러리 .so, .dll - 실행 시에 필요한 경우에 로드
  • linker
    • 여러 소스 코드 파일을 하나의 실행 파일로 결합하는 역할
    • 컴파일러는 각 소스 파일을 개별적으로 컴파일하여 중간 바이너리 파일을 생성, 링커는 이 중간 파일들을 결합하여 최종 실행 파일 생성.
    • 라이브러리 사용 시, 링커는 해당 함수가 어떤 라이브러리에 정의되어 있는지 찾아서 링크합니다.
    • 함수를 사용할 때, 어떤 라이브러리에 들어 있는 지 알고 써야 함.
      • ippcore: 기본 이미지 프로세싱 함수
      • ippi: 영상처리 관련
      • ipps: 신호처리 관련
      • ippvm: 벡터 연산 및 행렬 연산 함수

실제 방법

  • 요약: source code 작성 후 vs code 터미널에서 소싱한 후 컴파일하면 됨.
  • 자세히:
  • 환경변수 설정, 실행가능하게 권한설정
cd /home/ko/intel/oneapi # setvar.sh 경로
chmod setvars.sh # 실행 가능하게 바꿔줌
source setvars.sh # 기본형, 일회성, vs code 터미널에서 소싱하여 해당 터미널에서 사용할 수 있게 함. 
  • 환경변수 설정, 실제 컴파일할 VS CODE 환경에서 소싱
source /home/ko/intel/oneapi/setvars.sh # 실제 사용

# 혹은 . setvars.sh
 
# :: initializing oneAPI environment ...
#    bash: BASH_VERSION = 5.0.17(1)-release
#    args: Using "$@" for setvars.sh arguments: 
# :: compiler -- latest
# :: ipp -- latest
# :: tbb -- latest
# :: oneAPI environment initialized ::

  • verify
env | grep SETVARS_COMPLETED # verify
# SETVARS_COMPLETED=1 # 잘 설정되었다면, 1 출력. 그 외에는 모두 fail을 뜻 함.
echo $ONEAPI_ROOT # verify
  • 컴파일
gcc -o example example.c -I$ONEAPI_ROOT/ipp/latest/include -L$ONEAPI_ROOT/ipp/latest/lib/intel64 -lippcore -lippi -lipps -lippvm
gcc -o example example.c -I$ONEAPI_ROOT/ipp/latest/include -L$ONEAPI_ROOT/ipp/latest/lib/intel64 -lippcore -lippi -lipps -lippvm -lm
# gcc 컴파일러
# -o example 출력파일 이름 지정 옵션
# my_program.c 코드
# -I$ONEAPI_ROOT/ipp/latest/include : include headerfiles
# -L$ONEAPI_ROOT/ipp/latest/lib/intel64 library
# -lippcore -lippi -lipps -lippvm -lm linker
  • 참고 경로….
    • getting started https://www.intel.com/content/www/us/en/docs/ipp/get-started-guide-oneapi-linux/2021-10/overview.html
    • https://www.intel.com/content/www/us/en/docs/oneapi/programming-guide/2023-1/use-the-setvars-script-with-linux-or-macos.html
      • setvars.sh
      • https://www.intel.com/content/www/us/en/docs/oneapi/programming-guide/2023-1/use-a-config-file-for-setvars-sh-on-linux-or-macos.html
    • 환경 변수 설정 스크립트 위치
      • 환경변수 IPPROOT, LD_LIBRARY_PATH, and NLSPATH
      • 스크립트 위치 /home/ko/intel/oneapi/setvars.sh

VS CODE

  • 요약: 커맨드 라인에 치기 번거롭기 때문에 .vscode 폴더를 생성하고 그 아래에 tasks.json을 미리 만들어둔다.
  • 참고: https://www.intel.com/content/www/us/en/docs/oneapi/user-guide-vs-code/2024-0/developing-a-visual-studio-code-project.html
  • https://95mkr.tistory.com/entry/DD1
  • 스크립트 예시:
    • ./c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/intel/ipp/latest/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
- .vscode/tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "/usr/bin/gcc",
            "args": [
                "-o",
                "my_program",
                "my_program.c",
                "-I${workspaceFolder}/include",
                "-L/opt/intel/ipp/latest/lib/intel64",
                "-lippcore",
                "-lippi",
                "-lipps",
                "-lippvm"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "shared",
                "focus": false
            },
            "problemMatcher": "$gcc"
        }
    ]
}

IPP 함수 호출:

IPP는 다양한 이미지 처리 및 신호 처리 함수를 제공합니다. 필요한 작업에 따라 IPP 함수를 호출하여 이미지 또는 신호 처리를 수행할 수 있습니다. 예를 들어, 영상 블러링, 필터링, FFT, DCT, 컬러 변환 및 기타 작업을 수행하는 함수가 포함되어 있습니다.

라이센스 관리:

IPP는 상용 제품 또는 상용용으로 사용될 경우 라이선스를 구매해야 할 수 있습니다. Intel의 라이선스 정책을 준수하고 필요한 라이선스를 획득해야 합니다.

문서 및 예제 확인:

for Window

컴파일할 때 사용: .h .lib

  • Property manager 우클릭 > Project property pages > Configuration properties > C/C++ > General > Additional Include Directories > D:\Program Files (x86)\Intel\oneAPI\ipp\latest\include 입력 (설치경로에 따라)
  • Property manager 우클릭 > Project property pages > Configuration properties > Linker > General > Additional Library Directories > D:\Program Files %28x86%29\Intel\oneAPI\ipp\2021.10\lib (원래는 lib\intel64까지 선택하라고 되어 있었으나 버전별로 다르다고 함)
    • 여기서 solution explorer에 external dependency에 ipp.h같은 게 뜬다.
  • Property manager 우클릭 > Project property pages > Configuration properties > Linker > Input > AdditionalDependencies > ippcore.lib;ippi.lib;ipps.lib;ippvm.lib;
    • (주의) D:\Program Files (x86)\Intel\oneAPI\ipp\latest\lib에 실제로 ippcore.lib가 있는지 확인할 것.

실행 시 사용: .dll

  • 실행파일 .exe 경로에 .dll을 함께 배포해야 실행이 됨.
  • 옵션1. ippcore.dll, ippi.dll, ipps.dll, ippvm.dll, ippil9을 실행파일과 동일한 경로에 복사함.
  • 옵션2. 특정 폴더(ex. bin)를 생성하고 하위 경로에 넣어준 후, 배치 파일(.bat)을 이용하여 실행하는 방법을 통해 PATH 환경 변수에 bin 폴더를 임시로 추가

  • https://learn.microsoft.com/ko-kr/cpp/build/working-with-project-properties?view=msvc-170

디버깅 시 사용: .dll

  • 환경변수에 경로 등록

Example

Add

  • Adds pixel values of two images.

Syntax

Case 1: Not-in-place operation on integer or complex data

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, int src1Step, const Ipp<datatype>* pSrc2, int src2Step, Ipp<datatype>* pDst, int dstStep, IppiSize roiSize, int scaleFactor);
  • Supported values for mod:
     
8u_C1RSfs 16u_C1RSfs 16s_C1RSfs
8u_C3RSfs 16u_C3RSfs 16s_C3RSfs
8u_C4RSfs 16u_C4RSfs 16s_C4RSfs
8u_AC4RSfs 16u_AC4RSfs 16s_AC4RSfs

Case 2: Not-in-place operation on floating point or complex data

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, int src1Step, const Ipp<datatype>* pSrc2, int src2Step, Ipp<datatype>* pDst, int dstStep, IppiSize roiSize);

Supported values for mod:

     
32f_C1R 32f_C3R 32f_C4R
IppStatus ippiAdd_32f_AC4R(const Ipp32f* pSrc1, int src1Step, const Ipp32f* pSrc2, int src2Step, Ipp32f* pDst, int dstStep, IppiSize roiSize);

Case 3: In-place operation on integer or complex data

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc, int srcStep, Ipp<datatype>* pSrcDst, int srcDstStep, IppiSize roiSize, int scaleFactor);

Supported values for mod:

     
8u_C1IRSfs 16u_C1IRSfs 16s_C1IRSfs
8u_C3IRSfs 16u_C3IRSfs 16s_C3IRSfs
8u_AC4IRSfs 16u_AC4IRSfs 16s_AC4IRSfs
8u_C4IRSfs 16u_C4IRSfs 16s_C4IRSfs

Case 4: In-place operation on floating point or complex data

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc, int srcStep, Ipp<datatype>* pSrcDst, int srcDstStep, IppiSize roiSize);

Supported values for mod:

   
32f_C1IR 32f_C3IR
32f_AC4IR 32f_C4IR

Case 5: In-place operation using a floating point accumulator image

IppStatus ippiAdd_<mod>(const Ipp<srcDatatype>* pSrc, int srcStep, Ipp32f* pSrcDst, int srcDstStep, IppiSize roiSize);

Supported values for mod:

   
8u32f_C1IR 16u32f_C1IR

Case 6: Masked in-place operation using a floating point accumulator image

IppStatus ippiAdd_<mod>(const Ipp<srcDatatype>* pSrc, int srcStep, const Ipp8u* pMask, int maskStep, Ipp32f* pSrcDst, int srcDstStep, IppiSize roiSize);

Supported values for mod:

     
8u32f_C1IMR 16u32f_C1IMR 32f_C1IMR

Case 7: Not-in-place operation on integer data with platform-aware functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, IppSizeL src1Step, const Ipp<datatype>* pSrc2, IppSizeL src2Step, Ipp<datatype>* pDst, IppSizeL dstStep, IppiSizeL roiSize, int scaleFactor);

Supported values for mod:

     
8u_C1RSfs_L 16u_C1RSfs_L 16s_C1RSfs_L
8u_C3RSfs_L 16u_C3RSfs_L 16s_C3RSfs_L
8u_C4RSfs_L 16u_C4RSfs_L 16s_C4RSfs_L
8u_AC4RSfs_L 16u_AC4RSfs_L 16s_AC4RSfs_L

Case 8: Not-in-place operation on floating point data with platform-aware functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, IppSizeL src1Step, const Ipp<datatype>* pSrc2, IppSizeL src2Step, Ipp<datatype>* pDst, IppSizeL dstStep, IppiSizeL roiSize);

Supported values for mod:

       
32f_C1R_L 32f_C3R_L 32f_C4R_L 32f_AC4R_L

Case 9: In-place operation on integer data with platform-aware functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc, IppSizeL srcStep, Ipp<datatype>* pSrcDst, IppSizeL srcDstStep, IppiSizeL roiSize, int scaleFactor);

Supported values for mod:

     
8u_C1IRSfs_L 16u_C1IRSfs_L 16s_C1IRSfs_L
8u_C3IRSfs_L 16u_C3IRSfs_L 16s_C3IRSfs_L
8u_C4IRSfs_L 16u_C4IRSfs_L 16s_C4IRSfs_L
8u_AC4IRSfs_L 16u_AC4IRSfs_L 16s_AC4IRSfs_L

Case 10: In-place operation on floating point data with platform-aware functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, IppSizeL src1Step, Ipp<datatype>* pDst, IppSizeL dstStep, IppiSizeL roiSize);

Supported values for mod:

       
32f_C1IR_L 32f_C3IR_L 32f_C4IR_L 32f_AC4IR_L

Case 11: Not-in-place operation on integer data with threading layer (TL) functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, IppSizeL src1Step, const Ipp<datatype>* pSrc2, IppSizeL src2Step, Ipp<datatype>* pDst, IppSizeL dstStep, IppiSizeL roiSize, int scaleFactor);

Supported values for mod:

     
8u_C1RSfs_LT 16u_C1RSfs_LT 16s_C1RSfs_LT
8u_C3RSfs_LT 16u_C3RSfs_LT 16s_C3RSfs_LT
8u_C4RSfs_LT 16u_C4RSfs_LT 16s_C4RSfs_LT
8u_AC4RSfs_LT 16u_AC4RSfs_LT 16s_AC4RSfs_LT

Case 12: Not-in-place operation on floating point data with TL functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, IppSizeL src1Step, const Ipp<datatype>* pSrc2, IppSizeL src2Step, Ipp<datatype>* pDst, IppSizeL dstStep, IppiSizeL roiSize);

Supported values for mod:

       
32f_C1R_LT 32f_C3R_LT 32f_C4R_LT 32f_AC4R_LT

Case 13: In-place operation on integer data with TL functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc, IppSizeL srcStep, Ipp<datatype>* pSrcDst, IppSizeL srcDstStep, IppiSizeL roiSize, int scaleFactor);

Supported values for mod:

     
8u_C1IRSfs_LT 16u_C1IRSfs_LT 16s_C1IRSfs_LT
8u_C3IRSfs_LT 16u_C3IRSfs_LT 16s_C3IRSfs_LT
8u_C4IRSfs_LT 16u_C4IRSfs_LT 16s_C4IRSfs_LT
8u_AC4IRSfs_LT 16u_AC4IRSfs_LT 16s_AC4IRSfs_LT

Case 14: In-place operation on floating point data with TL functions

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc1, IppSizeL src1Step, Ipp<datatype>* pDst, IppSizeL dstStep, IppiSizeL roiSize);

Supported values for mod:

       
32f_C1IR_LT 32f_C3IR_LT 32f_C4IR_LT 32f_AC4IR_LT

Case 15: In-place operation on integer data with TL functions based on classic API

IppStatus ippiAdd_<mod>(const Ipp<datatype>* pSrc, IppSize srcStep, Ipp<datatype>* pSrcDst, IppSize srcDstStep, IppiSize roiSize, int scaleFactor);

Supported values for mod:

   
16s_C1IRSfs_T 32s_C1IRSfs_T
16s_C3IRSfs_T  
16s_C4IRSfs_T  

Include Files

ippcv.h
ippi.h
ippi_l.h
ippi_tl.h

Domain Dependencies

Flavors declared in ippi.h:
Headers: ippcore.h, ippvm.h, ipps.h
Libraries: ippcore.lib, ippvm.lib, ipps.lib
Flavors declared in ippcv.h:
Headers: ippcore.h, ippvm.h, ipps.h, ippi.h
Libraries: ippcore.lib, ippvm.lib, ipps.lib, ippi.lib
Flavors declared in ippi64x.h:
Libraries: ippcore.lib, ippvm.lib, ipps.lib, ippi.lib, ippcore_tl.lib, ippi_tl.lib

Parameters

파라미터 설명
pSrc, pSrc1, pSrc2 Pointer to the ROI in the source images.
srcStep, src1Step, src2Step Distance, in bytes, between the starting points of consecutive lines in the source images.
pDst Pointer to the destination image ROI.
dstStep Distance, in bytes, between the starting points of consecutive lines in the destination image.
pSrc Pointer to the first source image ROI for the in-place operation.
srcStep Distance, in bytes, between the starting points of consecutive lines in the first source image for the in-place operation.
pSrcDst Pointer to the second source and destination image ROI for the in-place operation.
srcDstStep Distance, in bytes, between the starting points of consecutive lines in the source and destination image for the in-place operation.
pMask Pointer to the mask image ROI for the masked operation.
maskStep Distance, in bytes, between the starting points of consecutive lines in the mask image for the masked operation.
roiSize Size of the source and destination ROI in pixels.
scaleFactor Scale factor (see Integer Result Scaling).

Description

This function operates with ROI.

This function adds corresponding pixel values of two source image buffers and places the results in a destination buffer. In case of operations on integer data, the resulting values are scaled by scaleFactor. For complex data, the function processes both real and imaginary parts of pixel values. Some function flavors add 8u, 8s, 16u, or 32f source image pixel values to a floating point accumulator image in-place. Addition of pixel values in case of a masked operation is performed only if the respective mask value is non-zero; otherwise, the accumulator pixel value remains unchanged.

NOTE:
For the functions that operate on complex data, step values must be positive. 
For the functions that use an accumulator image, step values must be no less than roiSize.width*<pixelSize>.

Functions with AC4 descriptor do not process alpha channels.

Function flavors described in Case 5 and Case 6 are declared in the ippcv.h.

Return Values

파라미터 설명
ippStsNoErr Indicates no error. Any other value indicates an error or a warning.
ippStsNullPtrErr Indicates an error condition when any of the specified pointers is NULL.
ippStsSizeErr Indicates an error condition when roiSize has a field with zero or negative value.
ippStsStepErr Indicates an error condition in the following cases:
  For functions that operate on complex data, if any of the specified step values is zero or negative.
  For functions using an accumulator image, if any of the specified step values is less than roiSize.width * .
ippStsNotEvenStepErr Indicates an error condition when one of step values for floating-point images cannot be divided by 4.

Source Code

The code example below shows how to use the function ippiAdd_8u_C1RSfs.

Ipp8u src1[8*4] = {8, 4, 2, 1, 0, 0, 0, 0,
                   8, 4, 2, 1, 0, 0, 0, 0,
                   8, 4, 2, 1, 0, 0, 0, 0,
                   8, 4, 2, 1, 0, 0, 0, 0};
Ipp8u src2[8*4] = {4, 3, 2, 1, 0, 0, 0, 0,
                   4, 3, 2, 1, 0, 0, 0, 0,
                   4, 3, 2, 1, 0, 0, 0, 0,
                   4, 3, 2, 1, 0, 0, 0, 0};
Ipp8u dst[8*4];
IppiSize srcRoi = { 4, 4 };
Int scaleFactor = 1;    // later examples for 2 and -2 values

ippiAdd_8u_C1RSfs (src1, 8, src2, 8, dst, 4, srcRoi, scaleFactor );

Result:
    src1                src2            
8 4 2 1 0 0 0 0    4 3 2 1 0 0 0 0
8 4 2 1 0 0 0 0    4 3 2 1 0 0 0 0
8 4 2 1 0 0 0 0    4 3 2 1 0 0 0 0
8 4 2 1 0 0 0 0    4 3 2 1 0 0 0 0

dst >>    scaleFactor = 1        scaleFactor = 2    ScaleFactor = -2
             6 4 2 1                3 2 1 0            48 28 16 8
             6 4 2 1                3 2 1 0            48 28 16 8
             6 4 2 1                3 2 1 0            48 28 16 8
             6 4 2 1                3 2 1 0            48 28 16 8

실습

#include "stdio.h"
#include "ipp.h"

int main() {
    Ipp8u src1[8*4] = {8, 4, 2, 1, 0, 0, 0, 0,
                       8, 4, 2, 1, 0, 0, 0, 0,
                       8, 4, 2, 1, 0, 0, 0, 0,
                       8, 4, 2, 1, 0, 0, 0, 0};

    Ipp8u src2[8*4] = {4, 3, 2, 1, 0, 0, 0, 0,
                       4, 3, 2, 1, 0, 0, 0, 0,
                       4, 3, 2, 1, 0, 0, 0, 0,
                       4, 3, 2, 1, 0, 0, 0, 0};

    Ipp8u dst[8*4];
    IppiSize srcRoi = {4, 4};
    int scaleFactor = 1;

    IppStatus status = ippiAdd_8u_C1RSfs(src1, 8, src2, 8, dst, 4, srcRoi, scaleFactor);

    if (status == ippStsNoErr) {
        printf("Result:\n");
        for (int i = 0; i < srcRoi.height; i++) {
            for (int j = 0; j < srcRoi.width; j++) {
                printf("%d ", dst[i * srcRoi.width + j]);
            }
            printf("\n");
        }
    } else {
        printf("Error: %s\n", ippGetStatusString(status));
    }

    return 0;
}
  • 컴파일 및 실행
# VS code terminal
# go to project path
source /home/ko/intel/oneapi/setvars.sh
gcc -o official_example official_example.cpp -I$ONEAPI_ROOT/ipp/latest/include -L$ONEAPI_ROOT/ipp/latest/lib/intel64 -lippcore -lippi -lipps -lippvm -lm
./official_example
# Result:
# 6 4 2 1 
# 6 4 2 1 
# 6 4 2 1 
# 6 4 2 1