1# LibCEED.jl Docs 2 3Documentation for the LibCEED.jl Julia interface to the 4[libCEED](https://github.com/ceed/libceed) library. 5 6For further information, see also the [libCEED 7documentation](https://libceed.readthedocs.io/). 8 9Several [short examples](Examples.md) are included to demonstrate the 10functionality. 11 12### Installation 13 14The LibCEED.jl package can be installed with Julia's package manager by running 15`] add LibCEED`. This will automatically install a pre-built binary of the 16libCEED library. If you require features of a specific build of libCEED (e.g. 17CUDA/GPU support, specific compiler flags, etc.) then you should compile your 18own version of the libCEED library, and configure LibCEED.jl to use this binary 19as described in the [Configuring LibCEED.jl](@ref) section. 20 21 22!!! warning "The pre-built libCEED binaries do not support CUDA backends" 23 The pre-built binaries automatically installed by LibCEED.jl (through the 24 [libCEED_jll](https://juliahub.com/ui/Packages/libCEED_jll/LB2fn) package) 25 are not built with CUDA support. If you want to run libCEED on the GPU, you 26 will have to build libCEED from source and configure LibCEED.jl as described 27 in the [Configuring LibCEED.jl](@ref) section. 28 29#### Configuring LibCEED.jl 30 31By default, LibCEED.jl will use the pre-built libCEED binaries provided by the 32[libCEED_jll](https://juliahub.com/ui/Packages/libCEED_jll/LB2fn) package. If 33you wish to use a different libCEED binary (e.g. one built from source), 34LibCEED.jl can be configured using the `JULIA_LIBCEED_LIB` environment variable 35set to the absolute path of the libCEED dynamic library. For the configuration 36to take effect, LibCEED.jl must be **built** with this environment variable, for 37example: 38 39```julia 40% JULIA_LIBCEED_LIB=/path/to/libceed.so julia 41julia> # press ] to enter package manager 42(env) pkg> build LibCEED 43``` 44or, equivalently, 45```julia 46julia> withenv("JULIA_LIBCEED_LIB" => "/path/to/libceed.so") do 47 Pkg.build("LibCEED") 48end 49``` 50 51 52### Features of the high-level interface for libCEED 53 54#### User Q-functions 55 56With LibCEED.jl, it is much easier to write dimension-independent user-defined 57Q-functions that automatically work on the GPU. See the [related 58documentation](UserQFunctions.md) for more information. 59 60#### Safe access to CeedVector objects 61 62When accessing [`CeedVector`](@ref) objects, the C interface requires the user 63to manually call `CeedVectorGetArray`, paired with `CeedVectorRestoreArray`. If 64the user wants read-only access, then the user must call 65`CeedVectorGetArrayRead`, paired with `CeedVectorRestoreArrayRead`. This can 66possibly be bug-prone, because the user may forget to restore the array, or may 67match the `Read` version to get the array with non-`Read` version to restore the 68array (or vice versa). 69 70In LibCEED.jl, this difficulty is mitigated using the [`witharray`](@ref) 71function and [`@witharray`](@ref) macro. There are also read-only versions, 72[`witharray_read`](@ref) and [`@witharray_read`](@ref). When using this 73functionality, it is impossible to forget to restore the array, and the correct 74version is always paired properly. 75 76For example, in `ex1-volume`, the following C code 77```c 78// Compute and print the sum of the entries of 'v' giving the mesh volume. 79const CeedScalar *v_host; 80CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_host); 81CeedScalar vol = 0.; 82for (CeedInt i = 0; i < sol_size; i++) { 83 vol += v_host[i]; 84} 85CeedVectorRestoreArrayRead(v, &v_host); 86``` 87is replaced with the following equivalent Julia code 88```julia 89# Compute and print the sum of the entries of 'v' giving the mesh volume. 90vol = witharray_read(sum, v, MEM_HOST) 91``` 92 93In `ex2-surface`, the following C code 94```c 95// Initialize 'u' with sum of coordinates, x+y+z. 96CeedScalar *u_host; 97const CeedScalar *x_host; 98CeedVectorGetArray(u, CEED_MEM_HOST, &u_host); 99CeedVectorGetArrayRead(mesh_coords, CEED_MEM_HOST, &x_host); 100for (CeedInt i = 0; i < sol_size; i++) { 101 u_host[i] = 0; 102 for (CeedInt d = 0; d < dim; d++) 103 u_host[i] += x_host[i+d*sol_size]; 104} 105CeedVectorRestoreArray(u, &u_host); 106CeedVectorRestoreArrayRead(mesh_coords, &x_host); 107``` 108is replaced with the following equivalent Julia code 109```julia 110@witharray_read(x_host=mesh_coords, size=(mesh_size÷dim, dim), 111 @witharray(u_host=u, size=(sol_size,1), 112 sum!(u_host, x_host))) 113``` 114The macro version can provide better performance if a closure is required, and 115allow for convenient reshaping of the vector into equivalently sized matrices 116or tensors. 117 118### Ceed objects 119```@contents 120Pages = [ 121 "Ceed.md", 122 "CeedVector.md", 123 "ElemRestriction.md", 124 "Basis.md", 125 "QFunction.md", 126 "Operator.md", 127] 128``` 129 130### Utilities 131```@contents 132Pages = [ 133 "Misc.md", 134 "Globals.md", 135 "Quadrature.md", 136] 137``` 138 139### C interface 140```@contents 141Pages = ["C.md"] 142``` 143