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