kernel.txx 62 KB

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