fmm_pts.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * \file fmm_pts.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 3-07-2011
  5. * \brief This file contains the definition of the FMM_Pts class.
  6. * This handles all the translations for point sources and targets.
  7. */
  8. #include <mpi.h>
  9. #include <string>
  10. #include <vector>
  11. #include <cstdlib>
  12. #include <pvfmm_common.hpp>
  13. #include <interac_list.hpp>
  14. #include <precomp_mat.hpp>
  15. #include <fft_wrapper.hpp>
  16. #include <mpi_tree.hpp>
  17. #include <mpi_node.hpp>
  18. #include <mem_mgr.hpp>
  19. #include <vector.hpp>
  20. #include <matrix.hpp>
  21. #include <kernel.hpp>
  22. #ifndef _PVFMM_FMM_PTS_HPP_
  23. #define _PVFMM_FMM_PTS_HPP_
  24. namespace pvfmm{
  25. /**
  26. * \brief This class contains FMM specific data that each node contains
  27. * along with the functions for manipulating the data.
  28. */
  29. template <class Real_t>
  30. class FMM_Data{
  31. public:
  32. virtual ~FMM_Data(){}
  33. virtual FMM_Data* NewData(){return mem::aligned_new<FMM_Data>();}
  34. /**
  35. * \brief Clear all data.
  36. */
  37. virtual void Clear();
  38. /**
  39. * \brief Pack multipole expansion.
  40. */
  41. virtual PackedData PackMultipole(void* buff_ptr=NULL);
  42. /**
  43. * \brief Add the multipole expansion from p0 to the current multipole
  44. * expansion.
  45. */
  46. virtual void AddMultipole(PackedData p0);
  47. /**
  48. * \brief Initialize multipole expansion using p0.
  49. */
  50. virtual void InitMultipole(PackedData p0, bool own_data=true);
  51. //FMM specific node data.
  52. Vector<Real_t> upward_equiv;
  53. Vector<Real_t> dnward_equiv;
  54. };
  55. template <class Real_t>
  56. struct SetupData{
  57. int level;
  58. const Kernel<Real_t>* kernel;
  59. std::vector<Mat_Type> interac_type;
  60. std::vector<void*> nodes_in ;
  61. std::vector<void*> nodes_out;
  62. std::vector<Vector<Real_t>*> input_vector;
  63. std::vector<Vector<Real_t>*> output_vector;
  64. //#####################################################
  65. Matrix< char> interac_data;
  66. Matrix< char>* precomp_data;
  67. Matrix<Real_t>* coord_data;
  68. Matrix<Real_t>* input_data;
  69. Matrix<Real_t>* output_data;
  70. };
  71. template <class FMM_Mat_t>
  72. class FMM_Tree;
  73. template <class FMMNode>
  74. class FMM_Pts{
  75. public:
  76. typedef FMM_Tree<FMM_Pts<FMMNode> > FMMTree_t;
  77. typedef typename FMMNode::Real_t Real_t;
  78. typedef FMMNode FMMNode_t;
  79. class FMMData: public FMM_Data<Real_t>{
  80. public:
  81. virtual ~FMMData(){}
  82. virtual FMM_Data<Real_t>* NewData(){return mem::aligned_new<FMMData>();}
  83. };
  84. /**
  85. * \brief Constructor.
  86. */
  87. FMM_Pts(mem::MemoryManager* mem_mgr_=NULL): mem_mgr(mem_mgr_),
  88. vprecomp_fft_flag(false), vlist_fft_flag(false),
  89. vlist_ifft_flag(false), mat(NULL), kernel(NULL){};
  90. /**
  91. * \brief Virtual destructor.
  92. */
  93. virtual ~FMM_Pts();
  94. /**
  95. * \brief Initialize all the translation matrices (or load from file).
  96. * \param[in] mult_order Order of multipole expansion.
  97. * \param[in] kernel Kernel functions and related data.
  98. */
  99. void Initialize(int mult_order, const MPI_Comm& comm, const Kernel<Real_t>* kernel);
  100. /**
  101. * \brief Order for the multipole expansion.
  102. */
  103. int MultipoleOrder(){return multipole_order;}
  104. /**
  105. * \brief Whether using scale-invariant kernel?
  106. */
  107. bool ScaleInvar(){return kernel->scale_invar;}
  108. virtual void CollectNodeData(FMMTree_t* tree, std::vector<FMMNode*>& nodes, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, std::vector<std::vector<Vector<Real_t>* > > vec_list = std::vector<std::vector<Vector<Real_t>* > >(0));
  109. void SetupPrecomp(SetupData<Real_t>& setup_data, bool device=false);
  110. void SetupInterac(SetupData<Real_t>& setup_data, bool device=false);
  111. template <int SYNC=__DEVICE_SYNC__>
  112. void EvalList (SetupData<Real_t>& setup_data, bool device=false); // Run on CPU by default.
  113. void PtSetup(SetupData<Real_t>& setup_data, void* data_);
  114. template <int SYNC=__DEVICE_SYNC__>
  115. void EvalListPts(SetupData<Real_t>& setup_data, bool device=false); // Run on CPU by default.
  116. /**
  117. * \brief Initialize multipole expansions for the given array of leaf nodes
  118. * at a given level.
  119. */
  120. virtual void Source2UpSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  121. virtual void Source2Up (SetupData<Real_t>& setup_data, bool device=false);
  122. /**
  123. * \brief Initialize multipole expansions for the given array of non-leaf
  124. * nodes from that of its children.
  125. */
  126. virtual void Up2UpSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  127. virtual void Up2Up (SetupData<Real_t>& setup_data, bool device=false);
  128. virtual void PeriodicBC(FMMNode* node);
  129. /**
  130. * \brief Compute V-List intractions.
  131. */
  132. virtual void V_ListSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  133. virtual void V_List (SetupData<Real_t>& setup_data, bool device=false);
  134. /**
  135. * \brief Compute X-List intractions.
  136. */
  137. virtual void X_ListSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  138. virtual void X_List (SetupData<Real_t>& setup_data, bool device=false);
  139. /**
  140. * \brief Compute contribution of local expansion from the parent.
  141. */
  142. virtual void Down2DownSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  143. virtual void Down2Down (SetupData<Real_t>& setup_data, bool device=false);
  144. /**
  145. * \brief Compute target potential from the local expansion.
  146. */
  147. virtual void Down2TargetSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  148. virtual void Down2Target (SetupData<Real_t>& setup_data, bool device=false);
  149. /**
  150. * \brief Compute W-List intractions.
  151. */
  152. virtual void W_ListSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  153. virtual void W_List (SetupData<Real_t>& setup_data, bool device=false);
  154. /**
  155. * \brief Compute U-List intractions.
  156. */
  157. virtual void U_ListSetup(SetupData<Real_t>& setup_data, FMMTree_t* tree, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  158. virtual void U_List (SetupData<Real_t>& setup_data, bool device=false);
  159. virtual void PostProcessing(FMMTree_t* tree, std::vector<FMMNode_t*>& nodes, BoundaryType bndry=FreeSpace);
  160. /**
  161. * \brief For each node, copy FMM output from FMM_Data to the node.
  162. */
  163. virtual void CopyOutput(FMMNode** nodes, size_t n);
  164. Vector<char> dev_buffer;
  165. Vector<char> staging_buffer;
  166. protected:
  167. virtual void PrecompAll(Mat_Type type, int level=-1);
  168. virtual Permutation<Real_t>& PrecompPerm(Mat_Type type, Perm_Type perm_indx);
  169. virtual Matrix<Real_t>& Precomp(int level, Mat_Type type, size_t mat_indx);
  170. typename FFTW_t<Real_t>::plan vprecomp_fftplan; bool vprecomp_fft_flag;
  171. void FFT_UpEquiv(size_t dof, size_t m, size_t ker_dim0, Vector<size_t>& fft_vec, Vector<Real_t>& fft_scl,
  172. Vector<Real_t>& input_data, Vector<Real_t>& output_data, Vector<Real_t>& buffer_);
  173. typename FFTW_t<Real_t>::plan vlist_fftplan; bool vlist_fft_flag;
  174. void FFT_Check2Equiv(size_t dof, size_t m, size_t ker_dim0, Vector<size_t>& ifft_vec, Vector<Real_t>& ifft_scl,
  175. Vector<Real_t>& input_data, Vector<Real_t>& output_data, Vector<Real_t>& buffer_);
  176. typename FFTW_t<Real_t>::plan vlist_ifftplan; bool vlist_ifft_flag;
  177. mem::MemoryManager* mem_mgr;
  178. InteracList<FMMNode> interac_list;
  179. const Kernel<Real_t>* kernel; //The kernel function.
  180. PrecompMat<Real_t>* mat; //Handles storage of matrices.
  181. std::string mat_fname;
  182. int multipole_order; //Order of multipole expansion.
  183. MPI_Comm comm;
  184. };
  185. }//end namespace
  186. #include <fmm_pts.txx>
  187. #endif //_PVFMM_FMM_PTS_HPP_