xref: /libCEED/examples/fluids/include/mat-ceed-impl.h (revision a171b6ef1192a8f5f3031330339aaab2ded4c8fb)
1 // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 #pragma once
8 
9 #include <ceed.h>
10 #include <petscdm.h>
11 #include <petscmat.h>
12 
13 #if defined(__clang_analyzer__)
14 #define MATCEED_EXTERN extern
15 #elif defined(__cplusplus)
16 #define MATCEED_EXTERN extern "C"
17 #else
18 #define MATCEED_EXTERN extern
19 #endif
20 
21 #if defined(__clang_analyzer__)
22 #define MATCEED_INTERN
23 #else
24 #define MATCEED_INTERN MATCEED_EXTERN __attribute__((visibility("hidden")))
25 #endif
26 
27 /**
28   @brief Calls a libCEED function and then checks the resulting error code.
29   If the error code is non-zero, then a PETSc error is set with the libCEED error message.
30 **/
31 #ifndef PetscCallCeed
32 #define PetscCallCeed(ceed_, ...)                                   \
33   do {                                                              \
34     int ierr_q_ = __VA_ARGS__;                                      \
35     if (ierr_q_ != CEED_ERROR_SUCCESS) {                            \
36       const char *error_message;                                    \
37       CeedGetErrorMessage(ceed_, &error_message);                   \
38       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "%s", error_message); \
39     }                                                               \
40   } while (0)
41 #endif
42 
43 // MatCeed context for applying composite CeedOperator on a DM
44 typedef struct MatCeedContext_private *MatCeedContext;
45 struct MatCeedContext_private {
46   Ceed           ceed;
47   char          *name, *internal_mat_type;
48   PetscMemType   mem_type;
49   PetscInt       ref_count, num_mats_assembled_full, num_mats_assembled_pbd;
50   PetscBool      is_destroyed, is_ceed_pbd_valid, is_ceed_vpbd_valid;
51   PetscLogEvent  log_event_mult, log_event_mult_transpose;
52   DM             dm_x, dm_y;
53   Mat           *mats_assembled_full, *mats_assembled_pbd, mat_assembled_full_internal, mat_assembled_pbd_internal;
54   Vec            X_loc, Y_loc_transpose;
55   CeedVector     x_loc, y_loc, coo_values_full, coo_values_pbd;
56   CeedOperator   op_mult, op_mult_transpose;
57   PetscLogDouble flops_mult, flops_mult_transpose;
58 };
59 
60 // Context data
61 MATCEED_INTERN PetscErrorCode MatCeedContextCreate(DM dm_x, DM dm_y, Vec X_loc, Vec Y_loc_transpose, CeedOperator op_mult,
62                                                    CeedOperator op_mult_transpose, PetscLogEvent log_event_mult,
63                                                    PetscLogEvent log_event_mult_transpose, MatCeedContext *ctx);
64 MATCEED_INTERN PetscErrorCode MatCeedContextReference(MatCeedContext ctx);
65 MATCEED_INTERN PetscErrorCode MatCeedContextReferenceCopy(MatCeedContext ctx, MatCeedContext *ctx_copy);
66 MATCEED_INTERN PetscErrorCode MatCeedContextDestroy(MatCeedContext ctx);
67 
68 // Mat Ceed
69 MATCEED_INTERN PetscErrorCode MatGetDiagonal_Ceed(Mat A, Vec D);
70 MATCEED_INTERN PetscErrorCode MatMult_Ceed(Mat A, Vec X, Vec Y);
71 MATCEED_INTERN PetscErrorCode MatMultTranspose_Ceed(Mat A, Vec Y, Vec X);
72 
73 extern PetscClassId  MATCEED_CLASSID;
74 extern PetscLogEvent MATCEED_MULT, MATCEED_MULT_TRANSPOSE;
75