kernel.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * \file kernel.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 12-20-2011
  5. * \brief This file contains the definition of the struct Kernel and also the
  6. * implementation of various kernels for FMM.
  7. */
  8. #include <string>
  9. #include <cstdlib>
  10. #include <pvfmm_common.hpp>
  11. #include <mem_mgr.hpp>
  12. #include <vector.hpp>
  13. #include <matrix.hpp>
  14. #ifndef _PVFMM_FMM_KERNEL_HPP_
  15. #define _PVFMM_FMM_KERNEL_HPP_
  16. namespace pvfmm{
  17. template <class T>
  18. struct Kernel{
  19. public:
  20. /**
  21. * \brief Evaluate potential due to source points at target coordinates.
  22. * \param[in] r_src Coordinates of source points.
  23. * \param[in] src_cnt Number of source points.
  24. * \param[in] v_src Strength of source points.
  25. * \param[in] r_trg Coordinates of target points.
  26. * \param[in] trg_cnt Number of target points.
  27. * \param[out] k_out Output array with potential values.
  28. */
  29. typedef void (*Ker_t)(T* r_src, int src_cnt, T* v_src, int dof,
  30. T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr);
  31. /**
  32. * \brief Volume potential solution for a constant density f
  33. * \param[in] coord Coordinates of target points.
  34. * \param[in] n Number of target points.
  35. * \param[out] out Elements of a matrix M of size (ker_dim0 x n*ker_dim1),
  36. * such that fxM gives the target potential.
  37. */
  38. typedef void (*VolPoten)(const T* coord, int n, T* out);
  39. /**
  40. * \brief Constructor.
  41. */
  42. Kernel(Ker_t poten, Ker_t dbl_poten, const char* name, int dim_, std::pair<int,int> k_dim,
  43. size_t dev_poten=(size_t)NULL, size_t dev_dbl_poten=(size_t)NULL);
  44. /**
  45. * \brief Initialize the kernel.
  46. */
  47. void Initialize(bool verbose=false) const;
  48. /**
  49. * \brief Compute the transformation matrix (on the source strength vector)
  50. * to get potential at target coordinates due to sources at the given
  51. * coordinates.
  52. * \param[in] r_src Coordinates of source points.
  53. * \param[in] src_cnt Number of source points.
  54. * \param[in] r_trg Coordinates of target points.
  55. * \param[in] trg_cnt Number of target points.
  56. * \param[out] k_out Output array with potential values.
  57. */
  58. void BuildMatrix(T* r_src, int src_cnt,
  59. T* r_trg, int trg_cnt, T* k_out) const;
  60. int dim;
  61. int ker_dim[2];
  62. std::string ker_name;
  63. Ker_t ker_poten;
  64. Ker_t dbl_layer_poten;
  65. size_t dev_ker_poten;
  66. size_t dev_dbl_layer_poten;
  67. mutable bool init;
  68. mutable bool scale_invar;
  69. mutable Vector<T> src_scal;
  70. mutable Vector<T> trg_scal;
  71. mutable Vector<Permutation<T> > perm_vec;
  72. mutable const Kernel<T>* k_s2m;
  73. mutable const Kernel<T>* k_s2l;
  74. mutable const Kernel<T>* k_s2t;
  75. mutable const Kernel<T>* k_m2m;
  76. mutable const Kernel<T>* k_m2l;
  77. mutable const Kernel<T>* k_m2t;
  78. mutable const Kernel<T>* k_l2l;
  79. mutable const Kernel<T>* k_l2t;
  80. mutable VolPoten vol_poten;
  81. private:
  82. Kernel();
  83. };
  84. template<typename T, void (*A)(T*, int, T*, int, T*, int, T*, mem::MemoryManager* mem_mgr),
  85. void (*B)(T*, int, T*, int, T*, int, T*, mem::MemoryManager* mem_mgr)>
  86. Kernel<T> BuildKernel(const char* name, int dim, std::pair<int,int> k_dim,
  87. const Kernel<T>* k_s2m=NULL, const Kernel<T>* k_s2l=NULL, const Kernel<T>* k_s2t=NULL,
  88. const Kernel<T>* k_m2m=NULL, const Kernel<T>* k_m2l=NULL, const Kernel<T>* k_m2t=NULL,
  89. const Kernel<T>* k_l2l=NULL, const Kernel<T>* k_l2t=NULL, typename Kernel<T>::VolPoten vol_poten=NULL, bool scale_invar_=true){
  90. size_t dev_ker_poten ;
  91. size_t dev_dbl_layer_poten;
  92. #ifdef __INTEL_OFFLOAD
  93. #pragma offload target(mic:0)
  94. #endif
  95. {
  96. dev_ker_poten =(size_t)((typename Kernel<T>::Ker_t)A);
  97. dev_dbl_layer_poten=(size_t)((typename Kernel<T>::Ker_t)B);
  98. }
  99. Kernel<T> K(A, B, name, dim, k_dim,
  100. dev_ker_poten, dev_dbl_layer_poten);
  101. K.k_s2m=k_s2m;
  102. K.k_s2l=k_s2l;
  103. K.k_s2t=k_s2t;
  104. K.k_m2m=k_m2m;
  105. K.k_m2l=k_m2l;
  106. K.k_m2t=k_m2t;
  107. K.k_l2l=k_l2l;
  108. K.k_l2t=k_l2t;
  109. K.vol_poten=vol_poten;
  110. K.scale_invar=scale_invar_;
  111. return K;
  112. }
  113. template<typename T, void (*A)(T*, int, T*, int, T*, int, T*, mem::MemoryManager* mem_mgr)>
  114. Kernel<T> BuildKernel(const char* name, int dim, std::pair<int,int> k_dim,
  115. const Kernel<T>* k_s2m=NULL, const Kernel<T>* k_s2l=NULL, const Kernel<T>* k_s2t=NULL,
  116. const Kernel<T>* k_m2m=NULL, const Kernel<T>* k_m2l=NULL, const Kernel<T>* k_m2t=NULL,
  117. const Kernel<T>* k_l2l=NULL, const Kernel<T>* k_l2t=NULL, typename Kernel<T>::VolPoten vol_poten=NULL, bool scale_invar_=true){
  118. size_t dev_ker_poten ;
  119. #ifdef __INTEL_OFFLOAD
  120. #pragma offload target(mic:0)
  121. #endif
  122. {
  123. dev_ker_poten =(size_t)((typename Kernel<T>::Ker_t)A);
  124. }
  125. Kernel<T> K(A, NULL, name, dim, k_dim,
  126. dev_ker_poten, (size_t)NULL);
  127. K.k_s2m=k_s2m;
  128. K.k_s2l=k_s2l;
  129. K.k_s2t=k_s2t;
  130. K.k_m2m=k_m2m;
  131. K.k_m2l=k_m2l;
  132. K.k_m2t=k_m2t;
  133. K.k_l2l=k_l2l;
  134. K.k_l2t=k_l2t;
  135. K.vol_poten=vol_poten;
  136. K.scale_invar=scale_invar_;
  137. return K;
  138. }
  139. }//end namespace
  140. #ifdef __INTEL_OFFLOAD
  141. #pragma offload_attribute(push,target(mic))
  142. #endif
  143. namespace pvfmm{ // Predefined Kernel-functions
  144. template<class T>
  145. struct LaplaceKernel{
  146. inline static const Kernel<T>& potential();
  147. inline static const Kernel<T>& gradient();
  148. };
  149. template<class T>
  150. struct StokesKernel{
  151. inline static const Kernel<T>& velocity();
  152. inline static const Kernel<T>& pressure();
  153. inline static const Kernel<T>& stress ();
  154. inline static const Kernel<T>& vel_grad();
  155. };
  156. template<class T>
  157. struct BiotSavartKernel{
  158. inline static const Kernel<T>& potential();
  159. };
  160. template<class T>
  161. struct HelmholtzKernel{
  162. inline static const Kernel<T>& potential();
  163. };
  164. }//end namespace
  165. #ifdef __INTEL_OFFLOAD
  166. #pragma offload_attribute(pop)
  167. #endif
  168. #include <kernel.txx>
  169. #endif //_PVFMM_FMM_KERNEL_HPP_