00001
00010 #ifndef __OCT_DA_H__
00011 #define __OCT_DA_H__
00012
00013 #include "mpi.h"
00014 #include "matRecord.h"
00015 #include "loopCounters.h"
00016 #include "updateCtx.h"
00017 #include "odaUtils.h"
00018 #include "cnumEtypes.h"
00019 #include "Point.h"
00020 #include <vector>
00021 #include "petsc.h"
00022 #include "petscvec.h"
00023 #include "petscmat.h"
00024 #include "dendro.h"
00025
00026 #ifndef iC
00027 #define iC(fun) {CHKERRQ(fun);}
00028 #endif
00029
00030 #ifdef __DEBUG__
00031 #ifndef __DEBUG_DA__
00032 #define __DEBUG_DA__
00033 #endif
00034 #endif
00035
00036 #ifdef PETSC_USE_LOG
00037
00038 #include "petscsys.h"
00039
00040 namespace ot {
00041 extern int readFromGhostNodesBeginEvent;
00042 extern int readFromGhostNodesEndEvent;
00043 extern int readFromGhostElemsBeginEvent;
00044 extern int readFromGhostElemsEndEvent;
00045 extern int writeToGhostNodesBeginEvent;
00046 extern int writeToGhostNodesEndEvent;
00047 extern int writeToGhostElemsBeginEvent;
00048 extern int writeToGhostElemsEndEvent;
00049 extern int buildDaEvent;
00050 extern int setMatValuesEvent;
00051 extern int buildNlistEvent;
00052 extern int buildNlistCommEvent;
00053
00054 extern int buildDAstage1Event;
00055 extern int buildDAstage2Event;
00056 extern int buildDAstage3Event;
00057 extern int buildDAstage4Event;
00058 extern int buildDAstage5Event;
00059 extern int buildDAstage6Event;
00060 extern int buildDAstage7Event;
00061 extern int buildDAstage8Event;
00062 extern int buildDAstage9Event;
00063 }
00064
00065 #define PROF_WRITE_GHOST_ELEMS_BEGIN_BEGIN PetscLogEventBegin(writeToGhostElemsBeginEvent,0,0,0,0);
00066 #define PROF_WRITE_GHOST_ELEMS_BEGIN_END \
00067 PetscLogEventEnd(writeToGhostElemsBeginEvent,0,0,0,0); \
00068 return 0;
00069
00070 #define PROF_WRITE_GHOST_ELEMS_END_BEGIN PetscLogEventBegin(writeToGhostElemsEndEvent,0,0,0,0);
00071 #define PROF_WRITE_GHOST_ELEMS_END_END \
00072 PetscLogEventEnd(writeToGhostElemsEndEvent,0,0,0,0); \
00073 return 0;
00074
00075 #define PROF_WRITE_GHOST_NODES_BEGIN_BEGIN PetscLogEventBegin(writeToGhostNodesBeginEvent,0,0,0,0);
00076 #define PROF_WRITE_GHOST_NODES_BEGIN_END \
00077 PetscLogEventEnd(writeToGhostNodesBeginEvent,0,0,0,0); \
00078 return 0;
00079
00080 #define PROF_WRITE_GHOST_NODES_END_BEGIN PetscLogEventBegin(writeToGhostNodesEndEvent,0,0,0,0);
00081 #define PROF_WRITE_GHOST_NODES_END_END \
00082 PetscLogEventEnd(writeToGhostNodesEndEvent,0,0,0,0); \
00083 return 0;
00084
00085 #define PROF_READ_GHOST_ELEMS_BEGIN_BEGIN PetscLogEventBegin(readFromGhostElemsBeginEvent,0,0,0,0);
00086 #define PROF_READ_GHOST_ELEMS_BEGIN_END \
00087 PetscLogEventEnd(readFromGhostElemsBeginEvent,0,0,0,0); \
00088 return 0;
00089
00090 #define PROF_READ_GHOST_ELEMS_END_BEGIN PetscLogEventBegin(readFromGhostElemsEndEvent,0,0,0,0);
00091 #define PROF_READ_GHOST_ELEMS_END_END \
00092 PetscLogEventEnd(readFromGhostElemsEndEvent,0,0,0,0); \
00093 return 0;
00094
00095 #define PROF_READ_GHOST_NODES_BEGIN_BEGIN PetscLogEventBegin(readFromGhostNodesBeginEvent,0,0,0,0);
00096 #define PROF_READ_GHOST_NODES_BEGIN_END \
00097 PetscLogEventEnd(readFromGhostNodesBeginEvent,0,0,0,0); \
00098 return 0;
00099
00100 #define PROF_READ_GHOST_NODES_END_BEGIN PetscLogEventBegin(readFromGhostNodesEndEvent,0,0,0,0);
00101 #define PROF_READ_GHOST_NODES_END_END \
00102 PetscLogEventEnd(readFromGhostNodesEndEvent,0,0,0,0); \
00103 return 0;
00104
00105 #define PROF_SET_MAT_VALUES_BEGIN PetscLogEventBegin(setMatValuesEvent,0,0,0,0);
00106 #define PROF_SET_MAT_VALUES_END \
00107 PetscLogEventEnd(setMatValuesEvent,0,0,0,0); \
00108 return 0;
00109
00110 #define PROF_BUILD_DA_BEGIN PetscLogEventBegin(buildDaEvent,0,0,0,0);
00111 #define PROF_BUILD_DA_END \
00112 PetscLogEventEnd(buildDaEvent,0,0,0,0); \
00113 return;
00114
00115 #define PROF_BUILD_NLIST_BEGIN PetscLogEventBegin(buildNlistEvent,0,0,0,0);
00116 #define PROF_BUILD_NLIST_END \
00117 PetscLogEventEnd(buildNlistEvent,0,0,0,0); \
00118 return ;
00119
00120 #define PROF_BUILD_NLIST_COMM_BEGIN PetscLogEventBegin(buildNlistCommEvent,0,0,0,0);
00121 #define PROF_BUILD_NLIST_COMM_END PetscLogEventEnd(buildNlistCommEvent,0,0,0,0);
00122
00123 #define PROF_BUILD_DA_STAGE1_BEGIN PetscLogEventBegin(buildDAstage1Event,0,0,0,0);
00124 #define PROF_BUILD_DA_STAGE1_END PetscLogEventEnd(buildDAstage1Event,0,0,0,0);
00125
00126 #define PROF_BUILD_DA_STAGE2_BEGIN PetscLogEventBegin(buildDAstage2Event,0,0,0,0);
00127 #define PROF_BUILD_DA_STAGE2_END PetscLogEventEnd(buildDAstage2Event,0,0,0,0);
00128
00129 #define PROF_BUILD_DA_STAGE3_BEGIN PetscLogEventBegin(buildDAstage3Event,0,0,0,0);
00130 #define PROF_BUILD_DA_STAGE3_END PetscLogEventEnd(buildDAstage3Event,0,0,0,0);
00131
00132 #define PROF_BUILD_DA_STAGE4_BEGIN PetscLogEventBegin(buildDAstage4Event,0,0,0,0);
00133 #define PROF_BUILD_DA_STAGE4_END PetscLogEventEnd(buildDAstage4Event,0,0,0,0);
00134
00135 #define PROF_BUILD_DA_STAGE5_BEGIN PetscLogEventBegin(buildDAstage5Event,0,0,0,0);
00136 #define PROF_BUILD_DA_STAGE5_END PetscLogEventEnd(buildDAstage5Event,0,0,0,0);
00137
00138 #define PROF_BUILD_DA_STAGE6_BEGIN PetscLogEventBegin(buildDAstage6Event,0,0,0,0);
00139 #define PROF_BUILD_DA_STAGE6_END PetscLogEventEnd(buildDAstage6Event,0,0,0,0);
00140
00141 #define PROF_BUILD_DA_STAGE7_BEGIN PetscLogEventBegin(buildDAstage7Event,0,0,0,0);
00142 #define PROF_BUILD_DA_STAGE7_END PetscLogEventEnd(buildDAstage7Event,0,0,0,0);
00143
00144 #define PROF_BUILD_DA_STAGE8_BEGIN PetscLogEventBegin(buildDAstage8Event,0,0,0,0);
00145 #define PROF_BUILD_DA_STAGE8_END PetscLogEventEnd(buildDAstage8Event,0,0,0,0);
00146
00147 #define PROF_BUILD_DA_STAGE9_BEGIN PetscLogEventBegin(buildDAstage9Event,0,0,0,0);
00148 #define PROF_BUILD_DA_STAGE9_END PetscLogEventEnd(buildDAstage9Event,0,0,0,0);
00149
00150 #else
00151
00152 #define PROF_READ_GHOST_NODES_BEGIN_BEGIN
00153 #define PROF_READ_GHOST_NODES_BEGIN_END return 0;
00154
00155 #define PROF_READ_GHOST_NODES_END_BEGIN
00156 #define PROF_READ_GHOST_NODES_END_END return 0;
00157
00158 #define PROF_READ_GHOST_ELEMS_BEGIN_BEGIN
00159 #define PROF_READ_GHOST_ELEMS_BEGIN_END return 0;
00160
00161 #define PROF_READ_GHOST_ELEMS_END_BEGIN
00162 #define PROF_READ_GHOST_ELEMS_END_END return 0;
00163
00164 #define PROF_WRITE_GHOST_NODES_BEGIN_BEGIN
00165 #define PROF_WRITE_GHOST_NODES_BEGIN_END return 0;
00166
00167 #define PROF_WRITE_GHOST_NODES_END_BEGIN
00168 #define PROF_WRITE_GHOST_NODES_END_END return 0;
00169
00170 #define PROF_WRITE_GHOST_ELEMS_BEGIN_BEGIN
00171 #define PROF_WRITE_GHOST_ELEMS_BEGIN_END return 0;
00172
00173 #define PROF_WRITE_GHOST_ELEMS_END_BEGIN
00174 #define PROF_WRITE_GHOST_ELEMS_END_END return 0;
00175
00176 #define PROF_SET_MAT_VALUES_BEGIN
00177 #define PROF_SET_MAT_VALUES_END return 0;
00178
00179 #define PROF_BUILD_DA_BEGIN
00180 #define PROF_BUILD_DA_END return;
00181
00182 #define PROF_BUILD_NLIST_BEGIN
00183 #define PROF_BUILD_NLIST_END return ;
00184
00185 #define PROF_BUILD_NLIST_COMM_BEGIN
00186 #define PROF_BUILD_NLIST_COMM_END
00187
00188 #define PROF_BUILD_DA_STAGE1_BEGIN
00189 #define PROF_BUILD_DA_STAGE1_END
00190
00191 #define PROF_BUILD_DA_STAGE2_BEGIN
00192 #define PROF_BUILD_DA_STAGE2_END
00193
00194 #define PROF_BUILD_DA_STAGE3_BEGIN
00195 #define PROF_BUILD_DA_STAGE3_END
00196
00197 #define PROF_BUILD_DA_STAGE4_BEGIN
00198 #define PROF_BUILD_DA_STAGE4_END
00199
00200 #define PROF_BUILD_DA_STAGE5_BEGIN
00201 #define PROF_BUILD_DA_STAGE5_END
00202
00203 #define PROF_BUILD_DA_STAGE6_BEGIN
00204 #define PROF_BUILD_DA_STAGE6_END
00205
00206 #define PROF_BUILD_DA_STAGE7_BEGIN
00207 #define PROF_BUILD_DA_STAGE7_END
00208
00209 #define PROF_BUILD_DA_STAGE8_BEGIN
00210 #define PROF_BUILD_DA_STAGE8_END
00211
00212 #define PROF_BUILD_DA_STAGE9_BEGIN
00213 #define PROF_BUILD_DA_STAGE9_END
00214
00215 #endif
00216
00217 namespace ot {
00218
00219
00220 class TreeNode;
00221
00310 class DA {
00311
00312 protected:
00313
00314 LoopCounters m_lcLoopInfo;
00315
00316
00317 unsigned char* m_ucpOctLevels;
00318 bool m_bCompressLut;
00319 bool m_bComputedLocalToGlobal;
00320 bool m_bComputedLocalToGlobalElems;
00321 bool m_bIamActive;
00322 unsigned int m_uiInputSize;
00323 std::vector<ot::TreeNode> m_tnBlocks;
00324 std::vector<ot::TreeNode> m_tnMinAllBlocks;
00325 std::vector<unsigned int> m_uiNlist;
00326 unsigned int* m_uiNlistPtr;
00327 DendroIntL* m_dilpLocalToGlobal;
00328 DendroIntL* m_dilpLocalToGlobalElems;
00329
00330
00331
00332 std::vector<unsigned char> m_ucpLutRemainders;
00333 unsigned char* m_ucpLutRemaindersPtr;
00334 std::vector<unsigned char> m_ucpSortOrders;
00335 unsigned char* m_ucpSortOrdersPtr;
00336
00337
00338
00339 std::vector<unsigned short> m_uspLutQuotients;
00340 unsigned short* m_uspLutQuotientsPtr;
00341
00342
00343
00344
00345 std::vector<unsigned char> m_ucpLutMasks;
00346 unsigned char* m_ucpLutMasksPtr;
00347
00348 std::vector<unsigned char> m_ucpPreGhostConnectivity;
00349 unsigned char* m_ucpPreGhostConnectivityPtr;
00350
00351 std::vector<Point> m_ptsPreGhostOffsets;
00352 Point* m_ptsPreGhostOffsetsPtr;
00353
00354 std::vector<Point> PreGhostAnchors;
00355
00356
00357
00358
00359 unsigned int m_uiQuotientCounter;
00360 unsigned int m_uiPreGhostQuotientCnt;
00361
00362
00363 unsigned int m_uiElementQuotient;
00364 unsigned int m_uiIndependentElementQuotient;
00365
00366
00367
00368 Point m_ptGhostedOffset;
00369
00370
00371
00372 Point m_ptOffset;
00373
00374 Point m_ptIndependentOffset;
00375 Point m_ptCurrentOffset;
00376
00377
00378
00379
00380 unsigned int m_uiNodeSize;
00381 unsigned int m_uiBoundaryNodeSize;
00382
00383 unsigned int m_uiElementSize;
00384
00385 unsigned int m_uiIndependentElementSize;
00386
00387
00388 unsigned int m_uiPreGhostElementSize;
00389
00390
00391
00392 unsigned int m_uiPreGhostNodeSize;
00393 unsigned int m_uiPreGhostBoundaryNodeSize;
00394
00395
00396 unsigned int m_uiPostGhostNodeSize;
00397
00398 unsigned int m_uiLocalBufferSize;
00399
00400
00401 unsigned int m_uiElementBegin;
00402 unsigned int m_uiElementEnd;
00403 unsigned int m_uiPostGhostBegin;
00404
00405 unsigned int m_uiIndependentElementBegin;
00406 unsigned int m_uiIndependentElementEnd;
00407
00408 unsigned int m_uiCurrent;
00409
00410
00411
00412 unsigned int m_uiDimension;
00413
00414
00415 unsigned int m_uiMaxDepth;
00416
00417
00418 MPI_Comm m_mpiCommAll;
00419 int m_iRankAll;
00420 int m_iNpesAll;
00421 MPI_Comm m_mpiCommActive;
00422 int m_iRankActive;
00423 int m_iNpesActive;
00424
00425
00426 std::vector<unsigned int> m_uipScatterMap;
00427 std::vector<unsigned int> m_uipSendOffsets;
00428 std::vector<unsigned int> m_uipSendProcs;
00429 std::vector<unsigned int> m_uipSendCounts;
00430
00431 std::vector<unsigned int> m_uipElemScatterMap;
00432 std::vector<unsigned int> m_uipElemSendOffsets;
00433 std::vector<unsigned int> m_uipElemSendProcs;
00434 std::vector<unsigned int> m_uipElemSendCounts;
00435
00436 std::vector<unsigned int> m_uipRecvOffsets;
00437 std::vector<unsigned int> m_uipRecvProcs;
00438 std::vector<unsigned int> m_uipRecvCounts;
00439
00440 std::vector<unsigned int> m_uipElemRecvOffsets;
00441 std::vector<unsigned int> m_uipElemRecvProcs;
00442 std::vector<unsigned int> m_uipElemRecvCounts;
00443
00444
00445 std::vector<updateContext> m_mpiContexts;
00446 unsigned int m_uiCommTag;
00447
00448 public:
00449
00454 std::vector<ot::TreeNode> getBlocks();
00455
00460 unsigned int getNumBlocks();
00461
00467 std::vector<ot::TreeNode> getMinAllBlocks();
00468
00473 unsigned int getInputSize();
00474
00479
00500 DA(std::vector<ot::TreeNode> &in, MPI_Comm comm, MPI_Comm inputActiveComm,
00501 bool compressLut = false, const std::vector<ot::TreeNode> *blocksPtr = NULL,
00502 bool *iAmActive = NULL);
00503
00512 DA(unsigned int dummy, std::vector<ot::TreeNode> &in, MPI_Comm comm,
00513 MPI_Comm inputActiveComm, bool compressLut = false,
00514 const std::vector<ot::TreeNode> *blocksPtr = NULL, bool *iAmActive = NULL);
00515
00520 ~DA();
00522
00527
00533 MPI_Comm getComm();
00534
00540 MPI_Comm getCommActive();
00541
00546 int getNpesAll();
00547
00552 int getNpesActive();
00553
00559 int getRankAll();
00560
00565 int getRankActive();
00566
00577 Point getCurrentOffset();
00578
00589 Point getGhostedOffset();
00590
00601 Point getOffset();
00602
00613 Point getNextOffset(Point p, unsigned char d);
00614
00621 void incrementCurrentOffset();
00622
00630 void incrementPreGhostOffset();
00631
00643 bool isBoundaryOctant(unsigned char *flags=NULL);
00644
00651 DendroIntL* getLocalToGlobalMap();
00652
00659 DendroIntL* getLocalToGlobalElemsMap();
00660
00668 unsigned int getNodeSize();
00669
00676 unsigned int getBoundaryNodeSize();
00677
00684 unsigned int getInternalNodeSize();
00685
00692 unsigned int getElementSize();
00693
00698 unsigned int getPreGhostElementSize();
00699
00705 unsigned int getIndependentSize();
00706
00714 unsigned int getGhostedNodeSize();
00715
00722 unsigned int getGhostedElementSize();
00723
00728 unsigned int getIdxElementBegin();
00729
00734 unsigned int getIdxElementEnd();
00735
00740 unsigned int getIdxPostGhostBegin();
00741
00751 unsigned int getMaxDepth();
00752
00758 unsigned int getDimension();
00759
00761
00766
00781
00782 template <typename T>
00783 int ReadFromGhostsBegin ( T* arr, unsigned int dof=1);
00784
00790 template <typename T>
00791 int ReadFromGhostsEnd(T* arr);
00792
00793
00799 template <typename T>
00800 int WriteToGhostsBegin ( T* arr, unsigned int dof=1);
00801
00806 template <typename T>
00807 int WriteToGhostsEnd(T* arr, unsigned int dof=1);
00808
00809
00815 template <typename T>
00816 int ReadFromGhostElemsBegin ( T* arr, unsigned int dof=1);
00817
00823 template <typename T>
00824 int ReadFromGhostElemsEnd(T* arr);
00825
00831 template <typename T>
00832 int WriteToGhostElemsBegin ( T* arr, unsigned int dof=1);
00833
00839 template <typename T>
00840 int WriteToGhostElemsEnd(T* arr, unsigned int dof=1);
00841
00843
00848
00859 int createVector(Vec &local, bool isElemental, bool isGhosted, unsigned int dof=1);
00860
00866 int createActiveVector(Vec &local, bool isElemental, bool isGhosted, unsigned int dof=1);
00867
00875 int createMatrix(Mat &M, MatType mtype, unsigned int dof=1);
00876
00882 int createActiveMatrix(Mat &M, MatType mtype, unsigned int dof=1);
00883
00892 int computeLocalToGlobalMappings();
00893
00900 int computeLocalToGlobalElemMappings();
00901
00907 bool computedLocalToGlobal();
00908
00914 bool computedLocalToGlobalElems();
00915
00933 int setValuesInMatrix(Mat mat, std::vector<ot::MatRecord>& records,
00934 unsigned int dof, InsertMode mode);
00935
00948 template <typename T>
00949 int createVector(std::vector<T> &local, bool isElemental,
00950 bool isGhosted, unsigned int dof=1);
00951
00975 int vecGetBuffer(Vec in, PetscScalar* &out, bool isElemental,
00976 bool isGhosted, bool isReadOnly, unsigned int dof=1);
00977
00998 template < typename T >
00999 int vecGetBuffer(std::vector<T> &in, T* &out, bool isElemental,
01000 bool isGhosted, bool isReadOnly, unsigned int dof=1);
01001
01018 int vecRestoreBuffer(Vec in, PetscScalar* out, bool isElemental,
01019 bool isGhosted, bool isReadOnly, unsigned int dof=1);
01020
01037 template < typename T >
01038 int vecRestoreBuffer(std::vector<T> &in, T* out, bool isElemental,
01039 bool isGhosted, bool isReadOnly, unsigned int dof=1);
01041
01042
01043
01048
01069 template<ot::DA_FLAGS::loopType type>
01070 void init();
01071
01093 unsigned int curr();
01094
01106 unsigned int currWithInfo();
01107
01129 template<ot::DA_FLAGS::loopType type>
01130 unsigned int end();
01131
01153 template<ot::DA_FLAGS::loopType type>
01154 unsigned int next();
01155
01161 unsigned char getChildNumber();
01162
01169 unsigned char getFlag(unsigned int i);
01170
01176 bool isHanging(unsigned int i);
01177
01183 bool isGhost(unsigned int i);
01184
01190 bool isNode(unsigned int i);
01191
01200 unsigned char getHangingNodeIndex(unsigned int i);
01201
01215 unsigned char getTypeMask(unsigned int i);
01216
01226 unsigned char getLevel(unsigned int i);
01227
01237 int getNodeIndices(unsigned int* nodes);
01239
01244 unsigned int getLocalBufferSize();
01245
01246
01253 void updateQuotientCounter();
01254
01259 bool isLUTcompressed();
01260
01265 bool iAmActive();
01266
01267 protected:
01268
01277 void buildNodeList(std::vector<ot::TreeNode> &in);
01278
01284 void DA_FactoryPart0(std::vector<ot::TreeNode>& in, MPI_Comm comm,
01285 MPI_Comm activeInputComm, bool compressLut, bool* iAmActive);
01286
01287 void DA_FactoryPart1(std::vector<ot::TreeNode>& in);
01288
01289 void DA_FactoryPart2(std::vector<ot::TreeNode>& in);
01290
01291 void DA_FactoryPart3(std::vector<ot::TreeNode>& in, MPI_Comm comm, bool compressLut,
01292 const std::vector<ot::TreeNode>* blocksPtr, bool* iAmActive);
01293
01294 };
01295
01296 }
01297
01298 #include "oda.txx"
01299
01300 #endif
01301
01302