Actual source code: vecreg.c


  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

 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;

 36:   PetscObjectTypeCompare((PetscObject)vec, method, &match);
 37:   if (match) return 0;

 39:   /* Return if asked for VECSTANDARD and Vec is already VECSEQ on 1 process or VECMPI on more.
 40:      Otherwise, we free the Vec array in the call to destroy below and never reallocate it,
 41:      since the VecType will be the same and VecSetType(v,VECSEQ) will return when called from VecCreate_Standard */
 42:   MPI_Comm_size(PetscObjectComm((PetscObject)vec), &size);
 43:   PetscStrcmp(method, VECSTANDARD, &match);
 44:   if (match) {
 45:     PetscObjectTypeCompare((PetscObject)vec, size > 1 ? VECMPI : VECSEQ, &match);
 46:     if (match) return 0;
 47:   }
 48:   /* same reasons for VECCUDA and VECVIENNACL */
 49: #if defined(PETSC_HAVE_CUDA)
 50:   PetscStrcmp(method, VECCUDA, &match);
 51:   if (match) {
 52:     PetscObjectTypeCompare((PetscObject)vec, size > 1 ? VECMPICUDA : VECSEQCUDA, &match);
 53:     if (match) return 0;
 54:   }
 55: #endif
 56: #if defined(PETSC_HAVE_HIP)
 57:   PetscStrcmp(method, VECHIP, &match);
 58:   if (match) {
 59:     PetscObjectTypeCompare((PetscObject)vec, size > 1 ? VECMPIHIP : VECSEQHIP, &match);
 60:     if (match) return 0;
 61:   }
 62: #endif
 63: #if defined(PETSC_HAVE_VIENNACL)
 64:   PetscStrcmp(method, VECVIENNACL, &match);
 65:   if (match) {
 66:     PetscObjectTypeCompare((PetscObject)vec, size > 1 ? VECMPIVIENNACL : VECSEQVIENNACL, &match);
 67:     if (match) return 0;
 68:   }
 69: #endif
 70: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
 71:   PetscStrcmp(method, VECKOKKOS, &match);
 72:   if (match) {
 73:     PetscObjectTypeCompare((PetscObject)vec, size > 1 ? VECMPIKOKKOS : VECSEQKOKKOS, &match);
 74:     if (match) return 0;
 75:   }
 76: #endif
 77:   PetscFunctionListFind(VecList, method, &r);
 79:   PetscTryTypeMethod(vec, destroy);
 80:   vec->ops->destroy = NULL;
 81:   PetscMemzero(vec->ops, sizeof(struct _VecOps));
 82:   PetscFree(vec->defaultrandtype);
 83:   PetscStrallocpy(PETSCRANDER48, &vec->defaultrandtype);
 84:   if (vec->map->n < 0 && vec->map->N < 0) {
 85:     vec->ops->create = r;
 86:     vec->ops->load   = VecLoad_Default;
 87:   } else {
 88:     (*r)(vec);
 89:   }
 90:   return 0;
 91: }

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

 96:   Not Collective

 98:   Input Parameter:
 99: . vec  - The vector

101:   Output Parameter:
102: . type - The vector type name

104:   Level: intermediate

106: .seealso: `VecSetType()`, `VecCreate()`
107: @*/
108: PetscErrorCode VecGetType(Vec vec, VecType *type)
109: {
112:   VecRegisterAll();
113:   *type = ((PetscObject)vec)->type_name;
114:   return 0;
115: }

117: PetscErrorCode VecGetRootType_Private(Vec vec, VecType *vtype)
118: {
119:   PetscBool iscuda, iship, iskokkos, isvcl;

123:   PetscObjectTypeCompareAny((PetscObject)vec, &iscuda, VECCUDA, VECMPICUDA, VECSEQCUDA, "");
124:   PetscObjectTypeCompareAny((PetscObject)vec, &iship, VECHIP, VECMPIHIP, VECSEQHIP, "");
125:   PetscObjectTypeCompareAny((PetscObject)vec, &iskokkos, VECKOKKOS, VECMPIKOKKOS, VECSEQKOKKOS, "");
126:   PetscObjectTypeCompareAny((PetscObject)vec, &isvcl, VECVIENNACL, VECMPIVIENNACL, VECSEQVIENNACL, "");
127:   if (iscuda) {
128:     *vtype = VECCUDA;
129:   } else if (iship) {
130:     *vtype = VECHIP;
131:   } else if (iskokkos) {
132:     *vtype = VECKOKKOS;
133:   } else if (isvcl) {
134:     *vtype = VECVIENNACL;
135:   } else {
136:     *vtype = VECSTANDARD;
137:   }
138:   return 0;
139: }

141: /*--------------------------------------------------------------------------------------------------------------------*/

143: /*@C
144:   VecRegister -  Adds a new vector component implementation

146:   Not Collective

148:   Input Parameters:
149: + name        - The name of a new user-defined creation routine
150: - create_func - The creation routine itself

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

155:   Sample usage:
156: .vb
157:     VecRegister("my_vec",MyVectorCreate);
158: .ve

160:   Then, your vector type can be chosen with the procedural interface via
161: .vb
162:     VecCreate(MPI_Comm, Vec *);
163:     VecSetType(Vec,"my_vector_name");
164: .ve
165:    or at runtime via the option
166: .vb
167:     -vec_type my_vector_name
168: .ve

170:   Level: advanced

172: .seealso: `VecRegisterAll()`, `VecRegisterDestroy()`
173: @*/
174: PetscErrorCode VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
175: {
176:   VecInitializePackage();
177:   PetscFunctionListAdd(&VecList, sname, function);
178:   return 0;
179: }