Actual source code: vecreg.c

petsc-3.15.0 2021-04-05
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_HIP)
 60:   PetscStrcmp(method,VECHIP,&match);
 61:   if (match) {
 62:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIHIP : VECSEQHIP, &match);
 63:     if (match) return(0);
 64:   }
 65: #endif
 66: #if defined(PETSC_HAVE_VIENNACL)
 67:   PetscStrcmp(method,VECVIENNACL,&match);
 68:   if (match) {
 69:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIVIENNACL : VECSEQVIENNACL, &match);
 70:     if (match) return(0);
 71:   }
 72: #endif
 73: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
 74:   PetscStrcmp(method,VECKOKKOS,&match);
 75:   if (match) {
 76:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIKOKKOS : VECSEQKOKKOS, &match);
 77:     if (match) return(0);
 78:   }
 79: #endif
 80:   PetscFunctionListFind(VecList,method,&r);
 81:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown vector type: %s", method);
 82:   if (vec->ops->destroy) {
 83:     (*vec->ops->destroy)(vec);
 84:     vec->ops->destroy = NULL;
 85:   }
 86:   PetscMemzero(vec->ops,sizeof(struct _VecOps));
 87:   PetscFree(vec->defaultrandtype);
 88:   PetscStrallocpy(PETSCRANDER48,&vec->defaultrandtype);
 89:   if (vec->map->n < 0 && vec->map->N < 0) {
 90:     vec->ops->create = r;
 91:     vec->ops->load   = VecLoad_Default;
 92:   } else {
 93:     (*r)(vec);
 94:   }
 95:   return(0);
 96: }

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

101:   Not Collective

103:   Input Parameter:
104: . vec  - The vector

106:   Output Parameter:
107: . type - The vector type name

109:   Level: intermediate

111: .seealso: VecSetType(), VecCreate()
112: @*/
113: PetscErrorCode VecGetType(Vec vec, VecType *type)
114: {

120:   VecRegisterAll();
121:   *type = ((PetscObject)vec)->type_name;
122:   return(0);
123: }


126: /*--------------------------------------------------------------------------------------------------------------------*/

128: /*@C
129:   VecRegister -  Adds a new vector component implementation

131:   Not Collective

133:   Input Parameters:
134: + name        - The name of a new user-defined creation routine
135: - create_func - The creation routine itself

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

140:   Sample usage:
141: .vb
142:     VecRegister("my_vec",MyVectorCreate);
143: .ve

145:   Then, your vector type can be chosen with the procedural interface via
146: .vb
147:     VecCreate(MPI_Comm, Vec *);
148:     VecSetType(Vec,"my_vector_name");
149: .ve
150:    or at runtime via the option
151: .vb
152:     -vec_type my_vector_name
153: .ve

155:   Level: advanced

157: .seealso: VecRegisterAll(), VecRegisterDestroy()
158: @*/
159: PetscErrorCode VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
160: {

164:   VecInitializePackage();
165:   PetscFunctionListAdd(&VecList,sname,function);
166:   return(0);
167: }