Actual source code: vecreg.c

petsc-3.5.4 2015-05-23
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:   if (!VecRegisterAllCalled) {
 84:     VecRegisterAll();
 85:   }
 86:   *type = ((PetscObject)vec)->type_name;
 87:   return(0);
 88: }


 91: /*--------------------------------------------------------------------------------------------------------------------*/

 95: /*@C
 96:   VecRegister -  Adds a new vector component implementation

 98:   Not Collective

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

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

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

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

122:   Level: advanced

124: .keywords: Vec, register

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

133:   PetscFunctionListAdd(&VecList,sname,function);
134:   return(0);
135: }