1# libCEED: Efficient Extensible Discretization 2 3[![GitHub Actions][github-badge]][github-link] 4[![GitLab-CI][gitlab-badge]][gitlab-link] 5[![Code coverage][codecov-badge]][codecov-link] 6[![BSD-2-Clause][license-badge]][license-link] 7[![Documentation][doc-badge]][doc-link] 8[![JOSS paper][joss-badge]][joss-link] 9[![Binder][binder-badge]][binder-link] 10 11## Summary and Purpose 12 13libCEED provides fast algebra for element-based discretizations, designed for performance portability, run-time flexibility, and clean embedding in higher level libraries and applications. 14It offers a C99 interface as well as bindings for Fortran, Python, Julia, and Rust. 15While our focus is on high-order finite elements, the approach is mostly algebraic and thus applicable to other discretizations in factored form, as explained in the [user manual](https://libceed.org/en/latest/) and API implementation portion of the [documentation](https://libceed.org/en/latest/api/). 16 17One of the challenges with high-order methods is that a global sparse matrix is no longer a good representation of a high-order linear operator, both with respect to the FLOPs needed for its evaluation, as well as the memory transfer needed for a matvec. 18Thus, high-order methods require a new "format" that still represents a linear (or more generally non-linear) operator, but not through a sparse matrix. 19 20The goal of libCEED is to propose such a format, as well as supporting implementations and data structures, that enable efficient operator evaluation on a variety of computational device types (CPUs, GPUs, etc.). 21This new operator description is based on algebraically [factored form](https://libceed.org/en/latest/libCEEDapi/#finite-element-operator-decomposition), which is easy to incorporate in a wide variety of applications, without significant refactoring of their own discretization infrastructure. 22 23The repository is part of the [CEED software suite](http://ceed.exascaleproject.org/software/), a collection of software benchmarks, miniapps, libraries and APIs for efficient exascale discretizations based on high-order finite element and spectral element methods. 24See <http://github.com/ceed> for more information and source code availability. 25 26The CEED research is supported by the [Exascale Computing Project](https://exascaleproject.org/exascale-computing-project) (17-SC-20-SC), a collaborative effort of two U.S. Department of Energy organizations (Office of Science and the National Nuclear Security Administration) responsible for the planning and preparation of a [capable exascale ecosystem](https://exascaleproject.org/what-is-exascale), including software, applications, hardware, advanced system engineering and early testbed platforms, in support of the nation’s exascale computing imperative. 27 28For more details on the CEED API see the [user manual](https://libceed.org/en/latest/). 29 30% gettingstarted-inclusion-marker 31 32## Building 33 34The CEED library, `libceed`, is a C99 library with no required dependencies, and with Fortran, Python, Julia, and Rust interfaces. 35It can be built using: 36 37```console 38$ make 39``` 40 41or, with optimization flags: 42 43```console 44$ make OPT='-O3 -march=skylake-avx512 -ffp-contract=fast' 45``` 46 47These optimization flags are used by all languages (C, C++, Fortran) and this makefile variable can also be set for testing and examples (below). 48 49The library attempts to automatically detect support for the AVX instruction set using gcc-style compiler options for the host. 50Support may need to be manually specified via: 51 52```console 53$ make AVX=1 54``` 55 56or: 57 58```console 59$ make AVX=0 60``` 61 62if your compiler does not support gcc-style options, if you are cross compiling, etc. 63 64To enable CUDA support, add `CUDA_DIR=/opt/cuda` or an appropriate directory to your `make` invocation. 65To enable HIP support, add `ROCM_DIR=/opt/rocm` or an appropriate directory. 66To enable SYCL support, add `SYCL_DIR=/opt/sycl` or an appropriate directory. 67Note that SYCL backends require building with oneAPI compilers as well: 68 69```console 70$ . /opt/intel/oneapi/setvars.sh 71$ make SYCL_DIR=/opt/intel/oneapi/compiler/latest/linux SYCLCXX=icpx CC=icx CXX=icpx 72``` 73 74The library can be configured for host applications which use OpenMP paralellism via: 75 76```console 77$ make OPENMP=1 78``` 79 80which will allow operators created and applied from different threads inside an `omp parallel` region. 81 82To store these or other arguments as defaults for future invocations of `make`, use: 83 84```console 85$ make configure CUDA_DIR=/usr/local/cuda ROCM_DIR=/opt/rocm OPT='-O3 -march=znver2' 86``` 87 88which stores these variables in `config.mk`. 89 90### WebAssembly 91 92libCEED can be built for WASM using [Emscripten](https://emscripten.org). For example, one can build the library and run a standalone WASM executable using 93 94``` console 95$ emmake make build/ex2-surface.wasm 96$ wasmer build/ex2-surface.wasm -- -s 200000 97``` 98 99## Additional Language Interfaces 100 101The Fortran interface is built alongside the library automatically. 102 103Python users can install using: 104 105```console 106$ pip install libceed 107``` 108 109or in a clone of the repository via `pip install .`. 110 111Julia users can install using: 112 113```console 114$ julia 115julia> ] 116pkg> add LibCEED 117``` 118 119See the [LibCEED.jl documentation](http://ceed.exascaleproject.org/libCEED-julia-docs/dev/) for more information. 120 121Rust users can include libCEED via `Cargo.toml`: 122 123```toml 124[dependencies] 125libceed = "0.12.0" 126``` 127 128See the [Cargo documentation](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories) for details. 129 130## Testing 131 132The test suite produces [TAP](https://testanything.org) output and is run by: 133 134```console 135$ make test 136``` 137 138or, using the `prove` tool distributed with Perl (recommended): 139 140```console 141$ make prove 142``` 143 144## Backends 145 146There are multiple supported backends, which can be selected at runtime in the examples: 147 148| CEED resource | Backend | Deterministic Capable | 149| :--- | :--- | :---: | 150|| 151| **CPU Native** | 152| `/cpu/self/ref/serial` | Serial reference implementation | Yes | 153| `/cpu/self/ref/blocked` | Blocked reference implementation | Yes | 154| `/cpu/self/opt/serial` | Serial optimized C implementation | Yes | 155| `/cpu/self/opt/blocked` | Blocked optimized C implementation | Yes | 156| `/cpu/self/avx/serial` | Serial AVX implementation | Yes | 157| `/cpu/self/avx/blocked` | Blocked AVX implementation | Yes | 158|| 159| **CPU Valgrind** | 160| `/cpu/self/memcheck/*` | Memcheck backends, undefined value checks | Yes | 161|| 162| **CPU LIBXSMM** | 163| `/cpu/self/xsmm/serial` | Serial LIBXSMM implementation | Yes | 164| `/cpu/self/xsmm/blocked` | Blocked LIBXSMM implementation | Yes | 165|| 166| **CUDA Native** | 167| `/gpu/cuda/ref` | Reference pure CUDA kernels | Yes | 168| `/gpu/cuda/shared` | Optimized pure CUDA kernels using shared memory | Yes | 169| `/gpu/cuda/gen` | Optimized pure CUDA kernels using code generation | No | 170|| 171| **HIP Native** | 172| `/gpu/hip/ref` | Reference pure HIP kernels | Yes | 173| `/gpu/hip/shared` | Optimized pure HIP kernels using shared memory | Yes | 174| `/gpu/hip/gen` | Optimized pure HIP kernels using code generation | No | 175|| 176| **SYCL Native** | 177| `/gpu/sycl/ref` | Reference pure SYCL kernels | Yes | 178| `/gpu/sycl/shared` | Optimized pure SYCL kernels using shared memory | Yes | 179|| 180| **MAGMA** | 181| `/gpu/cuda/magma` | CUDA MAGMA kernels | No | 182| `/gpu/cuda/magma/det` | CUDA MAGMA kernels | Yes | 183| `/gpu/hip/magma` | HIP MAGMA kernels | No | 184| `/gpu/hip/magma/det` | HIP MAGMA kernels | Yes | 185|| 186| **OCCA** | 187| `/*/occa` | Selects backend based on available OCCA modes | Yes | 188| `/cpu/self/occa` | OCCA backend with serial CPU kernels | Yes | 189| `/cpu/openmp/occa` | OCCA backend with OpenMP kernels | Yes | 190| `/cpu/dpcpp/occa` | OCCA backend with DPC++ kernels | Yes | 191| `/gpu/cuda/occa` | OCCA backend with CUDA kernels | Yes | 192| `/gpu/hip/occa` | OCCA backend with HIP kernels | Yes | 193 194The `/cpu/self/*/serial` backends process one element at a time and are intended for meshes with a smaller number of high order elements. 195The `/cpu/self/*/blocked` backends process blocked batches of eight interlaced elements and are intended for meshes with higher numbers of elements. 196 197The `/cpu/self/ref/*` backends are written in pure C and provide basic functionality. 198 199The `/cpu/self/opt/*` backends are written in pure C and use partial e-vectors to improve performance. 200 201The `/cpu/self/avx/*` backends rely upon AVX instructions to provide vectorized CPU performance. 202 203The `/cpu/self/memcheck/*` backends rely upon the [Valgrind](https://valgrind.org/) Memcheck tool to help verify that user QFunctions have no undefined values. 204To use, run your code with Valgrind and the Memcheck backends, e.g. `valgrind ./build/ex1 -ceed /cpu/self/ref/memcheck`. 205A 'development' or 'debugging' version of Valgrind with headers is required to use this backend. 206This backend can be run in serial or blocked mode and defaults to running in the serial mode if `/cpu/self/memcheck` is selected at runtime. 207 208The `/cpu/self/xsmm/*` backends rely upon the [LIBXSMM](https://github.com/libxsmm/libxsmm) package to provide vectorized CPU performance. 209If linking MKL and LIBXSMM is desired but the Makefile is not detecting `MKLROOT`, linking libCEED against MKL can be forced by setting the environment variable `MKL=1`. 210 211The `/gpu/cuda/*` backends provide GPU performance strictly using CUDA. 212 213The `/gpu/hip/*` backends provide GPU performance strictly using HIP. 214They are based on the `/gpu/cuda/*` backends. 215ROCm version 4.2 or newer is required. 216 217The `/gpu/sycl/*` backends provide GPU performance strictly using SYCL. 218They are based on the `/gpu/cuda/*` and `/gpu/hip/*` backends. 219 220The `/gpu/*/magma/*` backends rely upon the [MAGMA](https://bitbucket.org/icl/magma) package. 221To enable the MAGMA backends, the environment variable `MAGMA_DIR` must point to the top-level MAGMA directory, with the MAGMA library located in `$(MAGMA_DIR)/lib/`. 222By default, `MAGMA_DIR` is set to `../magma`; to build the MAGMA backends with a MAGMA installation located elsewhere, create a link to `magma/` in libCEED's parent directory, or set `MAGMA_DIR` to the proper location. 223MAGMA version 2.5.0 or newer is required. 224Currently, each MAGMA library installation is only built for either CUDA or HIP. 225The corresponding set of libCEED backends (`/gpu/cuda/magma/*` or `/gpu/hip/magma/*`) will automatically be built for the version of the MAGMA library found in `MAGMA_DIR`. 226 227Users can specify a device for all CUDA, HIP, and MAGMA backends through adding `:device_id=#` after the resource name. 228For example: 229 230> - `/gpu/cuda/gen:device_id=1` 231 232The `/*/occa` backends rely upon the [OCCA](http://github.com/libocca/occa) package to provide cross platform performance. 233To enable the OCCA backend, the environment variable `OCCA_DIR` must point to the top-level OCCA directory, with the OCCA library located in the `${OCCA_DIR}/lib` (By default, `OCCA_DIR` is set to `../occa`). 234OCCA version 1.4.0 or newer is required. 235 236Users can pass specific OCCA device properties after setting the CEED resource. 237For example: 238 239> - `"/*/occa:mode='CUDA',device_id=0"` 240 241Bit-for-bit reproducibility is important in some applications. 242However, some libCEED backends use non-deterministic operations, such as `atomicAdd` for increased performance. 243The backends which are capable of generating reproducible results, with the proper compilation options, are highlighted in the list above. 244 245## Examples 246 247libCEED comes with several examples of its usage, ranging from standalone C codes in the `/examples/ceed` directory to examples based on external packages, such as MFEM, PETSc, and Nek5000. 248Nek5000 v18.0 or greater is required. 249 250To build the examples, set the `MFEM_DIR`, `PETSC_DIR`, and `NEK5K_DIR` variables and run: 251 252```console 253$ cd examples/ 254``` 255 256% running-examples-inclusion-marker 257 258```console 259# libCEED examples on CPU and GPU 260$ cd ceed/ 261$ make 262$ ./ex1-volume -ceed /cpu/self 263$ ./ex1-volume -ceed /gpu/cuda 264$ ./ex2-surface -ceed /cpu/self 265$ ./ex2-surface -ceed /gpu/cuda 266$ cd .. 267 268# MFEM+libCEED examples on CPU and GPU 269$ cd mfem/ 270$ make 271$ ./bp1 -ceed /cpu/self -no-vis 272$ ./bp3 -ceed /gpu/cuda -no-vis 273$ cd .. 274 275# Nek5000+libCEED examples on CPU and GPU 276$ cd nek/ 277$ make 278$ ./nek-examples.sh -e bp1 -ceed /cpu/self -b 3 279$ ./nek-examples.sh -e bp3 -ceed /gpu/cuda -b 3 280$ cd .. 281 282# PETSc+libCEED examples on CPU and GPU 283$ cd petsc/ 284$ make 285$ ./bps -problem bp1 -ceed /cpu/self 286$ ./bps -problem bp2 -ceed /gpu/cuda 287$ ./bps -problem bp3 -ceed /cpu/self 288$ ./bps -problem bp4 -ceed /gpu/cuda 289$ ./bps -problem bp5 -ceed /cpu/self 290$ ./bps -problem bp6 -ceed /gpu/cuda 291$ cd .. 292 293$ cd petsc/ 294$ make 295$ ./bpsraw -problem bp1 -ceed /cpu/self 296$ ./bpsraw -problem bp2 -ceed /gpu/cuda 297$ ./bpsraw -problem bp3 -ceed /cpu/self 298$ ./bpsraw -problem bp4 -ceed /gpu/cuda 299$ ./bpsraw -problem bp5 -ceed /cpu/self 300$ ./bpsraw -problem bp6 -ceed /gpu/cuda 301$ cd .. 302 303$ cd petsc/ 304$ make 305$ ./bpssphere -problem bp1 -ceed /cpu/self 306$ ./bpssphere -problem bp2 -ceed /gpu/cuda 307$ ./bpssphere -problem bp3 -ceed /cpu/self 308$ ./bpssphere -problem bp4 -ceed /gpu/cuda 309$ ./bpssphere -problem bp5 -ceed /cpu/self 310$ ./bpssphere -problem bp6 -ceed /gpu/cuda 311$ cd .. 312 313$ cd petsc/ 314$ make 315$ ./area -problem cube -ceed /cpu/self -degree 3 316$ ./area -problem cube -ceed /gpu/cuda -degree 3 317$ ./area -problem sphere -ceed /cpu/self -degree 3 -dm_refine 2 318$ ./area -problem sphere -ceed /gpu/cuda -degree 3 -dm_refine 2 319 320$ cd fluids/ 321$ make 322$ ./navierstokes -ceed /cpu/self -degree 1 323$ ./navierstokes -ceed /gpu/cuda -degree 1 324$ cd .. 325 326$ cd solids/ 327$ make 328$ ./elasticity -ceed /cpu/self -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -problem Linear -forcing mms 329$ ./elasticity -ceed /gpu/cuda -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -problem Linear -forcing mms 330$ cd .. 331``` 332 333For the last example shown, sample meshes to be used in place of `[.exo file]` can be found at <https://github.com/jeremylt/ceedSampleMeshes> 334 335The above code assumes a GPU-capable machine with the CUDA backends enabled. 336Depending on the available backends, other CEED resource specifiers can be provided with the `-ceed` option. 337Other command line arguments can be found in [examples/petsc](https://github.com/CEED/libCEED/blob/main/examples/petsc/README.md). 338 339% benchmarks-marker 340 341## Benchmarks 342 343A sequence of benchmarks for all enabled backends can be run using: 344 345```console 346$ make benchmarks 347``` 348 349The results from the benchmarks are stored inside the `benchmarks/` directory and they can be viewed using the commands (requires python with matplotlib): 350 351```console 352$ cd benchmarks 353$ python postprocess-plot.py petsc-bps-bp1-*-output.txt 354$ python postprocess-plot.py petsc-bps-bp3-*-output.txt 355``` 356 357Using the `benchmarks` target runs a comprehensive set of benchmarks which may take some time to run. 358Subsets of the benchmarks can be run using the scripts in the `benchmarks` folder. 359 360For more details about the benchmarks, see the `benchmarks/README.md` file. 361 362## Install 363 364To install libCEED, run: 365 366```console 367$ make install prefix=/path/to/install/dir 368``` 369 370or (e.g., if creating packages): 371 372```console 373$ make install prefix=/usr DESTDIR=/packaging/path 374``` 375 376To build and install in separate steps, run: 377 378```console 379$ make for_install=1 prefix=/path/to/install/dir 380$ make install prefix=/path/to/install/dir 381``` 382 383The usual variables like `CC` and `CFLAGS` are used, and optimization flags for all languages can be set using the likes of `OPT='-O3 -march=native'`. 384Use `STATIC=1` to build static libraries (`libceed.a`). 385 386To install libCEED for Python, run: 387 388```console 389$ pip install libceed 390``` 391 392with the desired setuptools options, such as `--user`. 393 394### pkg-config 395 396In addition to library and header, libCEED provides a [pkg-config](https://en.wikipedia.org/wiki/Pkg-config) file that can be used to easily compile and link. 397[For example](https://people.freedesktop.org/~dbn/pkg-config-guide.html#faq), if `$prefix` is a standard location or you set the environment variable `PKG_CONFIG_PATH`: 398 399```console 400$ cc `pkg-config --cflags --libs ceed` -o myapp myapp.c 401``` 402 403will build `myapp` with libCEED. 404This can be used with the source or installed directories. 405Most build systems have support for pkg-config. 406 407## Contact 408 409You can reach the libCEED team by emailing [ceed-users@llnl.gov](mailto:ceed-users@llnl.gov) or by leaving a comment in the [issue tracker](https://github.com/CEED/libCEED/issues). 410 411## How to Cite 412 413If you utilize libCEED please cite: 414 415```bibtex 416@article{libceed-joss-paper, 417 author = {Jed Brown and Ahmad Abdelfattah and Valeria Barra and Natalie Beams and Jean Sylvain Camier and Veselin Dobrev and Yohann Dudouit and Leila Ghaffari and Tzanio Kolev and David Medina and Will Pazner and Thilina Ratnayaka and Jeremy Thompson and Stan Tomov}, 418 title = {{libCEED}: Fast algebra for high-order element-based discretizations}, 419 journal = {Journal of Open Source Software}, 420 year = {2021}, 421 publisher = {The Open Journal}, 422 volume = {6}, 423 number = {63}, 424 pages = {2945}, 425 doi = {10.21105/joss.02945} 426} 427``` 428 429The archival copy of the libCEED user manual is maintained on [Zenodo](https://doi.org/10.5281/zenodo.4302736). 430To cite the user manual: 431 432```bibtex 433@misc{libceed-user-manual, 434 author = {Abdelfattah, Ahmad and 435 Barra, Valeria and 436 Beams, Natalie and 437 Brown, Jed and 438 Camier, Jean-Sylvain and 439 Dobrev, Veselin and 440 Dudouit, Yohann and 441 Ghaffari, Leila and 442 Grimberg, Sebastian and 443 Kolev, Tzanio and 444 Medina, David and 445 Pazner, Will and 446 Ratnayaka, Thilina and 447 Shakeri, Rezgar and 448 Thompson, Jeremy L and 449 Tomov, Stanimire and 450 Wright III, James}, 451 title = {{libCEED} User Manual}, 452 month = nov, 453 year = 2023, 454 publisher = {Zenodo}, 455 version = {0.12.0}, 456 doi = {10.5281/zenodo.10062388} 457} 458``` 459 460For libCEED's Python interface please cite: 461 462```bibtex 463@InProceedings{libceed-paper-proc-scipy-2020, 464 author = {{V}aleria {B}arra and {J}ed {B}rown and {J}eremy {T}hompson and {Y}ohann {D}udouit}, 465 title = {{H}igh-performance operator evaluations with ease of use: lib{C}{E}{E}{D}'s {P}ython interface}, 466 booktitle = {{P}roceedings of the 19th {P}ython in {S}cience {C}onference}, 467 pages = {85 - 90}, 468 year = {2020}, 469 editor = {{M}eghann {A}garwal and {C}hris {C}alloway and {D}illon {N}iederhut and {D}avid {S}hupe}, 470 doi = {10.25080/Majora-342d178e-00c} 471} 472``` 473 474The BibTeX entries for these references can be found in the `doc/bib/references.bib` file. 475 476## Copyright 477 478The following copyright applies to each file in the CEED software suite, unless otherwise stated in the file: 479 480> Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 481> All rights reserved. 482 483See files LICENSE and NOTICE for details. 484 485[github-badge]: https://github.com/CEED/libCEED/workflows/C/Fortran/badge.svg 486[github-link]: https://github.com/CEED/libCEED/actions 487[gitlab-badge]: https://gitlab.com/libceed/libCEED/badges/main/pipeline.svg?key_text=GitLab-CI 488[gitlab-link]: https://gitlab.com/libceed/libCEED/-/pipelines?page=1&scope=all&ref=main 489[codecov-badge]: https://codecov.io/gh/CEED/libCEED/branch/main/graphs/badge.svg 490[codecov-link]: https://codecov.io/gh/CEED/libCEED/ 491[license-badge]: https://img.shields.io/badge/License-BSD%202--Clause-orange.svg 492[license-link]: https://opensource.org/licenses/BSD-2-Clause 493[doc-badge]: https://readthedocs.org/projects/libceed/badge/?version=latest 494[doc-link]: https://libceed.org/en/latest/?badge=latest 495[joss-badge]: https://joss.theoj.org/papers/10.21105/joss.02945/status.svg 496[joss-link]: https://doi.org/10.21105/joss.02945 497[binder-badge]: http://mybinder.org/badge_logo.svg 498[binder-link]: https://mybinder.org/v2/gh/CEED/libCEED/main?urlpath=lab/tree/examples/python/tutorial-0-ceed.ipynb 499