fmm_tree.txx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /**
  2. * \file fmm_tree.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 12-11-2010
  5. * \brief This file contains the implementation of the class FMM_Tree.
  6. */
  7. #include <omp.h>
  8. #include <sstream>
  9. #include <iomanip>
  10. #include <cassert>
  11. #include <cstdlib>
  12. #include <mpi_node.hpp>
  13. #include <fmm_node.hpp>
  14. #include <mem_mgr.hpp>
  15. #include <mortonid.hpp>
  16. #include <profile.hpp>
  17. #include <vector.hpp>
  18. namespace pvfmm{
  19. template <class FMM_Mat_t>
  20. void FMM_Tree<FMM_Mat_t>::Initialize(typename Node_t::NodeData* init_data) {
  21. Profile::Tic("InitTree",this->Comm(),true);{
  22. //Build octree from points.
  23. MPI_Tree<Node_t>::Initialize(init_data);
  24. Profile::Tic("InitFMMData",this->Comm(),true,5);
  25. { //Initialize FMM data.
  26. std::vector<Node_t*>& nodes=this->GetNodeList();
  27. #pragma omp parallel for
  28. for(size_t i=0;i<nodes.size();i++){
  29. if(nodes[i]->FMMData()==NULL) nodes[i]->FMMData()=mem::aligned_new<typename FMM_Mat_t::FMMData>();
  30. }
  31. }
  32. Profile::Toc();
  33. }Profile::Toc();
  34. }
  35. template <class FMM_Mat_t>
  36. void FMM_Tree<FMM_Mat_t>::InitFMM_Tree(bool refine, BoundaryType bndry_) {
  37. Profile::Tic("InitFMM_Tree",this->Comm(),true);{
  38. interac_list.Initialize(this->Dim());
  39. bndry=bndry_;
  40. if(refine){
  41. //RefineTree
  42. Profile::Tic("RefineTree",this->Comm(),true,5);
  43. this->RefineTree();
  44. Profile::Toc();
  45. }
  46. //2:1 Balancing
  47. Profile::Tic("2:1Balance",this->Comm(),true,5);
  48. this->Balance21(bndry);
  49. Profile::Toc();
  50. //Redistribute nodes.
  51. Profile::Tic("Redistribute",this->Comm(),true,5);
  52. this->RedistNodes();
  53. Profile::Toc();
  54. }Profile::Toc();
  55. }
  56. template <class FMM_Mat_t>
  57. void FMM_Tree<FMM_Mat_t>::SetupFMM(FMM_Mat_t* fmm_mat_) {
  58. Profile::Tic("SetupFMM",this->Comm(),true);{
  59. typedef typename FMM_Mat_t::FMMTree_t MatTree_t;
  60. bool device=true;
  61. #ifdef __INTEL_OFFLOAD
  62. Profile::Tic("InitLocks",this->Comm(),false,3);
  63. MIC_Lock::init();
  64. Profile::Toc();
  65. #endif
  66. //int omp_p=omp_get_max_threads();
  67. if(fmm_mat!=fmm_mat_){ // Clear previous setup
  68. setup_data.clear();
  69. precomp_lst.clear();
  70. fmm_mat=fmm_mat_;
  71. }
  72. //Construct LET
  73. Profile::Tic("ConstructLET",this->Comm(),false,2);
  74. this->ConstructLET(bndry);
  75. Profile::Toc();
  76. //Set Colleagues (Needed to build U, V, W and X lists.)
  77. Profile::Tic("SetColleagues",this->Comm(),false,3);
  78. this->SetColleagues(bndry);
  79. Profile::Toc();
  80. Profile::Tic("BuildLists",this->Comm(),false,3);
  81. BuildInteracLists();
  82. Profile::Toc();
  83. Profile::Tic("CollectNodeData",this->Comm(),false,3);
  84. //Build node list.
  85. Node_t* n=dynamic_cast<Node_t*>(this->PostorderFirst());
  86. std::vector<Node_t*> all_nodes;
  87. while(n!=NULL){
  88. all_nodes.push_back(n);
  89. n=static_cast<Node_t*>(this->PostorderNxt(n));
  90. }
  91. //Collect node data into continuous array.
  92. std::vector<Vector<Node_t*> > node_lists; // TODO: Remove this parameter, not really needed
  93. fmm_mat->CollectNodeData((MatTree_t*)this,all_nodes, node_data_buff, node_lists);
  94. Profile::Toc();
  95. setup_data.resize(8*MAX_DEPTH);
  96. precomp_lst.resize(8);
  97. Profile::Tic("UListSetup",this->Comm(),false,3);
  98. for(size_t i=0;i<MAX_DEPTH;i++){
  99. setup_data[i+MAX_DEPTH*0].precomp_data=&precomp_lst[0];
  100. fmm_mat->U_ListSetup(setup_data[i+MAX_DEPTH*0],(MatTree_t*)this,node_data_buff,node_lists,fmm_mat->ScaleInvar()?(i==0?-1:MAX_DEPTH+1):i, device);
  101. }
  102. Profile::Toc();
  103. Profile::Tic("WListSetup",this->Comm(),false,3);
  104. for(size_t i=0;i<MAX_DEPTH;i++){
  105. setup_data[i+MAX_DEPTH*1].precomp_data=&precomp_lst[1];
  106. fmm_mat->W_ListSetup(setup_data[i+MAX_DEPTH*1],(MatTree_t*)this,node_data_buff,node_lists,fmm_mat->ScaleInvar()?(i==0?-1:MAX_DEPTH+1):i, device);
  107. }
  108. Profile::Toc();
  109. Profile::Tic("XListSetup",this->Comm(),false,3);
  110. for(size_t i=0;i<MAX_DEPTH;i++){
  111. setup_data[i+MAX_DEPTH*2].precomp_data=&precomp_lst[2];
  112. fmm_mat->X_ListSetup(setup_data[i+MAX_DEPTH*2],(MatTree_t*)this,node_data_buff,node_lists,fmm_mat->ScaleInvar()?(i==0?-1:MAX_DEPTH+1):i, device);
  113. }
  114. Profile::Toc();
  115. Profile::Tic("VListSetup",this->Comm(),false,3);
  116. for(size_t i=0;i<MAX_DEPTH;i++){
  117. setup_data[i+MAX_DEPTH*3].precomp_data=&precomp_lst[3];
  118. fmm_mat->V_ListSetup(setup_data[i+MAX_DEPTH*3],(MatTree_t*)this,node_data_buff,node_lists,fmm_mat->ScaleInvar()?(i==0?-1:MAX_DEPTH+1):i, /*device*/ false);
  119. }
  120. Profile::Toc();
  121. Profile::Tic("D2DSetup",this->Comm(),false,3);
  122. for(size_t i=0;i<MAX_DEPTH;i++){
  123. setup_data[i+MAX_DEPTH*4].precomp_data=&precomp_lst[4];
  124. fmm_mat->Down2DownSetup(setup_data[i+MAX_DEPTH*4],(MatTree_t*)this,node_data_buff,node_lists,i, /*device*/ false);
  125. }
  126. Profile::Toc();
  127. Profile::Tic("D2TSetup",this->Comm(),false,3);
  128. for(size_t i=0;i<MAX_DEPTH;i++){
  129. setup_data[i+MAX_DEPTH*5].precomp_data=&precomp_lst[5];
  130. fmm_mat->Down2TargetSetup(setup_data[i+MAX_DEPTH*5],(MatTree_t*)this,node_data_buff,node_lists,fmm_mat->ScaleInvar()?(i==0?-1:MAX_DEPTH+1):i, /*device*/ false);
  131. }
  132. Profile::Toc();
  133. Profile::Tic("S2USetup",this->Comm(),false,3);
  134. for(size_t i=0;i<MAX_DEPTH;i++){
  135. setup_data[i+MAX_DEPTH*6].precomp_data=&precomp_lst[6];
  136. fmm_mat->Source2UpSetup(setup_data[i+MAX_DEPTH*6],(MatTree_t*)this,node_data_buff,node_lists,fmm_mat->ScaleInvar()?(i==0?-1:MAX_DEPTH+1):i, /*device*/ false);
  137. }
  138. Profile::Toc();
  139. Profile::Tic("U2USetup",this->Comm(),false,3);
  140. for(size_t i=0;i<MAX_DEPTH;i++){
  141. setup_data[i+MAX_DEPTH*7].precomp_data=&precomp_lst[7];
  142. fmm_mat->Up2UpSetup(setup_data[i+MAX_DEPTH*7],(MatTree_t*)this,node_data_buff,node_lists,i, /*device*/ false);
  143. }
  144. Profile::Toc();
  145. #ifdef __INTEL_OFFLOAD
  146. int wait_lock_idx=-1;
  147. wait_lock_idx=MIC_Lock::curr_lock();
  148. #pragma offload target(mic:0)
  149. {MIC_Lock::wait_lock(wait_lock_idx);}
  150. #endif
  151. ClearFMMData();
  152. }Profile::Toc();
  153. }
  154. template <class FMM_Mat_t>
  155. void FMM_Tree<FMM_Mat_t>::ClearFMMData() {
  156. Profile::Tic("ClearFMMData",this->Comm(),true);{
  157. bool device=true;
  158. int omp_p=omp_get_max_threads();
  159. #pragma omp parallel for
  160. for(int j=0;j<omp_p;j++){
  161. Matrix<Real_t>* mat;
  162. mat=setup_data[0+MAX_DEPTH*1]. input_data;
  163. if(mat!=NULL){
  164. size_t a=(mat->Dim(0)*mat->Dim(1)*(j+0))/omp_p;
  165. size_t b=(mat->Dim(0)*mat->Dim(1)*(j+1))/omp_p;
  166. memset(&(*mat)[0][a],0,(b-a)*sizeof(Real_t));
  167. }
  168. mat=setup_data[0+MAX_DEPTH*2].output_data;
  169. if(mat!=NULL){
  170. size_t a=(mat->Dim(0)*mat->Dim(1)*(j+0))/omp_p;
  171. size_t b=(mat->Dim(0)*mat->Dim(1)*(j+1))/omp_p;
  172. memset(&(*mat)[0][a],0,(b-a)*sizeof(Real_t));
  173. }
  174. mat=setup_data[0+MAX_DEPTH*0].output_data;
  175. if(mat!=NULL){
  176. size_t a=(mat->Dim(0)*mat->Dim(1)*(j+0))/omp_p;
  177. size_t b=(mat->Dim(0)*mat->Dim(1)*(j+1))/omp_p;
  178. memset(&(*mat)[0][a],0,(b-a)*sizeof(Real_t));
  179. }
  180. }
  181. if(device){ // Host2Device
  182. if(setup_data[0+MAX_DEPTH*1]. input_data!=NULL) setup_data[0+MAX_DEPTH*1]. input_data->AllocDevice(true);
  183. if(setup_data[0+MAX_DEPTH*2].output_data!=NULL) setup_data[0+MAX_DEPTH*2].output_data->AllocDevice(true);
  184. if(setup_data[0+MAX_DEPTH*0].output_data!=NULL) setup_data[0+MAX_DEPTH*0].output_data->AllocDevice(true);
  185. #ifdef __INTEL_OFFLOAD
  186. if(!fmm_mat->ScaleInvar()){ // Wait
  187. int wait_lock_idx=-1;
  188. wait_lock_idx=MIC_Lock::curr_lock();
  189. #pragma offload target(mic:0)
  190. {MIC_Lock::wait_lock(wait_lock_idx);}
  191. }
  192. MIC_Lock::init();
  193. #endif
  194. }
  195. }Profile::Toc();
  196. }
  197. template <class FMM_Mat_t>
  198. void FMM_Tree<FMM_Mat_t>::RunFMM() {
  199. Profile::Tic("RunFMM",this->Comm(),true);{
  200. //Upward Pass
  201. Profile::Tic("UpwardPass",this->Comm(),false,2);
  202. UpwardPass();
  203. Profile::Toc();
  204. //Multipole Reduce Broadcast.
  205. Profile::Tic("ReduceBcast",this->Comm(),true,2);
  206. MultipoleReduceBcast();
  207. Profile::Toc();
  208. //Local 2:1 Balancing.
  209. //This can cause load imbalance, always use global 2:1 balance instead.
  210. //Profile::Tic("2:1Balance(local)",this->Comm(),false,3);
  211. //this->Balance21_local(bndry);
  212. //UpwardPass(true);
  213. //Profile::Toc();
  214. //Downward Pass
  215. Profile::Tic("DownwardPass",this->Comm(),true,2);
  216. DownwardPass();
  217. Profile::Toc();
  218. }Profile::Toc();
  219. }
  220. template <class FMM_Mat_t>
  221. void FMM_Tree<FMM_Mat_t>::UpwardPass() {
  222. //bool device=true;
  223. int max_depth=0;
  224. { // Get max_depth
  225. int max_depth_loc=0;
  226. std::vector<Node_t*>& nodes=this->GetNodeList();
  227. for(size_t i=0;i<nodes.size();i++){
  228. Node_t* n=nodes[i];
  229. if(n->Depth()>max_depth_loc) max_depth_loc=n->Depth();
  230. }
  231. MPI_Allreduce(&max_depth_loc, &max_depth, 1, MPI_INT, MPI_MAX, *this->Comm());
  232. }
  233. //Upward Pass (initialize all leaf nodes)
  234. Profile::Tic("S2U",this->Comm(),false,5);
  235. for(int i=0; i<=(fmm_mat->ScaleInvar()?0:max_depth); i++){ // Source2Up
  236. if(!fmm_mat->ScaleInvar()) fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*6],/*device*/ false);
  237. fmm_mat->Source2Up(setup_data[i+MAX_DEPTH*6]);
  238. }
  239. Profile::Toc();
  240. //Upward Pass (level by level)
  241. Profile::Tic("U2U",this->Comm(),false,5);
  242. for(int i=max_depth-1; i>=0; i--){ // Up2Up
  243. if(!fmm_mat->ScaleInvar()) fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*7],/*device*/ false);
  244. fmm_mat->Up2Up(setup_data[i+MAX_DEPTH*7]);
  245. }
  246. Profile::Toc();
  247. }
  248. template <class FMM_Mat_t>
  249. void FMM_Tree<FMM_Mat_t>::BuildInteracLists() {
  250. std::vector<Node_t*>& n_list=this->GetNodeList();
  251. size_t node_cnt=n_list.size();
  252. std::vector<Mat_Type> type_lst;
  253. type_lst.push_back(S2U_Type);
  254. type_lst.push_back(U2U_Type);
  255. type_lst.push_back(D2D_Type);
  256. type_lst.push_back(D2T_Type);
  257. type_lst.push_back(U0_Type );
  258. type_lst.push_back(U1_Type );
  259. type_lst.push_back(U2_Type );
  260. type_lst.push_back(W_Type );
  261. type_lst.push_back(X_Type );
  262. type_lst.push_back(V1_Type );
  263. size_t all_interac_cnt=0;
  264. pvfmm::Vector<size_t> interac_cnt(type_lst.size());
  265. for(size_t i=0;i<type_lst.size();i++){
  266. interac_cnt[i]=interac_list.ListCount(type_lst[i]);
  267. all_interac_cnt+=interac_cnt[i];
  268. }
  269. node_interac_lst.ReInit(node_cnt,all_interac_cnt);
  270. // Build interaction lists.
  271. int omp_p=omp_get_max_threads();
  272. #pragma omp parallel for
  273. for(int j=0;j<omp_p;j++){
  274. size_t a=(node_cnt*(j ))/omp_p;
  275. size_t b=(node_cnt*(j+1))/omp_p;
  276. for(size_t i=a;i<b;i++){
  277. size_t offset=0;
  278. Node_t* n=n_list[i];
  279. for(size_t k=0;k<type_lst.size();k++){
  280. n->interac_list[type_lst[k]].ReInit(interac_cnt[k],&node_interac_lst[i][offset],false);
  281. interac_list.BuildList(n,type_lst[k]);
  282. offset+=interac_cnt[k];
  283. }
  284. }
  285. }
  286. }
  287. template <class FMM_Mat_t>
  288. void FMM_Tree<FMM_Mat_t>::MultipoleReduceBcast() {
  289. int num_p,rank;
  290. MPI_Comm_size(*this->Comm(),&num_p);
  291. MPI_Comm_rank(*this->Comm(),&rank );
  292. if(num_p==1) return;
  293. Profile::Tic("Reduce",this->Comm(),true,3);
  294. std::vector<MortonId> mins=this->GetMins();
  295. size_t bit_mask=1;
  296. size_t max_child=(1UL<<this->Dim());
  297. //Initialize initial send nodes.
  298. std::vector<Node_t*> send_nodes[2];
  299. //Initialize send_node[0]
  300. Node_t* tmp_node=static_cast<Node_t*>(this->RootNode());
  301. assert(!tmp_node->IsGhost());
  302. while(!tmp_node->IsLeaf()){
  303. Node_t* tmp_node_=NULL;
  304. for(size_t i=0;i<max_child;i++){
  305. tmp_node_=static_cast<Node_t*>(tmp_node->Child(i));
  306. if(tmp_node_!=NULL) if(!tmp_node_->IsGhost()) break;
  307. }
  308. tmp_node=tmp_node_; assert(tmp_node!=NULL);
  309. }
  310. int n[2];
  311. n[0]=tmp_node->Depth()+1;
  312. send_nodes[0].resize(n[0]);
  313. send_nodes[0][n[0]-1]=tmp_node;
  314. for(int i=n[0]-1;i>0;i--)
  315. send_nodes[0][i-1]=static_cast<Node_t*>(send_nodes[0][i]->Parent());
  316. //Initialize send_node[1]
  317. tmp_node=static_cast<Node_t*>(this->RootNode());
  318. while(!tmp_node->IsLeaf()){
  319. Node_t* tmp_node_=NULL;
  320. for(int i=max_child-1;i>=0;i--){
  321. tmp_node_=static_cast<Node_t*>(tmp_node->Child(i));
  322. if(tmp_node_!=NULL) if(!tmp_node_->IsGhost()) break;
  323. }
  324. tmp_node=tmp_node_; assert(tmp_node!=NULL);
  325. }
  326. n[1]=tmp_node->Depth()+1;
  327. send_nodes[1].resize(n[1]);
  328. send_nodes[1][n[1]-1]=tmp_node;
  329. for(int i=n[1]-1;i>0;i--)
  330. send_nodes[1][i-1]=static_cast<Node_t*>(send_nodes[1][i]->Parent());
  331. //Hypercube reduction.
  332. while(bit_mask<(size_t)num_p){
  333. size_t partner=rank^bit_mask; //Partner process id
  334. int merge_indx=((bit_mask & rank)==0?1:0);
  335. bit_mask=bit_mask<<1;
  336. //if(rank >= num_p - (num_p % bit_mask)) break;
  337. //Initialize send data.
  338. size_t s_node_cnt[2]={send_nodes[0].size(),send_nodes[1].size()};
  339. int send_size=2*sizeof(size_t)+(s_node_cnt[0]+s_node_cnt[1])*sizeof(MortonId);
  340. std::vector<PackedData> send_data(s_node_cnt[0]+s_node_cnt[1]);
  341. size_t s_iter=0;
  342. for(int i=0;i<2;i++)
  343. for(size_t j=0;j<s_node_cnt[i];j++){
  344. assert(send_nodes[i][j]!=NULL);
  345. send_data[s_iter]=send_nodes[i][j]->PackMultipole();
  346. send_size+=send_data[s_iter].length+sizeof(size_t);
  347. s_iter++;
  348. }
  349. char* send_buff=mem::aligned_new<char>(send_size);
  350. char* buff_iter=send_buff;
  351. ((size_t*)buff_iter)[0]=s_node_cnt[0];
  352. ((size_t*)buff_iter)[1]=s_node_cnt[1];
  353. buff_iter+=2*sizeof(size_t);
  354. s_iter=0;
  355. for(int i=0;i<2;i++)
  356. for(size_t j=0;j<s_node_cnt[i];j++){
  357. ((MortonId*)buff_iter)[0]=send_nodes[i][j]->GetMortonId();
  358. buff_iter+=sizeof(MortonId);
  359. ((size_t*)buff_iter)[0]=send_data[s_iter].length;
  360. buff_iter+=sizeof(size_t);
  361. mem::memcopy((void*)buff_iter,send_data[s_iter].data,send_data[s_iter].length);
  362. buff_iter+=send_data[s_iter].length;
  363. s_iter++;
  364. }
  365. //Exchange send and recv sizes
  366. int recv_size=0;
  367. MPI_Status status;
  368. char* recv_buff=NULL;
  369. if(partner<(size_t)num_p){
  370. MPI_Sendrecv(&send_size, 1, MPI_INT, partner, 0, &recv_size, 1, MPI_INT, partner, 0, *this->Comm(), &status);
  371. recv_buff=mem::aligned_new<char>(recv_size);
  372. MPI_Sendrecv(send_buff, send_size, MPI_BYTE, partner, 0, recv_buff, recv_size, MPI_BYTE, partner, 0, *this->Comm(), &status);
  373. }
  374. //Need an extra broadcast for incomplete hypercubes.
  375. size_t p0_start=num_p - (num_p % (bit_mask ));
  376. size_t p0_end =num_p - (num_p % (bit_mask>>1));
  377. if(((size_t)rank >= p0_start) && ((size_t)num_p>p0_end) && ((size_t)rank < p0_end) ){
  378. size_t bit_mask0=1;
  379. size_t num_p0=p0_end-p0_start;
  380. while( bit_mask0 < num_p0 ){
  381. if( (bit_mask0<<1) > (num_p - p0_end) ){
  382. size_t partner0=rank^bit_mask0;
  383. if( rank-p0_start < bit_mask0 ){
  384. //Send
  385. MPI_Send(&recv_size, 1, MPI_INT , partner0, 0, *this->Comm());
  386. MPI_Send( recv_buff, recv_size, MPI_BYTE, partner0, 0, *this->Comm());
  387. }else if( rank-p0_start < (bit_mask0<<1) ){
  388. //Receive
  389. if(recv_size>0) mem::aligned_delete<char>(recv_buff);
  390. MPI_Recv(&recv_size, 1, MPI_INT , partner0, 0, *this->Comm(), &status);
  391. recv_buff=mem::aligned_new<char>(recv_size);
  392. MPI_Recv( recv_buff, recv_size, MPI_BYTE, partner0, 0, *this->Comm(), &status);
  393. }
  394. }
  395. bit_mask0=bit_mask0<<1;
  396. }
  397. }
  398. //Construct nodes from received data.
  399. if(recv_size>0){
  400. buff_iter=recv_buff;
  401. size_t r_node_cnt[2]={((size_t*)buff_iter)[0],((size_t*)buff_iter)[1]};
  402. buff_iter+=2*sizeof(size_t);
  403. std::vector<MortonId> r_mid[2];
  404. r_mid[0].resize(r_node_cnt[0]);
  405. r_mid[1].resize(r_node_cnt[1]);
  406. std::vector<Node_t*> recv_nodes[2];
  407. recv_nodes[0].resize(r_node_cnt[0]);
  408. recv_nodes[1].resize(r_node_cnt[1]);
  409. std::vector<PackedData> recv_data[2];
  410. recv_data[0].resize(r_node_cnt[0]);
  411. recv_data[1].resize(r_node_cnt[1]);
  412. for(int i=0;i<2;i++)
  413. for(size_t j=0;j<r_node_cnt[i];j++){
  414. r_mid[i][j]=((MortonId*)buff_iter)[0];
  415. buff_iter+=sizeof(MortonId);
  416. recv_data[i][j].length=((size_t*)buff_iter)[0];
  417. buff_iter+=sizeof(size_t);
  418. recv_data[i][j].data=(void*)buff_iter;
  419. buff_iter+=recv_data[i][j].length;
  420. }
  421. // Add multipole expansion to existing nodes.
  422. for(size_t i=0;i<r_node_cnt[1-merge_indx];i++){
  423. if(i<send_nodes[merge_indx].size()){
  424. if(r_mid[1-merge_indx][i]==send_nodes[merge_indx][i]->GetMortonId()){
  425. send_nodes[merge_indx][i]->AddMultipole(recv_data[1-merge_indx][i]);
  426. }else break;
  427. }else break;
  428. }
  429. bool new_branch=false;
  430. for(size_t i=0;i<r_node_cnt[merge_indx];i++){
  431. if(i<send_nodes[merge_indx].size() && !new_branch){
  432. if(r_mid[merge_indx][i]==send_nodes[merge_indx][i]->GetMortonId()){
  433. recv_nodes[merge_indx][i]=send_nodes[merge_indx][i];
  434. }else{
  435. new_branch=true;
  436. size_t n_=(i<(size_t)n[merge_indx]?n[merge_indx]:i);
  437. for(size_t j=n_;j<send_nodes[merge_indx].size();j++)
  438. mem::aligned_delete(send_nodes[merge_indx][j]);
  439. if(i<(size_t)n[merge_indx]) n[merge_indx]=i;
  440. }
  441. }
  442. if(i>=send_nodes[merge_indx].size() || new_branch){
  443. recv_nodes[merge_indx][i]=static_cast<Node_t*>(this->NewNode());
  444. recv_nodes[merge_indx][i]->SetCoord(r_mid[merge_indx][i]);
  445. recv_nodes[merge_indx][i]->InitMultipole(recv_data[merge_indx][i]);
  446. }
  447. }
  448. send_nodes[merge_indx]=recv_nodes[merge_indx];
  449. }
  450. mem::aligned_delete<char>(send_buff);
  451. mem::aligned_delete<char>(recv_buff);
  452. }
  453. for(int i=0;i<2;i++)
  454. for(size_t j=n[i];j<send_nodes[i].size();j++)
  455. mem::aligned_delete(send_nodes[i][j]);
  456. Profile::Toc();
  457. //Now Broadcast nodes to build LET.
  458. Profile::Tic("Broadcast",this->Comm(),true,4);
  459. this->ConstructLET(bndry);
  460. Profile::Toc();
  461. }
  462. template <class FMM_Mat_t>
  463. void FMM_Tree<FMM_Mat_t>::DownwardPass() {
  464. bool device=true;
  465. Profile::Tic("Setup",this->Comm(),true,3);
  466. std::vector<Node_t*> leaf_nodes;
  467. int max_depth=0;
  468. { // Build leaf node list
  469. int max_depth_loc=0;
  470. std::vector<Node_t*>& nodes=this->GetNodeList();
  471. for(size_t i=0;i<nodes.size();i++){
  472. Node_t* n=nodes[i];
  473. if(!n->IsGhost() && n->IsLeaf()) leaf_nodes.push_back(n);
  474. if(n->Depth()>max_depth_loc) max_depth_loc=n->Depth();
  475. }
  476. MPI_Allreduce(&max_depth_loc, &max_depth, 1, MPI_INT, MPI_MAX, *this->Comm());
  477. }
  478. Profile::Toc();
  479. #if defined(__INTEL_OFFLOAD) || defined(PVFMM_HAVE_CUDA)
  480. if(device){ // Host2Device:Src
  481. Profile::Tic("Host2Device:Src",this->Comm(),false,5);
  482. if(setup_data[0+MAX_DEPTH*2]. coord_data!=NULL) setup_data[0+MAX_DEPTH*2]. coord_data->AllocDevice(true);
  483. if(setup_data[0+MAX_DEPTH*2]. input_data!=NULL) setup_data[0+MAX_DEPTH*2]. input_data->AllocDevice(true);
  484. Profile::Toc();
  485. }
  486. #endif
  487. if(bndry==Periodic){ //Add contribution from periodic infinite tiling.
  488. Profile::Tic("BoundaryCondition",this->Comm(),false,5);
  489. fmm_mat->PeriodicBC(dynamic_cast<Node_t*>(this->RootNode()));
  490. Profile::Toc();
  491. }
  492. for(size_t i=0; i<=(fmm_mat->ScaleInvar()?0:max_depth); i++){ // U,V,W,X-lists
  493. if(!fmm_mat->ScaleInvar()){ // Precomp
  494. std::stringstream level_str;
  495. level_str<<"Level-"<<std::setfill('0')<<std::setw(2)<<i<<"\0";
  496. Profile::Tic(level_str.str().c_str(),this->Comm(),false,5);
  497. Profile::Tic("Precomp",this->Comm(),false,5);
  498. {// Precomp U
  499. Profile::Tic("Precomp-U",this->Comm(),false,10);
  500. fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*0],device);
  501. Profile::Toc();
  502. }
  503. {// Precomp W
  504. Profile::Tic("Precomp-W",this->Comm(),false,10);
  505. fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*1],device);
  506. Profile::Toc();
  507. }
  508. {// Precomp X
  509. Profile::Tic("Precomp-X",this->Comm(),false,10);
  510. fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*2],device);
  511. Profile::Toc();
  512. }
  513. if(0){// Precomp V
  514. Profile::Tic("Precomp-V",this->Comm(),false,10);
  515. fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*3], /*device*/ false);
  516. Profile::Toc();
  517. }
  518. Profile::Toc();
  519. }
  520. {// X-List
  521. Profile::Tic("X-List",this->Comm(),false,5);
  522. fmm_mat->X_List(setup_data[i+MAX_DEPTH*2], device);
  523. Profile::Toc();
  524. }
  525. #if defined(__INTEL_OFFLOAD) || defined(PVFMM_HAVE_CUDA)
  526. if(i==0 && device){ // Host2Device:Mult
  527. Profile::Tic("Host2Device:Mult",this->Comm(),false,5);
  528. if(setup_data[0+MAX_DEPTH*1]. input_data!=NULL) setup_data[0+MAX_DEPTH*1]. input_data->AllocDevice(true);
  529. Profile::Toc();
  530. }
  531. if(device) if(i==(fmm_mat->ScaleInvar()?0:max_depth)){ // Device2Host: LocalExp
  532. Profile::Tic("Device2Host:LocExp",this->Comm(),false,5);
  533. if(setup_data[0+MAX_DEPTH*2].output_data!=NULL){
  534. Matrix<Real_t>& output_data=*setup_data[0+MAX_DEPTH*2].output_data;
  535. assert(fmm_mat->dev_buffer.Dim()>=output_data.Dim(0)*output_data.Dim(1));
  536. output_data.Device2Host((Real_t*)&fmm_mat->dev_buffer[0]);
  537. }
  538. Profile::Toc();
  539. }
  540. #endif
  541. {// W-List
  542. Profile::Tic("W-List",this->Comm(),false,5);
  543. fmm_mat->W_List(setup_data[i+MAX_DEPTH*1], device);
  544. Profile::Toc();
  545. }
  546. {// U-List
  547. Profile::Tic("U-List",this->Comm(),false,5);
  548. fmm_mat->U_List(setup_data[i+MAX_DEPTH*0], device);
  549. Profile::Toc();
  550. }
  551. {// V-List
  552. Profile::Tic("V-List",this->Comm(),false,5);
  553. fmm_mat->V_List(setup_data[i+MAX_DEPTH*3], /*device*/ false);
  554. Profile::Toc();
  555. }
  556. if(!fmm_mat->ScaleInvar()){ // Wait
  557. #ifdef __INTEL_OFFLOAD
  558. int wait_lock_idx=-1;
  559. if(device) wait_lock_idx=MIC_Lock::curr_lock();
  560. #pragma offload if(device) target(mic:0)
  561. {if(device) MIC_Lock::wait_lock(wait_lock_idx);}
  562. #endif
  563. Profile::Toc();
  564. }
  565. }
  566. #if defined(__INTEL_OFFLOAD) || defined(PVFMM_HAVE_CUDA)
  567. Profile::Tic("D2H_Wait:LocExp",this->Comm(),false,5);
  568. if(device) if(setup_data[0+MAX_DEPTH*2].output_data!=NULL){
  569. Real_t* dev_ptr=(Real_t*)&fmm_mat->dev_buffer[0];
  570. Matrix<Real_t>& output_data=*setup_data[0+MAX_DEPTH*2].output_data;
  571. size_t n=output_data.Dim(0)*output_data.Dim(1);
  572. Real_t* host_ptr=output_data[0];
  573. output_data.Device2HostWait();
  574. #pragma omp parallel for
  575. for(size_t i=0;i<n;i++){
  576. host_ptr[i]+=dev_ptr[i];
  577. }
  578. }
  579. Profile::Toc();
  580. Profile::Tic("Device2Host:Trg",this->Comm(),false,5);
  581. if(device) if(setup_data[0+MAX_DEPTH*0].output_data!=NULL){ // Device2Host: Target
  582. Matrix<Real_t>& output_data=*setup_data[0+MAX_DEPTH*0].output_data;
  583. assert(fmm_mat->dev_buffer.Dim()>=output_data.Dim(0)*output_data.Dim(1));
  584. output_data.Device2Host((Real_t*)&fmm_mat->dev_buffer[0]);
  585. }
  586. Profile::Toc();
  587. #endif
  588. Profile::Tic("D2D",this->Comm(),false,5);
  589. for(size_t i=0; i<=max_depth; i++){ // Down2Down
  590. if(!fmm_mat->ScaleInvar()) fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*4],/*device*/ false);
  591. fmm_mat->Down2Down(setup_data[i+MAX_DEPTH*4]);
  592. }
  593. Profile::Toc();
  594. Profile::Tic("D2T",this->Comm(),false,5);
  595. for(int i=0; i<=(fmm_mat->ScaleInvar()?0:max_depth); i++){ // Down2Target
  596. if(!fmm_mat->ScaleInvar()) fmm_mat->SetupPrecomp(setup_data[i+MAX_DEPTH*5],/*device*/ false);
  597. fmm_mat->Down2Target(setup_data[i+MAX_DEPTH*5]);
  598. }
  599. Profile::Toc();
  600. #if defined(__INTEL_OFFLOAD) || defined(PVFMM_HAVE_CUDA)
  601. Profile::Tic("D2H_Wait:Trg",this->Comm(),false,5);
  602. if(device) if(setup_data[0+MAX_DEPTH*0].output_data!=NULL){
  603. Real_t* dev_ptr=(Real_t*)&fmm_mat->dev_buffer[0];
  604. Matrix<Real_t>& output_data=*setup_data[0+MAX_DEPTH*0].output_data;
  605. size_t n=output_data.Dim(0)*output_data.Dim(1);
  606. Real_t* host_ptr=output_data[0];
  607. output_data.Device2HostWait();
  608. #pragma omp parallel for
  609. for(size_t i=0;i<n;i++){
  610. host_ptr[i]+=dev_ptr[i];
  611. }
  612. }
  613. Profile::Toc();
  614. #endif
  615. Profile::Tic("PostProc",this->Comm(),false,5);
  616. fmm_mat->PostProcessing(leaf_nodes);
  617. Profile::Toc();
  618. }
  619. template <class FMM_Mat_t>
  620. void FMM_Tree<FMM_Mat_t>::Copy_FMMOutput() {
  621. std::vector<Node_t*>& all_nodes=this->GetNodeList();
  622. int omp_p=omp_get_max_threads();
  623. // Copy output to the tree.
  624. {
  625. size_t k=all_nodes.size();
  626. #pragma omp parallel for
  627. for(int j=0;j<omp_p;j++){
  628. size_t a=(k*j)/omp_p;
  629. size_t b=(k*(j+1))/omp_p;
  630. fmm_mat->CopyOutput(&(all_nodes[a]),b-a);
  631. }
  632. }
  633. }
  634. }//end namespace