Actual source code: veccreate.c

  1: #include <petsc/private/vecimpl.h>
  2: #include <../src/vec/vec/impls/mpi/pvecimpl.h>

  4: static PetscErrorCode VecCreate_Common_Private(Vec v)
  5: {
  6:   PetscFunctionBegin;
  7:   v->array_gotten = PETSC_FALSE;
  8:   v->petscnative  = PETSC_FALSE;
  9:   v->offloadmask  = PETSC_OFFLOAD_UNALLOCATED;
 10: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
 11:   v->minimum_bytes_pinned_memory = 0;
 12:   v->pinned_memory               = PETSC_FALSE;
 13: #endif
 14: #if defined(PETSC_HAVE_DEVICE)
 15:   v->boundtocpu = PETSC_TRUE;
 16: #endif
 17:   PetscCall(PetscStrallocpy(PETSCRANDER48, &v->defaultrandtype));
 18:   PetscFunctionReturn(PETSC_SUCCESS);
 19: }

 21: /*@
 22:   VecCreate - Creates an empty vector object. The type can then be set with `VecSetType()`,
 23:   or `VecSetFromOptions().`

 25:   Collective

 27:   Input Parameter:
 28: . comm - The communicator for the vector object

 30:   Output Parameter:
 31: . vec - The vector object

 33:   Level: beginner

 35:   Notes:
 36:   If you never  call `VecSetType()` or `VecSetFromOptions()` it will generate an
 37:   error when you try to use the vector.

 39:   PETSc `Vec` always have all zero entries until routines such as `VecSet()` or `VecSetValues()` are used to change the values.
 40:   There is no reason to call `VecZeroEntries()` after creation.

 42: .seealso: [](ch_vectors), `Vec`, `VecSetType()`, `VecSetSizes()`, `VecCreateMPIWithArray()`, `VecCreateMPI()`, `VecDuplicate()`,
 43:           `VecDuplicateVecs()`, `VecCreateGhost()`, `VecCreateSeq()`, `VecPlaceArray()`
 44: @*/
 45: PetscErrorCode VecCreate(MPI_Comm comm, Vec *vec)
 46: {
 47:   Vec v;

 49:   PetscFunctionBegin;
 50:   PetscAssertPointer(vec, 2);
 51:   PetscCall(VecInitializePackage());

 53:   PetscCall(PetscHeaderCreate(v, VEC_CLASSID, "Vec", "Vector", "Vec", comm, VecDestroy, VecView));
 54:   PetscCall(PetscLayoutCreate(comm, &v->map));
 55:   PetscCall(VecCreate_Common_Private(v));
 56:   *vec = v;
 57:   PetscFunctionReturn(PETSC_SUCCESS);
 58: }

 60: /*@
 61:   VecCreateFromOptions - Creates a vector whose type is set from the options database

 63:   Collective

 65:   Input Parameters:
 66: + comm   - The communicator for the vector object
 67: . prefix - [optional] prefix for the options database
 68: . bs     - the block size (commonly 1)
 69: . m      - the local size (or `PETSC_DECIDE`)
 70: - n      - the global size (or `PETSC_DETERMINE`)

 72:   Output Parameter:
 73: . vec - The vector object

 75:   Options Database Keys:
 76: . -vec_type - see `VecType`, for example `seq`, `mpi`, `cuda`, defaults to `mpi`

 78:   Level: beginner

 80: .seealso: [](ch_vectors), `Vec`, `VecSetType()`, `VecSetSizes()`, `VecCreateMPIWithArray()`, `VecCreateMPI()`, `VecDuplicate()`,
 81:           `VecDuplicateVecs()`, `VecCreateGhost()`, `VecCreateSeq()`, `VecPlaceArray()`, `VecCreate()`, `VecType`
 82: @*/
 83: PetscErrorCode VecCreateFromOptions(MPI_Comm comm, const char *prefix, PetscInt bs, PetscInt m, PetscInt n, Vec *vec)
 84: {
 85:   PetscFunctionBegin;
 86:   PetscAssertPointer(vec, 6);
 87:   PetscCall(VecCreate(comm, vec));
 88:   if (prefix) PetscCall(VecSetOptionsPrefix(*vec, prefix));
 89:   PetscCall(VecSetBlockSize(*vec, bs));
 90:   PetscCall(VecSetSizes(*vec, m, n));
 91:   PetscCall(VecSetFromOptions(*vec));
 92:   PetscFunctionReturn(PETSC_SUCCESS);
 93: }

 95: /* Create a vector with the given layout.  The reference count of the input layout will be increased by 1 */
 96: PetscErrorCode VecCreateWithLayout_Private(PetscLayout map, Vec *vec)
 97: {
 98:   Vec v;

100:   PetscFunctionBegin;
101:   PetscAssertPointer(vec, 2);
102:   *vec = NULL;
103:   PetscCall(VecInitializePackage());
104:   PetscCall(PetscHeaderCreate(v, VEC_CLASSID, "Vec", "Vector", "Vec", map->comm, VecDestroy, VecView));
105:   v->map = map;
106:   map->refcnt++;
107:   PetscCall(VecCreate_Common_Private(v));
108:   v->bstash.bs = map->bs;
109:   *vec         = v;
110:   PetscFunctionReturn(PETSC_SUCCESS);
111: }