1 // Copyright (c) 2017-2026, 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 <petscsys.h>
11
12 // Translate PetscMemType to CeedMemType
MemTypeP2C(PetscMemType mem_type)13 static inline CeedMemType MemTypeP2C(PetscMemType mem_type) { return PetscMemTypeDevice(mem_type) ? CEED_MEM_DEVICE : CEED_MEM_HOST; }
14
15 /**
16 @brief Translate array of `PetscInt` to `CeedInt`.
17 If the types differ, `array_petsc` is freed with `PetscFree()` and `array_ceed` is allocated with `PetscMalloc1()`.
18 Caller is responsible for freeing `array_ceed` with `PetscFree()`.
19
20 Not collective across MPI processes.
21
22 @param[in] num_entries Number of array entries
23 @param[in,out] array_petsc Array of `PetscInt`
24 @param[out] array_ceed Array of `CeedInt`
25
26 @return An error code: 0 - success, otherwise - failure
27 **/
IntArrayCeedToPetsc(PetscInt num_entries,CeedInt ** array_ceed,PetscInt ** array_petsc)28 static inline PetscErrorCode IntArrayCeedToPetsc(PetscInt num_entries, CeedInt **array_ceed, PetscInt **array_petsc) {
29 const CeedInt int_c = 0;
30 const PetscInt int_p = 0;
31
32 PetscFunctionBeginUser;
33 if (sizeof(int_c) == sizeof(int_p)) {
34 *array_petsc = (PetscInt *)*array_ceed;
35 } else {
36 *array_petsc = malloc(num_entries * sizeof(PetscInt));
37 for (PetscInt i = 0; i < num_entries; i++) (*array_petsc)[i] = (*array_ceed)[i];
38 free(*array_ceed);
39 }
40 *array_ceed = NULL;
41 PetscFunctionReturn(PETSC_SUCCESS);
42 }
43
44 /**
45 @brief Translate array of `PetscInt` to `CeedInt`.
46 If the types differ, `array_petsc` is freed with `PetscFree()` and `array_ceed` is allocated with `PetscMalloc1()`.
47 Caller is responsible for freeing `array_ceed` with `PetscFree()`.
48
49 Not collective across MPI processes.
50
51 @param[in] num_entries Number of array entries
52 @param[in,out] array_petsc Array of `PetscInt`
53 @param[out] array_ceed Array of `CeedInt`
54
55 @return An error code: 0 - success, otherwise - failure
56 **/
IntArrayPetscToCeed(PetscInt num_entries,PetscInt ** array_petsc,CeedInt ** array_ceed)57 static inline PetscErrorCode IntArrayPetscToCeed(PetscInt num_entries, PetscInt **array_petsc, CeedInt **array_ceed) {
58 const CeedInt int_c = 0;
59 const PetscInt int_p = 0;
60
61 PetscFunctionBeginUser;
62 if (sizeof(int_c) == sizeof(int_p)) {
63 *array_ceed = (CeedInt *)*array_petsc;
64 } else {
65 PetscCall(PetscMalloc1(num_entries, array_ceed));
66 for (PetscInt i = 0; i < num_entries; i++) (*array_ceed)[i] = (*array_petsc)[i];
67 PetscCall(PetscFree(*array_petsc));
68 }
69 *array_petsc = NULL;
70 PetscFunctionReturn(PETSC_SUCCESS);
71 }
72