Date: 2025-03-29T05:58:54+00:00

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: .seealso: [](ch_vectors), `Vec`, `VecSetType()`, `VecSetSizes()`, `VecCreateMPIWithArray()`, `VecCreateMPI()`, `VecDuplicate()`,
     40:           `VecDuplicateVecs()`, `VecCreateGhost()`, `VecCreateSeq()`, `VecPlaceArray()`
     41: @*/
     42: PetscErrorCode VecCreate(MPI_Comm comm, Vec *vec)
     43: {
     44:   Vec v;

     46:   PetscFunctionBegin;
     47:   PetscAssertPointer(vec, 2);
     48:   PetscCall(VecInitializePackage());

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

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

     60:   Collective

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

     69:   Output Parameter:
     70: . vec - The vector object

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

     75:   Level: beginner

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

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

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