Actual source code: vecreg.c
petsc-3.11.4 2019-09-28
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: .keywords: vector, set, type
28: .seealso: VecGetType(), VecCreate()
29: @*/
30: PetscErrorCode VecSetType(Vec vec, VecType method)
31: {
32: PetscErrorCode (*r)(Vec);
33: PetscBool match;
38: PetscObjectTypeCompare((PetscObject) vec, method, &match);
39: if (match) return(0);
41: PetscFunctionListFind(VecList,method,&r);
42: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown vector type: %s", method);
43: if (vec->ops->destroy) {
44: (*vec->ops->destroy)(vec);
45: vec->ops->destroy = NULL;
46: }
47: if (vec->map->n < 0 && vec->map->N < 0) {
48: vec->ops->create = r;
49: vec->ops->load = VecLoad_Default;
50: } else {
51: (*r)(vec);
52: }
53: return(0);
54: }
56: /*@C
57: VecGetType - Gets the vector type name (as a string) from the Vec.
59: Not Collective
61: Input Parameter:
62: . vec - The vector
64: Output Parameter:
65: . type - The vector type name
67: Level: intermediate
69: .keywords: vector, get, type, name
70: .seealso: VecSetType(), VecCreate()
71: @*/
72: PetscErrorCode VecGetType(Vec vec, VecType *type)
73: {
79: VecRegisterAll();
80: *type = ((PetscObject)vec)->type_name;
81: return(0);
82: }
85: /*--------------------------------------------------------------------------------------------------------------------*/
87: /*@C
88: VecRegister - Adds a new vector component implementation
90: Not Collective
92: Input Parameters:
93: + name - The name of a new user-defined creation routine
94: - create_func - The creation routine itself
96: Notes:
97: VecRegister() may be called multiple times to add several user-defined vectors
99: Sample usage:
100: .vb
101: VecRegister("my_vec",MyVectorCreate);
102: .ve
104: Then, your vector type can be chosen with the procedural interface via
105: .vb
106: VecCreate(MPI_Comm, Vec *);
107: VecSetType(Vec,"my_vector_name");
108: .ve
109: or at runtime via the option
110: .vb
111: -vec_type my_vector_name
112: .ve
114: Level: advanced
116: .keywords: Vec, register
118: .seealso: VecRegisterAll(), VecRegisterDestroy()
119: @*/
120: PetscErrorCode VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
121: {
125: VecInitializePackage();
126: PetscFunctionListAdd(&VecList,sname,function);
127: return(0);
128: }