-
Notifications
You must be signed in to change notification settings - Fork 29
[WIP] Enzyme Test #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ax3l
wants to merge
16
commits into
BLAST-ImpactX:development
Choose a base branch
from
ax3l:topic-enzyme-test
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[WIP] Enzyme Test #825
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5296d8d
README
ax3l 7a019ce
Minimal Enzyme Example (No ImpactX)
ax3l 231b901
ImpactX: Single TU Example
ax3l 628b80c
[Hack] Disable Beam Monitor
ax3l 9d43af3
Simplify Logic to First Enzyme Error
ax3l c2467c3
README: `LLDEnzyme` on Ubuntu 24.04
ax3l f1bcc45
[Work-Around] Avoid Variants, Push Manually
ax3l 8663d9c
[Hack] Disable Tiny Profiler
ax3l 266cb45
[Hack] Assume `enzyme-globals-default-inactive=1`
ax3l 3c3368b
Compiles & Runs: Fwd/Rev, Fwd AD Correct
ax3l 7ee07a5
Reverse Mode Works, too!
ax3l 317e274
Envelope: Test Space Charge Push 2D/3D (works)
ax3l 94c349d
Envelope: Space Charge Match 2D (Draft)
ax3l 8bf1552
Envelope: Space Charge Match (Works)
ax3l 76d3623
Envelope: Space Charge Python
ax3l 230ced6
Check: k2=0 pass as drift
ax3l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,154 +1,131 @@ | ||
| # ImpactX | ||
| # Differentiable ImpactX | ||
|
|
||
| [](https://github.com/BLAST-ImpactX/impactx/actions/workflows/ubuntu.yml) | ||
| [](https://impactx.readthedocs.io) | ||
| [](https://spdx.org/licenses/BSD-3-Clause-LBNL.html) | ||
| [](https://impactx.readthedocs.io/en/latest/install/users.html) | ||
| [-10.5281/zenodo.6954922-blue.svg)](https://doi.org/10.5281/zenodo.6954922) | ||
| [-10.18429%2FJACoW--NAPAC2022--TUYE2-blue.svg)](https://doi.org/10.18429/JACoW-NAPAC2022-TUYE2) | ||
| [](https://isocpp.org/) | ||
| [](https://python.org/) | ||
| ## Prepare Developer Environment (Ubuntu 24.02 w/ Clang/LLVM 16) | ||
|
|
||
| ImpactX: an s-based beam dynamics code including space charge effects. | ||
| This is the next generation of the [IMPACT-Z](https://github.com/impact-lbl/IMPACT-Z) code. | ||
|
|
||
| ## Documentation | ||
|
|
||
| In order to learn how to install and run the code, please see the online documentation: | ||
| https://impactx.readthedocs.io | ||
|
|
||
| * ImpactX Doxygen: https://impactx.readthedocs.io/en/latest/_static/doxyhtml | ||
| * AMReX Doxygen: https://amrex-codes.github.io/amrex/doxygen | ||
| * WarpX Doxygen: https://warpx.readthedocs.io/en/latest/_static/doxyhtml | ||
|
|
||
| ## Contributing | ||
| ### Once | ||
| ```bash | ||
| # Ref: https://github.com/EnzymeAD/enzyme-dev-docker/blob/main/Dockerfile | ||
| sudo apt install llvm-dev clang-16 lld-16 zlib1g-dev libzstd-dev git automake autoconf cmake make lldb-16 ninja-build build-essential libtool llvm-16-dev libclang-16-dev libomp-16-dev libblas-dev libeigen3-dev libboost-dev python3 | ||
| python3 -m pip install --break-system-package pipx pathlib2 | ||
| python3 -m pipx install lit cmake | ||
|
|
||
| export CC="clang-16" | ||
| export CXX="clang++-16" | ||
|
|
||
| # Ref: https://enzyme.mit.edu/Installation/ | ||
| git clone https://github.com/EnzymeAD/Enzyme ~/src/Enzyme | ||
| cd ~/src/Enzyme | ||
| cmake --fresh -G Ninja -S enzyme -B build -DLLVM_DIR=/usr/lib/llvm-16/lib/cmake/llvm -DLLVM_EXTERNAL_LIT=$(which lit) | ||
| cmake --build build -j 6 | ||
|
|
||
| cd build | ||
| ninja check-enzyme | ||
|
|
||
| # linker and lld and lto enforcement in CMake is hard... | ||
| cd .. | ||
| mkdir mylld | ||
| cd mylld | ||
| ln -s /usr/lib/llvm-16/bin/lld-link lld | ||
| ln -s $(which ld.lld-16) ld.lld # note: not sufficient yet... somehow hard-coded in compiler detection... use docker | ||
| # manually linking /usr/bin/ld.lld-16 as /usr/bin/ld.lld works... | ||
| export PATH=$PWD:$PATH | ||
|
|
||
| # optional: create a venv for Python | ||
| rm -rf ~/src/venv-impactx-enzyme | ||
| python3 -m venv ~/src/venv-impactx-enzyme | ||
| source ~/src/venv-impactx-enzyme/bin/activate | ||
| python3 -m pip install --upgrade pip | ||
| python3 -m pip install --upgrade scipy numpy packaging setuptools[core] wheel pytest pytest-benchmark matplotlib PyQt6 | ||
| ``` | ||
|
|
||
| [](https://amrex-codes.github.io/) | ||
|
|
||
| Our workflow is described in [CONTRIBUTING.rst](CONTRIBUTING.rst). | ||
| ## Compile Options | ||
|
|
||
| ## Developer Environment | ||
| From the [homepage](https://enzyme.mit.edu/getting_started/UsingEnzyme/): | ||
| > Enzyme supports differentiating C/C++ code through ClangEnzyme and LLDEnzyme as compiler and linker plugins, respectively. | ||
| > Clang gives our plugin more flexibility in adding and ordering optimization passes than LLD and therefore using ClangEnzyme could result in better performance than LLDEnzyme. | ||
| > However, ClangEnzyme can only differentiate one compilation unit at a time and will therefore fail if the function which you try to differentiate calls functions in other compilation units (generally other .c or .cpp files). | ||
| > In these cases we recommend the use of LLDEnzyme in combination with LTO. | ||
|
|
||
| Please see our [developer installation section](https://impactx.readthedocs.io/en/latest/install/dependencies.html#install-dependencies) of the documentation for an easy install of our software dependencies. | ||
|
|
||
| ## Get the Source Code | ||
| ## Compile (ClangEnzyme, one TU) | ||
|
|
||
| Before you start, you will need a copy of the ImpactX source code: | ||
| ### Always | ||
|
|
||
| ```bash | ||
| git clone [email protected]:BLAST-ImpactX/impactx.git | ||
| cd impactx | ||
| source ~/src/venv-impactx-enzyme/bin/activate | ||
| alias cmake=$HOME/.local/pipx/venvs/cmake/bin/cmake | ||
| export CC="clang-16" | ||
| export CXX="clang++-16" | ||
|
|
||
| # one TU: Clang Plugin | ||
| # Extra Enzyme options, e.g., print https://enzyme.mit.edu/getting_started/UsingEnzyme/#semantic-options | ||
| # optional add for verbose output: -mllvm -enzyme-print | ||
| export CXXFLAGS="-fplugin=$HOME/src/Enzyme/build/Enzyme/ClangEnzyme-16.so" | ||
| ``` | ||
|
|
||
| ## Compile | ||
|
|
||
| With the active developer env above, inside the ImpactX source dir: | ||
| ```bash | ||
| # find dependencies & configure | ||
| cmake -S . -B build | ||
|
|
||
| # compile | ||
| cmake --build build -j 4 | ||
| cmake --fresh \ | ||
| -S . \ | ||
| -B build \ | ||
| -DImpactX_UNITY_BUILD=ON \ | ||
| -DImpactX_MPI=OFF \ | ||
| -DImpactX_COMPUTE=NOACC \ | ||
| -DImpactX_OPENPMD=OFF \ | ||
| -DCMAKE_LINKER_TYPE=LLD \ | ||
| -DCMAKE_LINKER=/usr/lib/llvm-16/bin/lld-link | ||
| # optional: | ||
| # -DImpactX_PYTHON=ON -DpyAMReX_IPO=OFF -DImpactX_PYTHON_IPO=OFF | ||
|
|
||
| cmake --build build -j 6 | ||
|
|
||
| # optional: | ||
| cmake --build build -j 6 --target pip_install | ||
| ``` | ||
|
|
||
| That's all! | ||
| ImpactX binaries are now in `build/bin/`. | ||
| Most people execute these binaries directly or copy them out. | ||
|
|
||
| You can inspect and modify build options after running `cmake -S . -B` build with either | ||
| ## Compile (LLDEnzyme, multiple TUs) | ||
|
|
||
| ```bash | ||
| ccmake build | ||
| ``` | ||
| TODO: redo this part using https://github.com/EnzymeAD/enzyme-dev-docker because ld, lld, ld.ldd with the non-system default is too tricky to get right in CMake (i.e. compiler detection). | ||
|
|
||
| or by adding arguments with `-D<OPTION>=<VALUE>` to the first CMake call, e.g.: | ||
| ### Always | ||
|
|
||
| ```bash | ||
| cmake -S . -B build -DImpactX_COMPUTE=CUDA -DImpactX_MPI=OFF | ||
| export PATH=$HOME/.local/pipx/venvs/cmake/bin:$PATH | ||
| export CC="clang-16" | ||
| export CXX="clang++-16" | ||
|
|
||
| # many TU: LDD Plugin | ||
| # https://github.com/EnzymeAD/Enzyme/blob/main/enzyme/Enzyme/CMakeLists.txt | ||
| # https://enzyme.mit.edu/getting_started/UsingEnzyme/#semantic-options | ||
| export CXXFLAGS="-fuse-ld=/usr/lib/llvm-16/bin/lld-link -flto" # -mllvm -enzyme-... | ||
| export LDFLAGS="-fuse-ld=/usr/lib/llvm-16/bin/lld-link -flto -Wl,-mllvm -Wl,-load=$HOME/src/Enzyme/build/Enzyme/LLDEnzyme-16.so -Wl,--load-pass-plugin=$HOME/src/Enzyme/build/Enzyme/LLDEnzyme-16.so" | ||
| ``` | ||
|
|
||
| ### Python Compile | ||
|
|
||
| With the active developer env above, inside the ImpactX source dir: | ||
| ```bash | ||
| # find dependencies & configure | ||
| cmake -S . -B build -DImpactX_PYTHON=ON | ||
|
|
||
| # compile & install | ||
| cmake --build build -j 4 --target pip_install | ||
| cmake --fresh \ | ||
| -S . \ | ||
| -B build \ | ||
| -DImpactX_UNITY_BUILD=ON \ | ||
| -DImpactX_MPI=OFF \ | ||
| -DImpactX_COMPUTE=NOACC \ | ||
| -DImpactX_OPENPMD=OFF \ | ||
| -DCMAKE_LINKER_TYPE=LLD \ | ||
| -DCMAKE_LINKER=/usr/lib/llvm-16/bin/lld-link | ||
|
|
||
| # note: -DCMAKE_LINKER_TYPE=LLD appends general lld not necessarily the right version | ||
| # bend ldd to ldd-16 and /usr/bin/ld.ldd to ld.ldd-16 | ||
| # -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON | ||
|
|
||
| cmake --build build -j 6 | ||
| ``` | ||
|
|
||
| ## Run | ||
|
|
||
| An executable ImpactX binary with the current compile-time options encoded in its file name will be created in `build/bin/`. | ||
|
|
||
| Additionally, a symbolic link named `impactx` can be found in that directory, which points to the last built ImpactX executable. | ||
|
|
||
| The command-line syntax for this executable is: | ||
| ```console | ||
| Usage: impactx <inputs-file> [some.overwritten.option=value]... | ||
|
|
||
| Mandatory arguments (remove the <>): | ||
| inputs-file the path to an input file; can be relative to the current | ||
| working directory or absolute. | ||
| Example: input_fodo.in | ||
|
|
||
| Optional arguments (remove the []): | ||
| options this can overwrite any line in an inputs-file | ||
| Example: quad1.ds=0.5 sbend1.rc=1.5 | ||
|
|
||
| Examples: | ||
| In the current working directory, there is a file "input_fodo.in" and the | ||
| "impactx" executable. | ||
| The line to execute would look like this: | ||
| ./impactx input_fodo.in | ||
| ## Test/Run | ||
|
|
||
| In the current working directory, there is a file "input_fodo.in" and the | ||
| executable "impactx" is in a directory that is listed in the "PATH" | ||
| environment variable. | ||
| The line to execute would look like this: | ||
| impactx input_fodo.in | ||
|
|
||
| In the current working directory, there is a file "input_fodo.in" and the | ||
| "impactx" executable. We want to overwrite the segment length of the beamline | ||
| element "quad1" that is already defined in it. We also want to change the | ||
| radius of curvature of the bending magnet "sbend1" to a different value than | ||
| in the file "input_fodo.in". | ||
| The line to execute would look like this: | ||
| ./impactx input_fodo.in quad1.ds=0.5 sbend1.rc=1.5 | ||
| ``` | ||
|
|
||
| ## Test | ||
|
|
||
| In order to run our tests, you need to have a few Python packages installed: | ||
| ```console | ||
| python3 -m pip install --upgrade pip | ||
| python3 -m pip install --upgrade build packaging setuptools[core] wheel pytest pytest-benchmark | ||
| python3 -m pip install --upgrade -r tests/python/requirements.txt | ||
| ``` | ||
|
|
||
| You can run all our tests with: | ||
|
|
||
| ```console | ||
| ctest --test-dir build --output-on-failure | ||
| With the active developer env above: | ||
| ```bash | ||
| ./build/bin/impactx examples/fodo/input_fodo.in | ||
| ``` | ||
|
|
||
| Further options: | ||
| * help: `ctest --test-dir build --help` | ||
| * list all tests: `ctest --test-dir build -N` | ||
| * only run tests that have "FODO" in their name: `ctest --test-dir build -R FODO` | ||
|
|
||
| ## Acknowledgements | ||
|
|
||
| This work was supported by the Laboratory Directed Research and Development Program of Lawrence Berkeley National Laboratory under U.S. Department of Energy Contract No. DE-AC02-05CH11231. | ||
|
|
||
| ImpactX is supported by the CAMPA collaboration, a project of the U.S. Department of Energy, Office of Science, Office of Advanced Scientific Computing Research and Office of High Energy Physics, Scientific Discovery through Advanced Computing (SciDAC) program. | ||
|
|
||
| ## Copyright Notice | ||
|
|
||
| ImpactX Copyright (c) 2022, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). | ||
| All rights reserved. | ||
|
|
||
| If you have questions about your rights to use or distribute this software, please contact Berkeley Lab's Intellectual Property Office at [email protected]. | ||
|
|
||
| Please see the full license agreement in [LICENSE.txt](LICENSE.txt). | ||
| Please see the notices in [NOTICE.txt](NOTICE.txt). | ||
| The SPDX license identifier is `BSD-3-Clause-LBNL`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Copyright 2025 ImpactX contributors | ||
| # Authors: Axel Huebl, Chad Mitchell | ||
| # License: BSD-3-Clause-LBNL | ||
| # | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
|
|
||
| from impactx import my_run | ||
|
|
||
| verbose = False | ||
| # mode = "forward" | ||
| mode = "backward" | ||
| inputs_file_beam = "examples/fodo_space_charge/input_fodo_envelope_sc.in" | ||
|
|
||
|
|
||
| eps = 1e-12 | ||
|
|
||
| mode = "gradient-free" | ||
| print(f"{mode}:") | ||
| values = my_run(-85.0, -eps, mode, inputs_file_beam, verbose=verbose) | ||
| error_zero_lo = values["error"] | ||
| values = my_run(-85.0, eps, mode, inputs_file_beam, verbose=verbose) | ||
| error_zero_hi = values["error"] | ||
| print(f" error_zero_lo={error_zero_lo}, error_zero_hi={error_zero_hi}") | ||
| print(f" FD gradient: derror_dq2_k={(error_zero_hi - error_zero_lo) / (2 * eps)}") | ||
|
|
||
| values = my_run(-85.0, 0.0, mode, inputs_file_beam, verbose=verbose) | ||
| print(f" error_forward={values['error']}") | ||
|
|
||
| mode = "forward" | ||
| print(f"\n{mode}:") | ||
| values = my_run(-85.0, 0.0, mode, inputs_file_beam, verbose=verbose) | ||
| print(f" error_forward={values['error']} derror_dq2_k={values['derror_dq2_k']}") | ||
|
|
||
| mode = "backward" | ||
| print(f"\n{mode}:") | ||
| values = my_run(-85.0, 0.0, mode, inputs_file_beam, verbose=verbose) | ||
| print(f" error_forward={values['error']} derror_dq2_k={values['derror_dq2_k']}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Variable defined multiple times Warning