IntelコンパイラーでLammpsは早くなるのか?

いつの間にやらintel compilerが無料で手に入るようになっている。すごい時代だ。昔はインテルコンパイラを使うとプログラムが爆速になったイメージがあるが、最近はどうなのだろうか。Lammpsで比較してみる。

対象マシン

対象とするマシンスペックは以下。

  • Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz (4core 8threads)
  • MemTotal: 32,815,024 kB (32GB)
  • Ubuntu 22.04 LTS

Intel Compilerをインストール

本家サイト(下記)に書かれているとおりにすればOK。

wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \
| gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null

echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list

sudo apt update
sudo apt install intel-basekit
sudo apt install intel-hpckit

環境設定

.bashrcに下記を追記。

source /opt/intel/oneapi/setvars.sh

ターミナルを再度立ち上げるか、source .bashrcして読み込む。

ターミナルを立ち上げるたびにインテルコンパイラについてのメッセージが出てくるのがうざいので、私は上記を> /dev/null 2>&1している。

Lammpsビルド

Lammpsをビルドする前に色々インストール(もしまだの場合)。

sudo apt install gcc gfortran cmake openmpi-bin libopenmpi-dev

gnuでコンパイル

git clone -b release https://github.com/lammps/lammps.git
cd lammps
mkdir build; cd build
$ cmake -D LAMMPS_MACHINE=gnu -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_Fortran_COMPILER=gfortran -D PKG_OPENMP=yes ../cmake

-- The CXX compiler identification is GNU 11.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: /usr/bin/git (found version "2.34.1")
-- Running check for auto-generated files from make-based build system
-- Found MPI_CXX: /usr/lib/x86_64-linux-gnu/openmpi/lib/libmpi_cxx.so (found version "3.1")
-- Found MPI: TRUE (found version "3.1")
-- Looking for C++ include omp.h
-- Looking for C++ include omp.h - found
-- Found OpenMP_CXX: -fopenmp (found version "4.5")
-- Found OpenMP: TRUE (found version "4.5")
-- Found GZIP: /usr/bin/gzip
-- Found FFMPEG: /usr/bin/ffmpeg
-- Looking for C++ include cmath
-- Looking for C++ include cmath - found
-- Generating style headers...
-- Generating package headers...
-- Generating lmpinstalledpkgs.h...
-- Could NOT find ClangFormat (missing: ClangFormat_EXECUTABLE) (Required is at least version "8.0")
-- The following tools and libraries have been found and configured:
 * Git
 * MPI
 * OpenMP

-- <<< Build configuration >>>
   LAMMPS Version:   20220602
   Operating System: Linux Ubuntu 22.04
   Build type:       RelWithDebInfo
   Install path:     /home/ky/.local
   Generator:        Unix Makefiles using /usr/bin/gmake
-- Enabled packages: OPENMP
-- <<< Compilers and Flags: >>>
-- C++ Compiler:     /usr/bin/g++
      Type:          GNU
      Version:       11.2.0
      C++ Flags:     -O2 -g -DNDEBUG
      Defines:       LAMMPS_SMALLBIG;LAMMPS_MEMALIGN=64;LAMMPS_OMP_COMPAT=4;LAMMPS_GZIP;LAMMPS_FFMPEG;LMP_OPENMP
-- <<< Linker flags: >>>
-- Executable name:  lmp_gnu
-- Static library flags:
-- <<< MPI flags >>>
-- MPI_defines:      MPICH_SKIP_MPICXX;OMPI_SKIP_MPICXX;_MPICC_H
-- MPI includes:     /usr/lib/x86_64-linux-gnu/openmpi/include;/usr/lib/x86_64-linux-gnu/openmpi/include/openmpi
-- MPI libraries:    /usr/lib/x86_64-linux-gnu/openmpi/lib/libmpi_cxx.so;/usr/lib/x86_64-linux-gnu/openmpi/lib/libmpi.so;
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_C_COMPILER
    CMAKE_Fortran_COMPILER


-- Build files have been written to: /home/ky/Documents/lammps/build

Intelでコンパイル

