Actual source code: vecreg.c
petsc-master 2019-12-10
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;
37: PetscObjectTypeCompare((PetscObject) vec, method, &match);
38: if (match) return(0);
40: PetscFunctionListFind(VecList,method,&r);
41: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown vector type: %s", method);
42: if (vec->ops->destroy) {
43: (*vec->ops->destroy)(vec);
44: vec->ops->destroy = NULL;
45: }
46: if (vec->map->n < 0 && vec->map->N < 0) {
47: vec->ops->create = r;
48: vec->ops->load = VecLoad_Default;
49: } else {
50: (*r)(vec);
51: }
52: return(0);
53: }
55: /*@C
56: VecGetType - Gets the vector type name (as a string) from the Vec.
58: Not Collective
60: Input Parameter:
61: . vec - The vector
63: Output Parameter:
64: . type - The vector type name
66: Level: intermediate
68: .seealso: VecSetType(), VecCreate()
69: @*/
70: PetscErrorCode VecGetType(Vec vec, VecType *type)
71: {
77: VecRegisterAll();
78: *type = ((PetscObject)vec)->type_name;
79: return(0);
80: }
83: /*--------------------------------------------------------------------------------------------------------------------*/
85: /*@C
86: VecRegister - Adds a new vector component implementation
88: Not Collective
90: Input Parameters:
91: + name - The name of a new user-defined creation routine
92: - create_func - The creation routine itself
94: Notes:
95: VecRegister() may be called multiple times to add several user-defined vectors
97: Sample usage:
98: .vb
99: VecRegister("my_vec",MyVectorCreate);
100: .ve
102: Then, your vector type can be chosen with the procedural interface via
103: .vb
104: VecCreate(MPI_Comm, Vec *);
105: VecSetType(Vec,"my_vector_name");
106: .ve
107: or at runtime via the option
108: .vb
109: -vec_type my_vector_name
110: .ve
112: Level: advanced
114: .seealso: VecRegisterAll(), VecRegisterDestroy()
115: @*/
116: PetscErrorCode VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
117: {
121: VecInitializePackage();
122: PetscFunctionListAdd(&VecList,sname,function);
123: return(0);
124: }