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 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;

 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) {

 46:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPI : VECSEQ, &match);
 47:     if (match) return 0;
 48:   }
 49:   /* same reasons for VECCUDA and VECVIENNACL */
 50: #if defined(PETSC_HAVE_CUDA)
 51:   PetscStrcmp(method,VECCUDA,&match);
 52:   if (match) {
 53:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPICUDA : VECSEQCUDA, &match);
 54:     if (match) return 0;
 55:   }
 56: #endif
 57: #if defined(PETSC_HAVE_HIP)
 58:   PetscStrcmp(method,VECHIP,&match);
 59:   if (match) {
 60:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIHIP : VECSEQHIP, &match);
 61:     if (match) return 0;
 62:   }
 63: #endif
 64: #if defined(PETSC_HAVE_VIENNACL)
 65:   PetscStrcmp(method,VECVIENNACL,&match);
 66:   if (match) {
 67:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIVIENNACL : VECSEQVIENNACL, &match);
 68:     if (match) return 0;
 69:   }
 70: #endif
 71: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
 72:   PetscStrcmp(method,VECKOKKOS,&match);
 73:   if (match) {
 74:     PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIKOKKOS : VECSEQKOKKOS, &match);
 75:     if (match) return 0;
 76:   }
 77: #endif
 78:   PetscFunctionListFind(VecList,method,&r);
 80:   if (vec->ops->destroy) {
 81:     (*vec->ops->destroy)(vec);
 82:     vec->ops->destroy = NULL;
 83:   }
 84:   PetscMemzero(vec->ops,sizeof(struct _VecOps));
 85:   PetscFree(vec->defaultrandtype);
 86:   PetscStrallocpy(PETSCRANDER48,&vec->defaultrandtype);
 87:   if (vec->map->n < 0 && vec->map->N < 0) {
 88:     vec->ops->create = r;
 89:     vec->ops->load   = VecLoad_Default;
 90:   } else {
 91:     (*r)(vec);
 92:   }
 93:   return 0;
 94: }

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

 99:   Not Collective

101:   Input Parameter:
102: . vec  - The vector

104:   Output Parameter:
105: . type - The vector type name

107:   Level: intermediate

109: .seealso: VecSetType(), VecCreate()
110: @*/
111: PetscErrorCode VecGetType(Vec vec, VecType *type)
112: {
115:   VecRegisterAll();
116:   *type = ((PetscObject)vec)->type_name;
117:   return 0;
118: }

120: PetscErrorCode VecGetRootType_Private(Vec vec, VecType *vtype)
121: {
122:   PetscBool      iscuda, iship, iskokkos, isvcl;

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

138: /*--------------------------------------------------------------------------------------------------------------------*/

140: /*@C
141:   VecRegister -  Adds a new vector component implementation

143:   Not Collective

145:   Input Parameters:
146: + name        - The name of a new user-defined creation routine
147: - create_func - The creation routine itself

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

152:   Sample usage:
153: .vb
154:     VecRegister("my_vec",MyVectorCreate);
155: .ve

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

167:   Level: advanced

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