fmm_pts.hpp 8.0 KB

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