$ cmake -D LAMMPS_MACHINE=intel -DCMAKE_C_COMPILER=icc -DCMAKE_CXX_COMPILER=icpc -DCMAKE_Fortran_COMPILER=ifort -D PKG_OPENMP=yes ../cmake

-- The CXX compiler identification is Intel 2021.6.0.20220226
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/intel/oneapi/compiler/2022.1.0/linux/bin/intel64/icpc - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: /usr/bin/git (found version "2.34.1")
-- Appending /opt/intel/oneapi/vpl/2022.1.0/lib:/opt/intel/oneapi/tbb/2021.6.0/env/../lib/intel64/gcc4.8:/opt/intel/oneapi/mpi/2021.6.0//libfabric/lib:/opt/intel/oneapi/mpi/2021.6.0//lib/release:/opt/intel/oneapi/mpi/2021.6.0//lib:/opt/intel/oneapi/mkl/2022.1.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/ippcp/2021.6.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/dnnl/2022.1.0/cpu_dpcpp_gpu_dpcpp/lib:/opt/intel/oneapi/dal/2021.6.0/lib/intel64:/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/compiler/2022.1.0/linux/lib:/opt/intel/oneapi/clck/2021.6.0/lib/intel64:/opt/intel/oneapi/ccl/2021.6.0/lib/cpu_gpu_dpcpp to CMAKE_LIBRARY_PATH: /opt/intel/oneapi/vpl/2022.1.0/lib:/opt/intel/oneapi/tbb/2021.6.0/env/../lib/intel64/gcc4.8:/opt/intel/oneapi/mpi/2021.6.0//libfabric/lib:/opt/intel/oneapi/mpi/2021.6.0//lib/release:/opt/intel/oneapi/mpi/2021.6.0//lib:/opt/intel/oneapi/mkl/2022.1.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/ippcp/2021.6.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/dnnl/2022.1.0/cpu_dpcpp_gpu_dpcpp/lib:/opt/intel/oneapi/dal/2021.6.0/lib/intel64:/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/compiler/2022.1.0/linux/lib:/opt/intel/oneapi/clck/2021.6.0/lib/intel64:/opt/intel/oneapi/ccl/2021.6.0/lib/cpu_gpu_dpcpp
-- Running check for auto-generated files from make-based build system
-- Found MPI_CXX: /opt/intel/oneapi/mpi/2021.6.0/lib/libmpicxx.so (found version "3.1")
-- Found MPI: TRUE (found version "3.1")
-- Looking for C++ include omp.h
-- Looking for C++ include omp.h - found
-- Found OpenMP_CXX: -qopenmp (found version "5.0")
-- Found OpenMP: TRUE (found version "5.0")
-- Found GZIP: /usr/bin/gzip
-- Found FFMPEG: /usr/bin/ffmpeg
-- Looking for C++ include cmath
-- Looking for C++ include cmath - found
-- Generating style headers...
-- Generating package headers...
-- Generating lmpinstalledpkgs.h...
-- Could NOT find ClangFormat (missing: ClangFormat_EXECUTABLE) (Required is at least version "8.0")
-- The following tools and libraries have been found and configured:
 * Git
 * MPI
 * OpenMP

-- <<< Build configuration >>>
   LAMMPS Version:   20220602
   Operating System: Linux Ubuntu 22.04
   Build type:       RelWithDebInfo
   Install path:     /home/ky/.local
   Generator:        Unix Makefiles using /usr/bin/gmake
-- Enabled packages: OPENMP
-- <<< Compilers and Flags: >>>
-- C++ Compiler:     /opt/intel/oneapi/compiler/2022.1.0/linux/bin/intel64/icpc
      Type:          Intel
      Version:       2021.6.0.20220226
      C++ Flags:     -restrict -O2 -g -DNDEBUG
      Defines:       LAMMPS_SMALLBIG;LAMMPS_MEMALIGN=64;LAMMPS_OMP_COMPAT=4;LAMMPS_GZIP;LAMMPS_FFMPEG;LMP_OPENMP
      Options:       -xHost
