Actual source code: veccreatememtype.c

  1: #include <petscvec.h>
  2: #include <petsc/private/petscimpl.h>

  4: /*@
  5:   VecCreateSeqWithArrayAndMemType - Creates a sequential array-style vector using a user-provided array with the specified memory type

  7:   Collective

  9:   Input Parameters:
 10: + comm  - the communicator, should be `PETSC_COMM_SELF`
 11: . mtype - the memory type of `array`, which must be the same on all processes in `comm`
 12: . bs    - the block size
 13: . n     - the vector length
 14: - array - memory where the vector elements are to be stored

 16:   Output Parameter:
 17: . V - the vector

 19:   Level: intermediate

 21:   Notes:
 22:   `mtype` determines whether the resulting vector is a standard, CUDA, or HIP vector. Since `PETSC_MEMTYPE_DEVICE` and
 23:   `PETSC_MEMTYPE_CUDA` have the same value, that value creates a CUDA vector when CUDA is configured and otherwise creates
 24:   a HIP vector when HIP is configured.

 26:   `mtype` alone does not identify the Kokkos implementation, and native SYCL vectors are not supported, so this function does
 27:   not create Kokkos or SYCL vectors.

 29:   `array` remains owned by the caller and is not freed when the vector is destroyed via `VecDestroy()`.

 31: .seealso: `VecCreateSeqWithArray()`, `VecCreateMPIWithArrayAndMemType()`, `VecCreateSeqCUDAWithArray()`, `VecCreateSeqHIPWithArray()`, `PetscMemType`
 32: @*/
 33: PetscErrorCode VecCreateSeqWithArrayAndMemType(MPI_Comm comm, PetscMemType mtype, PetscInt bs, PetscInt n, const PetscScalar array[], Vec *V)
 34: {
 35:   PetscFunctionBegin;
 37:   if (mtype == PETSC_MEMTYPE_DEVICE) {
 38:     PetscCheck(PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP), comm, PETSC_ERR_SUP, "Not for PetscMemType %s without CUDA or HIP support", PetscMemTypeToString(mtype));
 39:     if (PetscDefined(HAVE_CUDA)) PetscCall(VecCreateSeqCUDAWithArray(comm, bs, n, array, V));
 40:     else PetscCall(VecCreateSeqHIPWithArray(comm, bs, n, array, V));
 41:   } else if (PetscMemTypeCUDA(mtype)) {
 42:     PetscCheck(PetscDefined(HAVE_CUDA), comm, PETSC_ERR_SUP, "Not for PetscMemType %s without CUDA support", PetscMemTypeToString(mtype));
 43:     PetscCall(VecCreateSeqCUDAWithArray(comm, bs, n, array, V));
 44:   } else if (PetscMemTypeHIP(mtype)) {
 45:     PetscCheck(PetscDefined(HAVE_HIP), comm, PETSC_ERR_SUP, "Not for PetscMemType %s without HIP support", PetscMemTypeToString(mtype));
 46:     PetscCall(VecCreateSeqHIPWithArray(comm, bs, n, array, V));
 47:   } else {
 48:     PetscCheck(PetscMemTypeHost(mtype), comm, PETSC_ERR_SUP, "Not for PetscMemType %s", PetscMemTypeToString(mtype));
 49:     PetscCall(VecCreateSeqWithArray(comm, bs, n, array, V));
 50:   }
 51:   PetscFunctionReturn(PETSC_SUCCESS);
 52: }

 54: /*@
 55:   VecCreateMPIWithArrayAndMemType - Creates a parallel array-style vector using a user-provided array with the specified memory type

 57:   Collective

 59:   Input Parameters:
 60: + comm  - the MPI communicator to use
 61: . mtype - the memory type of `array`, which must be the same on all processes in `comm`
 62: . bs    - block size, same meaning as `VecSetBlockSize()`
 63: . n     - local vector length, cannot be `PETSC_DECIDE`
 64: . N     - global vector length (or `PETSC_DETERMINE` to have calculated)
 65: - array - memory where the vector elements are to be stored

 67:   Output Parameter:
 68: . V - the vector

 70:   Level: intermediate

 72:   Notes:
 73:   `mtype` determines whether the resulting vector is a standard, CUDA, or HIP vector. Since `PETSC_MEMTYPE_DEVICE` and
 74:   `PETSC_MEMTYPE_CUDA` have the same value, that value creates a CUDA vector when CUDA is configured and otherwise creates
 75:   a HIP vector when HIP is configured.

 77:   `mtype` alone does not identify the Kokkos implementation, and native SYCL vectors are not supported, so this function does
 78:   not create Kokkos or SYCL vectors.

 80:   `array` remains owned by the caller and is not freed when the vector is destroyed via `VecDestroy()`.

 82: .seealso: `VecCreateMPIWithArray()`, `VecCreateSeqWithArrayAndMemType()`, `VecCreateMPICUDAWithArray()`, `VecCreateMPIHIPWithArray()`, `PetscMemType`
 83: @*/
 84: PetscErrorCode VecCreateMPIWithArrayAndMemType(MPI_Comm comm, PetscMemType mtype, PetscInt bs, PetscInt n, PetscInt N, const PetscScalar array[], Vec *V)
 85: {
 86:   PetscFunctionBegin;
 88:   if (mtype == PETSC_MEMTYPE_DEVICE) {
 89:     PetscCheck(PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP), comm, PETSC_ERR_SUP, "Not for PetscMemType %s without CUDA or HIP support", PetscMemTypeToString(mtype));
 90:     if (PetscDefined(HAVE_CUDA)) PetscCall(VecCreateMPICUDAWithArray(comm, bs, n, N, array, V));
 91:     else PetscCall(VecCreateMPIHIPWithArray(comm, bs, n, N, array, V));
 92:   } else if (PetscMemTypeCUDA(mtype)) {
 93:     PetscCheck(PetscDefined(HAVE_CUDA), comm, PETSC_ERR_SUP, "Not for PetscMemType %s without CUDA support", PetscMemTypeToString(mtype));
 94:     PetscCall(VecCreateMPICUDAWithArray(comm, bs, n, N, array, V));
 95:   } else if (PetscMemTypeHIP(mtype)) {
 96:     PetscCheck(PetscDefined(HAVE_HIP), comm, PETSC_ERR_SUP, "Not for PetscMemType %s without HIP support", PetscMemTypeToString(mtype));
 97:     PetscCall(VecCreateMPIHIPWithArray(comm, bs, n, N, array, V));
 98:   } else {
 99:     PetscCheck(PetscMemTypeHost(mtype), comm, PETSC_ERR_SUP, "Not for PetscMemType %s", PetscMemTypeToString(mtype));
100:     PetscCall(VecCreateMPIWithArray(comm, bs, n, N, array, V));
101:   }
102:   PetscFunctionReturn(PETSC_SUCCESS);
103: }