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

Last change on this file since 26744 was 26744, checked in by Mathieu Morlighem, 3 years ago

merged trunk-jpl and trunk for revision 26742

File size: 8.4 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 #ifdef _HAVE_PETSC_
101 pvector=NULL;
102 #endif
103 ivector=NULL;
104
105 /*retrieve toolkittype: */
106 char* toolkittype=ToolkitOptions::GetToolkitType();
107 _assert_(toolkittype);
108
109 /*set vector type: */
110 if(strcmp(toolkittype,"petsc")==0){
111 #ifdef _HAVE_PETSC_
112 type=PetscVecType;
113 #else
114 _error_("cannot create petsc vector without PETSC compiled!");
115 #endif
116 }
117 else if(strcmp(toolkittype,"issm")==0){
118 /*let this choice stand:*/
119 type=IssmVecType;
120 }
121 else{
122 _error_("unknow toolkit type ");
123 }
124
125 /*Free ressources: */
126 xDelete<char>(toolkittype);
127 }
128 /*}}}*/
129
130 /*Vector specific routines*/
131 void Echo(void){_assert_(this);/*{{{*/
132
133 if(type==PetscVecType){
134 #ifdef _HAVE_PETSC_
135 this->pvector->Echo();
136 #endif
137 }
138 else this->ivector->Echo();
139
140 }
141 /*}}}*/
142 void Assemble(void){_assert_(this);/*{{{*/
143
144 if(type==PetscVecType){
145 #ifdef _HAVE_PETSC_
146 this->pvector->Assemble();
147 #endif
148 }
149 else this->ivector->Assemble();
150
151 }
152 /*}}}*/
153 void SetValues(int ssize, int* list, doubletype* values, InsMode mode){ _assert_(this);/*{{{*/
154 if(type==PetscVecType){
155 #ifdef _HAVE_PETSC_
156 this->pvector->SetValues(ssize,list,values,mode);
157 #endif
158 }
159 else this->ivector->SetValues(ssize,list,values,mode);
160
161 }
162 /*}}}*/
163 void SetValue(int dof, doubletype value, InsMode mode){_assert_(this);/*{{{*/
164
165 if(type==PetscVecType){
166 #ifdef _HAVE_PETSC_
167 this->pvector->SetValue(dof,value,mode);
168 #endif
169 }
170 else this->ivector->SetValue(dof,value,mode);
171
172 }
173 /*}}}*/
174 void GetValue(doubletype* pvalue,int dof){_assert_(this);/*{{{*/
175
176 if(type==PetscVecType){
177 #ifdef _HAVE_PETSC_
178 this->pvector->GetValue(pvalue,dof);
179 #endif
180 }
181 else this->ivector->GetValue(pvalue,dof);
182
183 }
184 /*}}}*/
185 void GetSize(int* pM){_assert_(this);/*{{{*/
186
187 if(type==PetscVecType){
188 #ifdef _HAVE_PETSC_
189 this->pvector->GetSize(pM);
190 #endif
191 }
192 else this->ivector->GetSize(pM);
193
194 }
195 /*}}}*/
196 bool IsEmpty(void){/*{{{*/
197 int M;
198
199 _assert_(this);
200 this->GetSize(&M);
201
202 if(M==0)
203 return true;
204 else
205 return false;
206 }
207 /*}}}*/
208 void GetLocalSize(int* pM){_assert_(this);/*{{{*/
209
210 if(type==PetscVecType){
211 #ifdef _HAVE_PETSC_
212 this->pvector->GetLocalSize(pM);
213 #endif
214 }
215 else this->ivector->GetLocalSize(pM);
216
217 }
218 /*}}}*/
219 void GetLocalVector(doubletype** pvector,int** pindices){_assert_(this);/*{{{*/
220
221 if(type==PetscVecType){
222 #ifdef _HAVE_PETSC_
223 this->pvector->GetLocalVector(pvector,pindices);
224 #endif
225 }
226 else this->ivector->GetLocalVector(pvector,pindices);
227
228 }
229 /*}}}*/
230 Vector<doubletype>* Duplicate(void){_assert_(this);/*{{{*/
231
232 Vector<doubletype>* output=NULL;
233
234 output=new Vector<doubletype>();
235
236 if(type==PetscVecType){
237 #ifdef _HAVE_PETSC_
238 output->pvector=this->pvector->Duplicate();
239 #endif
240 }
241 else output->ivector=this->ivector->Duplicate();
242
243 return output;
244 } /*}}}*/
245 void Set(doubletype value){_assert_(this);/*{{{*/
246
247 if(type==PetscVecType){
248 #ifdef _HAVE_PETSC_
249 this->pvector->Set(value);
250 #endif
251 }
252 else this->ivector->Set(value);
253
254 }
255 /*}}}*/
256 void AXPY(Vector* X, doubletype a){_assert_(this);/*{{{*/
257
258 if(type==PetscVecType){
259 #ifdef _HAVE_PETSC_
260 this->pvector->AXPY(X->pvector,a);
261 #endif
262 }
263 else this->ivector->AXPY(X->ivector,a);
264
265 }
266 /*}}}*/
267 void AYPX(Vector* X, doubletype a){_assert_(this);/*{{{*/
268
269 if(type==PetscVecType){
270 #ifdef _HAVE_PETSC_
271 this->pvector->AYPX(X->pvector,a);
272 #endif
273 }
274 else this->ivector->AYPX(X->ivector,a);
275 }
276 /*}}}*/
277 doubletype* ToMPISerial(void){/*{{{*/
278
279 doubletype* vec_serial=NULL;
280
281 _assert_(this);
282 if(type==PetscVecType){
283 #ifdef _HAVE_PETSC_
284 vec_serial=this->pvector->ToMPISerial();
285 #endif
286 }
287 else vec_serial=this->ivector->ToMPISerial();
288
289 return vec_serial;
290
291 }
292 /*}}}*/
293 doubletype* ToMPISerial0(void){/*{{{*/
294
295 doubletype* vec_serial=NULL;
296
297 _assert_(this);
298 if(type==PetscVecType){
299 #ifdef _HAVE_PETSC_
300 vec_serial=this->pvector->ToMPISerial0();
301 #endif
302 }
303 else vec_serial=this->ivector->ToMPISerial0();
304
305 return vec_serial;
306
307 }
308 /*}}}*/
309 void Shift(doubletype shift){_assert_(this);/*{{{*/
310
311 if(type==PetscVecType){
312 #ifdef _HAVE_PETSC_
313 this->pvector->Shift(shift);
314 #endif
315 }
316 else this->ivector->Shift(shift);
317 }
318 /*}}}*/
319 void Copy(Vector* to){_assert_(this);/*{{{*/
320
321 if(type==PetscVecType){
322 #ifdef _HAVE_PETSC_
323 this->pvector->Copy(to->pvector);
324 #endif
325 }
326 else this->ivector->Copy(to->ivector);
327 }
328 /*}}}*/
329 doubletype Max(void){_assert_(this);/*{{{*/
330
331 doubletype max=0;
332
333 if(type==PetscVecType){
334 #ifdef _HAVE_PETSC_
335 max=this->pvector->Max();
336 #endif
337 }
338 else _error_("operation not supported yet");
339 return max;
340 }
341 /*}}}*/
342 doubletype Norm(NormMode norm_type){_assert_(this);/*{{{*/
343
344 doubletype norm=0;
345
346 if(type==PetscVecType){
347 #ifdef _HAVE_PETSC_
348 norm=this->pvector->Norm(norm_type);
349 #endif
350 }
351 else norm=this->ivector->Norm(norm_type);
352 return norm;
353 }
354 /*}}}*/
355 void Scale(doubletype scale_factor){_assert_(this);/*{{{*/
356
357 if(type==PetscVecType){
358 #ifdef _HAVE_PETSC_
359 this->pvector->Scale(scale_factor);
360 #endif
361 }
362 else this->ivector->Scale(scale_factor);
363 }
364 /*}}}*/
365 doubletype Dot(Vector* vector){_assert_(this);/*{{{*/
366
367 doubletype dot;
368
369 if(type==PetscVecType){
370 #ifdef _HAVE_PETSC_
371 dot=this->pvector->Dot(vector->pvector);
372 #endif
373 }
374 else dot=this->ivector->Dot(vector->ivector);
375 return dot;
376 }
377 /*}}}*/
378 void PointwiseDivide(Vector* x,Vector* y){_assert_(this);/*{{{*/
379
380 if(type==PetscVecType){
381 #ifdef _HAVE_PETSC_
382 this->pvector->PointwiseDivide(x->pvector,y->pvector);
383 #endif
384 }
385 else this->ivector->PointwiseDivide(x->ivector,y->ivector);
386 }
387 /*}}}*/
388 void PointwiseMult(Vector* x,Vector* y){_assert_(this);/*{{{*/
389
390 if(type==PetscVecType){
391 #ifdef _HAVE_PETSC_
392 this->pvector->PointwiseMult(x->pvector,y->pvector);
393 #endif
394 }
395 else this->ivector->PointwiseMult(x->ivector,y->ivector);
396 }
397 /*}}}*/
398 void Pow(doubletype scale_factor){_assert_(this);/*{{{*/
399
400 if(type==PetscVecType){
401 #ifdef _HAVE_PETSC_
402 this->pvector->Pow(scale_factor);
403 #endif
404 }
405 else this->ivector->Pow(scale_factor);
406 }
407 /*}}}*/
408void Sum(doubletype* pvalue){ /*{{{*/
409 _assert_(this);/*{{{*/
410
411 if(type==PetscVecType){
412 #ifdef _HAVE_PETSC_
413 this->pvector->Sum(pvalue);
414 #endif
415 }
416 else this->ivector->Sum(pvalue);
417}
418/*}}}*/
419}; /*}}}*/
420#endif //#ifndef _VECTOR_H_
Note: See TracBrowser for help on using the repository browser.