-- <<< Linker flags: >>>
-- Executable name:  lmp_intel
-- Static library flags:
-- <<< MPI flags >>>
-- MPI_defines:      MPICH_SKIP_MPICXX;OMPI_SKIP_MPICXX;_MPICC_H
-- MPI includes:     /opt/intel/oneapi/mpi/2021.6.0/include
-- MPI libraries:    /opt/intel/oneapi/mpi/2021.6.0/lib/libmpicxx.so;/opt/intel/oneapi/mpi/2021.6.0/lib/libmpifort.so;/opt/intel/oneapi/mpi/2021.6.0/lib/release/libmpi.so;/usr/lib/x86_64-linux-gnu/libdl.a;/usr/lib/x86_64-linux-gnu/librt.a;/usr/lib/x86_64-linux-gnu/libpthread.a;
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ky/Documents/lammps/build

比較

上記でコンパイルしたlmp_gnulmp_intelを使って同様の計算を行い比較する。lammpsのexamplesからmeltを使う。

in.melt

元のままだとあっという間に計算が終わるので、セルサイズを50 x 50 x 50 に変更する。

# 3d Lennard-Jones melt

units		lj
atom_style	atomic

lattice		fcc 0.8442
region		box block 0 50 0 50 0 50
create_box	1 box
create_atoms	1 box
mass		1 1.0

velocity	all create 3.0 87287 loop geom

pair_style	lj/cut 2.5
pair_coeff	1 1 1.0 1.0 2.5

neighbor	0.3 bin
neigh_modify	every 20 delay 0 check no

fix		1 all nve

dump		id all atom 50 dump.melt

#dump		2 all image 25 image.*.jpg type type &
#		axes yes 0.8 0.02 view 60 -30
#dump_modify	2 pad 3

#dump		3 all movie 25 movie.mpg type type &
#		axes yes 0.8 0.02 view 60 -30
#dump_modify	3 pad 3

thermo		50
run		250

結果

# gnu
lmp_gnu -in in.melt
Total wall time: 0:00:55

# intel
lmp_intel -in in.melt
Total wall time: 0:00:50

# gnu + openMP
env OMP_NUM_THREADS=8 lmp_gnu -sf omp -in in.melt
Total wall time: 0:00:31 

# intel + openMP
env OMP_NUM_THREADS=8 lmp_intel -sf omp -in in.melt
Total wall time: 0:00:20

# gnu + MPI
mpirun -np 2 lmp_gnu -in in.melt
Total wall time: 0:00:30

# intel + MPI
mpirun -np 2 lmp_intel -in in.melt
Total wall time: 0:00:26

