金子邦彦研究室インストールWindows の種々のソフトウェア(インストール)OpenBLAS のインストール(ソースコードを使用)(clang, flang を利用)(Windows 上)

OpenBLAS のインストール(ソースコードを使用)(clang, flang を利用)(Windows 上)

BLAS(Basic Linear Algebra Subprograms)は,行列演算,ベクトル演算の機能をもったプログラム群である.

目次

  1. 前準備
  2. perl, nijna, clang, flang および関連パッケージのインストール(conda3 を使用)
  3. Windows で OpenBLAS をソースコードからビルドして,インストールする
  4. OpenBLAS の動作確認

サイト内の関連ページ

下に書いているように,vcpkg を利用してインストールすることができる. 特に問題がない場合には vcpkg の利用してのインストールを検討しよう. vcpkg使わずにインストールする必要がある場合は,このページの手順によりインストールを行うことができる.

OpenBlas のインストール(vcpkg を利用)(Windows 上)

  1. Git のインストール

    Windows での Git のインストール: 別ページ »で説明している.

  2. Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)のインストール

    Windows での Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022) のインストール: 別ページ »で説明している.

  3. vckpg のインストール

    Windows で,コマンドプロンプト管理者として実行.

    次のコマンドを実行

    if not exist c:\vcpkg (
      cd c:\
      rmdir /s /q vcpkg
      git clone https://github.com/microsoft/vcpkg
      cd c:\vcpkg
      git pull
      .\bootstrap-vcpkg.bat
      .\vcpkg update
      .\vcpkg integrate install  
      powershell -command "[System.Environment]::SetEnvironmentVariable(\"VCPKG_ROOT\", \"c:\vcpkg\", \"Machine\")"
      powershell -command "[System.Environment]::SetEnvironmentVariable(\"VCPKG_DEFAULT_TRIPLET\", \"x64-windows\", \"Machine\")"
      powershell -command "[System.Environment]::SetEnvironmentVariable(\"CMAKE_TOOLCHAIN_FILE\", \"c:/vcpkg/scripts/buildsystems/vcpkg.cmake\", \"Machine\")"
      powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"LIB\", \"Machine\"); $oldpath += \";c:\vcpkg\installed\x64-windows\lib\"; [System.Environment]::SetEnvironmentVariable(\"LIB\", $oldpath, \"Machine\")"
      powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"INCLUDE\", \"Machine\"); $oldpath += \";c:\vcpkg\installed\x64-windows\include\"; [System.Environment]::SetEnvironmentVariable(\"INCLUDE\", $oldpath, \"Machine\")"
      powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\vcpkg\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
      powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\vcpkg\installed\x64-windows\bin\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    )
    
  4. OpenBlas のインストール

    次のコマンドを実行

    c:\vcpkg\vcpkg search openblas
    c:\vcpkg\vcpkg install --triplet x64-windows openblas[threads]
    powershell -command "[System.Environment]::SetEnvironmentVariable(\"OpenBLAS\", \"C:\vcpkg\installed\x64-windows\", \"Machine\")"
    powershell -command "[System.Environment]::SetEnvironmentVariable(\"OpenBLAS_ROOT\", \"C:\vcpkg\installed\x64-windows\", \"Machine\")"
    

BLAS の主な機能(ごく一部を紹介)

前準備

Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022),Visual Studio 2022 のインストール(Windows 上)

インストールの判断

Build Tools for Visual Studio は,開発ツールセットである. Visual Studio は統合開発環境であり,いくつかの種類があり,Build Tools for Visual Studioの機能を含むか連携して使用するものである.インストールは以下の基準で判断してください:

不明な点がある場合は,Visual Studio 全体をインストール を行う方が良い.

Git のインストール(Windows 上)

Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.

サイト内の関連ページ

Windows での Git のインストール: 別ページ »で説明している.

関連する外部ページ

Git の公式ページ: https://git-scm.com/

CMake のインストール(Windows 上)

CMake はビルドツールである.

サイト内の関連ページ

Windows での cmake のインストール: 別ページ »で説明している.

関連する外部ページ

CMake の公式ダウンロードページ: https://cmake.org/download/

miniconda3 のインストール

Anaconda をすでにインストール済みのときは,miniconda をインストールしないことAnaconda3 の中の conda を使うことにする)

Windows での miniconda3 のインストール手順は, 別ページ »で説明している.

perl, nijna, clang, flang および関連パッケージのインストール(conda を使用)

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. perl, nijna, clang, flang および関連パッケージのインストール

    https://github.com/xianyi/OpenBLAS/wiki/Installation-Guide の記述による

    kitware/nijna の説明は https://github.com/Kitware/ninja

    miniconda3 をインストールしたときに,パスを通していなかった場合は,「conda」と書く代わりに,「c:\tools\miniconda3\Library\bin\conda.bat」あるいは「C:\ProgramData\miniconda3\scripts\conda」のようにフルパスで指定する.

    conda install -y perl 
    conda install -y -c conda-forge flang clangdev libflang
    conda config --remove channels conda-forge
    conda install -y -c isuruf kitware-ninja
    conda config --remove channels isuruf
    
    [image]
  3. 新しくWindowsコマンドプロンプトを開く
  4. OpenBLAS のインストールの前に、perl, cmake の確認

    エラーメッセージが出なければ OK.

    where perl
    where cmake
    

    [image]
  5. clang, flang のバージョン、インストールディレクトリ等の確認
    clang --version
    flang --version
    

    [image]
  6. システム環境変数 LIB先頭に次を追加

    miniconda3 の配下にインストールされたライブラリ等を有効にするため

    c:\tools\miniconda3」は実際のインストールディレクトリに置き換えること

    c:\tools\miniconda3\Library\lib
    

    [image]
  7. システム環境変数 CPATH先頭に次を追加

    Anaconda3 の配下にインストールされたインクルードファイル等を有効にするため

    c:\tools\miniconda3」は実際のインストールディレクトリに置き換えること

    c:\tools\miniconda3\Library\include
    

    [image]

