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