tree.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. #ifndef _SCTL_TREE_
  2. #define _SCTL_TREE_
  3. #include SCTL_INCLUDE(common.hpp)
  4. #include SCTL_INCLUDE(morton.hpp)
  5. #include SCTL_INCLUDE(comm.hpp)
  6. #include <fstream>
  7. #include <algorithm>
  8. namespace SCTL_NAMESPACE {
  9. struct VTUData {
  10. typedef float VTKReal;
  11. // Point data
  12. Vector<VTKReal> coord; // always 3D
  13. Vector<VTKReal> value;
  14. // Cell data
  15. Vector<int32_t> connect;
  16. Vector<int32_t> offset;
  17. Vector<uint8_t> types;
  18. void WriteVTK(const std::string& fname, Comm comm = Comm::Self()) const {
  19. typedef typename VTUData::VTKReal VTKReal;
  20. Integer rank = comm.Rank();
  21. Integer np = comm.Size();
  22. Long value_dof = 0;
  23. { // Write vtu file.
  24. std::ofstream vtufile;
  25. { // Open file for writing.
  26. std::stringstream vtufname;
  27. vtufname << fname << std::setfill('0') << std::setw(6) << rank << ".vtu";
  28. vtufile.open(vtufname.str().c_str());
  29. if (vtufile.fail()) return;
  30. }
  31. { // Write to file.
  32. Long pt_cnt = coord.Dim() / 3;
  33. Long cell_cnt = types.Dim();
  34. value_dof = (pt_cnt ? value.Dim() / pt_cnt : 0);
  35. Vector<int32_t> mpi_rank;
  36. { // Set mpi_rank
  37. Integer new_myrank = rank;
  38. mpi_rank.ReInit(pt_cnt);
  39. for (Long i = 0; i < mpi_rank.Dim(); i++) mpi_rank[i] = new_myrank;
  40. }
  41. bool isLittleEndian;
  42. { // Set isLittleEndian
  43. uint16_t number = 0x1;
  44. uint8_t *numPtr = (uint8_t *)&number;
  45. isLittleEndian = (numPtr[0] == 1);
  46. }
  47. Long data_size = 0;
  48. vtufile << "<?xml version=\"1.0\"?>\n";
  49. vtufile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"" << (isLittleEndian ? "LittleEndian" : "BigEndian") << "\">\n";
  50. // ===========================================================================
  51. vtufile << " <UnstructuredGrid>\n";
  52. vtufile << " <Piece NumberOfPoints=\"" << pt_cnt << "\" NumberOfCells=\"" << cell_cnt << "\">\n";
  53. //---------------------------------------------------------------------------
  54. vtufile << " <Points>\n";
  55. vtufile << " <DataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"3\" Name=\"Position\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  56. data_size += sizeof(uint32_t) + coord.Dim() * sizeof(VTKReal);
  57. vtufile << " </Points>\n";
  58. //---------------------------------------------------------------------------
  59. vtufile << " <PointData>\n";
  60. if (value_dof) { // value
  61. vtufile << " <DataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"" << value_dof << "\" Name=\"value\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  62. data_size += sizeof(uint32_t) + value.Dim() * sizeof(VTKReal);
  63. }
  64. { // mpi_rank
  65. vtufile << " <DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"mpi_rank\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  66. data_size += sizeof(uint32_t) + pt_cnt * sizeof(int32_t);
  67. }
  68. vtufile << " </PointData>\n";
  69. //---------------------------------------------------------------------------
  70. //---------------------------------------------------------------------------
  71. vtufile << " <Cells>\n";
  72. vtufile << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  73. data_size += sizeof(uint32_t) + connect.Dim() * sizeof(int32_t);
  74. vtufile << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  75. data_size += sizeof(uint32_t) + offset.Dim() * sizeof(int32_t);
  76. vtufile << " <DataArray type=\"UInt8\" Name=\"types\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  77. data_size += sizeof(uint32_t) + types.Dim() * sizeof(uint8_t);
  78. vtufile << " </Cells>\n";
  79. //---------------------------------------------------------------------------
  80. vtufile << " </Piece>\n";
  81. vtufile << " </UnstructuredGrid>\n";
  82. // ===========================================================================
  83. vtufile << " <AppendedData encoding=\"raw\">\n";
  84. vtufile << " _";
  85. int32_t block_size;
  86. { // coord
  87. block_size = coord.Dim() * sizeof(VTKReal);
  88. vtufile.write((char *)&block_size, sizeof(int32_t));
  89. if (coord.Dim()) vtufile.write((char *)&coord[0], coord.Dim() * sizeof(VTKReal));
  90. }
  91. if (value_dof) { // value
  92. block_size = value.Dim() * sizeof(VTKReal);
  93. vtufile.write((char *)&block_size, sizeof(int32_t));
  94. if (value.Dim()) vtufile.write((char *)&value[0], value.Dim() * sizeof(VTKReal));
  95. }
  96. { // mpi_rank
  97. block_size = mpi_rank.Dim() * sizeof(int32_t);
  98. vtufile.write((char *)&block_size, sizeof(int32_t));
  99. if (mpi_rank.Dim()) vtufile.write((char *)&mpi_rank[0], mpi_rank.Dim() * sizeof(int32_t));
  100. }
  101. { // block_size
  102. block_size = connect.Dim() * sizeof(int32_t);
  103. vtufile.write((char *)&block_size, sizeof(int32_t));
  104. if (connect.Dim()) vtufile.write((char *)&connect[0], connect.Dim() * sizeof(int32_t));
  105. }
  106. { // offset
  107. block_size = offset.Dim() * sizeof(int32_t);
  108. vtufile.write((char *)&block_size, sizeof(int32_t));
  109. if (offset.Dim()) vtufile.write((char *)&offset[0], offset.Dim() * sizeof(int32_t));
  110. }
  111. { // types
  112. block_size = types.Dim() * sizeof(uint8_t);
  113. vtufile.write((char *)&block_size, sizeof(int32_t));
  114. if (types.Dim()) vtufile.write((char *)&types[0], types.Dim() * sizeof(uint8_t));
  115. }
  116. vtufile << "\n";
  117. vtufile << " </AppendedData>\n";
  118. // ===========================================================================
  119. vtufile << "</VTKFile>\n";
  120. }
  121. vtufile.close(); // close file
  122. }
  123. if (!rank) { // Write pvtu file
  124. std::ofstream pvtufile;
  125. { // Open file for writing
  126. std::stringstream pvtufname;
  127. pvtufname << fname << ".pvtu";
  128. pvtufile.open(pvtufname.str().c_str());
  129. if (pvtufile.fail()) return;
  130. }
  131. { // Write to file.
  132. pvtufile << "<?xml version=\"1.0\"?>\n";
  133. pvtufile << "<VTKFile type=\"PUnstructuredGrid\">\n";
  134. pvtufile << " <PUnstructuredGrid GhostLevel=\"0\">\n";
  135. pvtufile << " <PPoints>\n";
  136. pvtufile << " <PDataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"3\" Name=\"Position\"/>\n";
  137. pvtufile << " </PPoints>\n";
  138. pvtufile << " <PPointData>\n";
  139. if (value_dof) { // value
  140. pvtufile << " <PDataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"" << value_dof << "\" Name=\"value\"/>\n";
  141. }
  142. { // mpi_rank
  143. pvtufile << " <PDataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"mpi_rank\"/>\n";
  144. }
  145. pvtufile << " </PPointData>\n";
  146. {
  147. // Extract filename from path.
  148. std::stringstream vtupath;
  149. vtupath << '/' << fname;
  150. std::string pathname = vtupath.str();
  151. std::string fname_ = pathname.substr(pathname.find_last_of("/\\") + 1);
  152. // char *fname_ = (char*)strrchr(vtupath.str().c_str(), '/') + 1;
  153. // std::string fname_ =
  154. // boost::filesystem::path(fname).filename().string().
  155. for (Integer i = 0; i < np; i++) pvtufile << " <Piece Source=\"" << fname_ << std::setfill('0') << std::setw(6) << i << ".vtu\"/>\n";
  156. }
  157. pvtufile << " </PUnstructuredGrid>\n";
  158. pvtufile << "</VTKFile>\n";
  159. }
  160. pvtufile.close(); // close file
  161. }
  162. };
  163. };
  164. template <class Real, Integer DIM> class Tree {
  165. public:
  166. struct NodeAttr {
  167. unsigned char Leaf : 1, Ghost : 1;
  168. };
  169. //struct PtrTree {
  170. // Long p2n;
  171. // Long parent;
  172. // Long child[1 << DIM];
  173. // Long nbr[pvfmm::pow<DIM>(3)];
  174. //};
  175. static constexpr Integer Dim() {
  176. return DIM;
  177. }
  178. Tree(const Comm& comm_ = Comm::Self()) {
  179. comm = comm_;
  180. Integer rank = comm.Rank();
  181. Integer np = comm.Size();
  182. Vector<Real> coord;
  183. { // Set coord
  184. Long N0 = 1;
  185. while (sctl::pow<DIM,Long>(N0) < np) N0++;
  186. Long N = sctl::pow<DIM,Long>(N0);
  187. Long start = N * (rank+0) / np;
  188. Long end = N * (rank+1) / np;
  189. coord.ReInit((end-start)*DIM);
  190. for (Long i = start; i < end; i++) {
  191. Long idx = i;
  192. for (Integer k = 0; k < DIM; k++) {
  193. coord[(i-start)*DIM+k] = (idx % N0) / (Real)N0;
  194. idx /= N0;
  195. }
  196. }
  197. }
  198. this->UpdateRefinement(coord);
  199. }
  200. ~Tree() {
  201. #ifdef SCTL_MEMDEBUG
  202. for (auto& pair : node_data) {
  203. SCTL_ASSERT(node_cnt.find(pair.first) != node_cnt.end());
  204. }
  205. #endif
  206. }
  207. const Vector<Morton<DIM>>& GetPartitionMID() const {
  208. return mins;
  209. }
  210. const Vector<Morton<DIM>>& GetNodeMID() const {
  211. return node_mid;
  212. }
  213. const Vector<NodeAttr>& GetNodeAttr() const {
  214. return node_attr;
  215. }
  216. const Comm& GetComm() const {
  217. return comm;
  218. }
  219. void UpdateRefinement(const Vector<Real>& coord, Long M = 1, bool balance21 = 0, bool periodic = 0) {
  220. Integer np = comm.Size();
  221. Integer rank = comm.Rank();
  222. Vector<Morton<DIM>> node_mid_orig;
  223. Long start_idx_orig, end_idx_orig;
  224. if (mins.Dim()) { // Set start_idx_orig, end_idx_orig
  225. start_idx_orig = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  226. end_idx_orig = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  227. node_mid_orig.ReInit(end_idx_orig - start_idx_orig, node_mid.begin() + start_idx_orig, true);
  228. } else {
  229. start_idx_orig = 0;
  230. end_idx_orig = 0;
  231. }
  232. auto coarsest_ancestor_mid = [](const Morton<DIM>& m0) {
  233. Morton<DIM> md;
  234. Integer d0 = m0.Depth();
  235. for (Integer d = 0; d <= d0; d++) {
  236. md = m0.Ancestor(d);
  237. if (md.Ancestor(d0) == m0) break;
  238. }
  239. return md;
  240. };
  241. Morton<DIM> pt_mid0;
  242. Vector<Morton<DIM>> pt_mid;
  243. { // Construct sorted pt_mid
  244. Long Npt = coord.Dim() / DIM;
  245. pt_mid.ReInit(Npt);
  246. for (Long i = 0; i < Npt; i++) {
  247. pt_mid[i] = Morton<DIM>(coord.begin() + i*DIM);
  248. }
  249. Vector<Morton<DIM>> sorted_mid;
  250. comm.HyperQuickSort(pt_mid, sorted_mid);
  251. pt_mid.Swap(sorted_mid);
  252. SCTL_ASSERT(pt_mid.Dim());
  253. pt_mid0 = pt_mid[0];
  254. }
  255. { // Update M = global_min(pt_mid.Dim(), M)
  256. Long M0, M1, Npt = pt_mid.Dim();
  257. comm.Allreduce(Ptr2ConstItr<Long>(&M,1), Ptr2Itr<Long>(&M0,1), 1, Comm::CommOp::MIN);
  258. comm.Allreduce(Ptr2ConstItr<Long>(&Npt,1), Ptr2Itr<Long>(&M1,1), 1, Comm::CommOp::MIN);
  259. M = std::min(M0,M1);
  260. SCTL_ASSERT(M > 0);
  261. }
  262. { // pt_mid <-- [M points from rank-1; pt_mid; M points from rank+1]
  263. Long send_size0 = (rank+1<np ? M : 0);
  264. Long send_size1 = (rank > 0 ? M : 0);
  265. Long recv_size0 = (rank > 0 ? M : 0);
  266. Long recv_size1 = (rank+1<np ? M : 0);
  267. Vector<Morton<DIM>> pt_mid_(recv_size0 + pt_mid.Dim() + recv_size1);
  268. memcopy(pt_mid_.begin()+recv_size0, pt_mid.begin(), pt_mid.Dim());
  269. void* recv_req0 = comm.Irecv(pt_mid_.begin(), recv_size0, (rank+np-1)%np, 0);
  270. void* recv_req1 = comm.Irecv(pt_mid_.begin() + recv_size0 + pt_mid.Dim(), recv_size1, (rank+1)%np, 1);
  271. void* send_req0 = comm.Isend(pt_mid .begin() + pt_mid.Dim() - send_size0, send_size0, (rank+1)%np, 0);
  272. void* send_req1 = comm.Isend(pt_mid .begin(), send_size1, (rank+np-1)%np, 1);
  273. comm.Wait(recv_req0);
  274. comm.Wait(recv_req1);
  275. comm.Wait(send_req0);
  276. comm.Wait(send_req1);
  277. pt_mid.Swap(pt_mid_);
  278. }
  279. { // Build linear MortonID tree from pt_mid
  280. node_mid.ReInit(0);
  281. Long idx = 0;
  282. Morton<DIM> m0;
  283. Morton<DIM> mend = Morton<DIM>().Next();
  284. while (m0 < mend) {
  285. Integer d = m0.Depth();
  286. Morton<DIM> m1 = (idx + M < pt_mid.Dim() ? pt_mid[idx+M] : Morton<DIM>().Next());
  287. while (d < Morton<DIM>::MAX_DEPTH && m0.Ancestor(d) == m1.Ancestor(d)) {
  288. node_mid.PushBack(m0.Ancestor(d));
  289. d++;
  290. }
  291. m0 = m0.Ancestor(d);
  292. node_mid.PushBack(m0);
  293. m0 = m0.Next();
  294. idx = std::lower_bound(pt_mid.begin(), pt_mid.end(), m0) - pt_mid.begin();
  295. }
  296. }
  297. { // Set mins
  298. mins.ReInit(np);
  299. Long min_idx = std::lower_bound(node_mid.begin(), node_mid.end(), pt_mid0) - node_mid.begin() - 1;
  300. if (!rank || min_idx < 0) min_idx = 0;
  301. Morton<DIM> m0 = coarsest_ancestor_mid(node_mid[min_idx]);
  302. comm.Allgather(Ptr2ConstItr<Morton<DIM>>(&m0,1), 1, mins.begin(), 1);
  303. }
  304. if (balance21) { // 2:1 balance refinement // TODO: optimize
  305. Vector<Morton<DIM>> parent_mid;
  306. { // add balancing Morton IDs
  307. Vector<std::set<Morton<DIM>>> parent_mid_set(Morton<DIM>::MAX_DEPTH+1);
  308. Vector<Morton<DIM>> nlst;
  309. for (const auto& m0 : node_mid) {
  310. Integer d0 = m0.Depth();
  311. parent_mid_set[m0.Depth()].insert(m0.Ancestor(d0-1));
  312. }
  313. for (Integer d = Morton<DIM>::MAX_DEPTH; d > 0; d--) {
  314. for (const auto& m : parent_mid_set[d]) {
  315. m.NbrList(nlst, d-1, periodic);
  316. parent_mid_set[d-1].insert(nlst.begin(), nlst.end());
  317. parent_mid.PushBack(m);
  318. }
  319. }
  320. }
  321. Vector<Morton<DIM>> parent_mid_sorted;
  322. { // sort and repartition
  323. comm.HyperQuickSort(parent_mid, parent_mid_sorted);
  324. comm.PartitionS(parent_mid_sorted, mins[comm.Rank()]);
  325. }
  326. Vector<Morton<DIM>> tmp_mid;
  327. { // add children
  328. Vector<Morton<DIM>> clst;
  329. tmp_mid.PushBack(Morton<DIM>()); // include root node
  330. for (Long i = 0; i < parent_mid_sorted.Dim(); i++) {
  331. if (i+1 == parent_mid_sorted.Dim() || parent_mid_sorted[i] != parent_mid_sorted[i+1]) {
  332. const auto& m = parent_mid_sorted[i];
  333. tmp_mid.PushBack(m);
  334. m.Children(clst);
  335. for (const auto& c : clst) tmp_mid.PushBack(c);
  336. }
  337. }
  338. auto insert_ancestor_children = [](Vector<Morton<DIM>>& mvec, const Morton<DIM>& m0) {
  339. Integer d0 = m0.Depth();
  340. Vector<Morton<DIM>> clst;
  341. for (Integer d = 0; d < d0; d++) {
  342. m0.Ancestor(d).Children(clst);
  343. for (const auto& m : clst) mvec.PushBack(m);
  344. }
  345. };
  346. insert_ancestor_children(tmp_mid, mins[rank]);
  347. omp_par::merge_sort(tmp_mid.begin(), tmp_mid.end());
  348. }
  349. node_mid.ReInit(0);
  350. for (Long i = 0; i < tmp_mid.Dim(); i++) { // remove duplicates
  351. if (i+1 == tmp_mid.Dim() || tmp_mid[i] != tmp_mid[i+1]) {
  352. node_mid.PushBack(tmp_mid[i]);
  353. }
  354. }
  355. }
  356. { // Set node_mid, node_attr
  357. Morton<DIM> m0 = (rank ? mins[rank] : Morton<DIM>() );
  358. Morton<DIM> m1 = (rank+1<np ? mins[rank+1] : Morton<DIM>().Next());
  359. Long Nnodes = node_mid.Dim();
  360. node_attr.ReInit(Nnodes);
  361. for (Long i = 0; i < Nnodes; i++) {
  362. node_attr[i].Leaf = !(i+1<Nnodes && node_mid[i].isAncestor(node_mid[i+1]));
  363. node_attr[i].Ghost = (node_mid[i] < m0 || node_mid[i] >= m1);
  364. }
  365. }
  366. { // Add place-holder for ghost nodes
  367. // TODO
  368. }
  369. { // Update node_data, node_cnt
  370. Long start_idx, end_idx;
  371. { // Set start_idx, end_idx
  372. start_idx = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  373. end_idx = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  374. }
  375. comm.PartitionS(node_mid_orig, mins[comm.Rank()]);
  376. Vector<Long> new_cnt_range0(node_mid.Dim()), new_cnt_range1(node_mid.Dim());
  377. { // Set new_cnt_range0, new_cnt_range1
  378. for (Long i = 0; i < start_idx; i++) {
  379. new_cnt_range0[i] = 0;
  380. new_cnt_range1[i] = 0;
  381. }
  382. for (Long i = start_idx; i < end_idx; i++) {
  383. auto m0 = (node_mid[i+0]);
  384. auto m1 = (i+1==end_idx ? Morton<DIM>().Next() : (node_mid[i+1]));
  385. new_cnt_range0[i] = std::lower_bound(node_mid_orig.begin(), node_mid_orig.begin() + node_mid_orig.Dim(), m0) - node_mid_orig.begin();
  386. new_cnt_range1[i] = std::lower_bound(node_mid_orig.begin(), node_mid_orig.begin() + node_mid_orig.Dim(), m1) - node_mid_orig.begin();
  387. }
  388. for (Long i = end_idx; i < node_mid.Dim(); i++) {
  389. new_cnt_range0[i] = 0;
  390. new_cnt_range1[i] = 0;
  391. }
  392. }
  393. Vector<Long> cnt_tmp;
  394. Vector<Real> data_tmp;
  395. for (const auto& pair : node_data) {
  396. const std::string& data_name = pair.first;
  397. Long dof = 0;
  398. Iterator<Vector<Real>> data_;
  399. Iterator<Vector<Long>> cnt_;
  400. GetData(data_, cnt_, data_name);
  401. { // Set dof
  402. StaticArray<Long,2> Nl, Ng;
  403. Nl[0] = data_->Dim();
  404. Nl[1] = omp_par::reduce(cnt_->begin(), cnt_->Dim());
  405. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  406. if (Ng[1]) dof = Ng[0] / Ng[1];
  407. SCTL_ASSERT(Nl[0] == Nl[1] * dof);
  408. SCTL_ASSERT(Ng[0] == Ng[1] * dof);
  409. }
  410. Long data_dsp = omp_par::reduce(cnt_->begin(), start_idx_orig);
  411. Long data_cnt = omp_par::reduce(cnt_->begin() + start_idx_orig, end_idx_orig - start_idx_orig);
  412. data_tmp.ReInit(data_cnt * dof, data_->begin() + data_dsp * dof, true);
  413. cnt_tmp.ReInit(end_idx_orig - start_idx_orig, cnt_->begin() + start_idx_orig, true);
  414. comm.PartitionN(cnt_tmp, node_mid_orig.Dim());
  415. cnt_->ReInit(node_mid.Dim());
  416. for (Long i = 0; i < node_mid.Dim(); i++) {
  417. Long sum = 0;
  418. Long j0 = new_cnt_range0[i];
  419. Long j1 = new_cnt_range1[i];
  420. for (Long j = j0; j < j1; j++) sum += cnt_tmp[j];
  421. cnt_[0][i] = sum;
  422. }
  423. SCTL_ASSERT(omp_par::reduce(cnt_->begin(), cnt_->Dim()) == omp_par::reduce(cnt_tmp.begin(), cnt_tmp.Dim()));
  424. Long Ndata = omp_par::reduce(cnt_->begin(), cnt_->Dim()) * dof;
  425. comm.PartitionN(data_tmp, Ndata);
  426. SCTL_ASSERT(data_tmp.Dim() == Ndata);
  427. data_->Swap(data_tmp);
  428. }
  429. }
  430. }
  431. void AddData(const std::string& name, const Vector<Real>& data, const Vector<Long>& cnt) {
  432. Long dof = 0;
  433. { // Check dof
  434. StaticArray<Long,2> Nl, Ng;
  435. Nl[0] = data.Dim();
  436. Nl[1] = omp_par::reduce(cnt.begin(), cnt.Dim());
  437. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  438. if (Ng[1]) dof = Ng[0] / Ng[1];
  439. SCTL_ASSERT(Nl[0] == Nl[1] * dof);
  440. SCTL_ASSERT(Ng[0] == Ng[1] * dof);
  441. }
  442. if (dof) SCTL_ASSERT(cnt.Dim() == node_mid.Dim());
  443. SCTL_ASSERT(node_data.find(name) == node_data.end());
  444. node_data[name] = data;
  445. node_cnt [name] = cnt;
  446. }
  447. void GetData(Iterator<Vector<Real>>& data, ConstIterator<Vector<Long>>& cnt, const std::string& name) {
  448. auto data_ = node_data.find(name);
  449. const auto cnt_ = node_cnt.find(name);
  450. SCTL_ASSERT(data_ != node_data.end());
  451. SCTL_ASSERT( cnt_ != node_cnt .end());
  452. data = Ptr2Itr<Vector<Real>>(&data_->second,1);
  453. cnt = Ptr2ConstItr<Vector<Long>>(& cnt_->second,1);
  454. }
  455. void GetData(ConstIterator<Vector<Real>>& data, ConstIterator<Vector<Long>>& cnt, const std::string& name) const {
  456. const auto data_ = node_data.find(name);
  457. const auto cnt_ = node_cnt.find(name);
  458. SCTL_ASSERT(data_ != node_data.end());
  459. SCTL_ASSERT( cnt_ != node_cnt .end());
  460. data = Ptr2ConstItr<Vector<Real>>(&data_->second,1);
  461. cnt = Ptr2ConstItr<Vector<Long>>(& cnt_->second,1);
  462. }
  463. void DeleteData(const std::string& name) {
  464. SCTL_ASSERT(node_data.find(name) != node_data.end());
  465. SCTL_ASSERT(node_cnt .find(name) != node_cnt .end());
  466. node_data.erase(name);
  467. node_cnt .erase(name);
  468. }
  469. void WriteTreeVTK(std::string fname, bool show_ghost = false) const {
  470. typedef typename VTUData::VTKReal VTKReal;
  471. VTUData vtu_data;
  472. if (DIM <= 3) { // Set vtu data
  473. static const Integer Ncorner = (1u << DIM);
  474. Vector<VTKReal> &coord = vtu_data.coord;
  475. //Vector<VTKReal> &value = vtu_data.value;
  476. Vector<int32_t> &connect = vtu_data.connect;
  477. Vector<int32_t> &offset = vtu_data.offset;
  478. Vector<uint8_t> &types = vtu_data.types;
  479. StaticArray<VTKReal, DIM> c;
  480. Long point_cnt = coord.Dim() / 3;
  481. Long connect_cnt = connect.Dim();
  482. for (Long nid = 0; nid < node_mid.Dim(); nid++) {
  483. const Morton<DIM> &mid = node_mid[nid];
  484. const NodeAttr &attr = node_attr[nid];
  485. if (!show_ghost && attr.Ghost) continue;
  486. if (!attr.Leaf) continue;
  487. mid.Coord((Iterator<VTKReal>)c);
  488. VTKReal s = sctl::pow<VTKReal>(0.5, mid.Depth());
  489. for (Integer j = 0; j < Ncorner; j++) {
  490. for (Integer i = 0; i < DIM; i++) coord.PushBack(c[i] + (j & (1u << i) ? 1 : 0) * s);
  491. for (Integer i = DIM; i < 3; i++) coord.PushBack(0);
  492. connect.PushBack(point_cnt);
  493. connect_cnt++;
  494. point_cnt++;
  495. }
  496. offset.PushBack(connect_cnt);
  497. if (DIM == 2)
  498. types.PushBack(8);
  499. else if (DIM == 3)
  500. types.PushBack(11);
  501. else
  502. types.PushBack(4);
  503. }
  504. }
  505. vtu_data.WriteVTK(fname, comm);
  506. }
  507. protected:
  508. void GetData(Iterator<Vector<Real>>& data, Iterator<Vector<Long>>& cnt, const std::string& name) {
  509. auto data_ = node_data.find(name);
  510. const auto cnt_ = node_cnt.find(name);
  511. SCTL_ASSERT(data_ != node_data.end());
  512. SCTL_ASSERT( cnt_ != node_cnt .end());
  513. data = Ptr2Itr<Vector<Real>>(&data_->second,1);
  514. cnt = Ptr2Itr<Vector<Long>>(& cnt_->second,1);
  515. }
  516. static void scan(Vector<Long>& dsp, const Vector<Long>& cnt) {
  517. dsp.ReInit(cnt.Dim());
  518. if (cnt.Dim()) dsp[0] = 0;
  519. omp_par::scan(cnt.begin(), dsp.begin(), cnt.Dim());
  520. }
  521. //template <typename A, typename B> struct SortPair {
  522. // int operator<(const SortPair<A, B> &p1) const { return key < p1.key; }
  523. // A key;
  524. // B data;
  525. //};
  526. private:
  527. Vector<Morton<DIM>> mins;
  528. Vector<Morton<DIM>> node_mid;
  529. Vector<NodeAttr> node_attr;
  530. std::map<std::string, Vector<Real>> node_data;
  531. std::map<std::string, Vector<Long>> node_cnt;
  532. Comm comm;
  533. };
  534. template <class Real, Integer DIM, class BaseTree = Tree<Real,DIM>> class PtTree : public BaseTree {
  535. public:
  536. PtTree(const Comm& comm = Comm::Self()) : BaseTree(comm) {}
  537. ~PtTree() {
  538. #ifdef SCTL_MEMDEBUG
  539. for (auto& pair : data_pt_name) {
  540. ConstIterator<Vector<Real>> data;
  541. ConstIterator<Vector<Long>> cnt;
  542. this->GetData(data, cnt, pair.second);
  543. SCTL_ASSERT(scatter_idx.find(pair.second) != scatter_idx.end());
  544. }
  545. #endif
  546. }
  547. void UpdateRefinement(const Vector<Real>& coord, Long M = 1, bool balance21 = 0, bool periodic = 0) {
  548. const auto& comm = this->GetComm();
  549. BaseTree::UpdateRefinement(coord, M, balance21, periodic);
  550. Long start_node_idx, end_node_idx;
  551. { // Set start_node_idx, end_node_idx
  552. const auto& mins = this->GetPartitionMID();
  553. const auto& node_mid = this->GetNodeMID();
  554. Integer np = comm.Size();
  555. Integer rank = comm.Rank();
  556. start_node_idx = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  557. end_node_idx = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  558. }
  559. const auto& mins = this->GetPartitionMID();
  560. const auto& node_mid = this->GetNodeMID();
  561. for (const auto& pair : pt_mid) {
  562. const auto& pt_name = pair.first;
  563. auto& pt_mid_ = pt_mid[pt_name];
  564. auto& scatter_idx_ = scatter_idx[pt_name];
  565. comm.PartitionS(pt_mid_, mins[comm.Rank()]);
  566. comm.PartitionN(scatter_idx_, pt_mid_.Dim());
  567. Vector<Long> pt_cnt(node_mid.Dim());
  568. for (Long i = 0; i < node_mid.Dim(); i++) { // Set pt_cnt
  569. Long start = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), node_mid[i]) - pt_mid_.begin();
  570. Long end = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), (i+1==node_mid.Dim() ? Morton<DIM>().Next() : node_mid[i+1])) - pt_mid_.begin();
  571. if (i == 0) SCTL_ASSERT(start == 0);
  572. if (i+1 == node_mid.Dim()) SCTL_ASSERT(end == pt_mid_.Dim());
  573. pt_cnt[i] = end - start;
  574. }
  575. for (const auto& pair : data_pt_name) {
  576. if (pair.second == pt_name) {
  577. const auto& data_name = pair.first;
  578. Iterator<Vector<Real>> data;
  579. Iterator<Vector<Long>> cnt;
  580. this->GetData(data, cnt, data_name);
  581. { // Update data
  582. Long dof = 0;
  583. { // Set dof
  584. StaticArray<Long,2> Nl = {0, 0}, Ng;
  585. Nl[0] = data->Dim();
  586. for (Long i = 0; i < cnt->Dim(); i++) Nl[1] += cnt[0][i];
  587. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  588. dof = Ng[0] / std::max<Long>(Ng[1],1);
  589. }
  590. Long offset = 0, count = 0;
  591. SCTL_ASSERT(0 <= start_node_idx);
  592. SCTL_ASSERT(start_node_idx <= end_node_idx);
  593. SCTL_ASSERT(end_node_idx <= cnt->Dim());
  594. for (Long i = 0; i < start_node_idx; i++) offset += cnt[0][i];
  595. for (Long i = start_node_idx; i < end_node_idx; i++) count += cnt[0][i];
  596. offset *= dof;
  597. count *= dof;
  598. Vector<Real> data_(count, data->begin() + offset);
  599. comm.PartitionN(data_, pt_mid_.Dim());
  600. data->Swap(data_);
  601. }
  602. cnt[0] = pt_cnt;
  603. }
  604. }
  605. }
  606. }
  607. void AddParticles(const std::string& name, const Vector<Real>& coord) {
  608. const auto& mins = this->GetPartitionMID();
  609. const auto& node_mid = this->GetNodeMID();
  610. const auto& comm = this->GetComm();
  611. SCTL_ASSERT(scatter_idx.find(name) == scatter_idx.end());
  612. Vector<Long>& scatter_idx_ = scatter_idx[name];
  613. Long N = coord.Dim() / DIM;
  614. SCTL_ASSERT(coord.Dim() == N * DIM);
  615. Nlocal[name] = N;
  616. Vector<Morton<DIM>>& pt_mid_ = pt_mid[name];
  617. if (pt_mid_.Dim() != N) pt_mid_.ReInit(N);
  618. for (Long i = 0; i < N; i++) {
  619. pt_mid_[i] = Morton<DIM>(coord.begin() + i*DIM);
  620. }
  621. comm.SortScatterIndex(pt_mid_, scatter_idx_, &mins[comm.Rank()]);
  622. comm.ScatterForward(pt_mid_, scatter_idx_);
  623. AddParticleData(name, name, coord);
  624. { // Set node_cnt
  625. Iterator<Vector<Real>> data_;
  626. Iterator<Vector<Long>> cnt_;
  627. this->GetData(data_,cnt_,name);
  628. cnt_[0].ReInit(node_mid.Dim());
  629. for (Long i = 0; i < node_mid.Dim(); i++) {
  630. Long start = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), node_mid[i]) - pt_mid_.begin();
  631. Long end = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), (i+1==node_mid.Dim() ? Morton<DIM>().Next() : node_mid[i+1])) - pt_mid_.begin();
  632. if (i == 0) SCTL_ASSERT(start == 0);
  633. if (i+1 == node_mid.Dim()) SCTL_ASSERT(end == pt_mid_.Dim());
  634. cnt_[0][i] = end - start;
  635. }
  636. }
  637. }
  638. void AddParticleData(const std::string& data_name, const std::string& particle_name, const Vector<Real>& data) {
  639. SCTL_ASSERT(scatter_idx.find(particle_name) != scatter_idx.end());
  640. SCTL_ASSERT(data_pt_name.find(data_name) == data_pt_name.end());
  641. data_pt_name[data_name] = particle_name;
  642. Iterator<Vector<Real>> data_;
  643. Iterator<Vector<Long>> cnt_;
  644. this->AddData(data_name, Vector<Real>(), Vector<Long>());
  645. this->GetData(data_,cnt_,data_name);
  646. { // Set data_[0]
  647. data_[0] = data;
  648. this->GetComm().ScatterForward(data_[0], scatter_idx[particle_name]);
  649. }
  650. if (data_name != particle_name) { // Set cnt_[0]
  651. Iterator<Vector<Real>> pt_coord;
  652. Iterator<Vector<Long>> pt_cnt;
  653. this->GetData(pt_coord, pt_cnt, particle_name);
  654. cnt_[0] = pt_cnt[0];
  655. }
  656. }
  657. void GetParticleData(Vector<Real>& data, const std::string& data_name) const {
  658. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  659. const std::string& particle_name = data_pt_name.find(data_name)->second;
  660. SCTL_ASSERT(scatter_idx.find(particle_name) != scatter_idx.end());
  661. const auto& scatter_idx_ = scatter_idx.find(particle_name)->second;
  662. const Long Nlocal_ = Nlocal.find(particle_name)->second;
  663. const auto& mins = this->GetPartitionMID();
  664. const auto& node_mid = this->GetNodeMID();
  665. const auto& comm = this->GetComm();
  666. Long dof = 0;
  667. Vector<Long> dsp;
  668. ConstIterator<Vector<Long>> cnt_;
  669. ConstIterator<Vector<Real>> data_;
  670. this->GetData(data_, cnt_, data_name);
  671. SCTL_ASSERT(cnt_->Dim() == node_mid.Dim());
  672. BaseTree::scan(dsp, cnt_[0]);
  673. { // Set dof
  674. Long Nn = node_mid.Dim();
  675. StaticArray<Long,2> Ng, Nl = {data_->Dim(), dsp[Nn-1]+cnt_[0][Nn-1]};
  676. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  677. if (Ng[1]) dof = Ng[0] / Ng[1];
  678. }
  679. { // Set data
  680. Integer np = comm.Size();
  681. Integer rank = comm.Rank();
  682. Long N0 = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  683. Long N1 = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  684. Long start = dsp[N0] * dof;
  685. Long end = (N1<dsp.Dim() ? dsp[N1] : dsp[N1-1]+cnt_[0][N1-1]) * dof;
  686. data.ReInit(end-start, (Iterator<Real>)data_->begin()+start, true);
  687. comm.ScatterReverse(data, scatter_idx_, Nlocal_ * dof);
  688. }
  689. }
  690. void DeleteParticleData(const std::string& data_name) {
  691. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  692. auto particle_name = data_pt_name[data_name];
  693. if (data_name == particle_name) {
  694. std::vector<std::string> data_name_lst;
  695. for (auto& pair : data_pt_name) {
  696. if (pair.second == particle_name) {
  697. data_name_lst.push_back(pair.first);
  698. }
  699. }
  700. for (auto x : data_name_lst) {
  701. if (x != particle_name) {
  702. DeleteParticleData(x);
  703. }
  704. }
  705. Nlocal.erase(particle_name);
  706. }
  707. this->DeleteData(data_name);
  708. data_pt_name.erase(data_name);
  709. }
  710. void WriteParticleVTK(std::string fname, std::string data_name, bool show_ghost = false) const {
  711. typedef typename VTUData::VTKReal VTKReal;
  712. const auto& node_mid = this->GetNodeMID();
  713. const auto& node_attr = this->GetNodeAttr();
  714. VTUData vtu_data;
  715. if (DIM <= 3) { // Set vtu data
  716. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  717. std::string particle_name = data_pt_name.find(data_name)->second;
  718. ConstIterator<Vector<Real>> pt_coord = NullIterator<Vector<Real>>();
  719. ConstIterator<Vector<Real>> pt_value = NullIterator<Vector<Real>>();
  720. ConstIterator<Vector<Long>> pt_cnt = NullIterator<Vector<Long>>();
  721. Vector<Long> pt_dsp;
  722. Long value_dof = 0;
  723. { // Set pt_coord, pt_cnt, pt_dsp
  724. this->GetData(pt_coord, pt_cnt, particle_name);
  725. Tree<Real,DIM>::scan(pt_dsp, pt_cnt[0]);
  726. }
  727. if (particle_name != data_name) { // Set pt_value, value_dof
  728. ConstIterator<Vector<Long>> pt_cnt = NullIterator<Vector<Long>>();
  729. this->GetData(pt_value, pt_cnt, data_name);
  730. Long Npt = omp_par::reduce(pt_cnt->begin(), pt_cnt->Dim());
  731. value_dof = pt_value->Dim() / std::max<Long>(Npt,1);
  732. }
  733. Vector<VTKReal> &coord = vtu_data.coord;
  734. Vector<VTKReal> &value = vtu_data.value;
  735. Vector<int32_t> &connect = vtu_data.connect;
  736. Vector<int32_t> &offset = vtu_data.offset;
  737. Vector<uint8_t> &types = vtu_data.types;
  738. Long point_cnt = coord.Dim() / DIM;
  739. Long connect_cnt = connect.Dim();
  740. value.ReInit(point_cnt * value_dof);
  741. value.SetZero();
  742. SCTL_ASSERT(node_mid.Dim() == node_attr.Dim());
  743. SCTL_ASSERT(node_mid.Dim() == pt_cnt->Dim());
  744. for (Long i = 0; i < node_mid.Dim(); i++) {
  745. if (!show_ghost && node_attr[i].Ghost) continue;
  746. if (!node_attr[i].Leaf) continue;
  747. for (Long j = 0; j < pt_cnt[0][i]; j++) {
  748. ConstIterator<Real> pt_coord_ = pt_coord->begin() + (pt_dsp[i] + j) * DIM;
  749. ConstIterator<Real> pt_value_ = (value_dof ? pt_value->begin() + (pt_dsp[i] + j) * value_dof : NullIterator<Real>());
  750. for (Integer k = 0; k < DIM; k++) coord.PushBack((VTKReal)pt_coord_[k]);
  751. for (Integer k = DIM; k < 3; k++) coord.PushBack(0);
  752. for (Integer k = 0; k < value_dof; k++) value.PushBack((VTKReal)pt_value_[k]);
  753. connect.PushBack(point_cnt);
  754. connect_cnt++;
  755. point_cnt++;
  756. offset.PushBack(connect_cnt);
  757. types.PushBack(1);
  758. }
  759. }
  760. }
  761. vtu_data.WriteVTK(fname, this->GetComm());
  762. }
  763. private:
  764. std::map<std::string, Long> Nlocal;
  765. std::map<std::string, Vector<Morton<DIM>>> pt_mid;
  766. std::map<std::string, Vector<Long>> scatter_idx;
  767. std::map<std::string, std::string> data_pt_name;
  768. };
  769. }
  770. #endif //_SCTL_TREE_