본문 바로가기

C C++

Module : 컴파일을 위해 여러 library를 사용해야 할 경우

C++을 이용한 컴파일시, 여러 Compiler를 사용해야 할 경우가 있다.

예를 들어 OpenFoam은 GCC컴파일러를, LAMMPS는 Intel Compiler를 사용해야 할 경우,

./bashrc에서 라이브러리 path를 지정하게되면, 컴파일 할 때 마다 라이브러리 path를 바꿔줘야 하는 불편함이 생긴다.

또는, 위에 상황이 복잡해 지면 혹자는 이런 상황을 "dependency hell"이라고 한다.

 

그렇지 않을 경우, 컴파일 된 프로그램에서 아래와 같은 에러 뱉어내는 경우가 있는데,

이는 LAMMPS의 Main code는 cmake를 통해 Intel compiler를 사용하도록 지정되었지만, 이미 설치된 VTK library는 apt install과정에서 GCC로 컴파일된 binary 라이브러리가 깔려있기 때문이다.

$ ./lmp -in lmp_test.in
Abort(805917701) on node 0 (rank 0 in comm 0): Fatal error in internal_Comm_rank: Unknown error class, error stack:
internal_Comm_rank(23414): MPI_Comm_rank(comm=0x92defc20, rank=0xc0ceb0) failed
internal_Comm_rank(23359): Invalid communicator

 

해결방법은 VTK소스코드를 intel compiler로 컴파일해서 별도의 라이브러리를 구축한 뒤, cmake에서 path를 지정하는 방법이 있다. 아니면 module을 사용하면 된다.

 

일단, OS에 module이 설치 되어 있는지 확인해 보자.

$ module avail
module: command not found

 Nope

 

설치하자

sudo apt-get update
sudo apt-get install environment-modules

 

설치되면, 스크립트 파일이 아래 디렉토리 중 하나에 설치된다고 한다.

/usr/share/modules/init/bash or /etc/profile.d/modules.sh

 

어디에 설치되었는지 확인하기 위해서 shell command "test"를 써보자

test -f /usr/share/modules/init/bash && echo "File exists." || echo "File does not exist."

test -f /etc/profile.d/modules.sh && echo "File exists." || echo "File does not exist."

 

두 군데 다 있으면, /usr/share/modules/init/bash 가 standard 란다.

 

==== 중략 ====

 

제일먼저 환경설정 먼저 하고, 두개 다 해야된다. 하나는 일반 C, C++, 나머지는 MPI

source /opt/intel/oneapi/setvars.sh
source /opt/intel/oneapi/mpi/latest/env/vars.sh

 

 

 

Intel oneAPI 컴파일러로 VTK 라이브러리를 생성하기 위하여 다음과 같은 cmake 옵션을 사용하였다.

cmake .. -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DCMAKE_Fortran_COMPILER=ifx -DCMAKE_BUILD_TYPE=Release -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=~/VTK_INTEL_build/lib -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=~/VTK_INTEL_build/lib -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=~/VTK_INTEL_build/bin -DCMAKE_INSTALL_PREFIX=~/VTK_INTEL_install

 

컴파일 후 인스톨

 

make -j4
make install

 

중요한 아웃풋 디렉토리만 보면,

cmake .. -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=~/VTK_INTEL_build/lib -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=~/VTK_INTEL_build/bin -DCMAKE_INSTALL_PREFIX=~/VTK_INTEL_install

 

요즘은 ChatGPT가 참으로 잘 설명해 준다.

1.	cmake ..
The cmake command is being run from a build directory. The .. tells CMake to look for the CMakeLists.txt and source files in the parent directory. In other words, if your project source code is in a directory called project, and you’ve created a separate build directory inside it, you run cmake .. from the build directory to configure the project in the directory above (which contains the main CMakeLists.txt).

2.	-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=~/VTK_INTEL_build/lib
This option sets the CMake variable CMAKE_ARCHIVE_OUTPUT_DIRECTORY to ~/VTK_INTEL_build/lib. This variable defines where all static libraries (.a files on Linux) and possibly other archive files are placed after compilation. By default, these might go into the build directory alongside the object files, but this setting centralizes them in a custom lib directory.

3.	-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=~/VTK_INTEL_build/bin
This sets the CMAKE_RUNTIME_OUTPUT_DIRECTORY variable to ~/VTK_INTEL_build/bin. Executable files (binaries) and possibly shared libraries (depending on your configuration) generated during the build will be placed into this bin directory, rather than the default location (which is typically the current build directory).

4.	-DCMAKE_INSTALL_PREFIX=~/VTK_INTEL_install
The CMAKE_INSTALL_PREFIX variable specifies the directory into which the project will be installed when you run make install or cmake --install. With this set to ~/VTK_INTEL_install, the bin, lib, include, and other installable components of your project will be placed inside that directory structure when you perform the installation step.

In summary:
	•	..: Source directory is the parent of the current folder.
	•	CMAKE_ARCHIVE_OUTPUT_DIRECTORY: Controls where library archive files go.
	•	CMAKE_RUNTIME_OUTPUT_DIRECTORY: Controls where executables go after being built.
	•	CMAKE_INSTALL_PREFIX: Controls where the final make install step places the installed files.

 

마지막으로 LAMMPS 컴파일을 위한 cmake

cmake ../cmake \
  -D BUILD_MPI=ON \
  -D BUILD_OMP=ON \
  -D CMAKE_C_COMPILER=icx \
  -D CMAKE_CXX_COMPILER=icx \
  -D GPU_API=cuda \
  -D GPU_ARCH=sm_61 \
  -D PKG_VTK=ON \
  -D PKG_GRANULAR=ON \
  -D VTK_DIR=~/VTK_INTEL_install/lib/cmake/vtk-<version>
  -D CMAKE_INSTALL_PREFIX=path

 

여기서, version은 직접 확인

Install을 위한 path까지 지정하고,

 

다음으로 이전 LAMMPS 빌드 찌꺼기를 제거하고,

rm -rf build
mkdir build
cd build

make clean-all

 

빌드하면 될까?

make                        # perform make after CMake command
make install                # perform the installation into prefix

'C C++' 카테고리의 다른 글

OpenMPI Compiling  (0) 2024.12.30
라이브러리 설치 위치 잡는 법  (0) 2024.12.26
Module 기본 사용법  (1) 2024.12.13
Intel Compiler  (0) 2024.12.11
CMAKE  (2) 2023.12.22