xref: /petsc/src/vec/vec/interface/veccreate.c (revision a623e290c7eaa252b385564179837fe27521fbac)
1 #include <petsc/private/vecimpl.h> /*I  "petscvec.h"   I*/
2 #include <../src/vec/vec/impls/mpi/pvecimpl.h>
3 
VecCreate_Common_Private(Vec v)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 }
20 
21 /*@
22   VecCreate - Creates an empty vector object. The type can then be set with `VecSetType()`,
23   or `VecSetFromOptions().`
24 
25   Collective
26 
27   Input Parameter:
28 . comm - The communicator for the vector object
29 
30   Output Parameter:
31 . vec - The vector object
32 
33   Level: beginner
34 
35   Notes:
36   If you never  call `VecSetType()` or `VecSetFromOptions()` it will generate an
37   error when you try to use the vector.
38 
39 .seealso: [](ch_vectors), `Vec`, `VecSetType()`, `VecSetSizes()`, `VecCreateMPIWithArray()`, `VecCreateMPI()`, `VecDuplicate()`,
40           `VecDuplicateVecs()`, `VecCreateGhost()`, `VecCreateSeq()`, `VecPlaceArray()`
41 @*/
VecCreate(MPI_Comm comm,Vec * vec)42 PetscErrorCode VecCreate(MPI_Comm comm, Vec *vec)
43 {
44   Vec v;
45 
46   PetscFunctionBegin;
47   PetscAssertPointer(vec, 2);
48   PetscCall(VecInitializePackage());
49 
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 }
56 
57 /*@
58   VecCreateFromOptions - Creates a vector whose type is set from the options database
59 
60   Collective
61 
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`)
68 
69   Output Parameter:
70 . vec - The vector object
71 
72   Options Database Keys:
73 . -vec_type - see `VecType`, for example `seq`, `mpi`, `cuda`, defaults to `mpi`
74 
75   Level: beginner
76 
77 .seealso: [](ch_vectors), `Vec`, `VecSetType()`, `VecSetSizes()`, `VecCreateMPIWithArray()`, `VecCreateMPI()`, `VecDuplicate()`,
78           `VecDuplicateVecs()`, `VecCreateGhost()`, `VecCreateSeq()`, `VecPlaceArray()`, `VecCreate()`, `VecType`
79 @*/
VecCreateFromOptions(MPI_Comm comm,const char * prefix,PetscInt bs,PetscInt m,PetscInt n,Vec * vec)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 }
91 
92 /* Create a vector with the given layout.  The reference count of the input layout will be increased by 1 */
VecCreateWithLayout_Private(PetscLayout map,Vec * vec)93 PetscErrorCode VecCreateWithLayout_Private(PetscLayout map, Vec *vec)
94 {
95   Vec v;
96 
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 }
109