mpi_node.hpp 4.6 KB

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