source: issm/trunk/src/c/toolkits/objects/Vector.h@ 20500

Last change on this file since 20500 was 20500, checked in by Mathieu Morlighem, 9 years ago

merged trunk-jpl and trunk for revision 20497

File size: 7.1 KB
Line 
1/*!\file: Vector.h
2 * \brief wrapper to vector objects. The goal is to control which API (PETSc,Scalpack, Plapack?)
3 * implements our underlying vector format.
4 */
5
6#ifndef _VECTOR_H_
7#define _VECTOR_H_
8
9/*Headers:*/
10/*{{{*/
11#ifdef HAVE_CONFIG_H
12 #include <config.h>
13#else
14#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
15#endif
16#include <cstring>
17#include "../../shared/Enum/Enum.h"
18#include "../petsc/petscincludes.h"
19#include "../issm/issmtoolkit.h"
20/*}}}*/
21
22enum vectortype { PetscVecType, IssmVecType };
23
24template <class doubletype>
25class Vector{
26
27 public:
28
29 int type;
30 #ifdef _HAVE_PETSC_
31 PetscVec* pvector;
32 #endif
33 IssmVec<doubletype>* ivector;
34
35 /*Vector constructors, destructors */
36 Vector(){ /*{{{*/
37
38 InitCheckAndSetType();
39 }
40 /*}}}*/
41 Vector(int M,bool fromlocalsize=false){ /*{{{*/
42
43 InitCheckAndSetType();
44
45 if(type==PetscVecType){
46 #ifdef _HAVE_PETSC_
47 this->pvector=new PetscVec(M,fromlocalsize);
48 #endif
49 }
50 else this->ivector=new IssmVec<doubletype>(M,fromlocalsize);
51
52 }
53 /*}}}*/
54 Vector(int m,int M){ /*{{{*/
55
56 InitCheckAndSetType();
57
58 if(type==PetscVecType){
59 #ifdef _HAVE_PETSC_
60 this->pvector=new PetscVec(m,M);
61 #endif
62 }
63 else this->ivector=new IssmVec<doubletype>(m,M);
64 }
65 /*}}}*/
66 Vector(doubletype* serial_vec,int M){ /*{{{*/
67
68 InitCheckAndSetType();
69
70 if(type==PetscVecType){
71 #ifdef _HAVE_PETSC_
72 this->pvector=new PetscVec(serial_vec,M);
73 #endif
74 }
75 else this->ivector=new IssmVec<doubletype>(serial_vec,M);
76 }
77 /*}}}*/
78 ~Vector(){ /*{{{*/
79
80 if(type==PetscVecType){
81 #ifdef _HAVE_PETSC_
82 delete this->pvector;
83 #endif
84 }
85 else delete this->ivector;
86 }
87 /*}}}*/
88 #ifdef _HAVE_PETSC_
89 Vector(Vec petsc_vector){ /*{{{*/
90
91 this->type=PetscVecType;
92 this->ivector=NULL;
93 this->pvector=new PetscVec(petsc_vector);
94
95 }
96 /*}}}*/
97 #endif
98 void InitCheckAndSetType(void){ /*{{{*/
99
100 char* toolkittype=NULL;
101
102 #ifdef _HAVE_PETSC_
103 pvector=NULL;
104 #endif
105 ivector=NULL;
106
107 /*retrieve toolkittype: */
108 toolkittype=ToolkitOptions::GetToolkitType();
109
110 /*set vector type: */
111 if (strcmp(toolkittype,"petsc")==0){
112 #ifdef _HAVE_PETSC_
113 type=PetscVecType;
114 #else
115 _error_("cannot create petsc vector without PETSC compiled!");
116 #endif
117 }
118 else if(strcmp(toolkittype,"issm")==0){
119 /*let this choice stand:*/
120 type=IssmVecType;
121 }
122 else _error_("unknow toolkit type ");
123
124 /*Free ressources: */
125 xDelete<char>(toolkittype);
126 }
127 /*}}}*/
128
129 /*Vector specific routines*/
130 void Echo(void){_assert_(this);/*{{{*/
131
132 if(type==PetscVecType){
133 #ifdef _HAVE_PETSC_
134 this->pvector->Echo();
135 #endif
136 }
137 else this->ivector->Echo();
138
139 }
140 /*}}}*/
141 void Assemble(void){_assert_(this);/*{{{*/
142
143 if(type==PetscVecType){
144 #ifdef _HAVE_PETSC_
145 this->pvector->Assemble();
146 #endif
147 }
148 else this->ivector->Assemble();
149
150 }
151 /*}}}*/
152 void SetValues(int ssize, int* list, doubletype* values, InsMode mode){ _assert_(this);/*{{{*/
153 if(type==PetscVecType){
154 #ifdef _HAVE_PETSC_
155 this->pvector->SetValues(ssize,list,values,mode);
156 #endif
157 }
158 else this->ivector->SetValues(ssize,list,values,mode);
159
160 }
161 /*}}}*/
162 void SetValue(int dof, doubletype value, InsMode mode){_assert_(this);/*{{{*/
163
164 if(type==PetscVecType){
165 #ifdef _HAVE_PETSC_
166 this->pvector->SetValue(dof,value,mode);
167 #endif
168 }
169 else this->ivector->SetValue(dof,value,mode);
170
171 }
172 /*}}}*/
173 void GetValue(doubletype* pvalue,int dof){_assert_(this);/*{{{*/
174
175 if(type==PetscVecType){
176 #ifdef _HAVE_PETSC_
177 this->pvector->GetValue(pvalue,dof);
178 #endif
179 }
180 else this->ivector->GetValue(pvalue,dof);
181
182 }
183 /*}}}*/
184 void GetSize(int* pM){_assert_(this);/*{{{*/
185
186 if(type==PetscVecType){
187 #ifdef _HAVE_PETSC_
188 this->pvector->GetSize(pM);
189 #endif
190 }
191 else this->ivector->GetSize(pM);
192
193 }
194 /*}}}*/
195 bool IsEmpty(void){/*{{{*/
196 int M;
197
198 _assert_(this);
199 this->GetSize(&M);
200
201 if(M==0)
202 return true;
203 else
204 return false;
205 }
206 /*}}}*/
207 void GetLocalSize(int* pM){_assert_(this);/*{{{*/
208
209 if(type==PetscVecType){
210 #ifdef _HAVE_PETSC_
211 this->pvector->GetLocalSize(pM);
212 #endif
213 }
214 else this->ivector->GetLocalSize(pM);
215
216 }
217 /*}}}*/
218 Vector<doubletype>* Duplicate(void){_assert_(this);/*{{{*/
219
220 Vector<doubletype>* output=NULL;
221
222 output=new Vector<doubletype>();
223
224 if(type==PetscVecType){
225 #ifdef _HAVE_PETSC_
226 output->pvector=this->pvector->Duplicate();
227 #endif
228 }
229 else output->ivector=this->ivector->Duplicate();
230
231 return output;
232
233 }
234 /*}}}*/
235 void Set(doubletype value){_assert_(this);/*{{{*/
236
237 if(type==PetscVecType){
238 #ifdef _HAVE_PETSC_
239 this->pvector->Set(value);
240 #endif
241 }
242 else this->ivector->Set(value);
243
244 }
245 /*}}}*/
246 void AXPY(Vector* X, doubletype a){_assert_(this);/*{{{*/
247
248 if(type==PetscVecType){
249 #ifdef _HAVE_PETSC_
250 this->pvector->AXPY(X->pvector,a);
251 #endif
252 }
253 else this->ivector->AXPY(X->ivector,a);
254
255 }
256 /*}}}*/
257 void AYPX(Vector* X, doubletype a){_assert_(this);/*{{{*/
258
259 if(type==PetscVecType){
260 #ifdef _HAVE_PETSC_
261 this->pvector->AYPX(X->pvector,a);
262 #endif
263 }
264 else this->ivector->AYPX(X->ivector,a);
265 }
266 /*}}}*/
267 doubletype* ToMPISerial(void){/*{{{*/
268
269 doubletype* vec_serial=NULL;
270
271 _assert_(this);
272 if(type==PetscVecType){
273 #ifdef _HAVE_PETSC_
274 vec_serial=this->pvector->ToMPISerial();
275 #endif
276 }
277 else vec_serial=this->ivector->ToMPISerial();
278
279 return vec_serial;
280
281 }
282 /*}}}*/
283 void Shift(doubletype shift){_assert_(this);/*{{{*/
284
285 if(type==PetscVecType){
286 #ifdef _HAVE_PETSC_
287 this->pvector->Shift(shift);
288 #endif
289 }
290 else this->ivector->Shift(shift);
291 }
292 /*}}}*/
293 void Copy(Vector* to){_assert_(this);/*{{{*/
294
295 if(type==PetscVecType){
296 #ifdef _HAVE_PETSC_
297 this->pvector->Copy(to->pvector);
298 #endif
299 }
300 else this->ivector->Copy(to->ivector);
301 }
302 /*}}}*/
303 doubletype Max(void){_assert_(this);/*{{{*/
304
305 doubletype max=0;
306
307 if(type==PetscVecType){
308 #ifdef _HAVE_PETSC_
309 max=this->pvector->Max();
310 #endif
311 }
312 else _error_("operation not supported yet");
313 return max;
314 }
315 /*}}}*/
316 doubletype Norm(NormMode norm_type){_assert_(this);/*{{{*/
317
318 doubletype norm=0;
319
320 if(type==PetscVecType){
321 #ifdef _HAVE_PETSC_
322 norm=this->pvector->Norm(norm_type);
323 #endif
324 }
325 else norm=this->ivector->Norm(norm_type);
326 return norm;
327 }
328 /*}}}*/
329 void Scale(doubletype scale_factor){_assert_(this);/*{{{*/
330
331 if(type==PetscVecType){
332 #ifdef _HAVE_PETSC_
333 this->pvector->Scale(scale_factor);
334 #endif
335 }
336 else this->ivector->Scale(scale_factor);
337 }
338 /*}}}*/
339 doubletype Dot(Vector* vector){_assert_(this);/*{{{*/
340
341 doubletype dot;
342
343 if(type==PetscVecType){
344 #ifdef _HAVE_PETSC_
345 dot=this->pvector->Dot(vector->pvector);
346 #endif
347 }
348 else dot=this->ivector->Dot(vector->ivector);
349 return dot;
350 }
351 /*}}}*/
352 void PointwiseDivide(Vector* x,Vector* y){_assert_(this);/*{{{*/
353
354 if(type==PetscVecType){
355 #ifdef _HAVE_PETSC_
356 this->pvector->PointwiseDivide(x->pvector,y->pvector);
357 #endif
358 }
359 else this->ivector->PointwiseDivide(x->ivector,y->ivector);
360 }
361 /*}}}*/
362};
363#endif //#ifndef _VECTOR_H_
Note: See TracBrowser for help on using the repository browser.