結果としては、intelが若干早くなる程度。思ったより変わらないことにびっくり。MPIやOpenMPの組み合わせで何が最速になるかは試行錯誤が必要(https://docs.lammps.org/Speed_omp.html)。

Lammps GPU

せっかくなのでGPUとも比較してみる。GPUはGTX1080Ti。

$ cmake -D PKG_MANYBODY=yes -D PKG_GPU=yes -D GPU_API=cuda -D GPU_ARCH=sm_61 -D BUILD_MPI=yes -D BUILD_OMP=yes -D LAMMPS_MACHINE=gpu -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_Fortran_COMPILER=gfortran ../cmake

-- The CXX compiler identification is GNU 11.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: /usr/bin/git (found version "2.34.1")
-- Appending /opt/intel/oneapi/vpl/2022.1.0/lib:/opt/intel/oneapi/tbb/2021.6.0/env/../lib/intel64/gcc4.8:/opt/intel/oneapi/mpi/2021.6.0//libfabric/lib:/opt/intel/oneapi/mpi/2021.6.0//lib/release:/opt/intel/oneapi/mpi/2021.6.0//lib:/opt/intel/oneapi/mkl/2022.1.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/ippcp/2021.6.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/dnnl/2022.1.0/cpu_dpcpp_gpu_dpcpp/lib:/opt/intel/oneapi/dal/2021.6.0/lib/intel64:/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/compiler/2022.1.0/linux/lib:/opt/intel/oneapi/clck/2021.6.0/lib/intel64:/opt/intel/oneapi/ccl/2021.6.0/lib/cpu_gpu_dpcpp to CMAKE_LIBRARY_PATH: /opt/intel/oneapi/vpl/2022.1.0/lib:/opt/intel/oneapi/tbb/2021.6.0/env/../lib/intel64/gcc4.8:/opt/intel/oneapi/mpi/2021.6.0//libfabric/lib:/opt/intel/oneapi/mpi/2021.6.0//lib/release:/opt/intel/oneapi/mpi/2021.6.0//lib:/opt/intel/oneapi/mkl/2022.1.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/ippcp/2021.6.0/lib/intel64:/opt/intel/oneapi/ipp/2021.6.0/lib/intel64:/opt/intel/oneapi/dnnl/2022.1.0/cpu_dpcpp_gpu_dpcpp/lib:/opt/intel/oneapi/dal/2021.6.0/lib/intel64:/opt/intel/oneapi/compiler/2022.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/compiler/2022.1.0/linux/lib:/opt/intel/oneapi/clck/2021.6.0/lib/intel64:/opt/intel/oneapi/ccl/2021.6.0/lib/cpu_gpu_dpcpp
-- Running check for auto-generated files from make-based build system
-- Found MPI_CXX: /opt/intel/oneapi/mpi/2021.6.0/lib/libmpicxx.so (found version "3.1")
-- Found MPI: TRUE (found version "3.1")
-- Looking for C++ include omp.h
-- Looking for C++ include omp.h - found
-- Found OpenMP_CXX: -fopenmp (found version "4.5")
-- Found OpenMP: TRUE (found version "4.5")
-- Found GZIP: /usr/bin/gzip
-- Found FFMPEG: /usr/bin/ffmpeg
-- Looking for C++ include cmath
-- Looking for C++ include cmath - found
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Found CUDA: /usr (found version "11.5")
-- Generating style headers...
-- Generating package headers...
-- Generating lmpinstalledpkgs.h...
-- Could NOT find ClangFormat (missing: ClangFormat_EXECUTABLE) (Required is at least version "8.0")
-- The following tools and libraries have been found and configured:
 * Git
 * MPI
 * OpenMP
 * Threads
 * CUDA

-- <<< Build configuration >>>
   LAMMPS Version:   20220602
   Operating System: Linux Ubuntu 22.04
   Build type:       RelWithDebInfo
   Install path:     /home/ky/.local
   Generator:        Unix Makefiles using /usr/bin/gmake
-- Enabled packages: GPU;MANYBODY
-- <<< Compilers and Flags: >>>
-- C++ Compiler:     /usr/bin/g++
      Type:          GNU
      Version:       11.2.0
      C++ Flags:     -O2 -g -DNDEBUG
      Defines:       LAMMPS_SMALLBIG;LAMMPS_MEMALIGN=64;LAMMPS_OMP_COMPAT=4;LAMMPS_GZIP;LAMMPS_FFMPEG;LMP_GPU
-- <<< Linker flags: >>>
-- Executable name:  lmp_gpu
-- Static library flags:
-- <<< MPI flags >>>
-- MPI_defines:      MPICH_SKIP_MPICXX;OMPI_SKIP_MPICXX;_MPICC_H
-- MPI includes:     /opt/intel/oneapi/mpi/2021.6.0/include
-- MPI libraries:    /opt/intel/oneapi/mpi/2021.6.0/lib/libmpicxx.so;/opt/intel/oneapi/mpi/2021.6.0/lib/release/libmpi.so;/usr/lib/x86_64-linux-gnu/librt.a;/usr/lib/x86_64-linux-gnu/libpthread.a;/usr/lib/x86_64-linux-gnu/libdl.a;
-- <<< GPU package settings >>>
-- GPU API:                  CUDA
-- CUDA Compiler:            /usr/bin/nvcc
-- GPU default architecture: sm_61
-- GPU binning with CUDPP:   OFF
-- CUDA MPS support:         OFF
-- GPU precision:            MIXED
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_Fortran_COMPILER


-- Build files have been written to: /home/ky/Documents/lammps/build

コンパイラをintelにするとmakeでエラーが出てコンパイルできなかったので、コンパイラはgnu。

結果

lmp_gpu -sf gpu -pk gpu 1 -in in.melt
Total wall time: 0:00:04

爆速!!!!!

結論

GPU使おう。

1件のコメント

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です