| 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 | 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 | /*FUNCTION Echo{{{*/
|
|---|
| 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 | /*FUNCTION Assemble{{{*/
|
|---|
| 143 | void Assemble(void){_assert_(this);
|
|---|
| 144 |
|
|---|
| 145 | if(type==PetscVecType){
|
|---|
| 146 | #ifdef _HAVE_PETSC_
|
|---|
| 147 | this->pvector->Assemble();
|
|---|
| 148 | #endif
|
|---|
| 149 | }
|
|---|
| 150 | else this->ivector->Assemble();
|
|---|
| 151 |
|
|---|
| 152 | }
|
|---|
| 153 | /*}}}*/
|
|---|
| 154 | /*FUNCTION SetValues{{{*/
|
|---|
| 155 | void SetValues(int ssize, int* list, doubletype* values, InsMode mode){ _assert_(this);
|
|---|
| 156 | if(type==PetscVecType){
|
|---|
| 157 | #ifdef _HAVE_PETSC_
|
|---|
| 158 | this->pvector->SetValues(ssize,list,values,mode);
|
|---|
| 159 | #endif
|
|---|
| 160 | }
|
|---|
| 161 | else this->ivector->SetValues(ssize,list,values,mode);
|
|---|
| 162 |
|
|---|
| 163 | }
|
|---|
| 164 | /*}}}*/
|
|---|
| 165 | /*FUNCTION SetValue{{{*/
|
|---|
| 166 | void SetValue(int dof, doubletype value, InsMode mode){_assert_(this);
|
|---|
| 167 |
|
|---|
| 168 | if(type==PetscVecType){
|
|---|
| 169 | #ifdef _HAVE_PETSC_
|
|---|
| 170 | this->pvector->SetValue(dof,value,mode);
|
|---|
| 171 | #endif
|
|---|
| 172 | }
|
|---|
| 173 | else this->ivector->SetValue(dof,value,mode);
|
|---|
| 174 |
|
|---|
| 175 | }
|
|---|
| 176 | /*}}}*/
|
|---|
| 177 | /*FUNCTION GetValue{{{*/
|
|---|
| 178 | void GetValue(doubletype* pvalue,int dof){_assert_(this);
|
|---|
| 179 |
|
|---|
| 180 | if(type==PetscVecType){
|
|---|
| 181 | #ifdef _HAVE_PETSC_
|
|---|
| 182 | this->pvector->GetValue(pvalue,dof);
|
|---|
| 183 | #endif
|
|---|
| 184 | }
|
|---|
| 185 | else this->ivector->GetValue(pvalue,dof);
|
|---|
| 186 |
|
|---|
| 187 | }
|
|---|
| 188 | /*}}}*/
|
|---|
| 189 | /*FUNCTION GetSize{{{*/
|
|---|
| 190 | void GetSize(int* pM){_assert_(this);
|
|---|
| 191 |
|
|---|
| 192 | if(type==PetscVecType){
|
|---|
| 193 | #ifdef _HAVE_PETSC_
|
|---|
| 194 | this->pvector->GetSize(pM);
|
|---|
| 195 | #endif
|
|---|
| 196 | }
|
|---|
| 197 | else this->ivector->GetSize(pM);
|
|---|
| 198 |
|
|---|
| 199 | }
|
|---|
| 200 | /*}}}*/
|
|---|
| 201 | /*FUNCTION IsEmpty{{{*/
|
|---|
| 202 | bool IsEmpty(void){
|
|---|
| 203 | int M;
|
|---|
| 204 |
|
|---|
| 205 | _assert_(this);
|
|---|
| 206 | this->GetSize(&M);
|
|---|
| 207 |
|
|---|
| 208 | if(M==0)
|
|---|
| 209 | return true;
|
|---|
| 210 | else
|
|---|
| 211 | return false;
|
|---|
| 212 | }
|
|---|
| 213 | /*}}}*/
|
|---|
| 214 | /*FUNCTION GetLocalSize{{{*/
|
|---|
| 215 | void GetLocalSize(int* pM){_assert_(this);
|
|---|
| 216 |
|
|---|
| 217 | if(type==PetscVecType){
|
|---|
| 218 | #ifdef _HAVE_PETSC_
|
|---|
| 219 | this->pvector->GetLocalSize(pM);
|
|---|
| 220 | #endif
|
|---|
| 221 | }
|
|---|
| 222 | else this->ivector->GetLocalSize(pM);
|
|---|
| 223 |
|
|---|
| 224 | }
|
|---|
| 225 | /*}}}*/
|
|---|
| 226 | /*FUNCTION Duplicate{{{*/
|
|---|
| 227 | Vector<doubletype>* Duplicate(void){_assert_(this);
|
|---|
| 228 |
|
|---|
| 229 | Vector<doubletype>* output=NULL;
|
|---|
| 230 |
|
|---|
| 231 | output=new Vector<doubletype>();
|
|---|
| 232 |
|
|---|
| 233 | if(type==PetscVecType){
|
|---|
| 234 | #ifdef _HAVE_PETSC_
|
|---|
| 235 | output->pvector=this->pvector->Duplicate();
|
|---|
| 236 | #endif
|
|---|
| 237 | }
|
|---|
| 238 | else output->ivector=this->ivector->Duplicate();
|
|---|
| 239 |
|
|---|
| 240 | return output;
|
|---|
| 241 |
|
|---|
| 242 | }
|
|---|
| 243 | /*}}}*/
|
|---|
| 244 | /*FUNCTION Set{{{*/
|
|---|
| 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 | /*FUNCTION AXPY{{{*/
|
|---|
| 257 | void AXPY(Vector* X, doubletype a){_assert_(this);
|
|---|
| 258 |
|
|---|
| 259 | if(type==PetscVecType){
|
|---|
| 260 | #ifdef _HAVE_PETSC_
|
|---|
| 261 | this->pvector->AXPY(X->pvector,a);
|
|---|
| 262 | #endif
|
|---|
| 263 | }
|
|---|
| 264 | else this->ivector->AXPY(X->ivector,a);
|
|---|
| 265 |
|
|---|
| 266 | }
|
|---|
| 267 | /*}}}*/
|
|---|
| 268 | /*FUNCTION AYPX{{{*/
|
|---|
| 269 | void AYPX(Vector* X, doubletype a){_assert_(this);
|
|---|
| 270 |
|
|---|
| 271 | if(type==PetscVecType){
|
|---|
| 272 | #ifdef _HAVE_PETSC_
|
|---|
| 273 | this->pvector->AYPX(X->pvector,a);
|
|---|
| 274 | #endif
|
|---|
| 275 | }
|
|---|
| 276 | else this->ivector->AYPX(X->ivector,a);
|
|---|
| 277 | }
|
|---|
| 278 | /*}}}*/
|
|---|
| 279 | /*FUNCTION ToMPISerial{{{*/
|
|---|
| 280 | doubletype* ToMPISerial(void){
|
|---|
| 281 |
|
|---|
| 282 | doubletype* vec_serial=NULL;
|
|---|
| 283 |
|
|---|
| 284 | _assert_(this);
|
|---|
| 285 | if(type==PetscVecType){
|
|---|
| 286 | #ifdef _HAVE_PETSC_
|
|---|
| 287 | vec_serial=this->pvector->ToMPISerial();
|
|---|
| 288 | #endif
|
|---|
| 289 | }
|
|---|
| 290 | else vec_serial=this->ivector->ToMPISerial();
|
|---|
| 291 |
|
|---|
| 292 | return vec_serial;
|
|---|
| 293 |
|
|---|
| 294 | }
|
|---|
| 295 | /*}}}*/
|
|---|
| 296 | /*FUNCTION Copy{{{*/
|
|---|
| 297 | void Copy(Vector* to){_assert_(this);
|
|---|
| 298 |
|
|---|
| 299 | if(type==PetscVecType){
|
|---|
| 300 | #ifdef _HAVE_PETSC_
|
|---|
| 301 | this->pvector->Copy(to->pvector);
|
|---|
| 302 | #endif
|
|---|
| 303 | }
|
|---|
| 304 | else this->ivector->Copy(to->ivector);
|
|---|
| 305 | }
|
|---|
| 306 | /*}}}*/
|
|---|
| 307 | /*FUNCTION Max{{{*/
|
|---|
| 308 | doubletype Max(void){_assert_(this);
|
|---|
| 309 |
|
|---|
| 310 | doubletype max=0;
|
|---|
| 311 |
|
|---|
| 312 | if(type==PetscVecType){
|
|---|
| 313 | #ifdef _HAVE_PETSC_
|
|---|
| 314 | max=this->pvector->Max();
|
|---|
| 315 | #endif
|
|---|
| 316 | }
|
|---|
| 317 | else _error_("operation not supported yet");
|
|---|
| 318 | return max;
|
|---|
| 319 | }
|
|---|
| 320 | /*}}}*/
|
|---|
| 321 | /*FUNCTION Norm{{{*/
|
|---|
| 322 | doubletype Norm(NormMode norm_type){_assert_(this);
|
|---|
| 323 |
|
|---|
| 324 | doubletype norm=0;
|
|---|
| 325 |
|
|---|
| 326 | if(type==PetscVecType){
|
|---|
| 327 | #ifdef _HAVE_PETSC_
|
|---|
| 328 | norm=this->pvector->Norm(norm_type);
|
|---|
| 329 | #endif
|
|---|
| 330 | }
|
|---|
| 331 | else norm=this->ivector->Norm(norm_type);
|
|---|
| 332 | return norm;
|
|---|
| 333 | }
|
|---|
| 334 | /*}}}*/
|
|---|
| 335 | /*FUNCTION Scale{{{*/
|
|---|
| 336 | void Scale(doubletype scale_factor){_assert_(this);
|
|---|
| 337 |
|
|---|
| 338 | if(type==PetscVecType){
|
|---|
| 339 | #ifdef _HAVE_PETSC_
|
|---|
| 340 | this->pvector->Scale(scale_factor);
|
|---|
| 341 | #endif
|
|---|
| 342 | }
|
|---|
| 343 | else this->ivector->Scale(scale_factor);
|
|---|
| 344 | }
|
|---|
| 345 | /*}}}*/
|
|---|
| 346 | /*FUNCTION Dot{{{*/
|
|---|
| 347 | doubletype Dot(Vector* vector){_assert_(this);
|
|---|
| 348 |
|
|---|
| 349 | doubletype dot;
|
|---|
| 350 |
|
|---|
| 351 | if(type==PetscVecType){
|
|---|
| 352 | #ifdef _HAVE_PETSC_
|
|---|
| 353 | dot=this->pvector->Dot(vector->pvector);
|
|---|
| 354 | #endif
|
|---|
| 355 | }
|
|---|
| 356 | else dot=this->ivector->Dot(vector->ivector);
|
|---|
| 357 | return dot;
|
|---|
| 358 | }
|
|---|
| 359 | /*}}}*/
|
|---|
| 360 | /*FUNCTION PointwiseDivide{{{*/
|
|---|
| 361 | void PointwiseDivide(Vector* x,Vector* y){_assert_(this);
|
|---|
| 362 |
|
|---|
| 363 | if(type==PetscVecType){
|
|---|
| 364 | #ifdef _HAVE_PETSC_
|
|---|
| 365 | this->pvector->PointwiseDivide(x->pvector,y->pvector);
|
|---|
| 366 | #endif
|
|---|
| 367 | }
|
|---|
| 368 | else this->ivector->PointwiseDivide(x->ivector,y->ivector);
|
|---|
| 369 | }
|
|---|
| 370 | /*}}}*/
|
|---|
| 371 | };
|
|---|
| 372 | #endif //#ifndef _VECTOR_H_
|
|---|