Actual source code: vecreg.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: #include <petsc/private/vecimpl.h>

  4: PetscFunctionList VecList              = NULL;
  5: PetscBool         VecRegisterAllCalled = PETSC_FALSE;

  7: /*@C
  8:   VecSetType - Builds a vector, for a particular vector implementation.

 10:   Collective on Vec

 12:   Input Parameters:
 13: + vec    - The vector object
 14: - method - The name of the vector type

 16:   Options Database Key:
 17: . -vec_type <type> - Sets the vector type; use -help for a list
 18:                      of available types

 20:   Notes:
 21:   See "petsc/include/petscvec.h" for available vector types (for instance, VECSEQ, VECMPI, or VECSHARED).

 23:   Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the same type as an existing vector.

 25:   Level: intermediate

 27: .seealso: VecGetType(), VecCreate()
 28: @*/
 29: PetscErrorCode VecSetType(Vec vec, VecType method)
 30: {
 31:   PetscErrorCode (*r)(Vec);
 32:   PetscBool      match;
 33:   PetscMPIInt    size;

 38:   PetscObjectTypeCompare((PetscObject) vec, method, &match);
 39:   if (match) return(0);

 41:   /* Return if asked for VECSTANDARD and Vec is already VECSEQ on 1 process or VECMPI on more.
 42:      Otherwise, we free the Vec array in the call to destroy below and never reallocate it,
 43:      since the VecType will be the same and VecSetType(v,VECSEQ) will return when called from VecCreate_Standard */
 44:   MPI_Comm_size(PetscObjectComm((PetscObject)vec),&size);
 45:   PetscStrcmp(method,VECSTANDARD,&match);
 46:   if (match) {

 48:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPI : VECSEQ, &match);
 49:     if (match) return(0);
 50:   }
 51:   /* same reasons for VECCUDA and VECVIENNACL */
 52: #if defined(PETSC_HAVE_CUDA)
 53:   PetscStrcmp(method,VECCUDA,&match);
 54:   if (match) {
 55:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPICUDA : VECSEQCUDA, &match);
 56:     if (match) return(0);
 57:   }
 58: #endif
 59: #if defined(PETSC_HAVE_VIENNACL)
 60:   PetscStrcmp(method,VECVIENNACL,&match);
 61:   if (match) {
 62:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIVIENNACL : VECSEQVIENNACL, &match);
 63:     if (match) return(0);
 64:   }
 65: #endif
 66: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
 67:   PetscStrcmp(method,VECKOKKOS,&match);
 68:   if (match) {
 69:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIKOKKOS : VECSEQKOKKOS, &match);
 70:     if (match) return(0);
 71:   }
 72: #endif
 73:   PetscFunctionListFind(VecList,method,&r);
 74:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown vector type: %s", method);
 75:   if (vec->ops->destroy) {
 76:     (*vec->ops->destroy)(vec);
 77:     vec->ops->destroy = NULL;
 78:   }
 79:   PetscMemzero(vec->ops,sizeof(struct _VecOps));
 80:   if (vec->map->n < 0 && vec->map->N < 0) {
 81:     vec->ops->create = r;
 82:     vec->ops->load   = VecLoad_Default;
 83:   } else {
 84:     (*r)(vec);
 85:   }
 86:   return(0);
 87: }

 89: /*@C
 90:   VecGetType - Gets the vector type name (as a string) from the Vec.

 92:   Not Collective

 94:   Input Parameter:
 95: . vec  - The vector

 97:   Output Parameter:
 98: . type - The vector type name

100:   Level: intermediate

102: .seealso: VecSetType(), VecCreate()
103: @*/
104: PetscErrorCode VecGetType(Vec vec, VecType *type)
105: {

111:   VecRegisterAll();
112:   *type = ((PetscObject)vec)->type_name;
113:   return(0);
114: }


117: /*--------------------------------------------------------------------------------------------------------------------*/

119: /*@C
120:   VecRegister -  Adds a new vector component implementation

122:   Not Collective

124:   Input Parameters:
125: + name        - The name of a new user-defined creation routine
126: - create_func - The creation routine itself

128:   Notes:
129:   VecRegister() may be called multiple times to add several user-defined vectors

131:   Sample usage:
132: .vb
133:     VecRegister("my_vec",MyVectorCreate);
134: .ve

136:   Then, your vector type can be chosen with the procedural interface via
137: .vb
138:     VecCreate(MPI_Comm, Vec *);
139:     VecSetType(Vec,"my_vector_name");
140: .ve
141:    or at runtime via the option
142: .vb
143:     -vec_type my_vector_name
144: .ve

146:   Level: advanced

148: .seealso: VecRegisterAll(), VecRegisterDestroy()
149: @*/
150: PetscErrorCode VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
151: {

155:   VecInitializePackage();
156:   PetscFunctionListAdd(&VecList,sname,function);
157:   return(0);
158: }