kernel.txx 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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_dbl_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){
  796. #ifndef __MIC__
  797. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(32*dof));
  798. #endif
  799. const T mu=1.0;
  800. const T SOEPMU = -6.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 dX_reg=r_trg[3*t ]-r_src[3*s ];
  806. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  807. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  808. T R = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  809. if (R!=0){
  810. R = sqrt(R);
  811. T invR=1.0/R;
  812. T invR5=invR*invR*invR*invR*invR;
  813. T inner_prod =(v_src[(s*dof+i)*6+0]*dX_reg +
  814. v_src[(s*dof+i)*6+1]*dY_reg +
  815. v_src[(s*dof+i)*6+2]*dZ_reg)*
  816. (v_src[(s*dof+i)*6+3]*dX_reg +
  817. v_src[(s*dof+i)*6+4]*dY_reg +
  818. v_src[(s*dof+i)*6+5]*dZ_reg)*invR5;
  819. p[0] += dX_reg*inner_prod;
  820. p[1] += dY_reg*inner_prod;
  821. p[2] += dZ_reg*inner_prod;
  822. }
  823. }
  824. k_out[(t*dof+i)*3+0] += p[0]*SOEPMU;
  825. k_out[(t*dof+i)*3+1] += p[1]*SOEPMU;
  826. k_out[(t*dof+i)*3+2] += p[2]*SOEPMU;
  827. }
  828. }
  829. }
  830. template <class T>
  831. 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){
  832. #ifndef __MIC__
  833. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(17*dof));
  834. #endif
  835. const T OOFP = 1.0/(4.0*const_pi<T>());
  836. for(int t=0;t<trg_cnt;t++){
  837. for(int i=0;i<dof;i++){
  838. T p=0;
  839. for(int s=0;s<src_cnt;s++){
  840. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  841. r_trg[3*t+1]-r_src[3*s+1],
  842. r_trg[3*t+2]-r_src[3*s+2]};
  843. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  844. if (R!=0){
  845. T invR2=1.0/R;
  846. T invR=sqrt(invR2);
  847. T invR3=invR2*invR;
  848. T v_src[3]={v_src_[(s*dof+i)*3 ],
  849. v_src_[(s*dof+i)*3+1],
  850. v_src_[(s*dof+i)*3+2]};
  851. T inner_prod=(v_src[0]*dR[0] +
  852. v_src[1]*dR[1] +
  853. v_src[2]*dR[2])* invR3;
  854. p += inner_prod;
  855. }
  856. }
  857. k_out[t*dof+i] += p*OOFP;
  858. }
  859. }
  860. }
  861. template <class T>
  862. 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){
  863. #ifndef __MIC__
  864. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(45*dof));
  865. #endif
  866. const T TOFP = -3.0/(4.0*const_pi<T>());
  867. for(int t=0;t<trg_cnt;t++){
  868. for(int i=0;i<dof;i++){
  869. T p[9]={0,0,0,
  870. 0,0,0,
  871. 0,0,0};
  872. for(int s=0;s<src_cnt;s++){
  873. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  874. r_trg[3*t+1]-r_src[3*s+1],
  875. r_trg[3*t+2]-r_src[3*s+2]};
  876. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  877. if (R!=0){
  878. T invR2=1.0/R;
  879. T invR=sqrt(invR2);
  880. T invR3=invR2*invR;
  881. T invR5=invR3*invR2;
  882. T v_src[3]={v_src_[(s*dof+i)*3 ],
  883. v_src_[(s*dof+i)*3+1],
  884. v_src_[(s*dof+i)*3+2]};
  885. T inner_prod=(v_src[0]*dR[0] +
  886. v_src[1]*dR[1] +
  887. v_src[2]*dR[2])* invR5;
  888. p[0] += inner_prod*dR[0]*dR[0]; p[1] += inner_prod*dR[1]*dR[0]; p[2] += inner_prod*dR[2]*dR[0];
  889. p[3] += inner_prod*dR[0]*dR[1]; p[4] += inner_prod*dR[1]*dR[1]; p[5] += inner_prod*dR[2]*dR[1];
  890. p[6] += inner_prod*dR[0]*dR[2]; p[7] += inner_prod*dR[1]*dR[2]; p[8] += inner_prod*dR[2]*dR[2];
  891. }
  892. }
  893. k_out[(t*dof+i)*9+0] += p[0]*TOFP;
  894. k_out[(t*dof+i)*9+1] += p[1]*TOFP;
  895. k_out[(t*dof+i)*9+2] += p[2]*TOFP;
  896. k_out[(t*dof+i)*9+3] += p[3]*TOFP;
  897. k_out[(t*dof+i)*9+4] += p[4]*TOFP;
  898. k_out[(t*dof+i)*9+5] += p[5]*TOFP;
  899. k_out[(t*dof+i)*9+6] += p[6]*TOFP;
  900. k_out[(t*dof+i)*9+7] += p[7]*TOFP;
  901. k_out[(t*dof+i)*9+8] += p[8]*TOFP;
  902. }
  903. }
  904. }
  905. template <class T>
  906. 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){
  907. #ifndef __MIC__
  908. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(89*dof));
  909. #endif
  910. const T mu=1.0;
  911. const T OOEPMU = 1.0/(8.0*const_pi<T>()*mu);
  912. for(int t=0;t<trg_cnt;t++){
  913. for(int i=0;i<dof;i++){
  914. T p[9]={0,0,0,
  915. 0,0,0,
  916. 0,0,0};
  917. for(int s=0;s<src_cnt;s++){
  918. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  919. r_trg[3*t+1]-r_src[3*s+1],
  920. r_trg[3*t+2]-r_src[3*s+2]};
  921. T R = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  922. if (R!=0){
  923. T invR2=1.0/R;
  924. T invR=sqrt(invR2);
  925. T invR3=invR2*invR;
  926. T v_src[3]={v_src_[(s*dof+i)*3 ],
  927. v_src_[(s*dof+i)*3+1],
  928. v_src_[(s*dof+i)*3+2]};
  929. T inner_prod=(v_src[0]*dR[0] +
  930. v_src[1]*dR[1] +
  931. v_src[2]*dR[2]);
  932. p[0] += ( inner_prod*(1-3*dR[0]*dR[0]*invR2))*invR3; //6
  933. p[1] += (dR[1]*v_src[0]-v_src[1]*dR[0]+inner_prod*( -3*dR[1]*dR[0]*invR2))*invR3; //9
  934. p[2] += (dR[2]*v_src[0]-v_src[2]*dR[0]+inner_prod*( -3*dR[2]*dR[0]*invR2))*invR3;
  935. p[3] += (dR[0]*v_src[1]-v_src[0]*dR[1]+inner_prod*( -3*dR[0]*dR[1]*invR2))*invR3;
  936. p[4] += ( inner_prod*(1-3*dR[1]*dR[1]*invR2))*invR3;
  937. p[5] += (dR[2]*v_src[1]-v_src[2]*dR[1]+inner_prod*( -3*dR[2]*dR[1]*invR2))*invR3;
  938. p[6] += (dR[0]*v_src[2]-v_src[0]*dR[2]+inner_prod*( -3*dR[0]*dR[2]*invR2))*invR3;
  939. p[7] += (dR[1]*v_src[2]-v_src[1]*dR[2]+inner_prod*( -3*dR[1]*dR[2]*invR2))*invR3;
  940. p[8] += ( inner_prod*(1-3*dR[2]*dR[2]*invR2))*invR3;
  941. }
  942. }
  943. k_out[(t*dof+i)*9+0] += p[0]*OOEPMU;
  944. k_out[(t*dof+i)*9+1] += p[1]*OOEPMU;
  945. k_out[(t*dof+i)*9+2] += p[2]*OOEPMU;
  946. k_out[(t*dof+i)*9+3] += p[3]*OOEPMU;
  947. k_out[(t*dof+i)*9+4] += p[4]*OOEPMU;
  948. k_out[(t*dof+i)*9+5] += p[5]*OOEPMU;
  949. k_out[(t*dof+i)*9+6] += p[6]*OOEPMU;
  950. k_out[(t*dof+i)*9+7] += p[7]*OOEPMU;
  951. k_out[(t*dof+i)*9+8] += p[8]*OOEPMU;
  952. }
  953. }
  954. }
  955. #ifndef __MIC__
  956. #ifdef USE_SSE
  957. namespace
  958. {
  959. #define IDEAL_ALIGNMENT 16
  960. #define SIMD_LEN (int)(IDEAL_ALIGNMENT / sizeof(double))
  961. #define DECL_SIMD_ALIGNED __declspec(align(IDEAL_ALIGNMENT))
  962. void stokesDirectVecSSE(
  963. const int ns,
  964. const int nt,
  965. const double *sx,
  966. const double *sy,
  967. const double *sz,
  968. const double *tx,
  969. const double *ty,
  970. const double *tz,
  971. const double *srcDen,
  972. double *trgVal,
  973. const double cof )
  974. {
  975. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  976. abort();
  977. double mu = cof;
  978. double OOEP = 1.0/(8.0*const_pi<double>());
  979. __m128d tempx;
  980. __m128d tempy;
  981. __m128d tempz;
  982. double oomeu = 1/mu;
  983. double aux_arr[3*SIMD_LEN+1];
  984. double *tempvalx;
  985. double *tempvaly;
  986. double *tempvalz;
  987. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  988. {
  989. tempvalx = aux_arr + 1;
  990. if (size_t(tempvalx)%IDEAL_ALIGNMENT)
  991. abort();
  992. }
  993. else
  994. tempvalx = aux_arr;
  995. tempvaly=tempvalx+SIMD_LEN;
  996. tempvalz=tempvaly+SIMD_LEN;
  997. /*! One over eight pi */
  998. __m128d ooep = _mm_set1_pd (OOEP);
  999. __m128d half = _mm_set1_pd (0.5);
  1000. __m128d opf = _mm_set1_pd (1.5);
  1001. __m128d zero = _mm_setzero_pd ();
  1002. __m128d oomu = _mm_set1_pd (1/mu);
  1003. // loop over sources
  1004. int i = 0;
  1005. for (; i < nt; i++) {
  1006. tempx = _mm_setzero_pd();
  1007. tempy = _mm_setzero_pd();
  1008. tempz = _mm_setzero_pd();
  1009. __m128d txi = _mm_load1_pd (&tx[i]);
  1010. __m128d tyi = _mm_load1_pd (&ty[i]);
  1011. __m128d tzi = _mm_load1_pd (&tz[i]);
  1012. int j = 0;
  1013. // Load and calculate in groups of SIMD_LEN
  1014. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1015. __m128d sxj = _mm_load_pd (&sx[j]);
  1016. __m128d syj = _mm_load_pd (&sy[j]);
  1017. __m128d szj = _mm_load_pd (&sz[j]);
  1018. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1019. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1020. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1021. __m128d dX, dY, dZ;
  1022. __m128d dR2;
  1023. __m128d S;
  1024. dX = _mm_sub_pd(txi , sxj);
  1025. dY = _mm_sub_pd(tyi , syj);
  1026. dZ = _mm_sub_pd(tzi , szj);
  1027. sxj = _mm_mul_pd(dX, dX);
  1028. syj = _mm_mul_pd(dY, dY);
  1029. szj = _mm_mul_pd(dZ, dZ);
  1030. dR2 = _mm_add_pd(sxj, syj);
  1031. dR2 = _mm_add_pd(szj, dR2);
  1032. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1033. __m128d xhalf = _mm_mul_pd (half, dR2);
  1034. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1035. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1036. __m128d S_d = _mm_cvtps_pd(S_s);
  1037. // To handle the condition when src and trg coincide
  1038. S_d = _mm_andnot_pd (temp, S_d);
  1039. S = _mm_mul_pd (S_d, S_d);
  1040. S = _mm_mul_pd (S, xhalf);
  1041. S = _mm_sub_pd (opf, S);
  1042. S = _mm_mul_pd (S, S_d);
  1043. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1044. __m128d doty = _mm_mul_pd (dY, sdeny);
  1045. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1046. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1047. dot_sum = _mm_add_pd (dot_sum, dotz);
  1048. dot_sum = _mm_mul_pd (dot_sum, S);
  1049. dot_sum = _mm_mul_pd (dot_sum, S);
  1050. dotx = _mm_mul_pd (dot_sum, dX);
  1051. doty = _mm_mul_pd (dot_sum, dY);
  1052. dotz = _mm_mul_pd (dot_sum, dZ);
  1053. sdenx = _mm_add_pd (sdenx, dotx);
  1054. sdeny = _mm_add_pd (sdeny, doty);
  1055. sdenz = _mm_add_pd (sdenz, dotz);
  1056. sdenx = _mm_mul_pd (sdenx, S);
  1057. sdeny = _mm_mul_pd (sdeny, S);
  1058. sdenz = _mm_mul_pd (sdenz, S);
  1059. tempx = _mm_add_pd (sdenx, tempx);
  1060. tempy = _mm_add_pd (sdeny, tempy);
  1061. tempz = _mm_add_pd (sdenz, tempz);
  1062. }
  1063. tempx = _mm_mul_pd (tempx, ooep);
  1064. tempy = _mm_mul_pd (tempy, ooep);
  1065. tempz = _mm_mul_pd (tempz, ooep);
  1066. tempx = _mm_mul_pd (tempx, oomu);
  1067. tempy = _mm_mul_pd (tempy, oomu);
  1068. tempz = _mm_mul_pd (tempz, oomu);
  1069. _mm_store_pd(tempvalx, tempx);
  1070. _mm_store_pd(tempvaly, tempy);
  1071. _mm_store_pd(tempvalz, tempz);
  1072. for (int k = 0; k < SIMD_LEN; k++) {
  1073. trgVal[i*3] += tempvalx[k];
  1074. trgVal[i*3+1] += tempvaly[k];
  1075. trgVal[i*3+2] += tempvalz[k];
  1076. }
  1077. for (; j < ns; j++) {
  1078. double x = tx[i] - sx[j];
  1079. double y = ty[i] - sy[j];
  1080. double z = tz[i] - sz[j];
  1081. double r2 = x*x + y*y + z*z;
  1082. double r = sqrt(r2);
  1083. double invdr;
  1084. if (r == 0)
  1085. invdr = 0;
  1086. else
  1087. invdr = 1/r;
  1088. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]) * invdr * invdr;
  1089. double denx = srcDen[j*3] + dot*x;
  1090. double deny = srcDen[j*3+1] + dot*y;
  1091. double denz = srcDen[j*3+2] + dot*z;
  1092. trgVal[i*3] += denx*invdr*OOEP*oomeu;
  1093. trgVal[i*3+1] += deny*invdr*OOEP*oomeu;
  1094. trgVal[i*3+2] += denz*invdr*OOEP*oomeu;
  1095. }
  1096. }
  1097. return;
  1098. }
  1099. void stokesPressureSSE(
  1100. const int ns,
  1101. const int nt,
  1102. const double *sx,
  1103. const double *sy,
  1104. const double *sz,
  1105. const double *tx,
  1106. const double *ty,
  1107. const double *tz,
  1108. const double *srcDen,
  1109. double *trgVal)
  1110. {
  1111. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  1112. abort();
  1113. double OOFP = 1.0/(4.0*const_pi<double>());
  1114. __m128d temp_press;
  1115. double aux_arr[SIMD_LEN+1];
  1116. double *tempval_press;
  1117. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  1118. {
  1119. tempval_press = aux_arr + 1;
  1120. if (size_t(tempval_press)%IDEAL_ALIGNMENT)
  1121. abort();
  1122. }
  1123. else
  1124. tempval_press = aux_arr;
  1125. /*! One over eight pi */
  1126. __m128d oofp = _mm_set1_pd (OOFP);
  1127. __m128d half = _mm_set1_pd (0.5);
  1128. __m128d opf = _mm_set1_pd (1.5);
  1129. __m128d zero = _mm_setzero_pd ();
  1130. // loop over sources
  1131. int i = 0;
  1132. for (; i < nt; i++) {
  1133. temp_press = _mm_setzero_pd();
  1134. __m128d txi = _mm_load1_pd (&tx[i]);
  1135. __m128d tyi = _mm_load1_pd (&ty[i]);
  1136. __m128d tzi = _mm_load1_pd (&tz[i]);
  1137. int j = 0;
  1138. // Load and calculate in groups of SIMD_LEN
  1139. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1140. __m128d sxj = _mm_load_pd (&sx[j]);
  1141. __m128d syj = _mm_load_pd (&sy[j]);
  1142. __m128d szj = _mm_load_pd (&sz[j]);
  1143. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1144. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1145. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1146. __m128d dX, dY, dZ;
  1147. __m128d dR2;
  1148. __m128d S;
  1149. dX = _mm_sub_pd(txi , sxj);
  1150. dY = _mm_sub_pd(tyi , syj);
  1151. dZ = _mm_sub_pd(tzi , szj);
  1152. sxj = _mm_mul_pd(dX, dX);
  1153. syj = _mm_mul_pd(dY, dY);
  1154. szj = _mm_mul_pd(dZ, dZ);
  1155. dR2 = _mm_add_pd(sxj, syj);
  1156. dR2 = _mm_add_pd(szj, dR2);
  1157. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1158. __m128d xhalf = _mm_mul_pd (half, dR2);
  1159. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1160. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1161. __m128d S_d = _mm_cvtps_pd(S_s);
  1162. // To handle the condition when src and trg coincide
  1163. S_d = _mm_andnot_pd (temp, S_d);
  1164. S = _mm_mul_pd (S_d, S_d);
  1165. S = _mm_mul_pd (S, xhalf);
  1166. S = _mm_sub_pd (opf, S);
  1167. S = _mm_mul_pd (S, S_d);
  1168. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1169. __m128d doty = _mm_mul_pd (dY, sdeny);
  1170. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1171. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1172. dot_sum = _mm_add_pd (dot_sum, dotz);
  1173. dot_sum = _mm_mul_pd (dot_sum, S);
  1174. dot_sum = _mm_mul_pd (dot_sum, S);
  1175. dot_sum = _mm_mul_pd (dot_sum, S);
  1176. temp_press = _mm_add_pd (dot_sum, temp_press);
  1177. }
  1178. temp_press = _mm_mul_pd (temp_press, oofp);
  1179. _mm_store_pd(tempval_press, temp_press);
  1180. for (int k = 0; k < SIMD_LEN; k++) {
  1181. trgVal[i] += tempval_press[k];
  1182. }
  1183. for (; j < ns; j++) {
  1184. double x = tx[i] - sx[j];
  1185. double y = ty[i] - sy[j];
  1186. double z = tz[i] - sz[j];
  1187. double r2 = x*x + y*y + z*z;
  1188. double r = sqrt(r2);
  1189. double invdr;
  1190. if (r == 0)
  1191. invdr = 0;
  1192. else
  1193. invdr = 1/r;
  1194. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]) * invdr * invdr * invdr;
  1195. trgVal[i] += dot*OOFP;
  1196. }
  1197. }
  1198. return;
  1199. }
  1200. void stokesStressSSE(
  1201. const int ns,
  1202. const int nt,
  1203. const double *sx,
  1204. const double *sy,
  1205. const double *sz,
  1206. const double *tx,
  1207. const double *ty,
  1208. const double *tz,
  1209. const double *srcDen,
  1210. double *trgVal)
  1211. {
  1212. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  1213. abort();
  1214. double TOFP = -3.0/(4.0*const_pi<double>());
  1215. __m128d tempxx; __m128d tempxy; __m128d tempxz;
  1216. __m128d tempyx; __m128d tempyy; __m128d tempyz;
  1217. __m128d tempzx; __m128d tempzy; __m128d tempzz;
  1218. double aux_arr[9*SIMD_LEN+1];
  1219. double *tempvalxx, *tempvalxy, *tempvalxz;
  1220. double *tempvalyx, *tempvalyy, *tempvalyz;
  1221. double *tempvalzx, *tempvalzy, *tempvalzz;
  1222. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  1223. {
  1224. tempvalxx = aux_arr + 1;
  1225. if (size_t(tempvalxx)%IDEAL_ALIGNMENT)
  1226. abort();
  1227. }
  1228. else
  1229. tempvalxx = aux_arr;
  1230. tempvalxy=tempvalxx+SIMD_LEN;
  1231. tempvalxz=tempvalxy+SIMD_LEN;
  1232. tempvalyx=tempvalxz+SIMD_LEN;
  1233. tempvalyy=tempvalyx+SIMD_LEN;
  1234. tempvalyz=tempvalyy+SIMD_LEN;
  1235. tempvalzx=tempvalyz+SIMD_LEN;
  1236. tempvalzy=tempvalzx+SIMD_LEN;
  1237. tempvalzz=tempvalzy+SIMD_LEN;
  1238. /*! One over eight pi */
  1239. __m128d tofp = _mm_set1_pd (TOFP);
  1240. __m128d half = _mm_set1_pd (0.5);
  1241. __m128d opf = _mm_set1_pd (1.5);
  1242. __m128d zero = _mm_setzero_pd ();
  1243. // loop over sources
  1244. int i = 0;
  1245. for (; i < nt; i++) {
  1246. tempxx = _mm_setzero_pd(); tempxy = _mm_setzero_pd(); tempxz = _mm_setzero_pd();
  1247. tempyx = _mm_setzero_pd(); tempyy = _mm_setzero_pd(); tempyz = _mm_setzero_pd();
  1248. tempzx = _mm_setzero_pd(); tempzy = _mm_setzero_pd(); tempzz = _mm_setzero_pd();
  1249. __m128d txi = _mm_load1_pd (&tx[i]);
  1250. __m128d tyi = _mm_load1_pd (&ty[i]);
  1251. __m128d tzi = _mm_load1_pd (&tz[i]);
  1252. int j = 0;
  1253. // Load and calculate in groups of SIMD_LEN
  1254. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1255. __m128d sxj = _mm_load_pd (&sx[j]);
  1256. __m128d syj = _mm_load_pd (&sy[j]);
  1257. __m128d szj = _mm_load_pd (&sz[j]);
  1258. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1259. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1260. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1261. __m128d dX, dY, dZ;
  1262. __m128d dR2;
  1263. __m128d S;
  1264. __m128d S2;
  1265. dX = _mm_sub_pd(txi , sxj);
  1266. dY = _mm_sub_pd(tyi , syj);
  1267. dZ = _mm_sub_pd(tzi , szj);
  1268. sxj = _mm_mul_pd(dX, dX);
  1269. syj = _mm_mul_pd(dY, dY);
  1270. szj = _mm_mul_pd(dZ, dZ);
  1271. dR2 = _mm_add_pd(sxj, syj);
  1272. dR2 = _mm_add_pd(szj, dR2);
  1273. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1274. __m128d xhalf = _mm_mul_pd (half, dR2);
  1275. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1276. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1277. __m128d S_d = _mm_cvtps_pd(S_s);
  1278. // To handle the condition when src and trg coincide
  1279. S_d = _mm_andnot_pd (temp, S_d);
  1280. S = _mm_mul_pd (S_d, S_d);
  1281. S = _mm_mul_pd (S, xhalf);
  1282. S = _mm_sub_pd (opf, S);
  1283. S = _mm_mul_pd (S, S_d);
  1284. S2 = _mm_mul_pd (S, S);
  1285. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1286. __m128d doty = _mm_mul_pd (dY, sdeny);
  1287. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1288. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1289. dot_sum = _mm_add_pd (dot_sum, dotz);
  1290. dot_sum = _mm_mul_pd (dot_sum, S);
  1291. dot_sum = _mm_mul_pd (dot_sum, S2);
  1292. dot_sum = _mm_mul_pd (dot_sum, S2);
  1293. dotx = _mm_mul_pd (dot_sum, dX);
  1294. doty = _mm_mul_pd (dot_sum, dY);
  1295. dotz = _mm_mul_pd (dot_sum, dZ);
  1296. tempxx = _mm_add_pd (_mm_mul_pd(dotx,dX), tempxx);
  1297. tempxy = _mm_add_pd (_mm_mul_pd(dotx,dY), tempxy);
  1298. tempxz = _mm_add_pd (_mm_mul_pd(dotx,dZ), tempxz);
  1299. tempyx = _mm_add_pd (_mm_mul_pd(doty,dX), tempyx);
  1300. tempyy = _mm_add_pd (_mm_mul_pd(doty,dY), tempyy);
  1301. tempyz = _mm_add_pd (_mm_mul_pd(doty,dZ), tempyz);
  1302. tempzx = _mm_add_pd (_mm_mul_pd(dotz,dX), tempzx);
  1303. tempzy = _mm_add_pd (_mm_mul_pd(dotz,dY), tempzy);
  1304. tempzz = _mm_add_pd (_mm_mul_pd(dotz,dZ), tempzz);
  1305. }
  1306. tempxx = _mm_mul_pd (tempxx, tofp);
  1307. tempxy = _mm_mul_pd (tempxy, tofp);
  1308. tempxz = _mm_mul_pd (tempxz, tofp);
  1309. tempyx = _mm_mul_pd (tempyx, tofp);
  1310. tempyy = _mm_mul_pd (tempyy, tofp);
  1311. tempyz = _mm_mul_pd (tempyz, tofp);
  1312. tempzx = _mm_mul_pd (tempzx, tofp);
  1313. tempzy = _mm_mul_pd (tempzy, tofp);
  1314. tempzz = _mm_mul_pd (tempzz, tofp);
  1315. _mm_store_pd(tempvalxx, tempxx); _mm_store_pd(tempvalxy, tempxy); _mm_store_pd(tempvalxz, tempxz);
  1316. _mm_store_pd(tempvalyx, tempyx); _mm_store_pd(tempvalyy, tempyy); _mm_store_pd(tempvalyz, tempyz);
  1317. _mm_store_pd(tempvalzx, tempzx); _mm_store_pd(tempvalzy, tempzy); _mm_store_pd(tempvalzz, tempzz);
  1318. for (int k = 0; k < SIMD_LEN; k++) {
  1319. trgVal[i*9 ] += tempvalxx[k];
  1320. trgVal[i*9+1] += tempvalxy[k];
  1321. trgVal[i*9+2] += tempvalxz[k];
  1322. trgVal[i*9+3] += tempvalyx[k];
  1323. trgVal[i*9+4] += tempvalyy[k];
  1324. trgVal[i*9+5] += tempvalyz[k];
  1325. trgVal[i*9+6] += tempvalzx[k];
  1326. trgVal[i*9+7] += tempvalzy[k];
  1327. trgVal[i*9+8] += tempvalzz[k];
  1328. }
  1329. for (; j < ns; j++) {
  1330. double x = tx[i] - sx[j];
  1331. double y = ty[i] - sy[j];
  1332. double z = tz[i] - sz[j];
  1333. double r2 = x*x + y*y + z*z;
  1334. double r = sqrt(r2);
  1335. double invdr;
  1336. if (r == 0)
  1337. invdr = 0;
  1338. else
  1339. invdr = 1/r;
  1340. double invdr2=invdr*invdr;
  1341. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]) * invdr2 * invdr2 * invdr;
  1342. double denx = dot*x;
  1343. double deny = dot*y;
  1344. double denz = dot*z;
  1345. trgVal[i*9 ] += denx*x*TOFP;
  1346. trgVal[i*9+1] += denx*y*TOFP;
  1347. trgVal[i*9+2] += denx*z*TOFP;
  1348. trgVal[i*9+3] += deny*x*TOFP;
  1349. trgVal[i*9+4] += deny*y*TOFP;
  1350. trgVal[i*9+5] += deny*z*TOFP;
  1351. trgVal[i*9+6] += denz*x*TOFP;
  1352. trgVal[i*9+7] += denz*y*TOFP;
  1353. trgVal[i*9+8] += denz*z*TOFP;
  1354. }
  1355. }
  1356. return;
  1357. }
  1358. void stokesGradSSE(
  1359. const int ns,
  1360. const int nt,
  1361. const double *sx,
  1362. const double *sy,
  1363. const double *sz,
  1364. const double *tx,
  1365. const double *ty,
  1366. const double *tz,
  1367. const double *srcDen,
  1368. double *trgVal,
  1369. const double cof )
  1370. {
  1371. if ( size_t(sx)%IDEAL_ALIGNMENT || size_t(sy)%IDEAL_ALIGNMENT || size_t(sz)%IDEAL_ALIGNMENT )
  1372. abort();
  1373. double mu = cof;
  1374. double OOEP = 1.0/(8.0*const_pi<double>());
  1375. __m128d tempxx; __m128d tempxy; __m128d tempxz;
  1376. __m128d tempyx; __m128d tempyy; __m128d tempyz;
  1377. __m128d tempzx; __m128d tempzy; __m128d tempzz;
  1378. double oomeu = 1/mu;
  1379. double aux_arr[9*SIMD_LEN+1];
  1380. double *tempvalxx, *tempvalxy, *tempvalxz;
  1381. double *tempvalyx, *tempvalyy, *tempvalyz;
  1382. double *tempvalzx, *tempvalzy, *tempvalzz;
  1383. if (size_t(aux_arr)%IDEAL_ALIGNMENT) // if aux_arr is misaligned
  1384. {
  1385. tempvalxx = aux_arr + 1;
  1386. if (size_t(tempvalxx)%IDEAL_ALIGNMENT)
  1387. abort();
  1388. }
  1389. else
  1390. tempvalxx = aux_arr;
  1391. tempvalxy=tempvalxx+SIMD_LEN;
  1392. tempvalxz=tempvalxy+SIMD_LEN;
  1393. tempvalyx=tempvalxz+SIMD_LEN;
  1394. tempvalyy=tempvalyx+SIMD_LEN;
  1395. tempvalyz=tempvalyy+SIMD_LEN;
  1396. tempvalzx=tempvalyz+SIMD_LEN;
  1397. tempvalzy=tempvalzx+SIMD_LEN;
  1398. tempvalzz=tempvalzy+SIMD_LEN;
  1399. /*! One over eight pi */
  1400. __m128d ooep = _mm_set1_pd (OOEP);
  1401. __m128d half = _mm_set1_pd (0.5);
  1402. __m128d opf = _mm_set1_pd (1.5);
  1403. __m128d three = _mm_set1_pd (3.0);
  1404. __m128d zero = _mm_setzero_pd ();
  1405. __m128d oomu = _mm_set1_pd (1/mu);
  1406. __m128d ooepmu = _mm_mul_pd(ooep,oomu);
  1407. // loop over sources
  1408. int i = 0;
  1409. for (; i < nt; i++) {
  1410. tempxx = _mm_setzero_pd(); tempxy = _mm_setzero_pd(); tempxz = _mm_setzero_pd();
  1411. tempyx = _mm_setzero_pd(); tempyy = _mm_setzero_pd(); tempyz = _mm_setzero_pd();
  1412. tempzx = _mm_setzero_pd(); tempzy = _mm_setzero_pd(); tempzz = _mm_setzero_pd();
  1413. __m128d txi = _mm_load1_pd (&tx[i]);
  1414. __m128d tyi = _mm_load1_pd (&ty[i]);
  1415. __m128d tzi = _mm_load1_pd (&tz[i]);
  1416. int j = 0;
  1417. // Load and calculate in groups of SIMD_LEN
  1418. for (; j + SIMD_LEN <= ns; j+=SIMD_LEN) {
  1419. __m128d sxj = _mm_load_pd (&sx[j]);
  1420. __m128d syj = _mm_load_pd (&sy[j]);
  1421. __m128d szj = _mm_load_pd (&sz[j]);
  1422. __m128d sdenx = _mm_set_pd (srcDen[(j+1)*3], srcDen[j*3]);
  1423. __m128d sdeny = _mm_set_pd (srcDen[(j+1)*3+1], srcDen[j*3+1]);
  1424. __m128d sdenz = _mm_set_pd (srcDen[(j+1)*3+2], srcDen[j*3+2]);
  1425. __m128d dX, dY, dZ;
  1426. __m128d dR2;
  1427. __m128d S;
  1428. __m128d S2;
  1429. __m128d S3;
  1430. dX = _mm_sub_pd(txi , sxj);
  1431. dY = _mm_sub_pd(tyi , syj);
  1432. dZ = _mm_sub_pd(tzi , szj);
  1433. sxj = _mm_mul_pd(dX, dX);
  1434. syj = _mm_mul_pd(dY, dY);
  1435. szj = _mm_mul_pd(dZ, dZ);
  1436. dR2 = _mm_add_pd(sxj, syj);
  1437. dR2 = _mm_add_pd(szj, dR2);
  1438. __m128d temp = _mm_cmpeq_pd (dR2, zero);
  1439. __m128d xhalf = _mm_mul_pd (half, dR2);
  1440. __m128 dR2_s = _mm_cvtpd_ps(dR2);
  1441. __m128 S_s = _mm_rsqrt_ps(dR2_s);
  1442. __m128d S_d = _mm_cvtps_pd(S_s);
  1443. // To handle the condition when src and trg coincide
  1444. S_d = _mm_andnot_pd (temp, S_d);
  1445. S = _mm_mul_pd (S_d, S_d);
  1446. S = _mm_mul_pd (S, xhalf);
  1447. S = _mm_sub_pd (opf, S);
  1448. S = _mm_mul_pd (S, S_d);
  1449. S2 = _mm_mul_pd (S, S);
  1450. S3 = _mm_mul_pd (S2, S);
  1451. __m128d dotx = _mm_mul_pd (dX, sdenx);
  1452. __m128d doty = _mm_mul_pd (dY, sdeny);
  1453. __m128d dotz = _mm_mul_pd (dZ, sdenz);
  1454. __m128d dot_sum = _mm_add_pd (dotx, doty);
  1455. dot_sum = _mm_add_pd (dot_sum, dotz);
  1456. dot_sum = _mm_mul_pd (dot_sum, S2);
  1457. 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);
  1458. 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);
  1459. 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);
  1460. 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);
  1461. 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);
  1462. 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);
  1463. 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);
  1464. 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);
  1465. 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);
  1466. }
  1467. tempxx = _mm_mul_pd (tempxx, ooepmu);
  1468. tempxy = _mm_mul_pd (tempxy, ooepmu);
  1469. tempxz = _mm_mul_pd (tempxz, ooepmu);
  1470. tempyx = _mm_mul_pd (tempyx, ooepmu);
  1471. tempyy = _mm_mul_pd (tempyy, ooepmu);
  1472. tempyz = _mm_mul_pd (tempyz, ooepmu);
  1473. tempzx = _mm_mul_pd (tempzx, ooepmu);
  1474. tempzy = _mm_mul_pd (tempzy, ooepmu);
  1475. tempzz = _mm_mul_pd (tempzz, ooepmu);
  1476. _mm_store_pd(tempvalxx, tempxx); _mm_store_pd(tempvalxy, tempxy); _mm_store_pd(tempvalxz, tempxz);
  1477. _mm_store_pd(tempvalyx, tempyx); _mm_store_pd(tempvalyy, tempyy); _mm_store_pd(tempvalyz, tempyz);
  1478. _mm_store_pd(tempvalzx, tempzx); _mm_store_pd(tempvalzy, tempzy); _mm_store_pd(tempvalzz, tempzz);
  1479. for (int k = 0; k < SIMD_LEN; k++) {
  1480. trgVal[i*9 ] += tempvalxx[k];
  1481. trgVal[i*9+1] += tempvalxy[k];
  1482. trgVal[i*9+2] += tempvalxz[k];
  1483. trgVal[i*9+3] += tempvalyx[k];
  1484. trgVal[i*9+4] += tempvalyy[k];
  1485. trgVal[i*9+5] += tempvalyz[k];
  1486. trgVal[i*9+6] += tempvalzx[k];
  1487. trgVal[i*9+7] += tempvalzy[k];
  1488. trgVal[i*9+8] += tempvalzz[k];
  1489. }
  1490. for (; j < ns; j++) {
  1491. double x = tx[i] - sx[j];
  1492. double y = ty[i] - sy[j];
  1493. double z = tz[i] - sz[j];
  1494. double r2 = x*x + y*y + z*z;
  1495. double r = sqrt(r2);
  1496. double invdr;
  1497. if (r == 0)
  1498. invdr = 0;
  1499. else
  1500. invdr = 1/r;
  1501. double invdr2=invdr*invdr;
  1502. double invdr3=invdr2*invdr;
  1503. double dot = (x*srcDen[j*3] + y*srcDen[j*3+1] + z*srcDen[j*3+2]);
  1504. trgVal[i*9 ] += OOEP*oomeu*invdr3*( x*srcDen[j*3 ] - srcDen[j*3 ]*x + dot*(1-3*x*x*invdr2) );
  1505. trgVal[i*9+1] += OOEP*oomeu*invdr3*( y*srcDen[j*3 ] - srcDen[j*3+1]*x + dot*(0-3*y*x*invdr2) );
  1506. trgVal[i*9+2] += OOEP*oomeu*invdr3*( z*srcDen[j*3 ] - srcDen[j*3+2]*x + dot*(0-3*z*x*invdr2) );
  1507. trgVal[i*9+3] += OOEP*oomeu*invdr3*( x*srcDen[j*3+1] - srcDen[j*3 ]*y + dot*(0-3*x*y*invdr2) );
  1508. trgVal[i*9+4] += OOEP*oomeu*invdr3*( y*srcDen[j*3+1] - srcDen[j*3+1]*y + dot*(1-3*y*y*invdr2) );
  1509. trgVal[i*9+5] += OOEP*oomeu*invdr3*( z*srcDen[j*3+1] - srcDen[j*3+2]*y + dot*(0-3*z*y*invdr2) );
  1510. trgVal[i*9+6] += OOEP*oomeu*invdr3*( x*srcDen[j*3+2] - srcDen[j*3 ]*z + dot*(0-3*x*z*invdr2) );
  1511. trgVal[i*9+7] += OOEP*oomeu*invdr3*( y*srcDen[j*3+2] - srcDen[j*3+1]*z + dot*(0-3*y*z*invdr2) );
  1512. trgVal[i*9+8] += OOEP*oomeu*invdr3*( z*srcDen[j*3+2] - srcDen[j*3+2]*z + dot*(1-3*z*z*invdr2) );
  1513. }
  1514. }
  1515. return;
  1516. }
  1517. #undef SIMD_LEN
  1518. #define X(s,k) (s)[(k)*COORD_DIM]
  1519. #define Y(s,k) (s)[(k)*COORD_DIM+1]
  1520. #define Z(s,k) (s)[(k)*COORD_DIM+2]
  1521. 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)
  1522. {
  1523. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1524. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1525. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1526. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1527. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1528. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1529. //1. reshuffle memory
  1530. for (int k =0;k<ns;k++){
  1531. xs[k+x_shift]=X(src,k);
  1532. ys[k+y_shift]=Y(src,k);
  1533. zs[k+z_shift]=Z(src,k);
  1534. }
  1535. for (int k=0;k<nt;k++){
  1536. xt[k]=X(trg,k);
  1537. yt[k]=Y(trg,k);
  1538. zt[k]=Z(trg,k);
  1539. }
  1540. //2. perform caclulation
  1541. stokesDirectVecSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot,kernel_coef);
  1542. return;
  1543. }
  1544. void stokesPressureSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  1545. {
  1546. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1547. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1548. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1549. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1550. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1551. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1552. //1. reshuffle memory
  1553. for (int k =0;k<ns;k++){
  1554. xs[k+x_shift]=X(src,k);
  1555. ys[k+y_shift]=Y(src,k);
  1556. zs[k+z_shift]=Z(src,k);
  1557. }
  1558. for (int k=0;k<nt;k++){
  1559. xt[k]=X(trg,k);
  1560. yt[k]=Y(trg,k);
  1561. zt[k]=Z(trg,k);
  1562. }
  1563. //2. perform caclulation
  1564. stokesPressureSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  1565. return;
  1566. }
  1567. void stokesStressSSEShuffle(const int ns, const int nt, double const src[], double const trg[], double const den[], double pot[], mem::MemoryManager* mem_mgr=NULL)
  1568. {
  1569. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1570. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1571. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1572. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1573. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1574. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1575. //1. reshuffle memory
  1576. for (int k =0;k<ns;k++){
  1577. xs[k+x_shift]=X(src,k);
  1578. ys[k+y_shift]=Y(src,k);
  1579. zs[k+z_shift]=Z(src,k);
  1580. }
  1581. for (int k=0;k<nt;k++){
  1582. xt[k]=X(trg,k);
  1583. yt[k]=Y(trg,k);
  1584. zt[k]=Z(trg,k);
  1585. }
  1586. //2. perform caclulation
  1587. stokesStressSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot);
  1588. return;
  1589. }
  1590. 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)
  1591. {
  1592. std::vector<double> xs(ns+1); std::vector<double> xt(nt);
  1593. std::vector<double> ys(ns+1); std::vector<double> yt(nt);
  1594. std::vector<double> zs(ns+1); std::vector<double> zt(nt);
  1595. int x_shift = size_t(&xs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1596. int y_shift = size_t(&ys[0]) % IDEAL_ALIGNMENT ? 1:0;
  1597. int z_shift = size_t(&zs[0]) % IDEAL_ALIGNMENT ? 1:0;
  1598. //1. reshuffle memory
  1599. for (int k =0;k<ns;k++){
  1600. xs[k+x_shift]=X(src,k);
  1601. ys[k+y_shift]=Y(src,k);
  1602. zs[k+z_shift]=Z(src,k);
  1603. }
  1604. for (int k=0;k<nt;k++){
  1605. xt[k]=X(trg,k);
  1606. yt[k]=Y(trg,k);
  1607. zt[k]=Z(trg,k);
  1608. }
  1609. //2. perform caclulation
  1610. stokesGradSSE(ns,nt,&xs[x_shift],&ys[y_shift],&zs[z_shift],&xt[0],&yt[0],&zt[0],den,pot,kernel_coef);
  1611. return;
  1612. }
  1613. #undef X
  1614. #undef Y
  1615. #undef Z
  1616. #undef IDEAL_ALIGNMENT
  1617. #undef DECL_SIMD_ALIGNED
  1618. }
  1619. template <>
  1620. 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){
  1621. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(28*dof));
  1622. const T mu=1.0;
  1623. stokesDirectSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mu, mem_mgr);
  1624. }
  1625. template <>
  1626. 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){
  1627. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(17*dof));
  1628. stokesPressureSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mem_mgr);
  1629. return;
  1630. }
  1631. template <>
  1632. 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){
  1633. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(45*dof));
  1634. stokesStressSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mem_mgr);
  1635. }
  1636. template <>
  1637. 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){
  1638. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(89*dof));
  1639. const T mu=1.0;
  1640. stokesGradSSEShuffle(src_cnt, trg_cnt, r_src, r_trg, v_src_, k_out, mu, mem_mgr);
  1641. }
  1642. #endif
  1643. #endif
  1644. ////////////////////////////////////////////////////////////////////////////////
  1645. //////// BIOT-SAVART KERNEL ////////
  1646. ////////////////////////////////////////////////////////////////////////////////
  1647. template <class T>
  1648. 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){
  1649. #ifndef __MIC__
  1650. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(26*dof));
  1651. #endif
  1652. const T OOFP = -1.0/(4.0*const_pi<T>());
  1653. for(int t=0;t<trg_cnt;t++){
  1654. for(int i=0;i<dof;i++){
  1655. T p[3]={0,0,0};
  1656. for(int s=0;s<src_cnt;s++){
  1657. T dR[3]={r_trg[3*t ]-r_src[3*s ],
  1658. r_trg[3*t+1]-r_src[3*s+1],
  1659. r_trg[3*t+2]-r_src[3*s+2]};
  1660. T R2 = (dR[0]*dR[0]+dR[1]*dR[1]+dR[2]*dR[2]);
  1661. if (R2!=0){
  1662. T invR2=1.0/R2;
  1663. T invR=sqrt(invR2);
  1664. T invR3=invR*invR2;
  1665. T v_src[3]={v_src_[(s*dof+i)*3 ],
  1666. v_src_[(s*dof+i)*3+1],
  1667. v_src_[(s*dof+i)*3+2]};
  1668. p[0] -= (v_src[1]*dR[2]-v_src[2]*dR[1])*invR3;
  1669. p[1] -= (v_src[2]*dR[0]-v_src[0]*dR[2])*invR3;
  1670. p[2] -= (v_src[0]*dR[1]-v_src[1]*dR[0])*invR3;
  1671. }
  1672. }
  1673. k_out[(t*dof+i)*3+0] += p[0]*OOFP;
  1674. k_out[(t*dof+i)*3+1] += p[1]*OOFP;
  1675. k_out[(t*dof+i)*3+2] += p[2]*OOFP;
  1676. }
  1677. }
  1678. }
  1679. ////////////////////////////////////////////////////////////////////////////////
  1680. //////// HELMHOLTZ KERNEL ////////
  1681. ////////////////////////////////////////////////////////////////////////////////
  1682. /**
  1683. * \brief Green's function for the Helmholtz's equation. Kernel tensor
  1684. * dimension = 2x2.
  1685. */
  1686. template <class T>
  1687. 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){
  1688. #ifndef __MIC__
  1689. Profile::Add_FLOP((long long)trg_cnt*(long long)src_cnt*(24*dof));
  1690. #endif
  1691. const T mu = (20.0*const_pi<T>());
  1692. for(int t=0;t<trg_cnt;t++){
  1693. for(int i=0;i<dof;i++){
  1694. T p[2]={0,0};
  1695. for(int s=0;s<src_cnt;s++){
  1696. T dX_reg=r_trg[3*t ]-r_src[3*s ];
  1697. T dY_reg=r_trg[3*t+1]-r_src[3*s+1];
  1698. T dZ_reg=r_trg[3*t+2]-r_src[3*s+2];
  1699. T R = (dX_reg*dX_reg+dY_reg*dY_reg+dZ_reg*dZ_reg);
  1700. if (R!=0){
  1701. R = sqrt(R);
  1702. T invR=1.0/R;
  1703. T G[2]={cos(mu*R)*invR, sin(mu*R)*invR};
  1704. p[0] += v_src[(s*dof+i)*2+0]*G[0] - v_src[(s*dof+i)*2+1]*G[1];
  1705. p[1] += v_src[(s*dof+i)*2+0]*G[1] + v_src[(s*dof+i)*2+1]*G[0];
  1706. }
  1707. }
  1708. k_out[(t*dof+i)*2+0] += p[0];
  1709. k_out[(t*dof+i)*2+1] += p[1];
  1710. }
  1711. }
  1712. }
  1713. template <class T>
  1714. 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){
  1715. //TODO Implement this.
  1716. }
  1717. }//end namespace