xref: /honee/include/mat-ceed-impl.h (revision fcb2c22a40d027d860b73be9ac89e2e220a55865)
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 #include <petsc/private/petscimpl.h>
13 
14 #if defined(__clang_analyzer__)
15 #define MATCEED_EXTERN extern
16 #elif defined(__cplusplus)
17 #define MATCEED_EXTERN extern "C"
18 #else
19 #define MATCEED_EXTERN extern
20 #endif
21 
22 #if defined(__clang_analyzer__)
23 #define MATCEED_INTERN
24 #else
25 #define MATCEED_INTERN MATCEED_EXTERN __attribute__((visibility("hidden")))
26 #endif
27 
28 /**
29   @brief Calls a libCEED function and then checks the resulting error code.
30   If the error code is non-zero, then a PETSc error is set with the libCEED error message.
31 **/
32 #ifndef PetscCallCeed
33 #define PetscCallCeed(ceed_, ...)                                   \
34   do {                                                              \
35     int ierr_q_ = __VA_ARGS__;                                      \
36     if (ierr_q_ != CEED_ERROR_SUCCESS) {                            \
37       const char *error_message;                                    \
38       CeedGetErrorMessage(ceed_, &error_message);                   \
39       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "%s", error_message); \
40     }                                                               \
41   } while (0)
42 #endif
43 
44 // MatCeed context for applying composite CeedOperator on a DM
45 typedef struct MatCeedContext_private *MatCeedContext;
46 struct MatCeedContext_private {
47   Ceed           ceed;
48   char          *name, *internal_mat_type;
49   PetscMemType   mem_type;
50   PetscInt       ref_count, num_mats_assembled_full, num_mats_assembled_pbd;
51   PetscBool      is_destroyed, is_ceed_pbd_valid, is_ceed_vpbd_valid;
52   PetscLogEvent  log_event_mult, log_event_mult_transpose;
53   DM             dm_x, dm_y;
54   Mat           *mats_assembled_full, *mats_assembled_pbd, mat_assembled_full_internal, mat_assembled_pbd_internal;
55   Vec            X_loc, Y_loc_transpose;
56   CeedVector     x_loc, y_loc, coo_values_full, coo_values_pbd;
57   CeedOperator   op_mult, op_mult_transpose;
58   PetscLogDouble flops_mult, flops_mult_transpose;
59 };
60 
61 // Context data
62 MATCEED_INTERN PetscErrorCode MatCeedContextCreate(DM dm_x, DM dm_y, Vec X_loc, Vec Y_loc_transpose, CeedOperator op_mult,
63                                                    CeedOperator op_mult_transpose, PetscLogEvent log_event_mult,
64                                                    PetscLogEvent log_event_mult_transpose, MatCeedContext *ctx);
65 MATCEED_INTERN PetscErrorCode MatCeedContextReference(MatCeedContext ctx);
66 MATCEED_INTERN PetscErrorCode MatCeedContextReferenceCopy(MatCeedContext ctx, MatCeedContext *ctx_copy);
67 MATCEED_INTERN PetscErrorCode MatCeedContextDestroy(MatCeedContext ctx);
68 
69 // Mat Ceed
70 MATCEED_INTERN PetscErrorCode MatGetDiagonal_Ceed(Mat A, Vec D);
71 MATCEED_INTERN PetscErrorCode MatMult_Ceed(Mat A, Vec X, Vec Y);
72 MATCEED_INTERN PetscErrorCode MatMultTranspose_Ceed(Mat A, Vec Y, Vec X);
73 
74 extern PetscClassId  MATCEED_CLASSID;
75 extern PetscLogEvent MATCEED_MULT, MATCEED_MULT_TRANSPOSE;
76