From 99e001fc4c2d908942151ca388011ef460694652 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Tue, 11 May 2021 18:21:40 +0200 Subject: [PATCH 1/4] TABLE -> Table --- libsrc/include/nginterface_v2_impl.hpp | 6 ++-- libsrc/interface/nginterface.cpp | 2 +- libsrc/meshing/topology.cpp | 42 +++++++++----------------- libsrc/meshing/topology.hpp | 8 ++--- 4 files changed, 22 insertions(+), 36 deletions(-) diff --git a/libsrc/include/nginterface_v2_impl.hpp b/libsrc/include/nginterface_v2_impl.hpp index 645eb080..b63a0462 100644 --- a/libsrc/include/nginterface_v2_impl.hpp +++ b/libsrc/include/nginterface_v2_impl.hpp @@ -271,18 +271,18 @@ template <> NGX_INLINE DLL_HEADER const Ng_Node<0> Ngx_Mesh :: GetNode<0> (int v node.elements.ne = ia.Size(); node.elements.ptr = (int*)&ia[0]; - NgFlatArray bia = mesh->GetTopology().GetVertexSegments(vnr); + auto bia = mesh->GetTopology().GetVertexSegments(vnr); node.bnd_elements.ne = bia.Size(); node.bnd_elements.ptr = (int*)&bia[0]; break; } case 1: { - NgFlatArray ia = mesh->GetTopology().GetVertexSegments(vnr); + auto ia = mesh->GetTopology().GetVertexSegments(vnr); node.elements.ne = ia.Size(); node.elements.ptr = (int*)&ia[0]; - NgFlatArray bia = mesh->GetTopology().GetVertexPointElements(vnr); + auto bia = mesh->GetTopology().GetVertexPointElements(vnr); node.bnd_elements.ne = bia.Size(); node.bnd_elements.ptr = (int*)&bia[0]; break; diff --git a/libsrc/interface/nginterface.cpp b/libsrc/interface/nginterface.cpp index 5b329569..24da6783 100644 --- a/libsrc/interface/nginterface.cpp +++ b/libsrc/interface/nginterface.cpp @@ -1653,7 +1653,7 @@ void Ng_GetVertexElements (int vnr, int * els) } case 1: { - NgFlatArray ia = mesh->GetTopology().GetVertexSegments(vnr); + auto ia = mesh->GetTopology().GetVertexSegments(vnr); for (int i = 0; i < ia.Size(); i++) els[i] = ia[i]+1; break; /* diff --git a/libsrc/meshing/topology.cpp b/libsrc/meshing/topology.cpp index e0c1ccf3..2172746e 100644 --- a/libsrc/meshing/topology.cpp +++ b/libsrc/meshing/topology.cpp @@ -408,36 +408,22 @@ namespace netgen vert2element = mesh->CreatePoint2ElementTable(); vert2surfelement = mesh->CreatePoint2SurfaceElementTable(0); - cnt = 0; - for (SegmentIndex si = 0; si < nseg; si++) - { - const Segment & seg = mesh->LineSegment(si); - cnt[seg[0]]++; - cnt[seg[1]]++; - } - - vert2segment = TABLE (cnt); - for (SegmentIndex si = 0; si < nseg; si++) - { - const Segment & seg = mesh->LineSegment(si); - vert2segment.AddSave (seg[0], si); - vert2segment.AddSave (seg[1], si); - } + vert2segment = ngcore::CreateSortedTable( mesh->LineSegments().Range(), + [&](auto & table, SegmentIndex segi) + { + const Segment & seg = (*mesh)[segi]; + table.Add (seg[0], segi); + table.Add (seg[1], segi); + }, np); + + vert2pointelement = ngcore::CreateSortedTable( mesh->pointelements.Range(), + [&](auto & table, int pei) + { + const Element0d & pointel = mesh->pointelements[pei]; + table.Add(pointel.pnum, pei); + }, np); - cnt = 0; - for (int pei = 0; pei < mesh->pointelements.Size(); pei++) - { - const Element0d & pointel = mesh->pointelements[pei]; - cnt[pointel.pnum]++; - } - - vert2pointelement = TABLE (cnt); - for (int pei = 0; pei < mesh->pointelements.Size(); pei++) - { - const Element0d & pointel = mesh->pointelements[pei]; - vert2pointelement.AddSave (pointel.pnum, pei); - } (*tracer) ("Topology::Update setup tables", true); diff --git a/libsrc/meshing/topology.hpp b/libsrc/meshing/topology.hpp index 9ce2e47a..e6bcf76f 100644 --- a/libsrc/meshing/topology.hpp +++ b/libsrc/meshing/topology.hpp @@ -67,8 +67,8 @@ class MeshTopology Array edge2segment; Table vert2element; Table vert2surfelement; - TABLE vert2segment; - TABLE vert2pointelement; + Table vert2segment; + Table vert2pointelement; int timestamp; public: int GetNSurfedges() const {return surfedges.Size();} @@ -183,10 +183,10 @@ public: FlatArray GetVertexSurfaceElements(PointIndex vnr) const { return vert2surfelement[vnr]; } - NgFlatArray GetVertexSegments (int vnr) const + FlatArray GetVertexSegments (int vnr) const { return vert2segment[vnr]; } - NgFlatArray GetVertexPointElements (int vnr) const + FlatArray GetVertexPointElements (int vnr) const { return vert2pointelement[vnr]; } int GetVerticesEdge ( int v1, int v2) const; From 5bba076c9ec92845ca7795f5c75c8d6b9a728477 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 12 May 2021 09:42:38 +0200 Subject: [PATCH 2/4] UpdateTopology - use TasksPerThread(4) -> faster table update --- libsrc/meshing/topology.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libsrc/meshing/topology.cpp b/libsrc/meshing/topology.cpp index 2172746e..5de13b40 100644 --- a/libsrc/meshing/topology.cpp +++ b/libsrc/meshing/topology.cpp @@ -5,6 +5,7 @@ namespace netgen { using ngcore::ParallelForRange; using ngcore::INT; + using ngcore::TasksPerThread; template void QuickSortRec (NgFlatArray data, @@ -521,7 +522,7 @@ namespace netgen }); cnt[v] = cnti; } - } ); + }, TasksPerThread(4) ); // accumulate number of edges int ned = edge2vert.Size(); @@ -610,7 +611,7 @@ namespace netgen } }); } - } ); + }, TasksPerThread(4) ); if (build_parent_edges) @@ -1037,7 +1038,7 @@ namespace netgen }); cnt[v] = cnti; } - } ); + }, TasksPerThread(4) ); // NgProfiler::StopTimer (timer2b1); // accumulate number of faces @@ -1142,7 +1143,7 @@ namespace netgen } }); } - }); + }, TasksPerThread(4) ); /* int oldnfa = face2vert.Size(); @@ -1471,7 +1472,7 @@ namespace netgen for (auto f : hfaces) AsAtomic(face_els[f-1])++; } - }); + }, TasksPerThread(4)); for (int i = 1; i <= nse; i++) face_surfels[GetSurfaceElementFace (i)-1]++; (*tracer) ("Topology::Update count face_els", true); From db494f4239b4fc2ed0aba1b276006d67606f8498 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 12 May 2021 10:56:21 +0200 Subject: [PATCH 3/4] more Timers in Mesh --- libsrc/core/table.hpp | 4 ++++ libsrc/meshing/clusters.cpp | 8 ++++---- libsrc/meshing/meshclass.cpp | 8 ++++++++ libsrc/meshing/topology.cpp | 11 +++++++---- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/libsrc/core/table.hpp b/libsrc/core/table.hpp index 2e6d37d6..6a851605 100644 --- a/libsrc/core/table.hpp +++ b/libsrc/core/table.hpp @@ -351,6 +351,8 @@ namespace ngcore template Table CreateTable( const TRange & range, const TFunc & func, std::optional< size_t > cnt ) { + static Timer timer("CreateTable"); + RegionTimer rt(timer); std::unique_ptr> pcreator; if(cnt) @@ -375,6 +377,8 @@ namespace ngcore template Table CreateSortedTable( const TRange & range, const TFunc & func, std::optional< size_t > cnt ) { + static Timer timer("CreateSortedTable"); + RegionTimer rt(timer); Table table = CreateTable(range, func, cnt); ParallelForRange (table.Range(), [&] (auto myrange) diff --git a/libsrc/meshing/clusters.cpp b/libsrc/meshing/clusters.cpp index dae820df..c01d1db3 100644 --- a/libsrc/meshing/clusters.cpp +++ b/libsrc/meshing/clusters.cpp @@ -18,11 +18,11 @@ namespace netgen void AnisotropicClusters :: Update(NgTaskManager tm, NgTracer tracer) { - static int timer = NgProfiler::CreateTimer ("clusters"); + static Timer timer("clusters"); // static int timer1 = NgProfiler::CreateTimer ("clusters1"); // static int timer2 = NgProfiler::CreateTimer ("clusters2"); // static int timer3 = NgProfiler::CreateTimer ("clusters3"); - NgProfiler::RegionTimer reg (timer); + RegionTimer reg (timer); const MeshTopology & top = mesh.GetTopology(); @@ -215,7 +215,8 @@ namespace netgen do { - (*tracer) ("update cluster, identify", false); + static Timer t("update cluster, identify"); + RegionTimer rtr(t); cnt++; changed = 0; @@ -338,7 +339,6 @@ namespace netgen } */ } - (*tracer) ("update cluster, identify", true); } while (changed); // NgProfiler::StopTimer(timer3); diff --git a/libsrc/meshing/meshclass.cpp b/libsrc/meshing/meshclass.cpp index d9fc8fb7..1d325d8a 100644 --- a/libsrc/meshing/meshclass.cpp +++ b/libsrc/meshing/meshclass.cpp @@ -916,6 +916,7 @@ namespace netgen void Mesh :: Load (istream & infile) { + static Timer timer("Mesh::Load"); RegionTimer rt(timer); if (! (infile.good()) ) { cout << "cannot load mesh" << endl; @@ -956,6 +957,7 @@ namespace netgen if (strcmp (str, "surfaceelements") == 0 || strcmp (str, "surfaceelementsgi")==0 || strcmp (str, "surfaceelementsuv") == 0) { + static Timer t1("read surface elements"); RegionTimer rt1(t1); infile >> n; PrintMessage (3, n, " surface elements"); @@ -1020,6 +1022,7 @@ namespace netgen if (strcmp (str, "volumeelements") == 0) { + static Timer t1("read volume elements"); RegionTimer rt1(t1); infile >> n; PrintMessage (3, n, " volume elements"); for (i = 1; i <= n; i++) @@ -1045,6 +1048,7 @@ namespace netgen if (strcmp (str, "edgesegments") == 0) { + static Timer t1("read edge segments"); RegionTimer rt1(t1); infile >> n; for (i = 1; i <= n; i++) { @@ -1059,6 +1063,7 @@ namespace netgen if (strcmp (str, "edgesegmentsgi") == 0) { + static Timer t1("read edge segmentsgi"); RegionTimer rt1(t1); infile >> n; for (i = 1; i <= n; i++) { @@ -1073,6 +1078,7 @@ namespace netgen if (strcmp (str, "edgesegmentsgi2") == 0) { + static Timer t1("read edge segmentsgi2"); RegionTimer rt1(t1); int a; infile >> a; n=a; @@ -1106,6 +1112,7 @@ namespace netgen if (strcmp (str, "points") == 0) { + static Timer t1("read points"); RegionTimer rt1(t1); infile >> n; PrintMessage (3, n, " points"); for (i = 1; i <= n; i++) @@ -1122,6 +1129,7 @@ namespace netgen if (strcmp (str, "pointelements") == 0) { + static Timer t1("read point elements"); RegionTimer rt1(t1); infile >> n; PrintMessage (3, n, " pointelements"); for (i = 1; i <= n; i++) diff --git a/libsrc/meshing/topology.cpp b/libsrc/meshing/topology.cpp index 5de13b40..95122a11 100644 --- a/libsrc/meshing/topology.cpp +++ b/libsrc/meshing/topology.cpp @@ -368,6 +368,7 @@ namespace netgen void MeshTopology :: Update (NgTaskManager tm_unused, NgTracer tracer) { static Timer timer("Topology::Update"); + static Timer timer_tables("Build vertex to element table"); RegionTimer reg (timer); #ifdef PARALLEL @@ -406,6 +407,7 @@ namespace netgen vertex to segment */ + timer_tables.Start(); vert2element = mesh->CreatePoint2ElementTable(); vert2surfelement = mesh->CreatePoint2SurfaceElementTable(0); @@ -423,6 +425,7 @@ namespace netgen const Element0d & pointel = mesh->pointelements[pei]; table.Add(pointel.pnum, pei); }, np); + timer_tables.Stop(); (*tracer) ("Topology::Update setup tables", true); @@ -430,8 +433,8 @@ namespace netgen if (buildedges) { - static int timer1 = NgProfiler::CreateTimer ("topology::buildedges"); - NgProfiler::RegionTimer reg1 (timer1); + static Timer timer1("topology::buildedges"); + RegionTimer reg1(timer1); if (id == 0) PrintMessage (5, "Update edges "); @@ -894,12 +897,12 @@ namespace netgen // generate faces if (buildfaces) { - static int timer2 = NgProfiler::CreateTimer ("topology::buildfaces"); + static Timer timer2("topology::buildfaces"); // static int timer2a = NgProfiler::CreateTimer ("topology::buildfacesa"); // static int timer2b = NgProfiler::CreateTimer ("topology::buildfacesb"); // static int timer2b1 = NgProfiler::CreateTimer ("topology::buildfacesb1"); // static int timer2c = NgProfiler::CreateTimer ("topology::buildfacesc"); - NgProfiler::RegionTimer reg2 (timer2); + RegionTimer reg2 (timer2); if (id == 0) PrintMessage (5, "Update faces "); From d1ee6efc14d42241f0bfceee8085f4fdb6bb609d Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 12 May 2021 10:56:34 +0200 Subject: [PATCH 4/4] parallel Cluster update --- libsrc/meshing/clusters.cpp | 24 +++++++++++++----------- libsrc/meshing/clusters.hpp | 2 +- libsrc/meshing/meshclass.cpp | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/libsrc/meshing/clusters.cpp b/libsrc/meshing/clusters.cpp index c01d1db3..97e77e36 100644 --- a/libsrc/meshing/clusters.cpp +++ b/libsrc/meshing/clusters.cpp @@ -16,7 +16,7 @@ namespace netgen ; } - void AnisotropicClusters :: Update(NgTaskManager tm, NgTracer tracer) + void AnisotropicClusters :: Update() { static Timer timer("clusters"); // static int timer1 = NgProfiler::CreateTimer ("clusters1"); @@ -81,13 +81,14 @@ namespace netgen cluster_reps.Elem(nnums[j]) = nnums[j]; } */ - ParallelForRange - (tm, ne, - [&] (size_t begin, size_t end) + ngcore::ParallelForRange + (mesh.VolumeElements().Range(), + [&] (auto myrange) { NgArray nnums, ednums, fanums; - for (int i = begin+1; i <= end; i++) + for (int i_ : myrange) { + int i = i_+1; const Element & el = mesh.VolumeElement(i); ELEMENT_TYPE typ = el.GetType(); @@ -110,7 +111,7 @@ namespace netgen for (int j = 0; j < nnums.Size(); j++) cluster_reps.Elem(nnums[j]) = nnums[j]; } - }); + }, ngcore::TasksPerThread(4)); // NgProfiler::StopTimer(timer1); // NgProfiler::StartTimer(timer2); @@ -137,13 +138,14 @@ namespace netgen cluster_reps.Elem(nnums[j]) = nnums[j]; } */ - ParallelForRange - (tm, nse, - [&] (size_t begin, size_t end) + ngcore::ParallelForRange + (mesh.SurfaceElements().Range(), + [&] (auto myrange) { NgArrayMem nnums, ednums; - for (int i = begin+1; i <= end; i++) + for (int i_ : myrange) { + int i = i_+1; const Element2d & el = mesh.SurfaceElement(i); ELEMENT_TYPE typ = el.GetType(); @@ -163,7 +165,7 @@ namespace netgen for (int j = 0; j < nnums.Size(); j++) cluster_reps.Elem(nnums[j]) = nnums[j]; } - }); + }, ngcore::TasksPerThread(4)); // NgProfiler::StopTimer(timer2); diff --git a/libsrc/meshing/clusters.hpp b/libsrc/meshing/clusters.hpp index 21e122fb..21854f39 100644 --- a/libsrc/meshing/clusters.hpp +++ b/libsrc/meshing/clusters.hpp @@ -27,7 +27,7 @@ public: AnisotropicClusters (const Mesh & amesh); ~AnisotropicClusters(); - void Update(NgTaskManager tm = &DummyTaskManager, NgTracer trace = &DummyTracer); + void Update(); int GetVertexRepresentant (int vnr) const { return cluster_reps.Get(vnr); } diff --git a/libsrc/meshing/meshclass.cpp b/libsrc/meshing/meshclass.cpp index 1d325d8a..c31a6a2c 100644 --- a/libsrc/meshing/meshclass.cpp +++ b/libsrc/meshing/meshclass.cpp @@ -6706,7 +6706,7 @@ namespace netgen static Timer t("Update Topology"); RegionTimer reg(t); topology.Update(tm, tracer); (*tracer)("call update clusters", false); - clusters->Update(tm, tracer); + clusters->Update(); (*tracer)("call update clusters", true); #ifdef PARALLEL if (paralleltop)