| 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 |
|
|---|
| 22 | enum vectortype { PetscVecType, IssmVecType };
|
|---|
| 23 |
|
|---|
| 24 | template <class doubletype>
|
|---|
| 25 | class 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 | Vector<doubletype>* Duplicate(void){_assert_(this);/*{{{*/
|
|---|
| 220 |
|
|---|
| 221 | Vector<doubletype>* output=NULL;
|
|---|
| 222 |
|
|---|
| 223 | output=new Vector<doubletype>();
|
|---|
| 224 |
|
|---|
| 225 | if(type==PetscVecType){
|
|---|
| 226 | #ifdef _HAVE_PETSC_
|
|---|
| 227 | output->pvector=this->pvector->Duplicate();
|
|---|
| 228 | #endif
|
|---|
| 229 | }
|
|---|
| 230 | else output->ivector=this->ivector->Duplicate();
|
|---|
| 231 |
|
|---|
| 232 | return output;
|
|---|
| 233 |
|
|---|
| 234 | }
|
|---|
| 235 | /*}}}*/
|
|---|
| 236 | void Set(doubletype value){_assert_(this);/*{{{*/
|
|---|
| 237 |
|
|---|
| 238 | if(type==PetscVecType){
|
|---|
| 239 | #ifdef _HAVE_PETSC_
|
|---|
| 240 | this->pvector->Set(value);
|
|---|
| 241 | #endif
|
|---|
| 242 | }
|
|---|
| 243 | else this->ivector->Set(value);
|
|---|
| 244 |
|
|---|
| 245 | }
|
|---|
| 246 | /*}}}*/
|
|---|
| 247 | void AXPY(Vector* X, doubletype a){_assert_(this);/*{{{*/
|
|---|
| 248 |
|
|---|
| 249 | if(type==PetscVecType){
|
|---|
| 250 | #ifdef _HAVE_PETSC_
|
|---|
| 251 | this->pvector->AXPY(X->pvector,a);
|
|---|
| 252 | #endif
|
|---|
| 253 | }
|
|---|
| 254 | else this->ivector->AXPY(X->ivector,a);
|
|---|
| 255 |
|
|---|
| 256 | }
|
|---|
| 257 | /*}}}*/
|
|---|
| 258 | void AYPX(Vector* X, doubletype a){_assert_(this);/*{{{*/
|
|---|
| 259 |
|
|---|
| 260 | if(type==PetscVecType){
|
|---|
| 261 | #ifdef _HAVE_PETSC_
|
|---|
| 262 | this->pvector->AYPX(X->pvector,a);
|
|---|
| 263 | #endif
|
|---|
| 264 | }
|
|---|
| 265 | else this->ivector->AYPX(X->ivector,a);
|
|---|
| 266 | }
|
|---|
| 267 | /*}}}*/
|
|---|
| 268 | doubletype* ToMPISerial(void){/*{{{*/
|
|---|
| 269 |
|
|---|
| 270 | doubletype* vec_serial=NULL;
|
|---|
| 271 |
|
|---|
| 272 | _assert_(this);
|
|---|
| 273 | if(type==PetscVecType){
|
|---|
| 274 | #ifdef _HAVE_PETSC_
|
|---|
| 275 | vec_serial=this->pvector->ToMPISerial();
|
|---|
| 276 | #endif
|
|---|
| 277 | }
|
|---|
| 278 | else vec_serial=this->ivector->ToMPISerial();
|
|---|
| 279 |
|
|---|
| 280 | return vec_serial;
|
|---|
| 281 |
|
|---|
| 282 | }
|
|---|
| 283 | /*}}}*/
|
|---|
| 284 | void Shift(doubletype shift){_assert_(this);/*{{{*/
|
|---|
| 285 |
|
|---|
| 286 | if(type==PetscVecType){
|
|---|
| 287 | #ifdef _HAVE_PETSC_
|
|---|
| 288 | this->pvector->Shift(shift);
|
|---|
| 289 | #endif
|
|---|
| 290 | }
|
|---|
| 291 | else this->ivector->Shift(shift);
|
|---|
| 292 | }
|
|---|
| 293 | /*}}}*/
|
|---|
| 294 | void Copy(Vector* to){_assert_(this);/*{{{*/
|
|---|
| 295 |
|
|---|
| 296 | if(type==PetscVecType){
|
|---|
| 297 | #ifdef _HAVE_PETSC_
|
|---|
| 298 | this->pvector->Copy(to->pvector);
|
|---|
| 299 | #endif
|
|---|
| 300 | }
|
|---|
| 301 | else this->ivector->Copy(to->ivector);
|
|---|
| 302 | }
|
|---|
| 303 | /*}}}*/
|
|---|
| 304 | doubletype Max(void){_assert_(this);/*{{{*/
|
|---|
| 305 |
|
|---|
| 306 | doubletype max=0;
|
|---|
| 307 |
|
|---|
| 308 | if(type==PetscVecType){
|
|---|
| 309 | #ifdef _HAVE_PETSC_
|
|---|
| 310 | max=this->pvector->Max();
|
|---|
| 311 | #endif
|
|---|
| 312 | }
|
|---|
| 313 | else _error_("operation not supported yet");
|
|---|
| 314 | return max;
|
|---|
| 315 | }
|
|---|
| 316 | /*}}}*/
|
|---|
| 317 | doubletype Norm(NormMode norm_type){_assert_(this);/*{{{*/
|
|---|
| 318 |
|
|---|
| 319 | doubletype norm=0;
|
|---|
| 320 |
|
|---|
| 321 | if(type==PetscVecType){
|
|---|
| 322 | #ifdef _HAVE_PETSC_
|
|---|
| 323 | norm=this->pvector->Norm(norm_type);
|
|---|
| 324 | #endif
|
|---|
| 325 | }
|
|---|
| 326 | else norm=this->ivector->Norm(norm_type);
|
|---|
| 327 | return norm;
|
|---|
| 328 | }
|
|---|
| 329 | /*}}}*/
|
|---|
| 330 | void Scale(doubletype scale_factor){_assert_(this);/*{{{*/
|
|---|
| 331 |
|
|---|
| 332 | if(type==PetscVecType){
|
|---|
| 333 | #ifdef _HAVE_PETSC_
|
|---|
| 334 | this->pvector->Scale(scale_factor);
|
|---|
| 335 | #endif
|
|---|
| 336 | }
|
|---|
| 337 | else this->ivector->Scale(scale_factor);
|
|---|
| 338 | }
|
|---|
| 339 | /*}}}*/
|
|---|
| 340 | doubletype Dot(Vector* vector){_assert_(this);/*{{{*/
|
|---|
| 341 |
|
|---|
| 342 | doubletype dot;
|
|---|
| 343 |
|
|---|
| 344 | if(type==PetscVecType){
|
|---|
| 345 | #ifdef _HAVE_PETSC_
|
|---|
| 346 | dot=this->pvector->Dot(vector->pvector);
|
|---|
| 347 | #endif
|
|---|
| 348 | }
|
|---|
| 349 | else dot=this->ivector->Dot(vector->ivector);
|
|---|
| 350 | return dot;
|
|---|
| 351 | }
|
|---|
| 352 | /*}}}*/
|
|---|
| 353 | void PointwiseDivide(Vector* x,Vector* y){_assert_(this);/*{{{*/
|
|---|
| 354 |
|
|---|
| 355 | if(type==PetscVecType){
|
|---|
| 356 | #ifdef _HAVE_PETSC_
|
|---|
| 357 | this->pvector->PointwiseDivide(x->pvector,y->pvector);
|
|---|
| 358 | #endif
|
|---|
| 359 | }
|
|---|
| 360 | else this->ivector->PointwiseDivide(x->ivector,y->ivector);
|
|---|
| 361 | }
|
|---|
| 362 | /*}}}*/
|
|---|
| 363 | };
|
|---|
| 364 | #endif //#ifndef _VECTOR_H_
|
|---|