fmm_pts.hpp 8.0 KB

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