기본 콘텐츠로 건너뛰기

Intel MKL 예제를 Microsoft Visual C++로 빌드하기

Intel MKL 예제를 Microsoft Visual C++로 빌드하기

인텔 프로세서 시스템에서 아래의 영역에 해당하는 수학 계산을 빠르게 수행하고자 한다면 Intel MKL 라이브러리를 사용할 수 있습니다.

  • Linear Algebra
  • Fast Fourier Transforms (FFT)
  • Vector Statistics & Data Fitting
  • Vector Math & Miscellaneous Solvers

이 문서는 Intel MKL이 제공하는 예제 파일을 Microsoft Visual C++ 로 컴파일하고 링크하여 실행 파일을 만드는 과정을 소개합니다.

빌드 환경

다음은 이 문서를 작성하는 과정에서 Intel MKL 예제를 빌드하기 위하여 사용한 환경입니다.

시스템

  • 운영체제: Windows 10 (64비트)
  • 프로세서: Intel Core i7

설치 제품

환경 변수

  1. 명령 프롬프트 창을 엽니다.

  2. 아래 스크립트를 실행하여 환경 변수INCLUDE, LIB, 그리고 PATH를 설정합니다.

    @echo off
    
    set CPRO_PATH=C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows
    set MKLROOT=%CPRO_PATH%\mkl
    set REDIST=%CPRO_PATH%\redist
    
    set INCLUDE=%MKLROOT%\include;%INCLUDE%
    
    set LIB=%MKLROOT%\lib\intel64;%LIB%
    set PATH=%REDIST%\intel64\mkl;%PATH%
    
    REM for OpenMP intel thread
    set LIB=%CPRO_PATH%\compiler\lib\intel64;%LIB%
    set PATH=%REDIST%\intel64\compiler;%PATH%
    
    REM for TBB thread
    set LIB=%CPRO_PATH%\tbb\lib\intel64\vc_mt;%LIB%
    set PATH=%REDIST%\intel64\tbb\vc_mt;%PATH%
    
    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
    
  3. 아래 스크립트를 실행하여 Visual C++ 사용 환경을 만듭니다. 64-bit로 빌드하기 위하여 vcvarsall.bat의 인자로 amd64를 지정합니다. 인자를 지정하지 않으면 32-bit로 빌드할 것입니다.

    @echo off
    
    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
    

예제-1. ex_nslqp_c

  1. 아래 위치에 있는 예제 압축 파일을 작업하고자 하는 폴더 <working folder>에 풉니다.

    C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl\examples\examples_core_c.zip
    
  2. 명령 프롬프트 창에서 아래의 폴더로 이동합니다.

    <working folder>\solverc
    
  3. 아래 스크립트를 실행하여 ex_nlsqp_c 예제를 빌드합니다.

    @echo off
    
    nmake dllintel64 threading=parallel compiler=msvs function="ex_nlsqp_c"
    nmake libintel64 threading=parallel compiler=msvs function="ex_nlsqp_c"
    nmake dllintel64 threading=sequential compiler=msvs function="ex_nlsqp_c"
    nmake libintel64 threading=sequential compiler=msvs function="ex_nlsqp_c"
    
  4. 아래 폴더에서 빌드 결과로 ex_nlsqp_c.exe 파일이 생성되었는지 확인합니다.

    _results\msvs_lp64_parallel_intel64
    _results\msvs_lp64_parallel_intel64_dll
    _results\msvs_lp64_sequential_intel64
    _results\msvs_lp64_sequential_intel64_dll
    
  5. ex_nlsqp_c.exe 파일을 실행하면 아래와 같은 메시지가 표시됩니다.

    >_results\msvs_lp64_sequential_intel64_dll\ex_nlsqp_c.exe
    |         dtrnlsp Powell............PASS
    

예제-2. matrix_multiplication

  1. 아래 사이트를 방문하여 예제 압축 파일을 다운로드하고 작업하고자 하는 폴더 <working folder>에 풉니다.

  1. 명령 프롬프트 창에서 아래의 폴더로 이동합니다.

    <working folder>\mkl\mkl_c_samples\matrix_multiplication\src
    

FOR 루프 계산

  1. 아래 스크립트를 실행하여 matrix_multiplication 예제를 빌드합니다.

    @echo off
    
    set THREADING=sequential
    set DLL_SUFF=
    set EXAMPLE=matrix_multiplication
    set OMP_LIB=
    cl.exe /c %EXAMPLE%.c
    link.exe mkl_intel_lp64%DLL_SUFF%.lib mkl_core%DLL_SUFF%.lib mkl_%THREADING%%DLL_SUFF%.lib %OMP_LIB% %EXAMPLE%.obj
    
  2. 현재 폴더에 matrix_multiplication.exe 파일이 생성되었는지 확인합니다.

  3. matrix_multiplication.exe 파일을 실행하면 아래와 같은 메시지가 표시됩니다.

    >matrix_multiplication.exe
     
     This example measures performance of rcomputing the real matrix product
     C=alpha*A*B+beta*C using a triple nested loop, where A, B, and C are
     matrices and alpha and beta are double precision scalars
    
     Initializing data for matrix multiplication C=A*B for matrix
     A(2000x200) and matrix B(200x1000)
    
     Allocating memory for matrices aligned on 64-byte boundary for better
     performance
    
     Intializing matrix data
    
     Making the first run of matrix product using triple nested loop
     to get stable run time measurements
    
     Measuring performance of matrix product using triple nested loop
    
     == Matrix multiplication using triple nested loop completed ==
     == at 1219.72656 milliseconds ==
    
     Deallocating memory
    
     Example completed.
    

    계산 소요 시간:

    • 1219.72656 milliseconds

