mpi_node.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * \file mpi_node.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 12-10-2010
  5. * \brief This file contains the definition of a virtual base class for a
  6. * locally essential tree node.
  7. */
  8. #ifndef _PVFMM_MPI_NODE_HPP_
  9. #define _PVFMM_MPI_NODE_HPP_
  10. #include <pvfmm_common.hpp>
  11. #include <assert.h>
  12. #include <tree_node.hpp>
  13. #include <mortonid.hpp>
  14. #include <vector.hpp>
  15. namespace pvfmm{
  16. /**
  17. * \brief A structure for storing packed data for transmitting a node to
  18. * another MPI process.
  19. */
  20. struct PackedData{
  21. size_t length;//Length of data
  22. void* data; //Pointer to data
  23. };
  24. /**
  25. * \brief Virtual base class for a locally essential tree node.
  26. */
  27. template <class T>
  28. class MPI_Node: public TreeNode{
  29. public:
  30. typedef T Real_t;
  31. /**
  32. * \brief Base class for node data. Contains initialization data for the node.
  33. */
  34. class NodeData: public TreeNode::NodeData{
  35. public:
  36. size_t max_pts;
  37. Vector<Real_t> pt_coord;
  38. Vector<Real_t> pt_value;
  39. };
  40. /**
  41. * \brief Initialize.
  42. */
  43. MPI_Node(): TreeNode(){ghost=false; weight=1;}
  44. /**
  45. * \brief Virtual destructor.
  46. */
  47. virtual ~MPI_Node();
  48. /**
  49. * \brief Initialize the node with relevant data.
  50. */
  51. virtual void Initialize(TreeNode* parent_, int path2node_, TreeNode::NodeData*) ;
  52. /**
  53. * \brief Returns list of coordinate and value vectors which need to be
  54. * sorted and partitioned across MPI processes and the scatter index is
  55. * saved.
  56. */
  57. virtual void NodeDataVec(std::vector<Vector<Real_t>*>& coord,
  58. std::vector<Vector<Real_t>*>& value,
  59. std::vector<Vector<size_t>*>& scatter){
  60. coord .push_back(&pt_coord );
  61. value .push_back(&pt_value );
  62. scatter.push_back(&pt_scatter);
  63. }
  64. /**
  65. * \brief Clear node data.
  66. */
  67. virtual void ClearData();
  68. /**
  69. * \brief Returns the colleague corresponding to the input index.
  70. */
  71. MPI_Node<Real_t>* Colleague(int index){return colleague[index];}
  72. /**
  73. * \brief Set the colleague corresponding to the input index.
  74. */
  75. void SetColleague(MPI_Node<Real_t>* node_, int index){colleague[index]=node_;}
  76. /**
  77. * \brief Returns the cost of this node. Used for load balancing.
  78. */
  79. virtual long long& NodeCost(){return weight;}
  80. /**
  81. * \brief Returns an array of size dim containing the coordinates of the
  82. * node.
  83. */
  84. Real_t* Coord(){assert(coord!=NULL); return coord;}
  85. /**
  86. * \brief Determines if the node is a Ghost node or not.
  87. */
  88. bool IsGhost(){return ghost;}
  89. /**
  90. * \brief Sets the ghost flag of this node.
  91. */
  92. void SetGhost(bool x){ghost=x;}
  93. /**
  94. * \brief Gets Morton Id of this node.
  95. */
  96. inline MortonId GetMortonId();
  97. /**
  98. * \brief Sets the coordinates of this node using the given Morton Id.
  99. */
  100. inline void SetCoord(MortonId& mid);
  101. /**
  102. * \brief Allocate a new object of the same type (as the derived class) and
  103. * return a pointer to it type cast as (TreeNode*).
  104. */
  105. virtual TreeNode* NewNode(TreeNode* n_=NULL);
  106. /**
  107. * \brief Evaluates and returns the subdivision condition for this node.
  108. * 'true' if node requires further subdivision.
  109. */
  110. virtual bool SubdivCond();
  111. /**
  112. * \brief Create child nodes and Initialize them.
  113. */
  114. virtual void Subdivide();
  115. /**
  116. * \brief Truncates the tree i.e. makes this a leaf node.
  117. */
  118. virtual void Truncate();
  119. /**
  120. * \brief Pack this node to be transmitted to another process. The node
  121. * is responsible for allocating and freeing the memory for the actual data.
  122. */
  123. virtual PackedData Pack(bool ghost=false, void* buff_ptr=NULL, size_t offset=0);
  124. /**
  125. * \brief Initialize the node with data from another process.
  126. */
  127. virtual void Unpack(PackedData data, bool own_data=true);
  128. /**
  129. * \brief Read source distribution at points on a grid defined by array of x,
  130. * y and z coordinates.
  131. */
  132. virtual void ReadVal(std::vector<Real_t> x,std::vector<Real_t> y, std::vector<Real_t> z, Real_t* val, bool show_ghost=true);
  133. /**
  134. * \brief Append node VTU data to vectors.
  135. */
  136. virtual void VTU_Data(std::vector<Real_t>& coord, std::vector<Real_t>& value, std::vector<int32_t>& connect, std::vector<int32_t>& offset, std::vector<uint8_t>& types, int lod=-1);
  137. Vector<Real_t> pt_coord; //coordinates of points
  138. Vector<Real_t> pt_value; //value at points
  139. Vector<size_t> pt_scatter; //scatter index mapping original data.
  140. protected:
  141. bool ghost;
  142. size_t max_pts;
  143. long long weight;
  144. Real_t coord[COORD_DIM];
  145. MPI_Node<Real_t>* colleague[COLLEAGUE_COUNT];
  146. Vector<char> packed_data;
  147. };
  148. }//end namespace
  149. #include <mpi_node.txx>
  150. #endif //_PVFMM_MPI_NODE_HPP_