kernel.txx 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. /**
  2. * \file kernel.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 12-20-2011
  5. * \brief This file contains the implementation of the struct Kernel and also the
  6. * implementation of various kernels for FMM.
  7. */
  8. #ifdef USE_SSE
  9. #include <emmintrin.h>
  10. #endif
  11. #include <math.h>
  12. #include <assert.h>
  13. #include <vector>
  14. #include <profile.hpp>
  15. namespace pvfmm{
  16. /**
  17. * \brief Constructor.
  18. */
  19. template <class T>
  20. Kernel<T>::Kernel(): dim(0){
  21. ker_dim[0]=0;
  22. ker_dim[1]=0;
  23. }
  24. /**
  25. * \brief Constructor.
  26. */
  27. template <class T>
  28. Kernel<T>::Kernel(Ker_t poten, Ker_t dbl_poten, const char* name, int dim_,
  29. const int (&k_dim)[2], bool homogen_, T ker_scale,
  30. size_t dev_poten, size_t dev_dbl_poten){
  31. dim=dim_;
  32. ker_dim[0]=k_dim[0];
  33. ker_dim[1]=k_dim[1];
  34. ker_poten=poten;
  35. dbl_layer_poten=dbl_poten;
  36. homogen=homogen_;
  37. poten_scale=ker_scale;
  38. ker_name=std::string(name);
  39. dev_ker_poten=dev_poten;
  40. dev_dbl_layer_poten=dev_dbl_poten;
  41. }
  42. /**
  43. * \brief Compute the transformation matrix (on the source strength vector)
  44. * to get potential at target coordinates due to sources at the given
  45. * coordinates.
  46. * \param[in] r_src Coordinates of source points.
  47. * \param[in] src_cnt Number of source points.
  48. * \param[in] r_trg Coordinates of target points.
  49. * \param[in] trg_cnt Number of target points.
  50. * \param[out] k_out Output array with potential values.
  51. */
  52. template <class T>
  53. void Kernel<T>::BuildMatrix(T* r_src, int src_cnt,
  54. T* r_trg, int trg_cnt, T* k_out){
  55. int dim=3; //Only supporting 3D
  56. memset(k_out, 0, src_cnt*ker_dim[0]*trg_cnt*ker_dim[1]*sizeof(T));
  57. for(int i=0;i<src_cnt;i++) //TODO Optimize this.
  58. for(int j=0;j<ker_dim[0];j++){
  59. std::vector<T> v_src(ker_dim[0],0);
  60. v_src[j]=1.0;
  61. ker_poten(&r_src[i*dim], 1, &v_src[0], 1, r_trg, trg_cnt,
  62. &k_out[(i*ker_dim[0]+j)*trg_cnt*ker_dim[1]], NULL);
  63. }
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////
  66. //////// LAPLACE KERNEL ////////
  67. ////////////////////////////////////////////////////////////////////////////////
  68. /**
  69. * \brief Green's function for the Poisson's equation. Kernel tensor
  70. * dimension = 1x1.
  71. */
  72. template <class T>
  73. void laplace_poten(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  74. #ifndef __MIC__
  75. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(12*dof));
  76. #endif
  77. const T OOFP = 1.0/(4.0*const_pi<T>());
  78. for(int t=0;t<trg_cnt;t++){
  79. for(int i=0;i<dof;i++){
  80. T p=0;
  81. for(int s=0;s<src_cnt;s++){
  82. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  83. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  84. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  85. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  86. if (invR!=0) invR = 1.0/sqrt(invR);
  87. p += v_src[s*dof+i]*invR;
  88. }
  89. k_out[t*dof+i] += p*OOFP;
  90. }
  91. }
  92. }
  93. template <class T>
  94. void laplace_poten_(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  95. //void laplace_poten(T* r_src_, int src_cnt, T* v_src_, int dof, T* r_trg_, int trg_cnt, T* k_out_){
  96. // int dim=3; //Only supporting 3D
  97. // T* r_src=new T[src_cnt*dim];
  98. // T* r_trg=new T[trg_cnt*dim];
  99. // T* v_src=new T[src_cnt ];
  100. // T* k_out=new T[trg_cnt ];
  101. // mem::memcopy(r_src,r_src_,src_cnt*dim*sizeof(T));
  102. // mem::memcopy(r_trg,r_trg_,trg_cnt*dim*sizeof(T));
  103. // mem::memcopy(v_src,v_src_,src_cnt *sizeof(T));
  104. // mem::memcopy(k_out,k_out_,trg_cnt *sizeof(T));
  105. #define EVAL_BLKSZ 32
  106. #define MAX_DOF 100
  107. //Compute source to target interactions.
  108. const T OOFP = 1.0/(4.0*const_pi<T>());
  109. if(dof==1){
  110. for (int t_=0; t_<trg_cnt; t_+=EVAL_BLKSZ)
  111. for (int s_=0; s_<src_cnt; s_+=EVAL_BLKSZ){
  112. int src_blk=s_+EVAL_BLKSZ; src_blk=(src_blk>src_cnt?src_cnt:src_blk);
  113. int trg_blk=t_+EVAL_BLKSZ; trg_blk=(trg_blk>trg_cnt?trg_cnt:trg_blk);
  114. for(int t=t_;t<trg_blk;t++){
  115. T p=0;
  116. for(int s=s_;s<src_blk;s++){
  117. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  118. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  119. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  120. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  121. if (invR!=0) invR = 1.0/sqrt(invR);
  122. p += v_src[s]*invR;
  123. }
  124. k_out[t] += p*OOFP;
  125. }
  126. }
  127. }else if(dof==2){
  128. T p[MAX_DOF];
  129. for (int t_=0; t_<trg_cnt; t_+=EVAL_BLKSZ)
  130. for (int s_=0; s_<src_cnt; s_+=EVAL_BLKSZ){
  131. int src_blk=s_+EVAL_BLKSZ; src_blk=(src_blk>src_cnt?src_cnt:src_blk);
  132. int trg_blk=t_+EVAL_BLKSZ; trg_blk=(trg_blk>trg_cnt?trg_cnt:trg_blk);
  133. for(int t=t_;t<trg_blk;t++){
  134. p[0]=0; p[1]=0;
  135. for(int s=s_;s<src_blk;s++){
  136. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  137. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  138. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  139. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  140. if (invR!=0) invR = 1.0/sqrt(invR);
  141. p[0] += v_src[s*dof+0]*invR;
  142. p[1] += v_src[s*dof+1]*invR;
  143. }
  144. k_out[t*dof+0] += p[0]*OOFP;
  145. k_out[t*dof+1] += p[1]*OOFP;
  146. }
  147. }
  148. }else if(dof==3){
  149. T p[MAX_DOF];
  150. for (int t_=0; t_<trg_cnt; t_+=EVAL_BLKSZ)
  151. for (int s_=0; s_<src_cnt; s_+=EVAL_BLKSZ){
  152. int src_blk=s_+EVAL_BLKSZ; src_blk=(src_blk>src_cnt?src_cnt:src_blk);
  153. int trg_blk=t_+EVAL_BLKSZ; trg_blk=(trg_blk>trg_cnt?trg_cnt:trg_blk);
  154. for(int t=t_;t<trg_blk;t++){
  155. p[0]=0; p[1]=0; p[2]=0;
  156. for(int s=s_;s<src_blk;s++){
  157. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  158. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  159. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  160. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  161. if (invR!=0) invR = 1.0/sqrt(invR);
  162. p[0] += v_src[s*dof+0]*invR;
  163. p[1] += v_src[s*dof+1]*invR;
  164. p[2] += v_src[s*dof+2]*invR;
  165. }
  166. k_out[t*dof+0] += p[0]*OOFP;
  167. k_out[t*dof+1] += p[1]*OOFP;
  168. k_out[t*dof+2] += p[2]*OOFP;
  169. }
  170. }
  171. }else{
  172. T p[MAX_DOF];
  173. for (int t_=0; t_<trg_cnt; t_+=EVAL_BLKSZ)
  174. for (int s_=0; s_<src_cnt; s_+=EVAL_BLKSZ){
  175. int src_blk=s_+EVAL_BLKSZ; src_blk=(src_blk>src_cnt?src_cnt:src_blk);
  176. int trg_blk=t_+EVAL_BLKSZ; trg_blk=(trg_blk>trg_cnt?trg_cnt:trg_blk);
  177. for(int t=t_;t<trg_blk;t++){
  178. for(int i=0;i<dof;i++) p[i]=0;
  179. for(int s=s_;s<src_blk;s++){
  180. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  181. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  182. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  183. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  184. if (invR!=0) invR = 1.0/sqrt(invR);
  185. for(int i=0;i<dof;i++)
  186. p[i] += v_src[s*dof+i]*invR;
  187. }
  188. for(int i=0;i<dof;i++)
  189. k_out[t*dof+i] += p[i]*OOFP;
  190. }
  191. }
  192. }
  193. #ifndef __MIC__
  194. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(10+2*dof));
  195. #endif
  196. #undef MAX_DOF
  197. #undef EVAL_BLKSZ
  198. // for (int t=0; t<trg_cnt; t++)
  199. // k_out_[t] += k_out[t];
  200. // delete[] r_src;
  201. // delete[] r_trg;
  202. // delete[] v_src;
  203. // delete[] k_out;
  204. }
  205. // Laplace double layer potential.
  206. template <class T>
  207. void laplace_dbl_poten(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  208. #ifndef __MIC__
  209. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(19*dof));
  210. #endif
  211. const T OOFP = -1.0/(4.0*const_pi<T>());
  212. for(int t=0;t<trg_cnt;t++){
  213. for(int i=0;i<dof;i++){
  214. T p=0;
  215. for(int s=0;s<src_cnt;s++){
  216. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  217. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  218. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  219. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  220. if (invR!=0) invR = 1.0/sqrt(invR);
  221. p = v_src[(s*dof+i)*4+3]*invR*invR*invR;
  222. k_out[t*dof+i] += p*OOFP*( dX_reg*v_src[(s*dof+i)*4+0] +
  223. dY_reg*v_src[(s*dof+i)*4+1] +
  224. dZ_reg*v_src[(s*dof+i)*4+2] );
  225. }
  226. }
  227. }
  228. }
  229. // Laplace grdient kernel.
  230. template <class T>
  231. void laplace_grad(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  232. #ifndef __MIC__
  233. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(10+12*dof));
  234. #endif
  235. const T OOFP = -1.0/(4.0*const_pi<T>());
  236. if(dof==1){
  237. for(int t=0;t<trg_cnt;t++){
  238. T p=0;
  239. for(int s=0;s<src_cnt;s++){
  240. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  241. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  242. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  243. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  244. if (invR!=0) invR = 1.0/sqrt(invR);
  245. p = v_src[s]*invR*invR*invR;
  246. k_out[(t)*3+0] += p*OOFP*dX_reg;
  247. k_out[(t)*3+1] += p*OOFP*dY_reg;
  248. k_out[(t)*3+2] += p*OOFP*dZ_reg;
  249. }
  250. }
  251. }else if(dof==2){
  252. for(int t=0;t<trg_cnt;t++){
  253. T p=0;
  254. for(int s=0;s<src_cnt;s++){
  255. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  256. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  257. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  258. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  259. if (invR!=0) invR = 1.0/sqrt(invR);
  260. p = v_src[s*dof+0]*invR*invR*invR;
  261. k_out[(t*dof+0)*3+0] += p*OOFP*dX_reg;
  262. k_out[(t*dof+0)*3+1] += p*OOFP*dY_reg;
  263. k_out[(t*dof+0)*3+2] += p*OOFP*dZ_reg;
  264. p = v_src[s*dof+1]*invR*invR*invR;
  265. k_out[(t*dof+1)*3+0] += p*OOFP*dX_reg;
  266. k_out[(t*dof+1)*3+1] += p*OOFP*dY_reg;
  267. k_out[(t*dof+1)*3+2] += p*OOFP*dZ_reg;
  268. }
  269. }
  270. }else if(dof==3){
  271. for(int t=0;t<trg_cnt;t++){
  272. T p=0;
  273. for(int s=0;s<src_cnt;s++){
  274. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  275. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  276. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  277. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  278. if (invR!=0) invR = 1.0/sqrt(invR);
  279. p = v_src[s*dof+0]*invR*invR*invR;
  280. k_out[(t*dof+0)*3+0] += p*OOFP*dX_reg;
  281. k_out[(t*dof+0)*3+1] += p*OOFP*dY_reg;
  282. k_out[(t*dof+0)*3+2] += p*OOFP*dZ_reg;
  283. p = v_src[s*dof+1]*invR*invR*invR;
  284. k_out[(t*dof+1)*3+0] += p*OOFP*dX_reg;
  285. k_out[(t*dof+1)*3+1] += p*OOFP*dY_reg;
  286. k_out[(t*dof+1)*3+2] += p*OOFP*dZ_reg;
  287. p = v_src[s*dof+2]*invR*invR*invR;
  288. k_out[(t*dof+2)*3+0] += p*OOFP*dX_reg;
  289. k_out[(t*dof+2)*3+1] += p*OOFP*dY_reg;
  290. k_out[(t*dof+2)*3+2] += p*OOFP*dZ_reg;
  291. }
  292. }
  293. }else{
  294. for(int t=0;t<trg_cnt;t++){
  295. for(int i=0;i<dof;i++){
  296. T p=0;
  297. for(int s=0;s<src_cnt;s++){
  298. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  299. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  300. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  301. T invR = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  302. if (invR!=0) invR = 1.0/sqrt(invR);
  303. p = v_src[s*dof+i]*invR*invR*invR;
  304. k_out[(t*dof+i)*3+0] += p*OOFP*dX_reg;
  305. k_out[(t*dof+i)*3+1] += p*OOFP*dY_reg;
  306. k_out[(t*dof+i)*3+2] += p*OOFP*dZ_reg;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. #ifndef __MIC__
  313. #ifdef USE_SSE
  314. namespace
  315. {
  316. #define IDEAL_ALIGNMENT 16
  317. #define SIMD_LEN (int)(IDEAL_ALIGNMENT / sizeof(double))
  318. #define DECL_SIMD_ALIGNED __declspec(align(IDEAL_ALIGNMENT))
  319. void laplaceSSE(
  320. const int ns,
  321. const int nt,
  322. const double *sx,
  323. const double *sy,
  324. const double *sz,
  325. const double *tx,
  326. const double *ty,
  327. const double *tz,
  328. const double *srcDen,
  329. double *trgVal)
  330. {
  331. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  332. abort();
  333. double OOFP = 1.0/(4.0*const_pi<double>());
  334. __m128d temp;
  335. double aux_arr[SIMD_LEN+1];
  336. double *tempval;
  337. // if aux_arr is misaligned
  338. if (size_t(aux_arr)%IDEAL_ALIGNMENT) tempval = aux_arr + 1;
  339. else tempval = aux_arr;
  340. if (size_t(tempval)%IDEAL_ALIGNMENT) abort();
  341. /*! One over four pi */
  342. __m128d oofp = _mm_set1_pd (OOFP);
  343. __m128d half = _mm_set1_pd (0.5);
  344. __m128d opf = _mm_set1_pd (1.5);
  345. __m128d zero = _mm_setzero_pd ();
  346. // loop over sources
  347. int i = 0;
  348. for (; i < nt; i++) {
  349. temp = _mm_setzero_pd();
  350. __m128d txi = _mm_load1_pd (&tx[i]);
  351. __m128d tyi = _mm_load1_pd (&ty[i]);
  352. __m128d tzi = _mm_load1_pd (&tz[i]);
  353. int j = 0;
  354. // Load and calculate in groups of SIMD_LEN
  355. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  356. __m128d sxj = _mm_load_pd (&sx[j]);
  357. __m128d syj = _mm_load_pd (&sy[j]);
  358. __m128d szj = _mm_load_pd (&sz[j]);
  359. __m128d sden = _mm_set_pd (srcDen[j+1], srcDen[j]);
  360. __m128d dX, dY, dZ;
  361. __m128d dR2;
  362. __m128d S;
  363. dX = _mm_sub_pd(txi , sxj);
  364. dY = _mm_sub_pd(tyi , syj);
  365. dZ = _mm_sub_pd(tzi , szj);
  366. sxj = _mm_mul_pd(dX, dX);
  367. syj = _mm_mul_pd(dY, dY);
  368. szj = _mm_mul_pd(dZ, dZ);
  369. dR2 = _mm_add_pd(sxj, syj);
  370. dR2 = _mm_add_pd(szj, dR2);
  371. __m128d reqzero = _mm_cmpeq_pd (dR2, zero);
  372. __m128d xhalf = _mm_mul_pd (half, dR2);
  373. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  374. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  375. __m128d S_d = _mm_cvtps_pd(S_s);
  376. // To handle the condition when src and trg coincide
  377. S_d = _mm_andnot_pd (reqzero, S_d);
  378. S = _mm_mul_pd (S_d, S_d);
  379. S = _mm_mul_pd (S, xhalf);
  380. S = _mm_sub_pd (opf, S);
  381. S = _mm_mul_pd (S, S_d);
  382. sden = _mm_mul_pd (sden, S);
  383. temp = _mm_add_pd (sden, temp);
  384. }
  385. temp = _mm_mul_pd (temp, oofp);
  386. _mm_store_pd(tempval, temp);
  387. for (int k = 0; k < SIMD_LEN; k++) {
  388. trgVal[i] += tempval[k];
  389. }
  390. for (; j < ns; j++) {
  391. double x = tx[i] - sx[j];
  392. double y = ty[i] - sy[j];
  393. double z = tz[i] - sz[j];
  394. double r2 = x*x + y*y + z*z;
  395. double r = sqrt(r2);
  396. double invdr;
  397. if (r == 0)
  398. invdr = 0;
  399. else
  400. invdr = 1/r;
  401. double den = srcDen[j];
  402. trgVal[i] += den*invdr*OOFP;
  403. }
  404. }
  405. return;
  406. }
  407. void laplaceDblSSE(
  408. const int ns,
  409. const int nt,
  410. const double *sx,
  411. const double *sy,
  412. const double *sz,
  413. const double *tx,
  414. const double *ty,
  415. const double *tz,
  416. const double *srcDen,
  417. double *trgVal)
  418. {
  419. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  420. abort();
  421. double OOFP = 1.0/(4.0*const_pi<double>());
  422. __m128d temp;
  423. double aux_arr[SIMD_LEN+1];
  424. double *tempval;
  425. // if aux_arr is misaligned
  426. if (size_t(aux_arr)%IDEAL_ALIGNMENT) tempval = aux_arr + 1;
  427. else tempval = aux_arr;
  428. if (size_t(tempval)%IDEAL_ALIGNMENT) abort();
  429. /*! One over four pi */
  430. __m128d oofp = _mm_set1_pd (OOFP);
  431. __m128d half = _mm_set1_pd (0.5);
  432. __m128d opf = _mm_set1_pd (1.5);
  433. __m128d zero = _mm_setzero_pd ();
  434. // loop over sources
  435. int i = 0;
  436. for (; i < nt; i++) {
  437. temp = _mm_setzero_pd();
  438. __m128d txi = _mm_load1_pd (&tx[i]);
  439. __m128d tyi = _mm_load1_pd (&ty[i]);
  440. __m128d tzi = _mm_load1_pd (&tz[i]);
  441. int j = 0;
  442. // Load and calculate in groups of SIMD_LEN
  443. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  444. __m128d sxj = _mm_load_pd (&sx[j]);
  445. __m128d syj = _mm_load_pd (&sy[j]);
  446. __m128d szj = _mm_load_pd (&sz[j]);
  447. __m128d snormx = _mm_set_pd (srcDen[(j+1)*4+0], srcDen[j*4+0]);
  448. __m128d snormy = _mm_set_pd (srcDen[(j+1)*4+1], srcDen[j*4+1]);
  449. __m128d snormz = _mm_set_pd (srcDen[(j+1)*4+2], srcDen[j*4+2]);
  450. __m128d sden = _mm_set_pd (srcDen[(j+1)*4+3], srcDen[j*4+3]);
  451. __m128d dX, dY, dZ;
  452. __m128d dR2;
  453. __m128d S;
  454. __m128d S2;
  455. __m128d S3;
  456. dX = _mm_sub_pd(txi , sxj);
  457. dY = _mm_sub_pd(tyi , syj);
  458. dZ = _mm_sub_pd(tzi , szj);
  459. sxj = _mm_mul_pd(dX, dX);
  460. syj = _mm_mul_pd(dY, dY);
  461. szj = _mm_mul_pd(dZ, dZ);
  462. dR2 = _mm_add_pd(sxj, syj);
  463. dR2 = _mm_add_pd(szj, dR2);
  464. __m128d reqzero = _mm_cmpeq_pd (dR2, zero);
  465. __m128d xhalf = _mm_mul_pd (half, dR2);
  466. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  467. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  468. __m128d S_d = _mm_cvtps_pd(S_s);
  469. // To handle the condition when src and trg coincide
  470. S_d = _mm_andnot_pd (reqzero, S_d);
  471. S = _mm_mul_pd (S_d, S_d);
  472. S = _mm_mul_pd (S, xhalf);
  473. S = _mm_sub_pd (opf, S);
  474. S = _mm_mul_pd (S, S_d);
  475. S2 = _mm_mul_pd (S, S);
  476. S3 = _mm_mul_pd (S2, S);
  477. __m128d S3_sden=_mm_mul_pd(S3, sden);
  478. __m128d dot_sum = _mm_add_pd(_mm_mul_pd(snormx,dX),_mm_mul_pd(snormy,dY));
  479. dot_sum = _mm_add_pd(dot_sum,_mm_mul_pd(snormz,dZ));
  480. temp = _mm_add_pd(_mm_mul_pd(S3_sden,dot_sum),temp);
  481. }
  482. temp = _mm_mul_pd (temp, oofp);
  483. _mm_store_pd(tempval, temp);
  484. for (int k = 0; k < SIMD_LEN; k++) {
  485. trgVal[i] += tempval[k];
  486. }
  487. for (; j < ns; j++) {
  488. double x = tx[i] - sx[j];
  489. double y = ty[i] - sy[j];
  490. double z = tz[i] - sz[j];
  491. double r2 = x*x + y*y + z*z;
  492. double r = sqrt(r2);
  493. double invdr;
  494. if (r == 0)
  495. invdr = 0;
  496. else
  497. invdr = 1/r;
  498. double invdr2=invdr*invdr;
  499. double invdr3=invdr2*invdr;
  500. double dot_sum = x*srcDen[j*4+0] + y*srcDen[j*4+1] + z*srcDen[j*4+2];
  501. trgVal[i] += OOFP*invdr3*x*srcDen[j*4+3]*dot_sum;
  502. }
  503. }
  504. return;
  505. }
  506. void laplaceGradSSE(
  507. const int ns,
  508. const int nt,
  509. const double *sx,
  510. const double *sy,
  511. const double *sz,
  512. const double *tx,
  513. const double *ty,
  514. const double *tz,
  515. const double *srcDen,
  516. double *trgVal)
  517. {
  518. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  519. abort();
  520. double OOFP = 1.0/(4.0*const_pi<double>());
  521. __m128d tempx; __m128d tempy; __m128d tempz;
  522. double aux_arr[3*SIMD_LEN+1];
  523. double *tempvalx, *tempvaly, *tempvalz;
  524. // if aux_arr is misaligned
  525. if (size_t(aux_arr)%IDEAL_ALIGNMENT) tempvalx = aux_arr + 1;
  526. else tempvalx = aux_arr;
  527. if (size_t(tempvalx)%IDEAL_ALIGNMENT) abort();
  528. tempvaly=tempvalx+SIMD_LEN;
  529. tempvalz=tempvaly+SIMD_LEN;
  530. /*! One over four pi */
  531. __m128d oofp = _mm_set1_pd (OOFP);
  532. __m128d half = _mm_set1_pd (0.5);
  533. __m128d opf = _mm_set1_pd (1.5);
  534. __m128d zero = _mm_setzero_pd ();
  535. // loop over sources
  536. int i = 0;
  537. for (; i < nt; i++) {
  538. tempx = _mm_setzero_pd();
  539. tempy = _mm_setzero_pd();
  540. tempz = _mm_setzero_pd();
  541. __m128d txi = _mm_load1_pd (&tx[i]);
  542. __m128d tyi = _mm_load1_pd (&ty[i]);
  543. __m128d tzi = _mm_load1_pd (&tz[i]);
  544. int j = 0;
  545. // Load and calculate in groups of SIMD_LEN
  546. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  547. __m128d sxj = _mm_load_pd (&sx[j]);
  548. __m128d syj = _mm_load_pd (&sy[j]);
  549. __m128d szj = _mm_load_pd (&sz[j]);
  550. __m128d sden = _mm_set_pd (srcDen[j+1], srcDen[j]);
  551. __m128d dX, dY, dZ;
  552. __m128d dR2;
  553. __m128d S;
  554. __m128d S2;
  555. __m128d S3;
  556. dX = _mm_sub_pd(txi , sxj);
  557. dY = _mm_sub_pd(tyi , syj);
  558. dZ = _mm_sub_pd(tzi , szj);
  559. sxj = _mm_mul_pd(dX, dX);
  560. syj = _mm_mul_pd(dY, dY);
  561. szj = _mm_mul_pd(dZ, dZ);
  562. dR2 = _mm_add_pd(sxj, syj);
  563. dR2 = _mm_add_pd(szj, dR2);
  564. __m128d reqzero = _mm_cmpeq_pd (dR2, zero);
  565. __m128d xhalf = _mm_mul_pd (half, dR2);
  566. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  567. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  568. __m128d S_d = _mm_cvtps_pd(S_s);
  569. // To handle the condition when src and trg coincide
  570. S_d = _mm_andnot_pd (reqzero, S_d);
  571. S = _mm_mul_pd (S_d, S_d);
  572. S = _mm_mul_pd (S, xhalf);
  573. S = _mm_sub_pd (opf, S);
  574. S = _mm_mul_pd (S, S_d);
  575. S2 = _mm_mul_pd (S, S);
  576. S3 = _mm_mul_pd (S2, S);
  577. __m128d S3_sden=_mm_mul_pd(S3, sden);
  578. tempx = _mm_add_pd(_mm_mul_pd(S3_sden,dX),tempx);
  579. tempy = _mm_add_pd(_mm_mul_pd(S3_sden,dY),tempy);
  580. tempz = _mm_add_pd(_mm_mul_pd(S3_sden,dZ),tempz);
  581. }
  582. tempx = _mm_mul_pd (tempx, oofp);
  583. tempy = _mm_mul_pd (tempy, oofp);
  584. tempz = _mm_mul_pd (tempz, oofp);
  585. _mm_store_pd(tempvalx, tempx);
  586. _mm_store_pd(tempvaly, tempy);
  587. _mm_store_pd(tempvalz, tempz);
  588. for (int k = 0; k < SIMD_LEN; k++) {
  589. trgVal[i*3 ] += tempvalx[k];
  590. trgVal[i*3+1] += tempvaly[k];
  591. trgVal[i*3+2] += tempvalz[k];
  592. }
  593. for (; j < ns; j++) {
  594. double x = tx[i] - sx[j];
  595. double y = ty[i] - sy[j];
  596. double z = tz[i] - sz[j];
  597. double r2 = x*x + y*y + z*z;
  598. double r = sqrt(r2);
  599. double invdr;
  600. if (r == 0)
  601. invdr = 0;
  602. else
  603. invdr = 1/r;
  604. double invdr2=invdr*invdr;
  605. double invdr3=invdr2*invdr;
  606. trgVal[i*3 ] += OOFP*invdr3*x*srcDen[j];
  607. trgVal[i*3+1] += OOFP*invdr3*y*srcDen[j];
  608. trgVal[i*3+2] += OOFP*invdr3*z*srcDen[j];
  609. }
  610. }
  611. return;
  612. }
  613. #undef SIMD_LEN
  614. #define X(s,k) (s)[(k)*COORD_DIM]
  615. #define Y(s,k) (s)[(k)*COORD_DIM+1]
  616. #define Z(s,k) (s)[(k)*COORD_DIM+2]
  617. void laplaceSSEShuffle(const int ns, const int nt, float const src[], float const trg[], float const den[], float pot[], mem::MemoryManager* mem_mgr=NULL)
  618. {
  619. // TODO
  620. }
  621. void laplaceSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  622. {
  623. double* buff=NULL;
  624. if(mem_mgr) buff=(double*)mem_mgr->malloc((ns+1+nt)*3*sizeof(double));
  625. else buff=(double*)malloc((ns+1+nt)*3*sizeof(double));
  626. double* buff_=buff;
  627. pvfmm::Vector<double> xs(ns+1,buff_,false); buff_+=ns+1;
  628. pvfmm::Vector<double> ys(ns+1,buff_,false); buff_+=ns+1;
  629. pvfmm::Vector<double> zs(ns+1,buff_,false); buff_+=ns+1;
  630. pvfmm::Vector<double> xt(nt ,buff_,false); buff_+=nt ;
  631. pvfmm::Vector<double> yt(nt ,buff_,false); buff_+=nt ;
  632. pvfmm::Vector<double> zt(nt ,buff_,false); buff_+=nt ;
  633. //std::vector<double> xs(ns+1);
  634. //std::vector<double> ys(ns+1);
  635. //std::vector<double> zs(ns+1);
  636. //std::vector<double> xt(nt );
  637. //std::vector<double> yt(nt );
  638. //std::vector<double> zt(nt );
  639. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  640. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  641. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  642. //1. reshuffle memory
  643. for (int k =0;k<ns;k++){
  644. xs[k+x_shift]=X(src,k);
  645. ys[k+y_shift]=Y(src,k);
  646. zs[k+z_shift]=Z(src,k);
  647. }
  648. for (int k=0;k<nt;k++){
  649. xt[k]=X(trg,k);
  650. yt[k]=Y(trg,k);
  651. zt[k]=Z(trg,k);
  652. }
  653. //2. perform caclulation
  654. laplaceSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  655. if(mem_mgr) mem_mgr->free(buff);
  656. else free(buff);
  657. return;
  658. }
  659. void laplaceDblSSEShuffle(const int ns, const int nt, float const src[], float const trg[], float const den[], float pot[], mem::MemoryManager* mem_mgr=NULL)
  660. {
  661. // TODO
  662. }
  663. void laplaceDblSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  664. {
  665. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  666. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  667. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  668. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  669. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  670. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  671. //1. reshuffle memory
  672. for (int k =0;k<ns;k++){
  673. xs[k+x_shift]=X(src,k);
  674. ys[k+y_shift]=Y(src,k);
  675. zs[k+z_shift]=Z(src,k);
  676. }
  677. for (int k=0;k<nt;k++){
  678. xt[k]=X(trg,k);
  679. yt[k]=Y(trg,k);
  680. zt[k]=Z(trg,k);
  681. }
  682. //2. perform caclulation
  683. laplaceDblSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  684. return;
  685. }
  686. void laplaceGradSSEShuffle(const int ns, const int nt, float const src[], float const trg[], float const den[], float pot[], mem::MemoryManager* mem_mgr=NULL)
  687. {
  688. // TODO
  689. }
  690. void laplaceGradSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  691. {
  692. int tid=omp_get_thread_num();
  693. static std::vector<std::vector<double> > xs_(100); static std::vector<std::vector<double> > xt_(100);
  694. static std::vector<std::vector<double> > ys_(100); static std::vector<std::vector<double> > yt_(100);
  695. static std::vector<std::vector<double> > zs_(100); static std::vector<std::vector<double> > zt_(100);
  696. std::vector<double>& xs=xs_[tid]; std::vector<double>& xt=xt_[tid];
  697. std::vector<double>& ys=ys_[tid]; std::vector<double>& yt=yt_[tid];
  698. std::vector<double>& zs=zs_[tid]; std::vector<double>& zt=zt_[tid];
  699. xs.resize(ns+1); xt.resize(nt);
  700. ys.resize(ns+1); yt.resize(nt);
  701. zs.resize(ns+1); zt.resize(nt);
  702. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  703. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  704. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  705. //1. reshuffle memory
  706. for (int k =0;k<ns;k++){
  707. xs[k+x_shift]=X(src,k);
  708. ys[k+y_shift]=Y(src,k);
  709. zs[k+z_shift]=Z(src,k);
  710. }
  711. for (int k=0;k<nt;k++){
  712. xt[k]=X(trg,k);
  713. yt[k]=Y(trg,k);
  714. zt[k]=Z(trg,k);
  715. }
  716. //2. perform caclulation
  717. laplaceGradSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  718. return;
  719. }
  720. #undef X
  721. #undef Y
  722. #undef Z
  723. #undef IDEAL_ALIGNMENT
  724. #undef DECL_SIMD_ALIGNED
  725. }
  726. template <>
  727. void laplace_poten<double>(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  728. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(12*dof));
  729. if(dof==1){
  730. laplaceSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src, k_out, mem_mgr);
  731. return;
  732. }
  733. }
  734. template <>
  735. void laplace_dbl_poten<double>(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  736. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(19*dof));
  737. if(dof==1){
  738. laplaceDblSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src, k_out, mem_mgr);
  739. return;
  740. }
  741. }
  742. template <>
  743. void laplace_grad<double>(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  744. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(10+12*dof));
  745. if(dof==1){
  746. laplaceGradSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src, k_out, mem_mgr);
  747. return;
  748. }
  749. }
  750. #endif
  751. #endif
  752. ////////////////////////////////////////////////////////////////////////////////
  753. //////// STOKES KERNEL ////////
  754. ////////////////////////////////////////////////////////////////////////////////
  755. /**
  756. * \brief Green's function for the Stokes's equation. Kernel tensor
  757. * dimension = 3x3.
  758. */
  759. template <class T>
  760. void stokes_vel(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  761. #ifndef __MIC__
  762. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(28*dof));
  763. #endif
  764. const T mu=1.0;
  765. const T OOEPMU = 1.0/(8.0*const_pi<T>()*mu);
  766. for(int t=0;t<trg_cnt;t++){
  767. for(int i=0;i<dof;i++){
  768. T p[3]={0,0,0};
  769. for(int s=0;s<src_cnt;s++){
  770. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  771. r_trg[3*t+1]-r_src[3*s+1],
  772. r_trg[3*t+2]-r_src[3*s+2]};
  773. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  774. if (R!=0){
  775. T invR2=1.0/R;
  776. T invR=sqrt(invR2);
  777. T v_src[3]={v_src_[(s*dof+i)*3 ],
  778. v_src_[(s*dof+i)*3+1],
  779. v_src_[(s*dof+i)*3+2]};
  780. T inner_prod=(v_src[0]*dR[0] +
  781. v_src[1]*dR[1] +
  782. v_src[2]*dR[2])* invR2;
  783. p[0] += (v_src[0] + dR[0]*inner_prod)*invR;
  784. p[1] += (v_src[1] + dR[1]*inner_prod)*invR;
  785. p[2] += (v_src[2] + dR[2]*inner_prod)*invR;
  786. }
  787. }
  788. k_out[(t*dof+i)*3+0] += p[0]*OOEPMU;
  789. k_out[(t*dof+i)*3+1] += p[1]*OOEPMU;
  790. k_out[(t*dof+i)*3+2] += p[2]*OOEPMU;
  791. }
  792. }
  793. }
  794. template <class T>
  795. void stokes_sym_dip(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  796. #ifndef __MIC__
  797. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(47*dof));
  798. #endif
  799. const T mu=1.0;
  800. const T OOEPMU = -1.0/(8.0*const_pi<T>()*mu);
  801. for(int t=0;t<trg_cnt;t++){
  802. for(int i=0;i<dof;i++){
  803. T p[3]={0,0,0};
  804. for(int s=0;s<src_cnt;s++){
  805. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  806. r_trg[3*t+1]-r_src[3*s+1],
  807. r_trg[3*t+2]-r_src[3*s+2]};
  808. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  809. if (R!=0){
  810. T invR2=1.0/R;
  811. T invR=sqrt(invR2);
  812. T invR3=invR2*invR;
  813. T* f=&v_src[(s*dof+i)*6+0];
  814. T* n=&v_src[(s*dof+i)*6+3];
  815. T r_dot_n=(n[0]*dR[0]+n[1]*dR[1]+n[2]*dR[2]);
  816. T r_dot_f=(f[0]*dR[0]+f[1]*dR[1]+f[2]*dR[2]);
  817. T n_dot_f=(f[0]* n[0]+f[1]* n[1]+f[2]* n[2]);
  818. p[0] += dR[0]*(n_dot_f - 3*r_dot_n*r_dot_f*invR2)*invR3;
  819. p[1] += dR[1]*(n_dot_f - 3*r_dot_n*r_dot_f*invR2)*invR3;
  820. p[2] += dR[2]*(n_dot_f - 3*r_dot_n*r_dot_f*invR2)*invR3;
  821. }
  822. }
  823. k_out[(t*dof+i)*3+0] += p[0]*OOEPMU;
  824. k_out[(t*dof+i)*3+1] += p[1]*OOEPMU;
  825. k_out[(t*dof+i)*3+2] += p[2]*OOEPMU;
  826. }
  827. }
  828. }
  829. template <class T>
  830. void stokes_press(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  831. #ifndef __MIC__
  832. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(17*dof));
  833. #endif
  834. const T OOFP = 1.0/(4.0*const_pi<T>());
  835. for(int t=0;t<trg_cnt;t++){
  836. for(int i=0;i<dof;i++){
  837. T p=0;
  838. for(int s=0;s<src_cnt;s++){
  839. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  840. r_trg[3*t+1]-r_src[3*s+1],
  841. r_trg[3*t+2]-r_src[3*s+2]};
  842. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  843. if (R!=0){
  844. T invR2=1.0/R;
  845. T invR=sqrt(invR2);
  846. T invR3=invR2*invR;
  847. T v_src[3]={v_src_[(s*dof+i)*3 ],
  848. v_src_[(s*dof+i)*3+1],
  849. v_src_[(s*dof+i)*3+2]};
  850. T inner_prod=(v_src[0]*dR[0] +
  851. v_src[1]*dR[1] +
  852. v_src[2]*dR[2])* invR3;
  853. p += inner_prod;
  854. }
  855. }
  856. k_out[t*dof+i] += p*OOFP;
  857. }
  858. }
  859. }
  860. template <class T>
  861. void stokes_stress(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  862. #ifndef __MIC__
  863. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(45*dof));
  864. #endif
  865. const T TOFP = -3.0/(4.0*const_pi<T>());
  866. for(int t=0;t<trg_cnt;t++){
  867. for(int i=0;i<dof;i++){
  868. T p[9]={0,0,0,
  869. 0,0,0,
  870. 0,0,0};
  871. for(int s=0;s<src_cnt;s++){
  872. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  873. r_trg[3*t+1]-r_src[3*s+1],
  874. r_trg[3*t+2]-r_src[3*s+2]};
  875. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  876. if (R!=0){
  877. T invR2=1.0/R;
  878. T invR=sqrt(invR2);
  879. T invR3=invR2*invR;
  880. T invR5=invR3*invR2;
  881. T v_src[3]={v_src_[(s*dof+i)*3 ],
  882. v_src_[(s*dof+i)*3+1],
  883. v_src_[(s*dof+i)*3+2]};
  884. T inner_prod=(v_src[0]*dR[0] +
  885. v_src[1]*dR[1] +
  886. v_src[2]*dR[2])* invR5;
  887. p[0] += inner_prod*dR[0]*dR[0]; p[1] += inner_prod*dR[1]*dR[0]; p[2] += inner_prod*dR[2]*dR[0];
  888. p[3] += inner_prod*dR[0]*dR[1]; p[4] += inner_prod*dR[1]*dR[1]; p[5] += inner_prod*dR[2]*dR[1];
  889. p[6] += inner_prod*dR[0]*dR[2]; p[7] += inner_prod*dR[1]*dR[2]; p[8] += inner_prod*dR[2]*dR[2];
  890. }
  891. }
  892. k_out[(t*dof+i)*9+0] += p[0]*TOFP;
  893. k_out[(t*dof+i)*9+1] += p[1]*TOFP;
  894. k_out[(t*dof+i)*9+2] += p[2]*TOFP;
  895. k_out[(t*dof+i)*9+3] += p[3]*TOFP;
  896. k_out[(t*dof+i)*9+4] += p[4]*TOFP;
  897. k_out[(t*dof+i)*9+5] += p[5]*TOFP;
  898. k_out[(t*dof+i)*9+6] += p[6]*TOFP;
  899. k_out[(t*dof+i)*9+7] += p[7]*TOFP;
  900. k_out[(t*dof+i)*9+8] += p[8]*TOFP;
  901. }
  902. }
  903. }
  904. template <class T>
  905. void stokes_grad(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  906. #ifndef __MIC__
  907. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(89*dof));
  908. #endif
  909. const T mu=1.0;
  910. const T OOEPMU = 1.0/(8.0*const_pi<T>()*mu);
  911. for(int t=0;t<trg_cnt;t++){
  912. for(int i=0;i<dof;i++){
  913. T p[9]={0,0,0,
  914. 0,0,0,
  915. 0,0,0};
  916. for(int s=0;s<src_cnt;s++){
  917. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  918. r_trg[3*t+1]-r_src[3*s+1],
  919. r_trg[3*t+2]-r_src[3*s+2]};
  920. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  921. if (R!=0){
  922. T invR2=1.0/R;
  923. T invR=sqrt(invR2);
  924. T invR3=invR2*invR;
  925. T v_src[3]={v_src_[(s*dof+i)*3 ],
  926. v_src_[(s*dof+i)*3+1],
  927. v_src_[(s*dof+i)*3+2]};
  928. T inner_prod=(v_src[0]*dR[0] +
  929. v_src[1]*dR[1] +
  930. v_src[2]*dR[2]);
  931. p[0] += ( inner_prod*(1-3*dR[0]*dR[0]*invR2))*invR3; //6
  932. p[1] += (dR[1]*v_src[0]-v_src[1]*dR[0]+inner_prod*( -3*dR[1]*dR[0]*invR2))*invR3; //9
  933. p[2] += (dR[2]*v_src[0]-v_src[2]*dR[0]+inner_prod*( -3*dR[2]*dR[0]*invR2))*invR3;
  934. p[3] += (dR[0]*v_src[1]-v_src[0]*dR[1]+inner_prod*( -3*dR[0]*dR[1]*invR2))*invR3;
  935. p[4] += ( inner_prod*(1-3*dR[1]*dR[1]*invR2))*invR3;
  936. p[5] += (dR[2]*v_src[1]-v_src[2]*dR[1]+inner_prod*( -3*dR[2]*dR[1]*invR2))*invR3;
  937. p[6] += (dR[0]*v_src[2]-v_src[0]*dR[2]+inner_prod*( -3*dR[0]*dR[2]*invR2))*invR3;
  938. p[7] += (dR[1]*v_src[2]-v_src[1]*dR[2]+inner_prod*( -3*dR[1]*dR[2]*invR2))*invR3;
  939. p[8] += ( inner_prod*(1-3*dR[2]*dR[2]*invR2))*invR3;
  940. }
  941. }
  942. k_out[(t*dof+i)*9+0] += p[0]*OOEPMU;
  943. k_out[(t*dof+i)*9+1] += p[1]*OOEPMU;
  944. k_out[(t*dof+i)*9+2] += p[2]*OOEPMU;
  945. k_out[(t*dof+i)*9+3] += p[3]*OOEPMU;
  946. k_out[(t*dof+i)*9+4] += p[4]*OOEPMU;
  947. k_out[(t*dof+i)*9+5] += p[5]*OOEPMU;
  948. k_out[(t*dof+i)*9+6] += p[6]*OOEPMU;
  949. k_out[(t*dof+i)*9+7] += p[7]*OOEPMU;
  950. k_out[(t*dof+i)*9+8] += p[8]*OOEPMU;
  951. }
  952. }
  953. }
  954. #ifndef __MIC__
  955. #ifdef USE_SSE
  956. namespace
  957. {
  958. #define IDEAL_ALIGNMENT 16
  959. #define SIMD_LEN (int)(IDEAL_ALIGNMENT / sizeof(double))
  960. #define DECL_SIMD_ALIGNED __declspec(align(IDEAL_ALIGNMENT))
  961. void stokesDirectVecSSE(
  962. const int ns,
  963. const int nt,
  964. const double *sx,
  965. const double *sy,
  966. const double *sz,
  967. const double *tx,
  968. const double *ty,
  969. const double *tz,
  970. const double *srcDen,
  971. double *trgVal,
  972. const double cof )
  973. {
  974. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  975. abort();
  976. double mu = cof;
  977. double OOEP = 1.0/(8.0*const_pi<double>());
  978. __m128d tempx;
  979. __m128d tempy;
  980. __m128d tempz;
  981. double oomeu = 1/mu;
  982. double aux_arr[3*SIMD_LEN+1];
  983. double *tempvalx;
  984. double *tempvaly;
  985. double *tempvalz;
  986. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  987. {
  988. tempvalx = aux_arr + 1;
  989. if (size_t(tempvalx)%IDEAL_ALIGNMENT)
  990. abort();
  991. }
  992. else
  993. tempvalx = aux_arr;
  994. tempvaly=tempvalx+SIMD_LEN;
  995. tempvalz=tempvaly+SIMD_LEN;
  996. /*! One over eight pi */
  997. __m128d ooep = _mm_set1_pd (OOEP);
  998. __m128d half = _mm_set1_pd (0.5);
  999. __m128d opf = _mm_set1_pd (1.5);
  1000. __m128d zero = _mm_setzero_pd ();
  1001. __m128d oomu = _mm_set1_pd (1/mu);
  1002. // loop over sources
  1003. int i = 0;
  1004. for (; i < nt; i++) {
  1005. tempx = _mm_setzero_pd();
  1006. tempy = _mm_setzero_pd();
  1007. tempz = _mm_setzero_pd();
  1008. __m128d txi = _mm_load1_pd (&tx[i]);
  1009. __m128d tyi = _mm_load1_pd (&ty[i]);
  1010. __m128d tzi = _mm_load1_pd (&tz[i]);
  1011. int j = 0;
  1012. // Load and calculate in groups of SIMD_LEN
  1013. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1014. __m128d sxj = _mm_load_pd (&sx[j]);
  1015. __m128d syj = _mm_load_pd (&sy[j]);
  1016. __m128d szj = _mm_load_pd (&sz[j]);
  1017. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1018. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1019. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1020. __m128d dX, dY, dZ;
  1021. __m128d dR2;
  1022. __m128d S;
  1023. dX = _mm_sub_pd(txi , sxj);
  1024. dY = _mm_sub_pd(tyi , syj);
  1025. dZ = _mm_sub_pd(tzi , szj);
  1026. sxj = _mm_mul_pd(dX, dX);
  1027. syj = _mm_mul_pd(dY, dY);
  1028. szj = _mm_mul_pd(dZ, dZ);
  1029. dR2 = _mm_add_pd(sxj, syj);
  1030. dR2 = _mm_add_pd(szj, dR2);
  1031. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1032. __m128d xhalf = _mm_mul_pd (half, dR2);
  1033. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1034. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1035. __m128d S_d = _mm_cvtps_pd(S_s);
  1036. // To handle the condition when src and trg coincide
  1037. S_d = _mm_andnot_pd (temp, S_d);
  1038. S = _mm_mul_pd (S_d, S_d);
  1039. S = _mm_mul_pd (S, xhalf);
  1040. S = _mm_sub_pd (opf, S);
  1041. S = _mm_mul_pd (S, S_d);
  1042. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1043. __m128d doty = _mm_mul_pd (dY, sdeny);
  1044. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1045. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1046. dot_sum = _mm_add_pd (dot_sum, dotz);
  1047. dot_sum = _mm_mul_pd (dot_sum, S);
  1048. dot_sum = _mm_mul_pd (dot_sum, S);
  1049. dotx = _mm_mul_pd (dot_sum, dX);
  1050. doty = _mm_mul_pd (dot_sum, dY);
  1051. dotz = _mm_mul_pd (dot_sum, dZ);
  1052. sdenx = _mm_add_pd (sdenx, dotx);
  1053. sdeny = _mm_add_pd (sdeny, doty);
  1054. sdenz = _mm_add_pd (sdenz, dotz);
  1055. sdenx = _mm_mul_pd (sdenx, S);
  1056. sdeny = _mm_mul_pd (sdeny, S);
  1057. sdenz = _mm_mul_pd (sdenz, S);
  1058. tempx = _mm_add_pd (sdenx, tempx);
  1059. tempy = _mm_add_pd (sdeny, tempy);
  1060. tempz = _mm_add_pd (sdenz, tempz);
  1061. }
  1062. tempx = _mm_mul_pd (tempx, ooep);
  1063. tempy = _mm_mul_pd (tempy, ooep);
  1064. tempz = _mm_mul_pd (tempz, ooep);
  1065. tempx = _mm_mul_pd (tempx, oomu);
  1066. tempy = _mm_mul_pd (tempy, oomu);
  1067. tempz = _mm_mul_pd (tempz, oomu);
  1068. _mm_store_pd(tempvalx, tempx);
  1069. _mm_store_pd(tempvaly, tempy);
  1070. _mm_store_pd(tempvalz, tempz);
  1071. for (int k = 0; k < SIMD_LEN; k++) {
  1072. trgVal[i*3] += tempvalx[k];
  1073. trgVal[i*3+1] += tempvaly[k];
  1074. trgVal[i*3+2] += tempvalz[k];
  1075. }
  1076. for (; j < ns; j++) {
  1077. double x = tx[i] - sx[j];
  1078. double y = ty[i] - sy[j];
  1079. double z = tz[i] - sz[j];
  1080. double r2 = x*x + y*y + z*z;
  1081. double r = sqrt(r2);
  1082. double invdr;
  1083. if (r == 0)
  1084. invdr = 0;
  1085. else
  1086. invdr = 1/r;
  1087. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]) * invdr * invdr;
  1088. double denx = srcDen[j*3] + dot*x;
  1089. double deny = srcDen[j*3+1] + dot*y;
  1090. double denz = srcDen[j*3+2] + dot*z;
  1091. trgVal[i*3] += denx*invdr*OOEP*oomeu;
  1092. trgVal[i*3+1] += deny*invdr*OOEP*oomeu;
  1093. trgVal[i*3+2] += denz*invdr*OOEP*oomeu;
  1094. }
  1095. }
  1096. return;
  1097. }
  1098. void stokesPressureSSE(
  1099. const int ns,
  1100. const int nt,
  1101. const double *sx,
  1102. const double *sy,
  1103. const double *sz,
  1104. const double *tx,
  1105. const double *ty,
  1106. const double *tz,
  1107. const double *srcDen,
  1108. double *trgVal)
  1109. {
  1110. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  1111. abort();
  1112. double OOFP = 1.0/(4.0*const_pi<double>());
  1113. __m128d temp_press;
  1114. double aux_arr[SIMD_LEN+1];
  1115. double *tempval_press;
  1116. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  1117. {
  1118. tempval_press = aux_arr + 1;
  1119. if (size_t(tempval_press)%IDEAL_ALIGNMENT)
  1120. abort();
  1121. }
  1122. else
  1123. tempval_press = aux_arr;
  1124. /*! One over eight pi */
  1125. __m128d oofp = _mm_set1_pd (OOFP);
  1126. __m128d half = _mm_set1_pd (0.5);
  1127. __m128d opf = _mm_set1_pd (1.5);
  1128. __m128d zero = _mm_setzero_pd ();
  1129. // loop over sources
  1130. int i = 0;
  1131. for (; i < nt; i++) {
  1132. temp_press = _mm_setzero_pd();
  1133. __m128d txi = _mm_load1_pd (&tx[i]);
  1134. __m128d tyi = _mm_load1_pd (&ty[i]);
  1135. __m128d tzi = _mm_load1_pd (&tz[i]);
  1136. int j = 0;
  1137. // Load and calculate in groups of SIMD_LEN
  1138. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1139. __m128d sxj = _mm_load_pd (&sx[j]);
  1140. __m128d syj = _mm_load_pd (&sy[j]);
  1141. __m128d szj = _mm_load_pd (&sz[j]);
  1142. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1143. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1144. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1145. __m128d dX, dY, dZ;
  1146. __m128d dR2;
  1147. __m128d S;
  1148. dX = _mm_sub_pd(txi , sxj);
  1149. dY = _mm_sub_pd(tyi , syj);
  1150. dZ = _mm_sub_pd(tzi , szj);
  1151. sxj = _mm_mul_pd(dX, dX);
  1152. syj = _mm_mul_pd(dY, dY);
  1153. szj = _mm_mul_pd(dZ, dZ);
  1154. dR2 = _mm_add_pd(sxj, syj);
  1155. dR2 = _mm_add_pd(szj, dR2);
  1156. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1157. __m128d xhalf = _mm_mul_pd (half, dR2);
  1158. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1159. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1160. __m128d S_d = _mm_cvtps_pd(S_s);
  1161. // To handle the condition when src and trg coincide
  1162. S_d = _mm_andnot_pd (temp, S_d);
  1163. S = _mm_mul_pd (S_d, S_d);
  1164. S = _mm_mul_pd (S, xhalf);
  1165. S = _mm_sub_pd (opf, S);
  1166. S = _mm_mul_pd (S, S_d);
  1167. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1168. __m128d doty = _mm_mul_pd (dY, sdeny);
  1169. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1170. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1171. dot_sum = _mm_add_pd (dot_sum, dotz);
  1172. dot_sum = _mm_mul_pd (dot_sum, S);
  1173. dot_sum = _mm_mul_pd (dot_sum, S);
  1174. dot_sum = _mm_mul_pd (dot_sum, S);
  1175. temp_press = _mm_add_pd (dot_sum, temp_press);
  1176. }
  1177. temp_press = _mm_mul_pd (temp_press, oofp);
  1178. _mm_store_pd(tempval_press, temp_press);
  1179. for (int k = 0; k < SIMD_LEN; k++) {
  1180. trgVal[i] += tempval_press[k];
  1181. }
  1182. for (; j < ns; j++) {
  1183. double x = tx[i] - sx[j];
  1184. double y = ty[i] - sy[j];
  1185. double z = tz[i] - sz[j];
  1186. double r2 = x*x + y*y + z*z;
  1187. double r = sqrt(r2);
  1188. double invdr;
  1189. if (r == 0)
  1190. invdr = 0;
  1191. else
  1192. invdr = 1/r;
  1193. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]) * invdr * invdr * invdr;
  1194. trgVal[i] += dot*OOFP;
  1195. }
  1196. }
  1197. return;
  1198. }
  1199. void stokesStressSSE(
  1200. const int ns,
  1201. const int nt,
  1202. const double *sx,
  1203. const double *sy,
  1204. const double *sz,
  1205. const double *tx,
  1206. const double *ty,
  1207. const double *tz,
  1208. const double *srcDen,
  1209. double *trgVal)
  1210. {
  1211. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  1212. abort();
  1213. double TOFP = -3.0/(4.0*const_pi<double>());
  1214. __m128d tempxx; __m128d tempxy; __m128d tempxz;
  1215. __m128d tempyx; __m128d tempyy; __m128d tempyz;
  1216. __m128d tempzx; __m128d tempzy; __m128d tempzz;
  1217. double aux_arr[9*SIMD_LEN+1];
  1218. double *tempvalxx, *tempvalxy, *tempvalxz;
  1219. double *tempvalyx, *tempvalyy, *tempvalyz;
  1220. double *tempvalzx, *tempvalzy, *tempvalzz;
  1221. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  1222. {
  1223. tempvalxx = aux_arr + 1;
  1224. if (size_t(tempvalxx)%IDEAL_ALIGNMENT)
  1225. abort();
  1226. }
  1227. else
  1228. tempvalxx = aux_arr;
  1229. tempvalxy=tempvalxx+SIMD_LEN;
  1230. tempvalxz=tempvalxy+SIMD_LEN;
  1231. tempvalyx=tempvalxz+SIMD_LEN;
  1232. tempvalyy=tempvalyx+SIMD_LEN;
  1233. tempvalyz=tempvalyy+SIMD_LEN;
  1234. tempvalzx=tempvalyz+SIMD_LEN;
  1235. tempvalzy=tempvalzx+SIMD_LEN;
  1236. tempvalzz=tempvalzy+SIMD_LEN;
  1237. /*! One over eight pi */
  1238. __m128d tofp = _mm_set1_pd (TOFP);
  1239. __m128d half = _mm_set1_pd (0.5);
  1240. __m128d opf = _mm_set1_pd (1.5);
  1241. __m128d zero = _mm_setzero_pd ();
  1242. // loop over sources
  1243. int i = 0;
  1244. for (; i < nt; i++) {
  1245. tempxx = _mm_setzero_pd(); tempxy = _mm_setzero_pd(); tempxz = _mm_setzero_pd();
  1246. tempyx = _mm_setzero_pd(); tempyy = _mm_setzero_pd(); tempyz = _mm_setzero_pd();
  1247. tempzx = _mm_setzero_pd(); tempzy = _mm_setzero_pd(); tempzz = _mm_setzero_pd();
  1248. __m128d txi = _mm_load1_pd (&tx[i]);
  1249. __m128d tyi = _mm_load1_pd (&ty[i]);
  1250. __m128d tzi = _mm_load1_pd (&tz[i]);
  1251. int j = 0;
  1252. // Load and calculate in groups of SIMD_LEN
  1253. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1254. __m128d sxj = _mm_load_pd (&sx[j]);
  1255. __m128d syj = _mm_load_pd (&sy[j]);
  1256. __m128d szj = _mm_load_pd (&sz[j]);
  1257. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1258. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1259. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1260. __m128d dX, dY, dZ;
  1261. __m128d dR2;
  1262. __m128d S;
  1263. __m128d S2;
  1264. dX = _mm_sub_pd(txi , sxj);
  1265. dY = _mm_sub_pd(tyi , syj);
  1266. dZ = _mm_sub_pd(tzi , szj);
  1267. sxj = _mm_mul_pd(dX, dX);
  1268. syj = _mm_mul_pd(dY, dY);
  1269. szj = _mm_mul_pd(dZ, dZ);
  1270. dR2 = _mm_add_pd(sxj, syj);
  1271. dR2 = _mm_add_pd(szj, dR2);
  1272. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1273. __m128d xhalf = _mm_mul_pd (half, dR2);
  1274. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1275. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1276. __m128d S_d = _mm_cvtps_pd(S_s);
  1277. // To handle the condition when src and trg coincide
  1278. S_d = _mm_andnot_pd (temp, S_d);
  1279. S = _mm_mul_pd (S_d, S_d);
  1280. S = _mm_mul_pd (S, xhalf);
  1281. S = _mm_sub_pd (opf, S);
  1282. S = _mm_mul_pd (S, S_d);
  1283. S2 = _mm_mul_pd (S, S);
  1284. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1285. __m128d doty = _mm_mul_pd (dY, sdeny);
  1286. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1287. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1288. dot_sum = _mm_add_pd (dot_sum, dotz);
  1289. dot_sum = _mm_mul_pd (dot_sum, S);
  1290. dot_sum = _mm_mul_pd (dot_sum, S2);
  1291. dot_sum = _mm_mul_pd (dot_sum, S2);
  1292. dotx = _mm_mul_pd (dot_sum, dX);
  1293. doty = _mm_mul_pd (dot_sum, dY);
  1294. dotz = _mm_mul_pd (dot_sum, dZ);
  1295. tempxx = _mm_add_pd (_mm_mul_pd(dotx,dX), tempxx);
  1296. tempxy = _mm_add_pd (_mm_mul_pd(dotx,dY), tempxy);
  1297. tempxz = _mm_add_pd (_mm_mul_pd(dotx,dZ), tempxz);
  1298. tempyx = _mm_add_pd (_mm_mul_pd(doty,dX), tempyx);
  1299. tempyy = _mm_add_pd (_mm_mul_pd(doty,dY), tempyy);
  1300. tempyz = _mm_add_pd (_mm_mul_pd(doty,dZ), tempyz);
  1301. tempzx = _mm_add_pd (_mm_mul_pd(dotz,dX), tempzx);
  1302. tempzy = _mm_add_pd (_mm_mul_pd(dotz,dY), tempzy);
  1303. tempzz = _mm_add_pd (_mm_mul_pd(dotz,dZ), tempzz);
  1304. }
  1305. tempxx = _mm_mul_pd (tempxx, tofp);
  1306. tempxy = _mm_mul_pd (tempxy, tofp);
  1307. tempxz = _mm_mul_pd (tempxz, tofp);
  1308. tempyx = _mm_mul_pd (tempyx, tofp);
  1309. tempyy = _mm_mul_pd (tempyy, tofp);
  1310. tempyz = _mm_mul_pd (tempyz, tofp);
  1311. tempzx = _mm_mul_pd (tempzx, tofp);
  1312. tempzy = _mm_mul_pd (tempzy, tofp);
  1313. tempzz = _mm_mul_pd (tempzz, tofp);
  1314. _mm_store_pd(tempvalxx, tempxx); _mm_store_pd(tempvalxy, tempxy); _mm_store_pd(tempvalxz, tempxz);
  1315. _mm_store_pd(tempvalyx, tempyx); _mm_store_pd(tempvalyy, tempyy); _mm_store_pd(tempvalyz, tempyz);
  1316. _mm_store_pd(tempvalzx, tempzx); _mm_store_pd(tempvalzy, tempzy); _mm_store_pd(tempvalzz, tempzz);
  1317. for (int k = 0; k < SIMD_LEN; k++) {
  1318. trgVal[i*9 ] += tempvalxx[k];
  1319. trgVal[i*9+1] += tempvalxy[k];
  1320. trgVal[i*9+2] += tempvalxz[k];
  1321. trgVal[i*9+3] += tempvalyx[k];
  1322. trgVal[i*9+4] += tempvalyy[k];
  1323. trgVal[i*9+5] += tempvalyz[k];
  1324. trgVal[i*9+6] += tempvalzx[k];
  1325. trgVal[i*9+7] += tempvalzy[k];
  1326. trgVal[i*9+8] += tempvalzz[k];
  1327. }
  1328. for (; j < ns; j++) {
  1329. double x = tx[i] - sx[j];
  1330. double y = ty[i] - sy[j];
  1331. double z = tz[i] - sz[j];
  1332. double r2 = x*x + y*y + z*z;
  1333. double r = sqrt(r2);
  1334. double invdr;
  1335. if (r == 0)
  1336. invdr = 0;
  1337. else
  1338. invdr = 1/r;
  1339. double invdr2=invdr*invdr;
  1340. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]) * invdr2 * invdr2 * invdr;
  1341. double denx = dot*x;
  1342. double deny = dot*y;
  1343. double denz = dot*z;
  1344. trgVal[i*9 ] += denx*x*TOFP;
  1345. trgVal[i*9+1] += denx*y*TOFP;
  1346. trgVal[i*9+2] += denx*z*TOFP;
  1347. trgVal[i*9+3] += deny*x*TOFP;
  1348. trgVal[i*9+4] += deny*y*TOFP;
  1349. trgVal[i*9+5] += deny*z*TOFP;
  1350. trgVal[i*9+6] += denz*x*TOFP;
  1351. trgVal[i*9+7] += denz*y*TOFP;
  1352. trgVal[i*9+8] += denz*z*TOFP;
  1353. }
  1354. }
  1355. return;
  1356. }
  1357. void stokesGradSSE(
  1358. const int ns,
  1359. const int nt,
  1360. const double *sx,
  1361. const double *sy,
  1362. const double *sz,
  1363. const double *tx,
  1364. const double *ty,
  1365. const double *tz,
  1366. const double *srcDen,
  1367. double *trgVal,
  1368. const double cof )
  1369. {
  1370. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  1371. abort();
  1372. double mu = cof;
  1373. double OOEP = 1.0/(8.0*const_pi<double>());
  1374. __m128d tempxx; __m128d tempxy; __m128d tempxz;
  1375. __m128d tempyx; __m128d tempyy; __m128d tempyz;
  1376. __m128d tempzx; __m128d tempzy; __m128d tempzz;
  1377. double oomeu = 1/mu;
  1378. double aux_arr[9*SIMD_LEN+1];
  1379. double *tempvalxx, *tempvalxy, *tempvalxz;
  1380. double *tempvalyx, *tempvalyy, *tempvalyz;
  1381. double *tempvalzx, *tempvalzy, *tempvalzz;
  1382. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  1383. {
  1384. tempvalxx = aux_arr + 1;
  1385. if (size_t(tempvalxx)%IDEAL_ALIGNMENT)
  1386. abort();
  1387. }
  1388. else
  1389. tempvalxx = aux_arr;
  1390. tempvalxy=tempvalxx+SIMD_LEN;
  1391. tempvalxz=tempvalxy+SIMD_LEN;
  1392. tempvalyx=tempvalxz+SIMD_LEN;
  1393. tempvalyy=tempvalyx+SIMD_LEN;
  1394. tempvalyz=tempvalyy+SIMD_LEN;
  1395. tempvalzx=tempvalyz+SIMD_LEN;
  1396. tempvalzy=tempvalzx+SIMD_LEN;
  1397. tempvalzz=tempvalzy+SIMD_LEN;
  1398. /*! One over eight pi */
  1399. __m128d ooep = _mm_set1_pd (OOEP);
  1400. __m128d half = _mm_set1_pd (0.5);
  1401. __m128d opf = _mm_set1_pd (1.5);
  1402. __m128d three = _mm_set1_pd (3.0);
  1403. __m128d zero = _mm_setzero_pd ();
  1404. __m128d oomu = _mm_set1_pd (1/mu);
  1405. __m128d ooepmu = _mm_mul_pd(ooep,oomu);
  1406. // loop over sources
  1407. int i = 0;
  1408. for (; i < nt; i++) {
  1409. tempxx = _mm_setzero_pd(); tempxy = _mm_setzero_pd(); tempxz = _mm_setzero_pd();
  1410. tempyx = _mm_setzero_pd(); tempyy = _mm_setzero_pd(); tempyz = _mm_setzero_pd();
  1411. tempzx = _mm_setzero_pd(); tempzy = _mm_setzero_pd(); tempzz = _mm_setzero_pd();
  1412. __m128d txi = _mm_load1_pd (&tx[i]);
  1413. __m128d tyi = _mm_load1_pd (&ty[i]);
  1414. __m128d tzi = _mm_load1_pd (&tz[i]);
  1415. int j = 0;
  1416. // Load and calculate in groups of SIMD_LEN
  1417. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1418. __m128d sxj = _mm_load_pd (&sx[j]);
  1419. __m128d syj = _mm_load_pd (&sy[j]);
  1420. __m128d szj = _mm_load_pd (&sz[j]);
  1421. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1422. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1423. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1424. __m128d dX, dY, dZ;
  1425. __m128d dR2;
  1426. __m128d S;
  1427. __m128d S2;
  1428. __m128d S3;
  1429. dX = _mm_sub_pd(txi , sxj);
  1430. dY = _mm_sub_pd(tyi , syj);
  1431. dZ = _mm_sub_pd(tzi , szj);
  1432. sxj = _mm_mul_pd(dX, dX);
  1433. syj = _mm_mul_pd(dY, dY);
  1434. szj = _mm_mul_pd(dZ, dZ);
  1435. dR2 = _mm_add_pd(sxj, syj);
  1436. dR2 = _mm_add_pd(szj, dR2);
  1437. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1438. __m128d xhalf = _mm_mul_pd (half, dR2);
  1439. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1440. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1441. __m128d S_d = _mm_cvtps_pd(S_s);
  1442. // To handle the condition when src and trg coincide
  1443. S_d = _mm_andnot_pd (temp, S_d);
  1444. S = _mm_mul_pd (S_d, S_d);
  1445. S = _mm_mul_pd (S, xhalf);
  1446. S = _mm_sub_pd (opf, S);
  1447. S = _mm_mul_pd (S, S_d);
  1448. S2 = _mm_mul_pd (S, S);
  1449. S3 = _mm_mul_pd (S2, S);
  1450. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1451. __m128d doty = _mm_mul_pd (dY, sdeny);
  1452. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1453. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1454. dot_sum = _mm_add_pd (dot_sum, dotz);
  1455. dot_sum = _mm_mul_pd (dot_sum, S2);
  1456. tempxx = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dX, sdenx), _mm_mul_pd(sdenx, dX)), _mm_mul_pd(dot_sum, _mm_sub_pd(dR2 , _mm_mul_pd(three, _mm_mul_pd(dX, dX)))))),tempxx);
  1457. tempxy = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dY, sdenx), _mm_mul_pd(sdeny, dX)), _mm_mul_pd(dot_sum, _mm_sub_pd(zero, _mm_mul_pd(three, _mm_mul_pd(dY, dX)))))),tempxy);
  1458. tempxz = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dZ, sdenx), _mm_mul_pd(sdenz, dX)), _mm_mul_pd(dot_sum, _mm_sub_pd(zero, _mm_mul_pd(three, _mm_mul_pd(dZ, dX)))))),tempxz);
  1459. tempyx = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dX, sdeny), _mm_mul_pd(sdenx, dY)), _mm_mul_pd(dot_sum, _mm_sub_pd(zero, _mm_mul_pd(three, _mm_mul_pd(dX, dY)))))),tempyx);
  1460. tempyy = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dY, sdeny), _mm_mul_pd(sdeny, dY)), _mm_mul_pd(dot_sum, _mm_sub_pd(dR2 , _mm_mul_pd(three, _mm_mul_pd(dY, dY)))))),tempyy);
  1461. tempyz = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dZ, sdeny), _mm_mul_pd(sdenz, dY)), _mm_mul_pd(dot_sum, _mm_sub_pd(zero, _mm_mul_pd(three, _mm_mul_pd(dZ, dY)))))),tempyz);
  1462. tempzx = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dX, sdenz), _mm_mul_pd(sdenx, dZ)), _mm_mul_pd(dot_sum, _mm_sub_pd(zero, _mm_mul_pd(three, _mm_mul_pd(dX, dZ)))))),tempzx);
  1463. tempzy = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dY, sdenz), _mm_mul_pd(sdeny, dZ)), _mm_mul_pd(dot_sum, _mm_sub_pd(zero, _mm_mul_pd(three, _mm_mul_pd(dY, dZ)))))),tempzy);
  1464. tempzz = _mm_add_pd(_mm_mul_pd(S3,_mm_add_pd(_mm_sub_pd(_mm_mul_pd(dZ, sdenz), _mm_mul_pd(sdenz, dZ)), _mm_mul_pd(dot_sum, _mm_sub_pd(dR2 , _mm_mul_pd(three, _mm_mul_pd(dZ, dZ)))))),tempzz);
  1465. }
  1466. tempxx = _mm_mul_pd (tempxx, ooepmu);
  1467. tempxy = _mm_mul_pd (tempxy, ooepmu);
  1468. tempxz = _mm_mul_pd (tempxz, ooepmu);
  1469. tempyx = _mm_mul_pd (tempyx, ooepmu);
  1470. tempyy = _mm_mul_pd (tempyy, ooepmu);
  1471. tempyz = _mm_mul_pd (tempyz, ooepmu);
  1472. tempzx = _mm_mul_pd (tempzx, ooepmu);
  1473. tempzy = _mm_mul_pd (tempzy, ooepmu);
  1474. tempzz = _mm_mul_pd (tempzz, ooepmu);
  1475. _mm_store_pd(tempvalxx, tempxx); _mm_store_pd(tempvalxy, tempxy); _mm_store_pd(tempvalxz, tempxz);
  1476. _mm_store_pd(tempvalyx, tempyx); _mm_store_pd(tempvalyy, tempyy); _mm_store_pd(tempvalyz, tempyz);
  1477. _mm_store_pd(tempvalzx, tempzx); _mm_store_pd(tempvalzy, tempzy); _mm_store_pd(tempvalzz, tempzz);
  1478. for (int k = 0; k < SIMD_LEN; k++) {
  1479. trgVal[i*9 ] += tempvalxx[k];
  1480. trgVal[i*9+1] += tempvalxy[k];
  1481. trgVal[i*9+2] += tempvalxz[k];
  1482. trgVal[i*9+3] += tempvalyx[k];
  1483. trgVal[i*9+4] += tempvalyy[k];
  1484. trgVal[i*9+5] += tempvalyz[k];
  1485. trgVal[i*9+6] += tempvalzx[k];
  1486. trgVal[i*9+7] += tempvalzy[k];
  1487. trgVal[i*9+8] += tempvalzz[k];
  1488. }
  1489. for (; j < ns; j++) {
  1490. double x = tx[i] - sx[j];
  1491. double y = ty[i] - sy[j];
  1492. double z = tz[i] - sz[j];
  1493. double r2 = x*x + y*y + z*z;
  1494. double r = sqrt(r2);
  1495. double invdr;
  1496. if (r == 0)
  1497. invdr = 0;
  1498. else
  1499. invdr = 1/r;
  1500. double invdr2=invdr*invdr;
  1501. double invdr3=invdr2*invdr;
  1502. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]);
  1503. trgVal[i*9 ] += OOEP*oomeu*invdr3*( x*srcDen[j*3 ] - srcDen[j*3 ]*x + dot*(1-3*x*x*invdr2) );
  1504. trgVal[i*9+1] += OOEP*oomeu*invdr3*( y*srcDen[j*3 ] - srcDen[j*3+1]*x + dot*(0-3*y*x*invdr2) );
  1505. trgVal[i*9+2] += OOEP*oomeu*invdr3*( z*srcDen[j*3 ] - srcDen[j*3+2]*x + dot*(0-3*z*x*invdr2) );
  1506. trgVal[i*9+3] += OOEP*oomeu*invdr3*( x*srcDen[j*3+1] - srcDen[j*3 ]*y + dot*(0-3*x*y*invdr2) );
  1507. trgVal[i*9+4] += OOEP*oomeu*invdr3*( y*srcDen[j*3+1] - srcDen[j*3+1]*y + dot*(1-3*y*y*invdr2) );
  1508. trgVal[i*9+5] += OOEP*oomeu*invdr3*( z*srcDen[j*3+1] - srcDen[j*3+2]*y + dot*(0-3*z*y*invdr2) );
  1509. trgVal[i*9+6] += OOEP*oomeu*invdr3*( x*srcDen[j*3+2] - srcDen[j*3 ]*z + dot*(0-3*x*z*invdr2) );
  1510. trgVal[i*9+7] += OOEP*oomeu*invdr3*( y*srcDen[j*3+2] - srcDen[j*3+1]*z + dot*(0-3*y*z*invdr2) );
  1511. trgVal[i*9+8] += OOEP*oomeu*invdr3*( z*srcDen[j*3+2] - srcDen[j*3+2]*z + dot*(1-3*z*z*invdr2) );
  1512. }
  1513. }
  1514. return;
  1515. }
  1516. #undef SIMD_LEN
  1517. #define X(s,k) (s)[(k)*COORD_DIM]
  1518. #define Y(s,k) (s)[(k)*COORD_DIM+1]
  1519. #define Z(s,k) (s)[(k)*COORD_DIM+2]
  1520. void stokesDirectSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], const double kernel_coef, mem::MemoryManager* mem_mgr=NULL)
  1521. {
  1522. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1523. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1524. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1525. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1526. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1527. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1528. //1. reshuffle memory
  1529. for (int k =0;k<ns;k++){
  1530. xs[k+x_shift]=X(src,k);
  1531. ys[k+y_shift]=Y(src,k);
  1532. zs[k+z_shift]=Z(src,k);
  1533. }
  1534. for (int k=0;k<nt;k++){
  1535. xt[k]=X(trg,k);
  1536. yt[k]=Y(trg,k);
  1537. zt[k]=Z(trg,k);
  1538. }
  1539. //2. perform caclulation
  1540. stokesDirectVecSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot,kernel_coef);
  1541. return;
  1542. }
  1543. void stokesPressureSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  1544. {
  1545. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1546. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1547. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1548. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1549. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1550. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1551. //1. reshuffle memory
  1552. for (int k =0;k<ns;k++){
  1553. xs[k+x_shift]=X(src,k);
  1554. ys[k+y_shift]=Y(src,k);
  1555. zs[k+z_shift]=Z(src,k);
  1556. }
  1557. for (int k=0;k<nt;k++){
  1558. xt[k]=X(trg,k);
  1559. yt[k]=Y(trg,k);
  1560. zt[k]=Z(trg,k);
  1561. }
  1562. //2. perform caclulation
  1563. stokesPressureSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  1564. return;
  1565. }
  1566. void stokesStressSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  1567. {
  1568. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1569. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1570. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1571. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1572. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1573. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1574. //1. reshuffle memory
  1575. for (int k =0;k<ns;k++){
  1576. xs[k+x_shift]=X(src,k);
  1577. ys[k+y_shift]=Y(src,k);
  1578. zs[k+z_shift]=Z(src,k);
  1579. }
  1580. for (int k=0;k<nt;k++){
  1581. xt[k]=X(trg,k);
  1582. yt[k]=Y(trg,k);
  1583. zt[k]=Z(trg,k);
  1584. }
  1585. //2. perform caclulation
  1586. stokesStressSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  1587. return;
  1588. }
  1589. void stokesGradSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], const double kernel_coef, mem::MemoryManager* mem_mgr=NULL)
  1590. {
  1591. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1592. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1593. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1594. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1595. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1596. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1597. //1. reshuffle memory
  1598. for (int k =0;k<ns;k++){
  1599. xs[k+x_shift]=X(src,k);
  1600. ys[k+y_shift]=Y(src,k);
  1601. zs[k+z_shift]=Z(src,k);
  1602. }
  1603. for (int k=0;k<nt;k++){
  1604. xt[k]=X(trg,k);
  1605. yt[k]=Y(trg,k);
  1606. zt[k]=Z(trg,k);
  1607. }
  1608. //2. perform caclulation
  1609. stokesGradSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot,kernel_coef);
  1610. return;
  1611. }
  1612. #undef X
  1613. #undef Y
  1614. #undef Z
  1615. #undef IDEAL_ALIGNMENT
  1616. #undef DECL_SIMD_ALIGNED
  1617. }
  1618. template <>
  1619. void stokes_vel<double>(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1620. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(28*dof));
  1621. const T mu=1.0;
  1622. stokesDirectSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mu, mem_mgr);
  1623. }
  1624. template <>
  1625. void stokes_press<double>(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1626. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(17*dof));
  1627. stokesPressureSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mem_mgr);
  1628. return;
  1629. }
  1630. template <>
  1631. void stokes_stress<double>(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1632. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(45*dof));
  1633. stokesStressSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mem_mgr);
  1634. }
  1635. template <>
  1636. void stokes_grad<double>(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1637. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(89*dof));
  1638. const T mu=1.0;
  1639. stokesGradSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mu, mem_mgr);
  1640. }
  1641. #endif
  1642. #endif
  1643. ////////////////////////////////////////////////////////////////////////////////
  1644. //////// BIOT-SAVART KERNEL ////////
  1645. ////////////////////////////////////////////////////////////////////////////////
  1646. template <class T>
  1647. void biot_savart(T* r_src, int src_cnt, T* v_src_, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1648. #ifndef __MIC__
  1649. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(26*dof));
  1650. #endif
  1651. const T OOFP = -1.0/(4.0*const_pi<T>());
  1652. for(int t=0;t<trg_cnt;t++){
  1653. for(int i=0;i<dof;i++){
  1654. T p[3]={0,0,0};
  1655. for(int s=0;s<src_cnt;s++){
  1656. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  1657. r_trg[3*t+1]-r_src[3*s+1],
  1658. r_trg[3*t+2]-r_src[3*s+2]};
  1659. T R2 = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  1660. if (R2!=0){
  1661. T invR2=1.0/R2;
  1662. T invR=sqrt(invR2);
  1663. T invR3=invR*invR2;
  1664. T v_src[3]={v_src_[(s*dof+i)*3 ],
  1665. v_src_[(s*dof+i)*3+1],
  1666. v_src_[(s*dof+i)*3+2]};
  1667. p[0] -= (v_src[1]*dR[2]-v_src[2]*dR[1])*invR3;
  1668. p[1] -= (v_src[2]*dR[0]-v_src[0]*dR[2])*invR3;
  1669. p[2] -= (v_src[0]*dR[1]-v_src[1]*dR[0])*invR3;
  1670. }
  1671. }
  1672. k_out[(t*dof+i)*3+0] += p[0]*OOFP;
  1673. k_out[(t*dof+i)*3+1] += p[1]*OOFP;
  1674. k_out[(t*dof+i)*3+2] += p[2]*OOFP;
  1675. }
  1676. }
  1677. }
  1678. ////////////////////////////////////////////////////////////////////////////////
  1679. //////// HELMHOLTZ KERNEL ////////
  1680. ////////////////////////////////////////////////////////////////////////////////
  1681. /**
  1682. * \brief Green's function for the Helmholtz's equation. Kernel tensor
  1683. * dimension = 2x2.
  1684. */
  1685. template <class T>
  1686. void helmholtz_poten(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1687. #ifndef __MIC__
  1688. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(24*dof));
  1689. #endif
  1690. const T mu = (20.0*const_pi<T>());
  1691. for(int t=0;t<trg_cnt;t++){
  1692. for(int i=0;i<dof;i++){
  1693. T p[2]={0,0};
  1694. for(int s=0;s<src_cnt;s++){
  1695. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  1696. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  1697. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  1698. T R = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  1699. if (R!=0){
  1700. R = sqrt(R);
  1701. T invR=1.0/R;
  1702. T G[2]={cos(mu*R)*invR, sin(mu*R)*invR};
  1703. p[0] += v_src[(s*dof+i)*2+0]*G[0] - v_src[(s*dof+i)*2+1]*G[1];
  1704. p[1] += v_src[(s*dof+i)*2+0]*G[1] + v_src[(s*dof+i)*2+1]*G[0];
  1705. }
  1706. }
  1707. k_out[(t*dof+i)*2+0] += p[0];
  1708. k_out[(t*dof+i)*2+1] += p[1];
  1709. }
  1710. }
  1711. }
  1712. template <class T>
  1713. void helmholtz_grad(T* r_src, int src_cnt, T* v_src, int dof, T* r_trg, int trg_cnt, T* k_out, mem::MemoryManager* mem_mgr){
  1714. //TODO Implement this.
  1715. }
  1716. }//end namespace