00001
00013 #ifndef __OCT_MG_H__
00014 #define __OCT_MG_H__
00015
00016 #include "petscpc.h"
00017 #include "petscksp.h"
00018 #include <vector>
00019
00020 #ifdef PETSC_USE_LOG
00021
00022 #include "petscsys.h"
00023
00024 namespace ot {
00025 extern int pcKspShellSetupEvent;
00026 extern int pcKspShellDestroyEvent;
00027 extern int pcKspShellApplyEvent;
00028 extern int setDaEvent;
00029 extern int setUpEvent;
00030 extern int createRp1Event;
00031 extern int createRp2Event;
00032 extern int setKspEvent;
00033 extern int restrictEvent;
00034 extern int dummyRestrictEvent;
00035 extern int prolongEvent;
00036 extern int scatterEvent;
00037 extern int damgInitEvent;
00038 extern int damgFinalEvent;
00039
00040 extern int setDAstage1Event;
00041 extern int setDAstage2Event;
00042 extern int setDAstage3Event;
00043 extern int setDAstage4Event;
00044 extern int setDAstage5Event;
00045 extern int setDAstage6Event;
00046 }
00047
00048 #define PROF_SET_DA_STAGE1_BEGIN PetscLogEventBegin(setDAstage1Event,0,0,0,0);
00049 #define PROF_SET_DA_STAGE1_END PetscLogEventEnd(setDAstage1Event,0,0,0,0);
00050
00051 #define PROF_SET_DA_STAGE2_BEGIN PetscLogEventBegin(setDAstage2Event,0,0,0,0);
00052 #define PROF_SET_DA_STAGE2_END PetscLogEventEnd(setDAstage2Event,0,0,0,0);
00053
00054 #define PROF_SET_DA_STAGE3_BEGIN PetscLogEventBegin(setDAstage3Event,0,0,0,0);
00055 #define PROF_SET_DA_STAGE3_END PetscLogEventEnd(setDAstage3Event,0,0,0,0);
00056
00057 #define PROF_SET_DA_STAGE4_BEGIN PetscLogEventBegin(setDAstage4Event,0,0,0,0);
00058 #define PROF_SET_DA_STAGE4_END PetscLogEventEnd(setDAstage4Event,0,0,0,0);
00059
00060 #define PROF_SET_DA_STAGE5_BEGIN PetscLogEventBegin(setDAstage5Event,0,0,0,0);
00061 #define PROF_SET_DA_STAGE5_END PetscLogEventEnd(setDAstage5Event,0,0,0,0);
00062
00063 #define PROF_SET_DA_STAGE6_BEGIN PetscLogEventBegin(setDAstage6Event,0,0,0,0);
00064 #define PROF_SET_DA_STAGE6_END PetscLogEventEnd(setDAstage6Event,0,0,0,0);
00065
00066 #define PROF_PC_KSP_SHELL_SETUP_BEGIN PetscFunctionBegin; \
00067 PetscLogEventBegin(pcKspShellSetupEvent,0,0,0,0);
00068
00069 #define PROF_PC_KSP_SHELL_SETUP_END PetscLogEventEnd(pcKspShellSetupEvent,0,0,0,0); \
00070 PetscFunctionReturn(0);
00071
00072 #define PROF_PC_KSP_SHELL_DESTROY_BEGIN PetscFunctionBegin; \
00073 PetscLogEventBegin(pcKspShellDestroyEvent,0,0,0,0);
00074
00075 #define PROF_PC_KSP_SHELL_DESTROY_END PetscLogEventEnd(pcKspShellDestroyEvent,0,0,0,0); \
00076 PetscFunctionReturn(0);
00077
00078 #define PROF_PC_KSP_SHELL_APPLY_BEGIN PetscFunctionBegin; \
00079 PetscLogEventBegin(pcKspShellApplyEvent,0,0,0,0);
00080
00081 #define PROF_PC_KSP_SHELL_APPLY_END PetscLogEventEnd(pcKspShellApplyEvent,0,0,0,0); \
00082 PetscFunctionReturn(0);
00083
00084 #define PROF_MG_INIT_BEGIN PetscFunctionBegin; \
00085 PetscLogEventBegin(damgInitEvent,0,0,0,0);
00086
00087 #define PROF_MG_INIT_END PetscLogEventEnd(damgInitEvent,0,0,0,0); \
00088 PetscFunctionReturn(0);
00089
00090 #define PROF_MG_FINAL_BEGIN PetscFunctionBegin; \
00091 PetscLogEventBegin(damgFinalEvent,0,0,0,0);
00092
00093 #define PROF_MG_FINAL_END PetscLogEventEnd(damgFinalEvent,0,0,0,0); \
00094 PetscFunctionReturn(0);
00095
00096 #define PROF_MG_SET_KSP_BEGIN PetscFunctionBegin; \
00097 PetscLogEventBegin(setKspEvent,0,0,0,0);
00098
00099 #define PROF_MG_SET_KSP_END PetscLogEventEnd(setKspEvent,0,0,0,0); \
00100 PetscFunctionReturn(0);
00101
00102 #define PROF_MG_SET_DA_BEGIN PetscFunctionBegin; \
00103 PetscLogEventBegin(setDaEvent,0,0,0,0);
00104
00105 #define PROF_MG_SET_DA_END PetscLogEventEnd(setDaEvent,0,0,0,0); \
00106 PetscFunctionReturn(0);
00107
00108 #define PROF_MG_SETUP_BEGIN PetscFunctionBegin; \
00109 PetscLogEventBegin(setUpEvent,0,0,0,0);
00110
00111 #define PROF_MG_SETUP_END PetscLogEventEnd(setUpEvent,0,0,0,0); \
00112 PetscFunctionReturn(0);
00113
00114 #define PROF_MG_CREATE_RP1_BEGIN PetscFunctionBegin; \
00115 PetscLogEventBegin(createRp1Event,0,0,0,0);
00116
00117 #define PROF_MG_CREATE_RP1_END PetscLogEventEnd(createRp1Event,0,0,0,0); \
00118 PetscFunctionReturn(0);
00119
00120 #define PROF_MG_CREATE_RP2_BEGIN PetscFunctionBegin; \
00121 PetscLogEventBegin(createRp2Event,0,0,0,0);
00122
00123 #define PROF_MG_CREATE_RP2_END PetscLogEventEnd(createRp2Event,0,0,0,0); \
00124 PetscFunctionReturn(0);
00125
00126 #define PROF_MG_RESTRICT_BEGIN PetscFunctionBegin; \
00127 PetscLogEventBegin(restrictEvent,0,0,0,0);
00128
00129 #define PROF_MG_RESTRICT_END PetscLogEventEnd(restrictEvent,0,0,0,0); \
00130 PetscFunctionReturn(0);
00131
00132 #define PROF_MG_RESTRICT_DUMMY_BEGIN PetscFunctionBegin; \
00133 PetscLogEventBegin(dummyRestrictEvent,0,0,0,0);
00134
00135 #define PROF_MG_RESTRICT_DUMMY_END PetscLogEventEnd(dummyRestrictEvent,0,0,0,0); \
00136 PetscFunctionReturn(0);
00137
00138 #define PROF_MG_PROLONG_BEGIN PetscFunctionBegin; \
00139 PetscLogEventBegin(prolongEvent,0,0,0,0);
00140
00141 #define PROF_MG_PROLONG_END PetscLogEventEnd(prolongEvent,0,0,0,0); \
00142 PetscFunctionReturn(0);
00143
00144 #define PROF_MG_SCATTER_BEGIN PetscLogEventBegin(scatterEvent,0,0,0,0);
00145
00146 #define PROF_MG_SCATTER_END PetscLogEventEnd(scatterEvent,0,0,0,0); \
00147 return 0;
00148
00149 #else
00150
00151 #define PROF_SET_DA_STAGE1_BEGIN
00152 #define PROF_SET_DA_STAGE1_END
00153
00154 #define PROF_SET_DA_STAGE2_BEGIN
00155 #define PROF_SET_DA_STAGE2_END
00156
00157 #define PROF_SET_DA_STAGE3_BEGIN
00158 #define PROF_SET_DA_STAGE3_END
00159
00160 #define PROF_SET_DA_STAGE4_BEGIN
00161 #define PROF_SET_DA_STAGE4_END
00162
00163 #define PROF_SET_DA_STAGE5_BEGIN
00164 #define PROF_SET_DA_STAGE5_END
00165
00166 #define PROF_SET_DA_STAGE6_BEGIN
00167 #define PROF_SET_DA_STAGE6_END
00168
00169 #define PROF_PC_KSP_SHELL_SETUP_BEGIN PetscFunctionBegin;
00170
00171 #define PROF_PC_KSP_SHELL_SETUP_END PetscFunctionReturn(0);
00172
00173 #define PROF_PC_KSP_SHELL_DESTROY_BEGIN PetscFunctionBegin;
00174
00175 #define PROF_PC_KSP_SHELL_DESTROY_END PetscFunctionReturn(0);
00176
00177 #define PROF_PC_KSP_SHELL_APPLY_BEGIN PetscFunctionBegin;
00178
00179 #define PROF_PC_KSP_SHELL_APPLY_END PetscFunctionReturn(0);
00180
00181 #define PROF_MG_INIT_BEGIN PetscFunctionBegin;
00182
00183 #define PROF_MG_INIT_END PetscFunctionReturn(0);
00184
00185 #define PROF_MG_FINAL_BEGIN PetscFunctionBegin;
00186
00187 #define PROF_MG_FINAL_END PetscFunctionReturn(0);
00188
00189 #define PROF_MG_SET_KSP_BEGIN PetscFunctionBegin;
00190
00191 #define PROF_MG_SET_KSP_END PetscFunctionReturn(0);
00192
00193 #define PROF_MG_SET_DA_BEGIN PetscFunctionBegin;
00194
00195 #define PROF_MG_SET_DA_END PetscFunctionReturn(0);
00196
00197 #define PROF_MG_SETUP_BEGIN PetscFunctionBegin;
00198
00199 #define PROF_MG_SETUP_END PetscFunctionReturn(0);
00200
00201 #define PROF_MG_CREATE_RP1_BEGIN PetscFunctionBegin;
00202
00203 #define PROF_MG_CREATE_RP1_END PetscFunctionReturn(0);
00204
00205 #define PROF_MG_CREATE_RP2_BEGIN PetscFunctionBegin;
00206
00207 #define PROF_MG_CREATE_RP2_END PetscFunctionReturn(0);
00208
00209 #define PROF_MG_SCATTER_BEGIN
00210
00211 #define PROF_MG_SCATTER_END return 0;
00212
00213 #define PROF_MG_RESTRICT_BEGIN PetscFunctionBegin;
00214
00215 #define PROF_MG_RESTRICT_END PetscFunctionReturn(0);
00216
00217 #define PROF_MG_RESTRICT_DUMMY_BEGIN PetscFunctionBegin;
00218
00219 #define PROF_MG_RESTRICT_DUMMY_END PetscFunctionReturn(0);
00220
00221 #define PROF_MG_PROLONG_BEGIN PetscFunctionBegin;
00222
00223 #define PROF_MG_PROLONG_END PetscFunctionReturn(0);
00224
00225 #endif
00226
00227 namespace ot {
00228
00236 typedef struct {
00237 bool iAmActive;
00238 MPI_Comm commActive;
00239 PC pc;
00240 KSP ksp_private;
00241 Vec rhs_private;
00242 Vec sol_private;
00243 } PC_KSP_Shell;
00244
00245 PetscErrorCode PC_KSP_Shell_SetUp(void* ctx);
00246
00247 PetscErrorCode PC_KSP_Shell_Apply(void* ctx, Vec in, Vec out);
00248
00249 PetscErrorCode PC_KSP_Shell_Destroy(void* ctx);
00250
00258 class FineTouchedStatus {
00259 public:
00260 unsigned char flags[8];
00261
00264 FineTouchedStatus() {
00265 for(int i=0;i<8;i++) {
00266 flags[i] = 0;
00267 }
00268 }
00269
00270 FineTouchedStatus(const FineTouchedStatus & other) {
00271 for(int i=0; i<8; i++) {
00272 this->flags[i] = other.flags[i];
00273 }
00274 }
00276
00279 FineTouchedStatus & operator = (FineTouchedStatus const & other) {
00280 if(this == (&other)) {return *this;}
00281 for(int i=0; i<8; i++) {
00282 this->flags[i] = other.flags[i];
00283 }
00284 return *this;
00285 }
00286
00287 FineTouchedStatus & operator +=(FineTouchedStatus const & other) {
00288
00289 for(int i=0; i<8; i++) {
00290 this->flags[i] |= other.flags[i];
00291 }
00292 return *this;
00293 }
00294
00295 bool operator == ( FineTouchedStatus const &other) const {
00296 for(int i = 0; i < 8; i++ ) {
00297 if(this->flags[i] != other.flags[i]) {
00298 return false;
00299 }
00300 }
00301 return true;
00302 }
00303
00304 bool operator != ( FineTouchedStatus const &other) const {
00305 return (!((*this) == other));
00306 }
00307
00308 const FineTouchedStatus operator + (FineTouchedStatus const &other) const {
00309 return ((FineTouchedStatus(*this)) += other);
00310 }
00312
00313 };
00314
00320 class FineTouchedDummyStatus {
00321 public:
00322 unsigned char flags[16];
00323
00326 FineTouchedDummyStatus() {
00327 for(int i=0;i<16;i++) {
00328 flags[i] = 0;
00329 }
00330 }
00331
00332 FineTouchedDummyStatus(const FineTouchedDummyStatus & other) {
00333 for(int i=0; i<16; i++) {
00334 this->flags[i] = other.flags[i];
00335 }
00336 }
00338
00341 FineTouchedDummyStatus & operator = (FineTouchedDummyStatus const & other) {
00342 if(this == (&other)) {return *this;}
00343 for(int i=0; i<16; i++) {
00344 this->flags[i] = other.flags[i];
00345 }
00346 return *this;
00347 }
00348
00349 FineTouchedDummyStatus & operator +=(FineTouchedDummyStatus const & other) {
00350
00351 for(int i=0; i<16; i++) {
00352 this->flags[i] |= other.flags[i];
00353 }
00354 return *this;
00355 }
00356
00357 bool operator == ( FineTouchedDummyStatus const &other) const {
00358 for(int i = 0; i < 16; i++ ) {
00359 if(this->flags[i] != other.flags[i]) {
00360 return false;
00361 }
00362 }
00363 return true;
00364 }
00365
00366 bool operator != ( FineTouchedDummyStatus const &other) const {
00367 return (!((*this) == other));
00368 }
00369
00370 const FineTouchedDummyStatus operator + (FineTouchedDummyStatus const &other) const {
00371 return ((FineTouchedDummyStatus(*this)) += other);
00372 }
00374
00375 };
00376
00377
00378 class DA;
00379 class TreeNode;
00380
00386 typedef struct {
00387 ot::DA* dac;
00388 ot::DA* daf;
00389 unsigned int minIndependentSize;
00393 unsigned char* suppressedDOFc;
00394 unsigned char* suppressedDOFf;
00395 std::vector<ot::FineTouchedStatus >* fineTouchedFlags;
00396 unsigned int dof;
00397 MPI_Comm comm;
00398 Vec tmp;
00399 Vec addRtmp;
00400 Vec addPtmp;
00403 int * sendSzP;
00404 int * sendOffP;
00405 int * recvSzP;
00406 int * recvOffP;
00407 int * sendSzR;
00408 int * sendOffR;
00409 int * recvSzR;
00410 int * recvOffR;
00412 } TransferOpData;
00413
00419 struct _p_DAMG {
00420
00421 ot::DA* da;
00422 ot::DA* da_aux;
00425 unsigned int dof;
00427 unsigned char* suppressedDOF;
00428 unsigned char* suppressedDOFaux;
00430 Vec x,b,r;
00431 Mat J;
00432 Mat B;
00433 Mat R;
00434 int nlevels;
00435 int totalLevels;
00436 MPI_Comm comm;
00443 PetscErrorCode (*solve)(_p_DAMG**, int);
00444
00445 void *user;
00447 KSP ksp;
00448 PetscErrorCode (*initialguess)(_p_DAMG*, Vec);
00449 PetscErrorCode (*rhs)(_p_DAMG*,Vec);
00450 };
00451
00453 typedef struct _p_DAMG* DAMG;
00454
00455
00456
00470 PetscErrorCode DAMGCreateAndSetDA(MPI_Comm comm, int& nlevels,
00471 void* user, DAMG** damg, std::vector<ot::TreeNode>& finestOctree,
00472 unsigned int dof = 1, double loadFac = 1.5,
00473 bool compressLut = true, bool incCorner = true);
00474
00479 PetscErrorCode DAMGCreateJMatrix(DAMG damg, PetscErrorCode (*crJ)(DAMG, Mat*J));
00480
00488 PetscErrorCode DAMGSetKSP(DAMG* damg, PetscErrorCode (*crjac)(DAMG, Mat* B),
00489 PetscErrorCode (*computeJac)(DAMG, Mat J, Mat B), PetscErrorCode (*rhs)(DAMG, Vec));
00490
00492 PetscErrorCode DAMGSolve(DAMG* damg);
00493
00495 PetscErrorCode DAMGSetInitialGuess(DAMG*, PetscErrorCode (*)(DAMG, Vec));
00496
00498 PetscErrorCode DAMGInitialGuessCurrent(DAMG, Vec);
00499
00501 void PrintDAMG(DAMG*);
00502
00504 int DAMGCreateSuppressedDOFs(DAMG* damg);
00505
00509 void DAMG_InitPrivateType1(MPI_Comm comm);
00510
00515 void DAMG_InitPrivateType2(MPI_Comm comm);
00516
00519 void DAMG_InitPrivateType3(MPI_Comm comm);
00520
00522 PetscErrorCode DAMG_Initialize(MPI_Comm comm);
00523
00525 PetscErrorCode DAMG_Finalize();
00526
00528 PetscErrorCode DAMGDestroy(DAMG* damg);
00529
00534 #define DAMGGetRHS(ctx) (ctx)[(ctx)[0]->nlevels-1]->b
00535
00540 #define DAMGGetr(ctx) (ctx)[(ctx)[0]->nlevels-1]->r
00541
00546 #define DAMGGetx(ctx) (ctx)[(ctx)[0]->nlevels-1]->x
00547
00552 #define DAMGGetComm(ctx) (ctx)[(ctx)[0]->nlevels-1]->comm
00553
00558 #define DAMGGetJ(ctx) (ctx)[(ctx)[0]->nlevels-1]->J
00559
00564 #define DAMGGetB(ctx) (ctx)[(ctx)[0]->nlevels-1]->B
00565
00570 #define DAMGGetFine(ctx) (ctx)[(ctx)[0]->nlevels-1]
00571
00576 #define DAMGGetKSP(ctx) (ctx)[(ctx)[0]->nlevels-1]->ksp
00577
00582 #define DAMGGetDA(ctx) ((ctx)[(ctx)[0]->nlevels-1]->da)
00583
00588 #define DAMGGetUser(ctx,level) ((ctx)[level]->user)
00589
00594 #define DAMGSetUser(ctx,level,usr) ((ctx)[level]->user = usr,0)
00595
00600 #define DAMGGetLevels(ctx) (ctx)[0]->nlevels
00601
00606 #define DAMGGetDAMG(ctx) (ctx)[(ctx)[0]->nlevels-1]
00607
00608 PetscErrorCode DAMGSetNullSpace(DAMG* damg, PetscTruth, int, PetscErrorCode (*)(DAMG,Vec[]));
00609
00621 int scatterValues(Vec in, Vec out, PetscInt inSz, PetscInt outSz,
00622 int *& sendSz, int *& sendOff, int *& recvSz, int *& recvOff, MPI_Comm comm);
00623
00624 PetscErrorCode DAMGSetUp(DAMG*);
00625
00626 PetscErrorCode DAMGSolveKSP(DAMG *damg, int level);
00627
00628 PetscErrorCode DAMGSetUpLevel(DAMG* damg, KSP ksp, int nlevels);
00629
00630
00631 int destroyRmatType1Stencil(double *****&lut);
00632 int destroyRmatType2Stencil(double ****&lut);
00633 int destroyVtxMaps(unsigned short ****&map1, unsigned short *****&map2,
00634 unsigned short *****&map3, unsigned short ******&map4);
00635
00636 int readRmatType1Stencil(double *****&lut);
00637 int readRmatType2Stencil(double ****&lut);
00638 int readVtxMaps(unsigned short ****&map1, unsigned short *****&map2,
00639 unsigned short *****&map3, unsigned short ******&map4);
00640
00641 int IreadRmatType1Stencil(double *****&lut, int rank);
00642 int IreadRmatType2Stencil(double ****&lut, int rank);
00643 int IreadVtxMaps(unsigned short ****&map1, unsigned short *****&map2,
00644 unsigned short *****&map3, unsigned short ******&map4, int rank);
00645
00646
00647
00648 PetscErrorCode createInterpolationType1(DAMG , DAMG, Mat *);
00649 PetscErrorCode prolongMatVecType1(Mat, Vec, Vec);
00650 PetscErrorCode restrictMatVecType1(Mat, Vec, Vec);
00651 PetscErrorCode dummyRestrictMatVecType1(TransferOpData* data);
00652
00653
00654 PetscErrorCode createInterpolationType2(DAMG , DAMG, Mat *);
00655 PetscErrorCode prolongMatVecType2(Mat, Vec, Vec);
00656 PetscErrorCode restrictMatVecType2(Mat, Vec, Vec);
00657
00658 PetscErrorCode addProlongMatVec(Mat, Vec, Vec, Vec);
00659 PetscErrorCode addRestrictMatVec(Mat, Vec, Vec, Vec);
00660
00661 PetscErrorCode rpMatDestroy(Mat);
00662
00664
00665 }
00666
00667
00668 namespace par {
00669
00670
00671 template <typename T>
00672 class Mpi_datatype;
00673
00674
00675 template <>
00676 class Mpi_datatype< ot::FineTouchedStatus > {
00677 public:
00678 static MPI_Datatype value()
00679 {
00680 static bool first = true;
00681 static MPI_Datatype datatype;
00682
00683 if (first)
00684 {
00685 first = false;
00686 MPI_Type_contiguous(sizeof(ot::FineTouchedStatus), MPI_BYTE, &datatype);
00687 MPI_Type_commit(&datatype);
00688 }
00689
00690 return datatype;
00691 }
00692 };
00693
00694
00695 template <>
00696 class Mpi_datatype< ot::FineTouchedDummyStatus > {
00697 public:
00698 static MPI_Datatype value()
00699 {
00700 static bool first = true;
00701 static MPI_Datatype datatype;
00702
00703 if (first)
00704 {
00705 first = false;
00706 MPI_Type_contiguous(sizeof(ot::FineTouchedDummyStatus), MPI_BYTE, &datatype);
00707 MPI_Type_commit(&datatype);
00708 }
00709
00710 return datatype;
00711 }
00712 };
00713
00714 }
00715
00716
00717 #endif
00718