CBLAS API + 멀티쓰레드 계산

  1. 아래 스크립트를 실행하여 dgemm_threading_effect_example 예제를 빌드합니다.

    @echo off
    
    set THREADING=intel_thread
    set DLL_SUFF=
    set EXAMPLE=dgemm_threading_effect_example
    set OMP_LIB=libiomp5md.lib
    cl.exe /c %EXAMPLE%.c
    link.exe mkl_intel_lp64%DLL_SUFF%.lib mkl_core%DLL_SUFF%.lib mkl_%THREADING%%DLL_SUFF%.lib %OMP_LIB% %EXAMPLE%.obj
    
  2. 현재 폴더에 dgemm_threading_effect_example.exe 파일이 생성되었는지 확인합니다.

  3. dgemm_threading_effect_example.exe 파일을 실행하면 아래와 같은 메시지가 표시됩니다.

    >dgemm_threading_effect_example.exe
    
     This example demonstrates threading impact on computing real matrix product
     C=alpha*A*B+beta*C using Intel(R) MKL function dgemm, where A, B, and C are
     matrices and alpha and beta are double precision scalars
    
     Initializing data for matrix multiplication C=A*B for matrix
     A(2000x200) and matrix B(200x1000)
    
     Allocating memory for matrices aligned on 64-byte boundary for better
     performance
    
     Intializing matrix data
    
     Finding max number of threads Intel(R) MKL can use for parallel runs
    
     Running Intel(R) MKL from 1 to 4 threads
    
     Requesting Intel(R) MKL to use 1 thread(s)
    
     Making the first run of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface to get stable run time measurements
    
     Measuring performance of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface on 1 thread(s)
    
     == Matrix multiplication using Intel(R) MKL dgemm completed ==
     == at 17.27277 milliseconds using 1 thread(s) ==
    
     Requesting Intel(R) MKL to use 2 thread(s)
    
     Making the first run of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface to get stable run time measurements
    
     Measuring performance of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface on 2 thread(s)
    
     == Matrix multiplication using Intel(R) MKL dgemm completed ==
     == at 9.67240 milliseconds using 2 thread(s) ==
    
     Requesting Intel(R) MKL to use 3 thread(s)
    
     Making the first run of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface to get stable run time measurements
    
     Measuring performance of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface on 3 thread(s)
    
     == Matrix multiplication using Intel(R) MKL dgemm completed ==
     == at 8.70466 milliseconds using 3 thread(s) ==
    
     Requesting Intel(R) MKL to use 4 thread(s)
    
     Making the first run of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface to get stable run time measurements
    
     Measuring performance of matrix product using Intel(R) MKL dgemm function
     via CBLAS interface on 4 thread(s)
    
     == Matrix multiplication using Intel(R) MKL dgemm completed ==
     == at 7.69962 milliseconds using 4 thread(s) ==
    
     Deallocating memory
    
     It is highly recommended to define LOOP_COUNT for this example on your
     computer as 130 to have total execution time about 1 second for reliability
     of measurements
    
     Example completed.
    

    계산 소요 시간:

    • 17.3 milliseconds using 1 thread(s)
    • 9.7 milliseconds using 2 thread(s)
    • 8.7 milliseconds using 3 thread(s)
    • 7.7 milliseconds using 4 thread(s)

정리

계산 소요 시간 비교

행렬곱을 계산하기 위하여 Intel MKL이 제공하는 CBLAS API와 멀티쓰레드를 사용하면 FOR 루프를 사용할 때에 비하여 소요 시간이 1/158 수준으로 단축됨을 알 수 있었습니다.

  • FOR 루프 계산: 1219.7 milliseconds
  • CBLAS API + 멀티쓰레드 계산: 7.7 milliseconds using 4 thread(s)

스크립트 및 소스 파일

본문에서 소개한 환경 설정 스크립트, 빌드 스크립트, 그리고 행결곱 계산 소스 파일을 GitHub에 올려 놓았습니다.

참고 문서

  1. Developer Guide for Intel® Math Kernel Library for Windows
  2. Using Intel® Math Kernel Library for Matrix Multiplication - C
  3. Intel® Math Kernel Library Link Line Advisor
  4. Compiling and Linking Intel® Math Kernel Library with Microsoft* Visual C++*

Written with StackEdit.

댓글

이 블로그의 인기 게시물

