Skip to content

Commit

Permalink
Merge pull request #4 from hhslepicka/build_cmake
Browse files Browse the repository at this point in the history
BLD: Improvements to Makefile and Adding Support for CMake
  • Loading branch information
qianglbl committed Jun 16, 2021
2 parents 221cfb3 + 7cfce5d commit f98eedd
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 32 deletions.
99 changes: 99 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: CI

on: [push, pull_request]

jobs:
make:
strategy:
fail-fast: false
matrix:
os: ['ubuntu-latest']
mpi: ['nompi', 'openmpi', 'mpich']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Install GFortran
run: |
sudo apt-get update
sudo apt install gfortran make
- name: Install MPI - ${{ matrix.mpi }}
if: matrix.mpi != 'nompi'
run: |
sudo apt-get update
sudo apt install lib${{ matrix.mpi }}-dev
- name: Build - No MPI
if: matrix.mpi == 'nompi'
run: |
cd src/
make FC=gfortran LINK=gfortran
- name: Build - MPI - ${{ matrix.mpi }}
if: matrix.mpi != 'nompi'
run: |
cd src/
make FC=mpifort LINK=mpifort USE_MPI=1
- name: Test - No MPI
if: matrix.mpi == 'nompi'
run: |
cd $GITHUB_WORKSPACE/examples/Example1
$GITHUB_WORKSPACE/src/ImpactZexe
- name: Test - MPI - ${{ matrix.mpi }}
if: matrix.mpi != 'nompi'
run: |
cd $GITHUB_WORKSPACE/examples/Example1
sed -i"" '11s/1 1/2 1/' ImpactZ.in
mpirun -n 2 $GITHUB_WORKSPACE/src/ImpactZexe-mpi
cmake:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
mpi: [ nompi, openmpi, mpich ]
steps:
- uses: actions/checkout@v2
- name: Install GFortran
run: |
sudo apt-get update
sudo apt install gfortran cmake
- name: Install MPI - ${{ matrix.mpi }}
if: matrix.mpi != 'nompi'
run: |
sudo apt-get update
sudo apt install lib${{ matrix.mpi }}-dev
- name: Build - No MPI
if: matrix.mpi == 'nompi'
run: |
mkdir build
cd build
cmake ../src
make
- name: Build - MPI - ${{ matrix.mpi }}
if: matrix.mpi != 'nompi'
run: |
mkdir build
cd build
cmake ../src -DUSE_MPI=ON
make
- name: Test - No MPI
if: matrix.mpi == 'nompi'
run: |
cd $GITHUB_WORKSPACE/examples/Example1
$GITHUB_WORKSPACE/build/ImpactZexe
- name: Test - MPI - ${{ matrix.mpi }}
if: matrix.mpi != 'nompi'
run: |
cd $GITHUB_WORKSPACE/examples/Example1
sed -i"" '11s/1 1/2 1/' ImpactZ.in
mpirun -n 2 $GITHUB_WORKSPACE/build/ImpactZexe-mpi
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# IDE project files
.idea

# macOS attribute file
.DS_Store

# Temporary build files
*.o
*.mod
mpif.h