OpenBLAS のインストール(Windows 上)

関連する外部ページhttps://github.com/xianyi/OpenBLAS/wiki/Installation-Guide

  1. OpenBLAS のウェブページを開く

    https://www.openblas.net/

  2. このウェブページで利用条件などを確認

    必ず、最新情報を確認すること

    [image]
  3. Visual Studio の x64 Native Tools コマンドプロンプトを起動.

    起動は,Windows のメニューで「Visual Studio 20..」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ.「x64」は,64ビット版の意味である.

    「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」がないとき:

    C++ ビルドツール (Build Tools) のインストールを行うことで, 「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」がインストールされる.その手順は,別ページ »で説明

  4. OpenBLAS のインストールディレクトリを空にする
    mkdir c:\tools
    cd c:\tools
    rmdir /s /q OpenBLAS
    

    [image]
  5. ソースコードのダウンロード
    cd c:\tools
    git clone https://github.com/xianyi/OpenBLAS.git
    

    [image]
  6. cmake の実行

    https://github.com/xianyi/OpenBLAS/wiki/Installation-Guide の記述による

    cd c:\tools
    cd OpenBLAS
    rmdir /s /q build
    mkdir build
    cd build
    del CMakeCache.txt
    cmake .. -G "Ninja" ^
        -DCMAKE_C_COMPILER=clang-cl ^
        -DCMAKE_Fortran_COMPILER=flang ^
        -DNOFORTRAN=0 -DDYNAMIC_ARCH=OFF ^
        -DCMAKE_INSTALL_PREFIX="c:\tools\OpenBLAS" ^
        -DCMAKE_BUILD_TYPE=Release
    

    [image]
  7. cmake の結果の確認

    エラーメッセージが出ていないこと

    ※ ここでエラーが出た場合には、 「x64 Native Tools コマンドプロンプト」を使っていることを確認する ※ ここでエラーが出て、 「x64 Native Tools コマンドプロンプト」を使っている場合は、 システム環境変数 LIBシステム環境変数 CPATHを確認する

    [image]
  8. ビルド
    cmake --build . --config Release --target install 
    

    [image]
  9. ビルド結果の確認

    [image]
  10. c:\tools\OpenBLAS の下にファイルができるので確認する

    [image]

    [image]

    [image]
  11. Windowsシステム環境変数CPATHc:\tools\OpenBLAS\include追加

    Windows での環境変数の設定は,マイコンピュータを右クリック → プロパティ→ 詳細設定 → 環境変数をクリック

    Windowsの画面の表示では、「\」(円マーク)が表示される

    [image]
  12. Windowsシステム環境変数 LIBc:\tools\OpenBLAS\lib追加

    [image]
  13. Windowsシステム環境変数 Pathに,c:\tools\OpenBLAS\bin追加することにより,パスを通す

    Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

    次のコマンドを実行

    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\tools\OpenBLAS\bin\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    

    [image]
  14. Windowsシステム環境変数 OpenBLAS, OpenBLAS_HOMEc:\tools\OpenBLAS を設定

    これは OpenCV のビルドのときに利用される環境変数

OpenBLAS の動作確認

https://gist.github.com/xianyi/6930656 に掲載の プログラムを利用

Windows での確認手順と結果は次の通り

  1. Visual Studio の x64 Native Tools コマンドプロンプトを起動.

    起動は,Windows のメニューで「Visual Studio 20..」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ.「x64」は,64ビット版の意味である.

    「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」がないとき:

    C++ ビルドツール (Build Tools) のインストールを行うことで, 「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」がインストールされる.その手順は,別ページ »で説明

  2. プログラムの準備

    https://gist.github.com/xianyi/6930656 に掲載の プログラムを利用. hoge.cのようなファイル名で保存.

  3. ビルドして実行

    ビルド時にエラーが出たときは、システム環境変数 LIB を確認すること

    cl hoge.c /I "c:\tools\OpenBLAS\include\openblas" openblas.lib
    .\hoge.exe
    

    [image]

    次のように「コンピュータに openblas.dllがないため、プログラムを開始できません。」と表示される場合がある

    [image]

    このときは、 Windowsシステム環境変数 Path に c:\tools\OpenBLAS\bin を追加しているか、確認すること

サンプルプログラム

OpenBLAS を用いて行列の積を求める.実行結果は何も表示されない.

#include "stdafx.h"
#include<stdlib.h>
#include<cblas.h>
#define N 2000

int main()
{
	// C = AB
	int i;
	double *A, *B, *C;

	A = (double *)malloc(sizeof(double) * N * N);
	B = (double *)malloc(sizeof(double) * N * N);
	C = (double *)malloc(sizeof(double) * N * N);

	for (i = 0; i < N * N; i++) {
		A[i] = (double)rand() / RAND_MAX;
		B[i] = (double)rand() / RAND_MAX;
	}
	cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, N, N, N, 1.0, A, N, B, N, 0.0, C, N);

	free(A);
	free(B);
	free(C);

	return 0;
}