xref: /libCEED/examples/solids/include/utils.h (revision 4d00b080eb3f95d2e04e55c0ff369c5c847bb288)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
33d8e8822SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
53d8e8822SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
724a65d3dSJeremy L Thompson #pragma once
85754ecacSJeremy L Thompson 
95754ecacSJeremy L Thompson #include <ceed.h>
1049aac155SJeremy L Thompson #include <petscsys.h>
115754ecacSJeremy L Thompson 
125754ecacSJeremy L Thompson // Translate PetscMemType to CeedMemType
132b730f8bSJeremy L Thompson static inline CeedMemType MemTypeP2C(PetscMemType mem_type) { return PetscMemTypeDevice(mem_type) ? CEED_MEM_DEVICE : CEED_MEM_HOST; }
14*4d00b080SJeremy L Thompson 
15*4d00b080SJeremy L Thompson /**
16*4d00b080SJeremy L Thompson   @brief Translate array of `PetscInt` to `CeedInt`.
17*4d00b080SJeremy L Thompson     If the types differ, `array_petsc` is freed with `PetscFree()` and `array_ceed` is allocated with `PetscMalloc1()`.
18*4d00b080SJeremy L Thompson     Caller is responsible for freeing `array_ceed` with `PetscFree()`.
19*4d00b080SJeremy L Thompson 
20*4d00b080SJeremy L Thompson   Not collective across MPI processes.
21*4d00b080SJeremy L Thompson 
22*4d00b080SJeremy L Thompson   @param[in]      num_entries  Number of array entries
23*4d00b080SJeremy L Thompson   @param[in,out]  array_petsc  Array of `PetscInt`
24*4d00b080SJeremy L Thompson   @param[out]     array_ceed   Array of `CeedInt`
25*4d00b080SJeremy L Thompson 
26*4d00b080SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
27*4d00b080SJeremy L Thompson **/
28*4d00b080SJeremy L Thompson static inline PetscErrorCode IntArrayCeedToPetsc(PetscInt num_entries, CeedInt **array_ceed, PetscInt **array_petsc) {
29*4d00b080SJeremy L Thompson   const CeedInt  int_c = 0;
30*4d00b080SJeremy L Thompson   const PetscInt int_p = 0;
31*4d00b080SJeremy L Thompson 
32*4d00b080SJeremy L Thompson   PetscFunctionBeginUser;
33*4d00b080SJeremy L Thompson   if (sizeof(int_c) == sizeof(int_p)) {
34*4d00b080SJeremy L Thompson     *array_petsc = (PetscInt *)*array_ceed;
35*4d00b080SJeremy L Thompson   } else {
36*4d00b080SJeremy L Thompson     *array_petsc = malloc(num_entries * sizeof(PetscInt));
37*4d00b080SJeremy L Thompson     for (PetscInt i = 0; i < num_entries; i++) (*array_petsc)[i] = (*array_ceed)[i];
38*4d00b080SJeremy L Thompson     free(*array_ceed);
39*4d00b080SJeremy L Thompson   }
40*4d00b080SJeremy L Thompson   *array_ceed = NULL;
41*4d00b080SJeremy L Thompson   PetscFunctionReturn(PETSC_SUCCESS);
42*4d00b080SJeremy L Thompson }
43*4d00b080SJeremy L Thompson 
44*4d00b080SJeremy L Thompson /**
45*4d00b080SJeremy L Thompson   @brief Translate array of `PetscInt` to `CeedInt`.
46*4d00b080SJeremy L Thompson     If the types differ, `array_petsc` is freed with `PetscFree()` and `array_ceed` is allocated with `PetscMalloc1()`.
47*4d00b080SJeremy L Thompson     Caller is responsible for freeing `array_ceed` with `PetscFree()`.
48*4d00b080SJeremy L Thompson 
49*4d00b080SJeremy L Thompson   Not collective across MPI processes.
50*4d00b080SJeremy L Thompson 
51*4d00b080SJeremy L Thompson   @param[in]      num_entries  Number of array entries
52*4d00b080SJeremy L Thompson   @param[in,out]  array_petsc  Array of `PetscInt`
53*4d00b080SJeremy L Thompson   @param[out]     array_ceed   Array of `CeedInt`
54*4d00b080SJeremy L Thompson 
55*4d00b080SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
56*4d00b080SJeremy L Thompson **/
57*4d00b080SJeremy L Thompson static inline PetscErrorCode IntArrayPetscToCeed(PetscInt num_entries, PetscInt **array_petsc, CeedInt **array_ceed) {
58*4d00b080SJeremy L Thompson   const CeedInt  int_c = 0;
59*4d00b080SJeremy L Thompson   const PetscInt int_p = 0;
60*4d00b080SJeremy L Thompson 
61*4d00b080SJeremy L Thompson   PetscFunctionBeginUser;
62*4d00b080SJeremy L Thompson   if (sizeof(int_c) == sizeof(int_p)) {
63*4d00b080SJeremy L Thompson     *array_ceed = (CeedInt *)*array_petsc;
64*4d00b080SJeremy L Thompson   } else {
65*4d00b080SJeremy L Thompson     PetscCall(PetscMalloc1(num_entries, array_ceed));
66*4d00b080SJeremy L Thompson     for (PetscInt i = 0; i < num_entries; i++) (*array_ceed)[i] = (*array_petsc)[i];
67*4d00b080SJeremy L Thompson     PetscCall(PetscFree(*array_petsc));
68*4d00b080SJeremy L Thompson   }
69*4d00b080SJeremy L Thompson   *array_petsc = NULL;
70*4d00b080SJeremy L Thompson   PetscFunctionReturn(PETSC_SUCCESS);
71*4d00b080SJeremy L Thompson }
72