2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kokkosのチュートリアルをやってみた #1

Last updated at Posted at 2024-07-15

Kokkos1の勉強をしています。本記事は"The Kokkos Lectures - module 2"2を参考にしています。

Tutorial Exercise 01

Kokkos Tutorials3のExercise01をやってみました。
これはBegin配下のプログラムをKokkos対応させるという練習問題になっています。

まず、事前準備としてSerialとOpenMPを有効にしたKokkosを構築しました。

Kokkosのビルド・インストール
$ cmake -B <build directory> -S </path/to/kokkos> \
  -DCMAKE_INSTALL_PREFIX=<install prefix> \
  -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++ \
  -DKokkos_ENABLE_SERIAL=ON -DKokkos_ENABLE_OPENMP=ON
$ cmake --build <build directory>
$ cmake --install <build directory>

configureのログでKokkos::OpenMPが有効になっていることが確認できました。

:
-- Built-in Execution Spaces:
--     Device Parallel: NoTypeDefined
--     Host Parallel: Kokkos::OpenMP
--       Host Serial: SERIAL
:
-- Kokkos Backends: OPENMP;SERIAL
:

また、kokkos-tutorial3のソースコードを取得しました。

$ git clone https://github.com/kokkos/kokkos-tutorials.git

01/Biginのビルド・実行

Exercise01について、まずはBeginのコードをビルド・実行してみます。

$ cd kokkos-tutorials/Exercises/01/Begin

Makefileは各環境で書き換える必要があります。
KOKKOS_PATHKokkosのソースコードのディレクトリを指定します。
この配下の、ビルドしたbinMakefile.kokkosを参照しています。

KOKKOS_PATH=</path/to/kokkos>

ソースコードexercise_1_begin.cppをビルドし実行してみました。

$ make
:
$ ./01_Exercise.host 
  Total size S = 4194304 N = 4096 M = 1024
  Computed result for 4096 x 1024 is 4194304.000000
  N( 4096 ) M( 1024 ) nrepeat ( 100 ) problem( 33.5954 MB ) time( 0.421103 s ) bandwidth( 7.97795 GB/s )

01/Solutionのビルド・実行

Beginを修正してkokkos対応されたソースコードにしてください、というのがこのTutorialですが、答えはSolutionの方にあります。
同様にMakefileを修正しビルド・実行してみました。

$ make
:
$ OMP_PROC_BIND=spread OMP_PLACES=threads ./01_Exercise.host 
  Total size S = 4194304 N = 4096 M = 1024
  Computed result for 4096 x 1024 is 4194304.000000
  N( 4096 ) M( 1024 ) nrepeat ( 100 ) problem( 33.5954 MB ) time( 0.0957646 s ) bandwidth( 35.0812 GB/s )

実行時間が1/4ほどになりました。OpenMPの効果のようです。
OMP_PROC_BINDOPM_PLACESは付けなくても実行できましたが、以下のように言われたので付けました。

In general, for best performance with OpenMP 4.0 or better set OMP_PROC_BIND=spread and OMP_PLACES=threads

BeginとSolutionのコード比較

このチュートリアルではコードを変更する練習でした。
コードの対応として下記が必要ということでした。

  • Kokkos_Core.hppのインクルードを追加
  • Kokkos::initialize()の追加
  • forKokkos::parallel_for, Kokkos::parallel_reduceに書き換え
  • Kokkos::finalize()の追加

詳しくは以下の記事に記載しました。

  1. https://github.com/kokkos/kokkos

  2. https://www.youtube.com/watch?v=O-asHTtO7O4&t=134s

  3. https://github.com/kokkos/kokkos-tutorials 2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?