# Example artifacts
examples/*/fort.*

# Binaries
src/ImpactZexe
src/ImapctZexe-mpi
80 changes: 80 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# Uncomment for VERBOSE Makefiles. Useful for debugging.
#set(CMAKE_VERBOSE_MAKEFILE ON)

# Uncomment for Debug Build
if(NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# project name and language
project(ImpactZ LANGUAGES Fortran)

option(USE_MPI "Activate MPI build" OFF)


list(APPEND _sources
DataStruct/NumConst.f90
DataStruct/Pgrid.f90
DataStruct/Data.f90
DataStruct/PhysConst.f90
Func/FFT.f90
Func/Timer.f90
Func/Fldmger.f90
Func/Transpose.f90
Func/Ptclmger.f90
Appl/BPM.f90
Appl/CCDTL.f90
Appl/ConstFoc.f90
Appl/Distribution.f90
Appl/Field.f90
Appl/SC.f90
Appl/TWS.f90
Appl/BeamBunch.f90
Appl/CCL.f90
Appl/DTL.f90
Appl/DriftTube.f90
Appl/Multipole.f90
Appl/Sol.f90
Appl/BeamLineElem.f90
Appl/CompDom.f90
Appl/Dipole.f90
Appl/EMfld.f90
Appl/Quadrupole.f90
Appl/SolRF.f90
Contrl/Output.f90
Contrl/AccSimulator.f90
Contrl/Input.f90
Contrl/main.f90
)

set(OUTPUT_NAME "ImpactZexe")
if(USE_MPI)
set(OUTPUT_NAME "ImpactZexe-mpi")
endif()
add_executable(${OUTPUT_NAME})

if(USE_MPI)
find_package(MPI REQUIRED)
target_compile_definitions(${OUTPUT_NAME} PUBLIC USE_MPI)
target_link_libraries(${OUTPUT_NAME} PUBLIC MPI::MPI_Fortran)
include_directories(${MPI_Fortran_INCLUDE_PATH})
else()
# Add mpistub to source list
list(APPEND _sources mpistub.f90)

# Create mpif.h from mpif_single.h
configure_file(mpif_single.h ${ImpactZ_BINARY_DIR}/include/mpif.h COPYONLY)
# Add include dir above into search path
target_include_directories(${OUTPUT_NAME} PRIVATE $<BUILD_INTERFACE:${ImpactZ_BINARY_DIR}/include>)
endif()

# Associate source files with target
target_sources(${OUTPUT_NAME} PRIVATE ${_sources})

# Enables preprocessing
target_compile_options(${OUTPUT_NAME} PRIVATE -cpp)

install(TARGETS ${OUTPUT_NAME} RUNTIME DESTINATION bin)
2 changes: 2 additions & 0 deletions src/Contrl/Input.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
! input file "ImpactZ.in".
!----------------------------------------------------------------
module Inputclass
#if USE_MPI != 1
use mpistub
#endif
interface in_Input
module procedure in1_Input, in2_Input
end interface
Expand Down
6 changes: 0 additions & 6 deletions src/Contrl/mpif.h

This file was deleted.

2 changes: 2 additions & 0 deletions src/DataStruct/Data.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
! Comments:
!----------------------------------------------------------------
module Dataclass
#if USE_MPI != 1
use mpistub
#endif
save
!-----------------------------------------------------------------------
! using the x-y-z field data (Ex,Ey,Ez,Bx,By,Bz) directly.
Expand Down
2 changes: 2 additions & 0 deletions src/DataStruct/Pgrid.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
! Comments:
!----------------------------------------------------------------
module Pgrid2dclass
#if USE_MPI != 1
use mpistub
#endif
type Pgrid2d
private
integer comm_2d ! comunicator for entire grid
Expand Down
2 changes: 2 additions & 0 deletions src/DataStruct/PhysConst.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
! Comments:
!----------------------------------------------------------------
module PhysConstclass
#if USE_MPI != 1
use mpistub
#endif
implicit none

!physical parameters and constants ---------------------
Expand Down
6 changes: 0 additions & 6 deletions src/DataStruct/mpif.h

This file was deleted.

2 changes: 2 additions & 0 deletions src/Func/Timer.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
! Comments:
!----------------------------------------------------------------
module Timerclass
#if USE_MPI != 1
use mpistub
#endif
double precision :: t_integ
double precision :: t_map1
double precision :: t_map2
Expand Down
6 changes: 0 additions & 6 deletions src/Func/mpif.h

This file was deleted.

111 changes: 111 additions & 0 deletions src/INSTRUCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Using Anaconda

Information about Anaconda, including install instructions, can be found on the [Conda Reference](https://docs.conda.io/projects/conda/en/latest/) website.

IMPACT-Z is available through conda-forge and can be installed via:
```bash
conda create -n impact
source activate impact # or conda activate impact
# For non-MPI
conda install -c conda-forge impact-z

# For OpenMPI
conda install -c conda-forge impact-z=*=mpi_openmpi*

# For MPICH
conda install -c conda-forge impact-z=*=mpi_mpich*
```
After these steps, the IMPACT-Z executable `ImpactZexe` or `ImpactZexe-mpi`, respectively, will be in your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) environment variable and is thus ready to use like any regular command-line command.

# Compiling The Code

If you are new to CMake, [this short tutorial](https://hsf-training.github.io/hsf-training-cmake-webpage/) from the HEP Software foundation is the perfect place to get started with it.

If you just want to use CMake to build the project, jump into sections *1. Introduction*, *2. Building with CMake* and *9. Finding Packages*.

## Unix

### Single Processor Code:

```shell script
# inside the IMPACT-Z src/ directory:
cmake -S . -B build
cmake --build build
# the executable in now in build/bin/

# this command needs sudo if you install into system paths:
cmake --build build --target install
```
If you like to install IMPACT-Z into another directory than the default, pass to the `cmake -S . -B build` line the additional argument `-DCMAKE_INSTALL_PREFIX=/your/custom/install/path`.

### Multi Processor Code:

```shell script
# inside the IMPACT-Z src/ directory:
cmake -S . -B build -DUSE_MPI=ON
cmake --build build
cmake --build build --target install
```

## Windows

For Windows it will be necessary to use `NMake` to read and execute the generated makefiles.

`NMake` is command-line tool included with Visual Studio that builds projects based on commands that are contained in a description file.

More information on `NMake` can be found on the [NMAKE Reference](https://docs.microsoft.com/en-us/cpp/build/reference/nmake-reference?view=msvc-160) website.

### Single Processor Code:

```shell script
cmake -S . -B build -G "NMake Makefiles"
cmake --build build
cmake --build build --target install
cmake --install
```

### Multi Processor Code:

**Not Tested**


## Testing

```shell script
cd examples
cd Example1
# Execute non-MPI
ImpactZexe

# Execute MPI
mpirun -n <cores> ImpactZexe-mpi
```

# Using at NERSC

If you decide to use IMPACT-Z at NERSC you can do so via Anaconda or building from source.
The instructions for Anaconda are the same as above.

## Compiling the code

### For Haswell
```bash
module load openmpi # if using OpenMPI otherwise skip for MPICH
cmake -S . -B build -DUSE_MPI=ON
cmake --build build
# find the executable in build/bin/
```

### For KNL
```bash
module swap craype-haswell craype-mic-knl
module load openmpi # if using OpenMPI otherwise skip for MPICH
cmake -S . -B build -DUSE_MPI=ON
cmake --build build
# find the executable in build/bin/
```

## Running at NERSC

There is one caveat with the conda-forge installed MPI version of the code.
Instead of running with `srun -n <cores> ` you must use `mpirun -n <cores>`.
Loading

0 comments on commit f98eedd

Please sign in to comment.