Actual source code: vecreg.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: #include <petsc/private/vecimpl.h>    /*I "petscvec.h"  I*/

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

  9: /*@C
 10:   VecSetType - Builds a vector, for a particular vector implementation.

 12:   Collective on Vec

 14:   Input Parameters:
 15: + vec    - The vector object
 16: - method - The name of the vector type

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

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

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

 27:   Level: intermediate

 29: .keywords: vector, set, type
 30: .seealso: VecGetType(), VecCreate()
 31: @*/
 32: PetscErrorCode  VecSetType(Vec vec, VecType method)
 33: {
 34:   PetscErrorCode (*r)(Vec);
 35:   PetscBool      match;

 40:   PetscObjectTypeCompare((PetscObject) vec, method, &match);
 41:   if (match) return(0);

 43:   PetscFunctionListFind(VecList,method,&r);
 44:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown vector type: %s", method);
 45:   if (vec->ops->destroy) {
 46:     (*vec->ops->destroy)(vec);
 47:     vec->ops->destroy = NULL;
 48:   }
 49:   if (vec->map->n < 0 && vec->map->N < 0) {
 50:     vec->ops->create = r;
 51:     vec->ops->load   = VecLoad_Default;
 52:   } else {
 53:     (*r)(vec);
 54:   }
 55:   return(0);
 56: }

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

 63:   Not Collective

 65:   Input Parameter:
 66: . vec  - The vector

 68:   Output Parameter:
 69: . type - The vector type name

 71:   Level: intermediate

 73: .keywords: vector, get, type, name
 74: .seealso: VecSetType(), VecCreate()
 75: @*/
 76: PetscErrorCode  VecGetType(Vec vec, VecType *type)
 77: {

 83:   VecRegisterAll();
 84:   *type = ((PetscObject)vec)->type_name;
 85:   return(0);
 86: }


 89: /*--------------------------------------------------------------------------------------------------------------------*/

 93: /*@C
 94:   VecRegister -  Adds a new vector component implementation

 96:   Not Collective

 98:   Input Parameters:
 99: + name        - The name of a new user-defined creation routine
100: - create_func - The creation routine itself

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

105:   Sample usage:
106: .vb
107:     VecRegister("my_vec",MyVectorCreate);
108: .ve

110:   Then, your vector type can be chosen with the procedural interface via
111: .vb
112:     VecCreate(MPI_Comm, Vec *);
113:     VecSetType(Vec,"my_vector_name");
114: .ve
115:    or at runtime via the option
116: .vb
117:     -vec_type my_vector_name
118: .ve

120:   Level: advanced

122: .keywords: Vec, register

124: .seealso: VecRegisterAll(), VecRegisterDestroy()
125: @*/
126: PetscErrorCode  VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
127: {

131:   PetscFunctionListAdd(&VecList,sname,function);
132:   return(0);
133: }