mpi_node.hpp 4.6 KB

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