xref: /honee/include/mat-ceed-impl.h (revision 18fb77588857caa2723da956ec9edbb6264c2de7)
1*18fb7758SJeremy L Thompson // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2*18fb7758SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*18fb7758SJeremy L Thompson //
4*18fb7758SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5*18fb7758SJeremy L Thompson //
6*18fb7758SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7*18fb7758SJeremy L Thompson #pragma once
858600ac3SJames Wright 
958600ac3SJames Wright #include <ceed.h>
1058600ac3SJames Wright #include <petscdm.h>
1158600ac3SJames Wright #include <petscmat.h>
1258600ac3SJames Wright 
1358600ac3SJames Wright #if defined(__clang_analyzer__)
1458600ac3SJames Wright #define MATCEED_EXTERN extern
1558600ac3SJames Wright #elif defined(__cplusplus)
1658600ac3SJames Wright #define MATCEED_EXTERN extern "C"
1758600ac3SJames Wright #else
1858600ac3SJames Wright #define MATCEED_EXTERN extern
1958600ac3SJames Wright #endif
2058600ac3SJames Wright 
2158600ac3SJames Wright #if defined(__clang_analyzer__)
2258600ac3SJames Wright #define MATCEED_INTERN
2358600ac3SJames Wright #else
2458600ac3SJames Wright #define MATCEED_INTERN MATCEED_EXTERN __attribute__((visibility("hidden")))
2558600ac3SJames Wright #endif
2658600ac3SJames Wright 
2758600ac3SJames Wright /**
2858600ac3SJames Wright   @brief Calls a libCEED function and then checks the resulting error code.
2958600ac3SJames Wright   If the error code is non-zero, then a PETSc error is set with the libCEED error message.
3058600ac3SJames Wright **/
31a7dac1d5SJames Wright #ifndef PetscCallCeed
3250f50432SJames Wright #define PetscCallCeed(ceed_, ...)                                   \
3358600ac3SJames Wright   do {                                                              \
3458600ac3SJames Wright     int ierr_q_ = __VA_ARGS__;                                      \
3558600ac3SJames Wright     if (ierr_q_ != CEED_ERROR_SUCCESS) {                            \
3658600ac3SJames Wright       const char *error_message;                                    \
3758600ac3SJames Wright       CeedGetErrorMessage(ceed_, &error_message);                   \
3858600ac3SJames Wright       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "%s", error_message); \
3958600ac3SJames Wright     }                                                               \
4058600ac3SJames Wright   } while (0)
41a7dac1d5SJames Wright #endif
4258600ac3SJames Wright 
4358600ac3SJames Wright // MatCeed context for applying composite CeedOperator on a DM
4458600ac3SJames Wright typedef struct MatCeedContext_private *MatCeedContext;
4558600ac3SJames Wright struct MatCeedContext_private {
4658600ac3SJames Wright   Ceed           ceed;
4758600ac3SJames Wright   char          *name, *internal_mat_type;
4858600ac3SJames Wright   PetscMemType   mem_type;
4958600ac3SJames Wright   PetscInt       ref_count, num_mats_assembled_full, num_mats_assembled_pbd;
5058600ac3SJames Wright   PetscBool      is_destroyed, is_ceed_pbd_valid, is_ceed_vpbd_valid;
5158600ac3SJames Wright   PetscLogEvent  log_event_mult, log_event_mult_transpose;
5258600ac3SJames Wright   DM             dm_x, dm_y;
5358600ac3SJames Wright   Mat           *mats_assembled_full, *mats_assembled_pbd, mat_assembled_full_internal, mat_assembled_pbd_internal;
5458600ac3SJames Wright   Vec            X_loc, Y_loc_transpose;
5558600ac3SJames Wright   CeedVector     x_loc, y_loc, coo_values_full, coo_values_pbd;
5658600ac3SJames Wright   CeedOperator   op_mult, op_mult_transpose;
5758600ac3SJames Wright   PetscLogDouble flops_mult, flops_mult_transpose;
5858600ac3SJames Wright };
5958600ac3SJames Wright 
6058600ac3SJames Wright // Context data
6158600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatCeedContextCreate(DM dm_x, DM dm_y, Vec X_loc, Vec Y_loc_transpose, CeedOperator op_mult,
6258600ac3SJames Wright                                                    CeedOperator op_mult_transpose, PetscLogEvent log_event_mult,
6358600ac3SJames Wright                                                    PetscLogEvent log_event_mult_transpose, MatCeedContext *ctx);
6458600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatCeedContextReference(MatCeedContext ctx);
6558600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatCeedContextReferenceCopy(MatCeedContext ctx, MatCeedContext *ctx_copy);
6658600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatCeedContextDestroy(MatCeedContext ctx);
6758600ac3SJames Wright 
6858600ac3SJames Wright // Mat Ceed
6958600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatGetDiagonal_Ceed(Mat A, Vec D);
7058600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatMult_Ceed(Mat A, Vec X, Vec Y);
7158600ac3SJames Wright MATCEED_INTERN PetscErrorCode MatMultTranspose_Ceed(Mat A, Vec Y, Vec X);
7258600ac3SJames Wright 
7358600ac3SJames Wright extern PetscClassId  MATCEED_CLASSID;
7458600ac3SJames Wright extern PetscLogEvent MATCEED_MULT, MATCEED_MULT_TRANSPOSE;
75