Windows에 AMP와 MediaWiki 설치하기

1. 들어가기     AMP는 Apache + MySQL +  Perl/PHP/Python에 대한 줄임말이다. LAMP (Linux + AMP)라고 하여 Linux에 설치하는 것으로 많이 소개하고 있지만 Windows에서도 간편하게 설치하여 사용할 수 있다.       이 글은 Windows 7에 Apache + MySQL + PHP를 설치하고 그 기반에서 MediaWiki를 설치하여 실행하는 과정을 간략히 정리한 것이다. 2. MySQL     * 버전 5.6.12     1) 다운로드         http://dev.mysql.com/downloads/installer/         MySQL Installer 5.6.12         Windows (x86, 32-bit), MSI Installer         (mysql-installer-web-community-5.6.12.0.msi)     2) 다운로드한 MSI 파일을 더블클릭하여 설치를 진행한다.           설치 위치:                   C:\Program Files\MySQL               선택 사항:                       Install MySQL Products             Choosing a Se...

MATLAB Rutime 설치하기

MATLAB Rutime 설치하기 미설치시 에러 MATLAB Runtime 을 설치하지 않은 환경에서 MATLAB 응용프로그램이나 공유 라이브러리를 사용하려고 하면 아래와 같은 에러 메시지가 표시될 것입니다. 처리되지 않은 예외: System.TypeInitializationException: 'MathWorks.MATLAB.NET.Utility.MWMCR'의 형식 이니셜라이저에서 예 외를 Throw했습니다. ---> System.TypeInitializationException: 'MathWorks.MATLAB.NET.Arrays.MWArray'의 형식 이니셜라이저에서 예외를 Throw했습니다. ---> System.DllNotFoundException: DLL 'mclmcrrt9_3.dll'을(를) 로드할 수 없습니다. 지정된 모듈을 찾을 수 없습니다. (예외가 발생한 HRESULT: 0x8007007E) 위치: MathWorks.MATLAB.NET.Arrays.MWArray.mclmcrInitialize2(Int32 primaryMode) 위치: MathWorks.MATLAB.NET.Arrays.MWArray..cctor() --- 내부 예외 스택 추적의 끝 --- 위치: MathWorks.MATLAB.NET.Utility.MWMCR..cctor() --- 내부 예외 스택 추적의 끝 --- 위치: MathWorks.MATLAB.NET.Utility.MWMCR.processExiting(Exception exception) 해결 방법 이 문제를 해결하기 위해서는 MATLAB Runtime 을 설치해야 합니다. 여러 가지 방법으로 MATLAB Runtime 을 설치할 수 있습니다. MATLAB 이 설치되어 있는 경우에는 MATLAB 설치 폴더 아래에 있는 MATLAB Runtime 설치 프로그램을 실행하여 설치합니다. ...

Wi-Fi 카드 2.4GHz로만 동작시키기

Wi-Fi 카드 2.4GHz로만 동작시키기 별도의 Wi-Fi AP 장치를 두지 않고 아래와 같은 기기들로만 Wi-Fi 네트워크를 구성하고자 할 때 주변 기기들이 2.4GHz만 지원하기 때문에 PC에서 실행하는 AP가 항상 2.4GHz를 사용하도록 Wi-Fi 카드를 설정해 주어야 합니다. 기기 Wi-Fi 카드 주파수 대역 Wi-Fi Direct 지원 PC (Windows 10) 2.4GHz, 5GHz O 주변 기기들 2.4GHz X Wi-Fi 카드별 주파수 대역 선택 방법 Windows 시작 메뉴에서 설정 을 클릭합니다. Windows 설정 화면에서 네트워크 및 인터넷 을 클릭합니다. 설정 화면의 왼쪽 메뉴바에서 Wi-Fi 를 클릭합니다. 화면 오른쪽 관련 설정 구역에 있는 어댑터 옵션 변경 을 클릭합니다. 설정을 바꾸고자 하는 Wi-Fi 카드 항목을 선택하고 마우스 오른쪽을 누른 다음 속성 메뉴를 클릭합니다. 대화상자의 네트워킹 탭 화면에 있는 구성 버튼을 클릭합니다. 장치 속성 대화상자의 고급 탭 화면으로 이동합니다. 제시되는 속성 항목들은 제품별로 다르며 자세한 사항은 아래의 제품별 설명을 참고하여 값을 설정하시기 바랍니다. Intel Dual Band Wireless-AC 7265 기술 사양 주파수 대역: 2.4GHz, 5GHz 무선 표준: 802.11ac 주파수 대역 선택 장치 속성 대화상자에서 아래와 같이 선택합니다. Wireless Mode 1. 802.11a => 5GHz 4. 802.11b/g => 2.4GHz (이 항목 선택) 6. 802.11a/b/g => 2.4GHz, 5GHz Intel Dual Band Wireless-AC 8265 기술 사양 주파수 대역: 2.4GHz, 5GHz 무선 표준: 802.11ac 주파수 대역 선택 장치 속성 대화상자에서 아래와 같이 ...