fmm_pts.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <mem_utils.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 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. 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 FMMNode>
  72. class FMM_Pts{
  73. public:
  74. typedef typename FMMNode::Real_t Real_t;
  75. typedef FMMNode FMMNode_t;
  76. class FMMData: public FMM_Data<Real_t>{
  77. public:
  78. virtual ~FMMData(){}
  79. virtual FMM_Data<Real_t>* NewData(){return new FMMData;}
  80. };
  81. /**
  82. * \brief Constructor.
  83. */
  84. FMM_Pts(mem::MemoryManager* mem_mgr_=NULL): mem_mgr(mem_mgr_),
  85. vprecomp_fft_flag(false), vlist_fft_flag(false),
  86. vlist_ifft_flag(false), mat(NULL){};
  87. /**
  88. * \brief Virtual destructor.
  89. */
  90. virtual ~FMM_Pts();
  91. /**
  92. * \brief Initialize all the translation matrices (or load from file).
  93. * \param[in] mult_order Order of multipole expansion.
  94. * \param[in] kernel Kernel functions and related data.
  95. */
  96. void Initialize(int mult_order, const MPI_Comm& comm, const Kernel<Real_t>* kernel, const Kernel<Real_t>* aux_kernel=NULL);
  97. /**
  98. * \brief Order for the multipole expansion.
  99. */
  100. int& MultipoleOrder(){return multipole_order;}
  101. /**
  102. * \brief Whether using homogeneous kernel?
  103. */
  104. bool& Homogen(){return kernel.homogen;}
  105. virtual void CollectNodeData(std::vector<FMMNode*>& nodes, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, std::vector<size_t> extra_size = std::vector<size_t>(0));
  106. void SetupPrecomp(SetupData<Real_t>& setup_data, bool device=false);
  107. void SetupInterac(SetupData<Real_t>& setup_data, bool device=false);
  108. template <int SYNC=__DEVICE_SYNC__>
  109. void EvalList (SetupData<Real_t>& setup_data, bool device=false); // Run on CPU by default.
  110. void SetupInteracPts(SetupData<Real_t>& setup_data, bool shift_src, bool shift_trg, Matrix<Real_t>* M, bool device);
  111. template <int SYNC=__DEVICE_SYNC__>
  112. void EvalListPts (SetupData<Real_t>& setup_data, bool device=false); // Run on CPU by default.
  113. /**
  114. * \brief Initialize multipole expansions for the given array of leaf nodes
  115. * at a given level.
  116. */
  117. virtual void Source2UpSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  118. virtual void Source2Up (SetupData<Real_t>& setup_data, bool device=false);
  119. /**
  120. * \brief Initialize multipole expansions for the given array of non-leaf
  121. * nodes from that of its children.
  122. */
  123. virtual void Up2UpSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  124. virtual void Up2Up (SetupData<Real_t>& setup_data, bool device=false);
  125. virtual void PeriodicBC(FMMNode* node);
  126. /**
  127. * \brief Compute V-List intractions.
  128. */
  129. virtual void V_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  130. virtual void V_List (SetupData<Real_t>& setup_data, bool device=false);
  131. /**
  132. * \brief Compute X-List intractions.
  133. */
  134. virtual void X_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  135. virtual void X_List (SetupData<Real_t>& setup_data, bool device=false);
  136. /**
  137. * \brief Compute contribution of local expansion from the parent.
  138. */
  139. virtual void Down2DownSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  140. virtual void Down2Down (SetupData<Real_t>& setup_data, bool device=false);
  141. /**
  142. * \brief Compute target potential from the local expansion.
  143. */
  144. virtual void Down2TargetSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  145. virtual void Down2Target (SetupData<Real_t>& setup_data, bool device=false);
  146. /**
  147. * \brief Compute W-List intractions.
  148. */
  149. virtual void W_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  150. virtual void W_List (SetupData<Real_t>& setup_data, bool device=false);
  151. /**
  152. * \brief Compute U-List intractions.
  153. */
  154. virtual void U_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& node_data, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device);
  155. virtual void U_List (SetupData<Real_t>& setup_data, bool device=false);
  156. virtual void PostProcessing(std::vector<FMMNode_t*>& nodes);
  157. /**
  158. * \brief For each node, copy FMM output from FMM_Data to the node.
  159. */
  160. virtual void CopyOutput(FMMNode** nodes, size_t n);
  161. Vector<char> dev_buffer;
  162. Vector<char> cpu_buffer;
  163. protected:
  164. virtual void PrecompAll(Mat_Type type, int level=-1);
  165. virtual Permutation<Real_t>& PrecompPerm(Mat_Type type, Perm_Type perm_indx);
  166. virtual Matrix<Real_t>& Precomp(int level, Mat_Type type, size_t mat_indx);
  167. typename FFTW_t<Real_t>::plan vprecomp_fftplan; bool vprecomp_fft_flag;
  168. void FFT_UpEquiv(size_t dof, size_t m, size_t ker_dim0, Vector<size_t>& fft_vec,
  169. Vector<Real_t>& input_data, Vector<Real_t>& output_data, Vector<Real_t>& buffer_);
  170. typename FFTW_t<Real_t>::plan vlist_fftplan; bool vlist_fft_flag;
  171. void FFT_Check2Equiv(size_t dof, size_t m, size_t ker_dim0, Vector<size_t>& ifft_vec,
  172. Vector<Real_t>& input_data, Vector<Real_t>& output_data, Vector<Real_t>& buffer_, Matrix<Real_t>& M);
  173. typename FFTW_t<Real_t>::plan vlist_ifftplan; bool vlist_ifft_flag;
  174. std::vector<Vector<Real_t> > upwd_check_surf;
  175. std::vector<Vector<Real_t> > upwd_equiv_surf;
  176. std::vector<Vector<Real_t> > dnwd_check_surf;
  177. std::vector<Vector<Real_t> > dnwd_equiv_surf;
  178. mem::MemoryManager* mem_mgr;
  179. InteracList<FMMNode> interac_list;
  180. Kernel<Real_t> kernel; //The kernel function.
  181. Kernel<Real_t> aux_kernel; //Auxiliary kernel for source-to-source translations.
  182. PrecompMat<Real_t>* mat; //Handles storage of matrices.
  183. std::string mat_fname;
  184. int multipole_order; //Order of multipole expansion.
  185. MPI_Comm comm;
  186. };
  187. }//end namespace
  188. #include <fmm_pts.txx>
  189. #endif //_PVFMM_FMM_PTS_HPP_