xref: /libCEED/README.md (revision cc2ec7ec82ea595f9373397304c8373f122cc9f8)
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<!-- getting-started-inclusion -->
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
187The `/cpu/self/*/serial` backends process one element at a time and are intended for meshes with a smaller number of high order elements.
188The `/cpu/self/*/blocked` backends process blocked batches of eight interlaced elements and are intended for meshes with higher numbers of elements.
189
190The `/cpu/self/ref/*` backends are written in pure C and provide basic functionality.
191
192The `/cpu/self/opt/*` backends are written in pure C and use partial e-vectors to improve performance.
193
194The `/cpu/self/avx/*` backends rely upon AVX instructions to provide vectorized CPU performance.
195
196The `/cpu/self/memcheck/*` backends rely upon the [Valgrind](https://valgrind.org/) Memcheck tool to help verify that user QFunctions have no undefined values.
197To use, run your code with Valgrind and the Memcheck backends, e.g. `valgrind ./build/ex1 -ceed /cpu/self/ref/memcheck`.
198A 'development' or 'debugging' version of Valgrind with headers is required to use this backend.
199This 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.
200
201The `/cpu/self/xsmm/*` backends rely upon the [LIBXSMM](https://github.com/libxsmm/libxsmm) package to provide vectorized CPU performance.
202If 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`.
203The LIBXSMM `main` development branch from 7 April 2024 or newer is required.
204
205The `/gpu/cuda/*` backends provide GPU performance strictly using CUDA.
206
207The `/gpu/hip/*` backends provide GPU performance strictly using HIP.
208They are based on the `/gpu/cuda/*` backends.
209ROCm version 4.2 or newer is required.
210
211The `/gpu/sycl/*` backends provide GPU performance strictly using SYCL.
212They are based on the `/gpu/cuda/*` and `/gpu/hip/*` backends.
213
214The `/gpu/*/magma/*` backends rely upon the [MAGMA](https://bitbucket.org/icl/magma) package.
215To 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/`.
216By 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.
217MAGMA version 2.5.0 or newer is required.
218Currently, each MAGMA library installation is only built for either CUDA or HIP.
219The 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`.
220
221Users can specify a device for all CUDA, HIP, and MAGMA backends through adding `:device_id=#` after the resource name.
222For example:
223
224> - `/gpu/cuda/gen:device_id=1`
225
226Bit-for-bit reproducibility is important in some applications.
227However, some libCEED backends use non-deterministic operations, such as `atomicAdd` for increased performance.
228The backends which are capable of generating reproducible results, with the proper compilation options, are highlighted in the list above.
229
230<!-- getting-started-exclusion -->
231
232## Examples
233
234libCEED 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.
235Nek5000 v18.0 or greater is required.
236
237To build the examples, set the `MFEM_DIR`, `PETSC_DIR` (and optionally `PETSC_ARCH`), and `NEK5K_DIR` variables and run:
238
239```console
240$ cd examples/
241```
242
243<!-- running-examples-inclusion -->
244
245```console
246# libCEED examples on CPU and GPU
247$ cd ceed/
248$ make
249$ ./ex1-volume -ceed /cpu/self
250$ ./ex1-volume -ceed /gpu/cuda
251$ ./ex2-surface -ceed /cpu/self
252$ ./ex2-surface -ceed /gpu/cuda
253$ cd ..
254
255# MFEM+libCEED examples on CPU and GPU
256$ cd mfem/
257$ make
258$ ./bp1 -ceed /cpu/self -no-vis
259$ ./bp3 -ceed /gpu/cuda -no-vis
260$ cd ..
261
262# Nek5000+libCEED examples on CPU and GPU
263$ cd nek/
264$ make
265$ ./nek-examples.sh -e bp1 -ceed /cpu/self -b 3
266$ ./nek-examples.sh -e bp3 -ceed /gpu/cuda -b 3
267$ cd ..
268
269# PETSc+libCEED examples on CPU and GPU
270$ cd petsc/
271$ make
272$ ./bps -problem bp1 -ceed /cpu/self
273$ ./bps -problem bp2 -ceed /gpu/cuda
274$ ./bps -problem bp3 -ceed /cpu/self
275$ ./bps -problem bp4 -ceed /gpu/cuda
276$ ./bps -problem bp5 -ceed /cpu/self
277$ ./bps -problem bp6 -ceed /gpu/cuda
278$ cd ..
279
280$ cd petsc/
281$ make
282$ ./bpsraw -problem bp1 -ceed /cpu/self
283$ ./bpsraw -problem bp2 -ceed /gpu/cuda
284$ ./bpsraw -problem bp3 -ceed /cpu/self
285$ ./bpsraw -problem bp4 -ceed /gpu/cuda
286$ ./bpsraw -problem bp5 -ceed /cpu/self
287$ ./bpsraw -problem bp6 -ceed /gpu/cuda
288$ cd ..
289
290$ cd petsc/
291$ make
292$ ./bpssphere -problem bp1 -ceed /cpu/self
293$ ./bpssphere -problem bp2 -ceed /gpu/cuda
294$ ./bpssphere -problem bp3 -ceed /cpu/self
295$ ./bpssphere -problem bp4 -ceed /gpu/cuda
296$ ./bpssphere -problem bp5 -ceed /cpu/self
297$ ./bpssphere -problem bp6 -ceed /gpu/cuda
298$ cd ..
299
300$ cd petsc/
301$ make
302$ ./area -problem cube -ceed /cpu/self -degree 3
303$ ./area -problem cube -ceed /gpu/cuda -degree 3
304$ ./area -problem sphere -ceed /cpu/self -degree 3 -dm_refine 2
305$ ./area -problem sphere -ceed /gpu/cuda -degree 3 -dm_refine 2
306
307$ cd fluids/
308$ make
309$ ./navierstokes -ceed /cpu/self -degree 1
310$ ./navierstokes -ceed /gpu/cuda -degree 1
311$ cd ..
312
313$ cd solids/
314$ make
315$ ./elasticity -ceed /cpu/self -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -problem Linear -forcing mms
316$ ./elasticity -ceed /gpu/cuda -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -problem Linear -forcing mms
317$ cd ..
318```
319
320For the last example shown, sample meshes to be used in place of `[.exo file]` can be found at <https://github.com/jeremylt/ceedSampleMeshes>
321
322The above code assumes a GPU-capable machine with the CUDA backends enabled.
323Depending on the available backends, other CEED resource specifiers can be provided with the `-ceed` option.
324Other command line arguments can be found in [examples/petsc](https://github.com/CEED/libCEED/blob/main/examples/petsc/README.md).
325
326<!-- running-examples-exclusion -->
327
328## Benchmarks
329
330A sequence of benchmarks for all enabled backends can be run using:
331
332```console
333$ make benchmarks
334```
335
336The results from the benchmarks are stored inside the `benchmarks/` directory and they can be viewed using the commands (requires python with matplotlib):
337
338```console
339$ cd benchmarks
340$ python postprocess-plot.py petsc-bps-bp1-*-output.txt
341$ python postprocess-plot.py petsc-bps-bp3-*-output.txt
342```
343
344Using the `benchmarks` target runs a comprehensive set of benchmarks which may take some time to run.
345Subsets of the benchmarks can be run using the scripts in the `benchmarks` folder.
346
347For more details about the benchmarks, see the `benchmarks/README.md` file.
348
349## Install
350
351To install libCEED, run:
352
353```console
354$ make install prefix=/path/to/install/dir
355```
356
357or (e.g., if creating packages):
358
359```console
360$ make install prefix=/usr DESTDIR=/packaging/path
361```
362
363To build and install in separate steps, run:
364
365```console
366$ make for_install=1 prefix=/path/to/install/dir
367$ make install prefix=/path/to/install/dir
368```
369
370The 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'`.
371Use `STATIC=1` to build static libraries (`libceed.a`).
372
373To install libCEED for Python, run:
374
375```console
376$ pip install libceed
377```
378
379with the desired setuptools options, such as `--user`.
380
381### pkg-config
382
383In 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.
384[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`:
385
386```console
387$ cc `pkg-config --cflags --libs ceed` -o myapp myapp.c
388```
389
390will build `myapp` with libCEED.
391This can be used with the source or installed directories.
392Most build systems have support for pkg-config.
393
394## Contact
395
396You 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).
397
398## How to Cite
399
400If you utilize libCEED please cite:
401
402```bibtex
403@article{libceed-joss-paper,
404  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},
405  title        = {{libCEED}: Fast algebra for high-order element-based discretizations},
406  journal      = {Journal of Open Source Software},
407  year         = {2021},
408  publisher    = {The Open Journal},
409  volume       = {6},
410  number       = {63},
411  pages        = {2945},
412  doi          = {10.21105/joss.02945}
413}
414```
415
416The archival copy of the libCEED user manual is maintained on [Zenodo](https://doi.org/10.5281/zenodo.4302736).
417To cite the user manual:
418
419```bibtex
420@misc{libceed-user-manual,
421  author       = {Abdelfattah, Ahmad and
422                  Barra, Valeria and
423                  Beams, Natalie and
424                  Brown, Jed and
425                  Camier, Jean-Sylvain and
426                  Dobrev, Veselin and
427                  Dudouit, Yohann and
428                  Ghaffari, Leila and
429                  Grimberg, Sebastian and
430                  Kolev, Tzanio and
431                  Medina, David and
432                  Pazner, Will and
433                  Ratnayaka, Thilina and
434                  Shakeri, Rezgar and
435                  Thompson, Jeremy L and
436                  Tomov, Stanimire and
437                  Wright III, James},
438  title        = {{libCEED} User Manual},
439  month        = nov,
440  year         = 2023,
441  publisher    = {Zenodo},
442  version      = {0.12.0},
443  doi          = {10.5281/zenodo.10062388}
444}
445```
446
447For libCEED's Python interface please cite:
448
449```bibtex
450@InProceedings{libceed-paper-proc-scipy-2020,
451  author    = {{V}aleria {B}arra and {J}ed {B}rown and {J}eremy {T}hompson and {Y}ohann {D}udouit},
452  title     = {{H}igh-performance operator evaluations with ease of use: lib{C}{E}{E}{D}'s {P}ython interface},
453  booktitle = {{P}roceedings of the 19th {P}ython in {S}cience {C}onference},
454  pages     = {85 - 90},
455  year      = {2020},
456  editor    = {{M}eghann {A}garwal and {C}hris {C}alloway and {D}illon {N}iederhut and {D}avid {S}hupe},
457  doi       = {10.25080/Majora-342d178e-00c}
458}
459```
460
461The BibTeX entries for these references can be found in the `doc/bib/references.bib` file.
462
463## Copyright
464
465The following copyright applies to each file in the CEED software suite, unless otherwise stated in the file:
466
467> Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
468> All rights reserved.
469
470See files LICENSE and NOTICE for details.
471
472[github-badge]: https://github.com/CEED/libCEED/workflows/C/Fortran/badge.svg
473[github-link]: https://github.com/CEED/libCEED/actions
474[gitlab-badge]: https://gitlab.com/libceed/libCEED/badges/main/pipeline.svg?key_text=GitLab-CI
475[gitlab-link]: https://gitlab.com/libceed/libCEED/-/pipelines?page=1&scope=all&ref=main
476[codecov-badge]: https://codecov.io/gh/CEED/libCEED/branch/main/graphs/badge.svg
477[codecov-link]: https://codecov.io/gh/CEED/libCEED/
478[license-badge]: https://img.shields.io/badge/License-BSD%202--Clause-orange.svg
479[license-link]: https://opensource.org/licenses/BSD-2-Clause
480[doc-badge]: https://readthedocs.org/projects/libceed/badge/?version=latest
481[doc-link]: https://libceed.org/en/latest/?badge=latest
482[joss-badge]: https://joss.theoj.org/papers/10.21105/joss.02945/status.svg
483[joss-link]: https://doi.org/10.21105/joss.02945
484[binder-badge]: http://mybinder.org/badge_logo.svg
485[binder-link]: https://mybinder.org/v2/gh/CEED/libCEED/main?urlpath=lab/tree/examples/python/tutorial-0-ceed.ipynb
486