Actual source code: vecreg.c
petsc-3.14.0 2020-09-29
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;
38: PetscObjectTypeCompare((PetscObject) vec, method, &match);
39: if (match) return(0);
41: /* Return if asked for VECSTANDARD and Vec is already VECSEQ on 1 process or VECMPI on more.
42: Otherwise, we free the Vec array in the call to destroy below and never reallocate it,
43: since the VecType will be the same and VecSetType(v,VECSEQ) will return when called from VecCreate_Standard */
44: MPI_Comm_size(PetscObjectComm((PetscObject)vec),&size);
45: PetscStrcmp(method,VECSTANDARD,&match);
46: if (match) {
48: PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPI : VECSEQ, &match);
49: if (match) return(0);
50: }
51: /* same reasons for VECCUDA and VECVIENNACL */
52: #if defined(PETSC_HAVE_CUDA)
53: PetscStrcmp(method,VECCUDA,&match);
54: if (match) {
55: PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPICUDA : VECSEQCUDA, &match);
56: if (match) return(0);
57: }
58: #endif
59: #if defined(PETSC_HAVE_VIENNACL)
60: PetscStrcmp(method,VECVIENNACL,&match);
61: if (match) {
62: PetscObjectTypeCompare((PetscObject) vec, size > 1 ? VECMPIVIENNACL : VECSEQVIENNACL, &match);
63: if (match) return(0);
64: }
65: #endif
66: PetscFunctionListFind(VecList,method,&r);
67: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown vector type: %s", method);
68: if (vec->ops->destroy) {
69: (*vec->ops->destroy)(vec);
70: vec->ops->destroy = NULL;
71: }
72: if (vec->map->n < 0 && vec->map->N < 0) {
73: vec->ops->create = r;
74: vec->ops->load = VecLoad_Default;
75: } else {
76: (*r)(vec);
77: }
78: return(0);
79: }
81: /*@C
82: VecGetType - Gets the vector type name (as a string) from the Vec.
84: Not Collective
86: Input Parameter:
87: . vec - The vector
89: Output Parameter:
90: . type - The vector type name
92: Level: intermediate
94: .seealso: VecSetType(), VecCreate()
95: @*/
96: PetscErrorCode VecGetType(Vec vec, VecType *type)
97: {
103: VecRegisterAll();
104: *type = ((PetscObject)vec)->type_name;
105: return(0);
106: }
109: /*--------------------------------------------------------------------------------------------------------------------*/
111: /*@C
112: VecRegister - Adds a new vector component implementation
114: Not Collective
116: Input Parameters:
117: + name - The name of a new user-defined creation routine
118: - create_func - The creation routine itself
120: Notes:
121: VecRegister() may be called multiple times to add several user-defined vectors
123: Sample usage:
124: .vb
125: VecRegister("my_vec",MyVectorCreate);
126: .ve
128: Then, your vector type can be chosen with the procedural interface via
129: .vb
130: VecCreate(MPI_Comm, Vec *);
131: VecSetType(Vec,"my_vector_name");
132: .ve
133: or at runtime via the option
134: .vb
135: -vec_type my_vector_name
136: .ve
138: Level: advanced
140: .seealso: VecRegisterAll(), VecRegisterDestroy()
141: @*/
142: PetscErrorCode VecRegister(const char sname[], PetscErrorCode (*function)(Vec))
143: {
147: VecInitializePackage();
148: PetscFunctionListAdd(&VecList,sname,function);
149: return(0);
150: }