From 4b53c63fbaf1275020e84b216ab65312c1fe0492 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Mon, 10 May 2021 11:17:59 +0200 Subject: [PATCH] helper functions for table creation --- libsrc/core/table.hpp | 40 +++++++++++++++++++++ libsrc/meshing/meshclass.cpp | 70 ++++++++++-------------------------- 2 files changed, 58 insertions(+), 52 deletions(-) diff --git a/libsrc/core/table.hpp b/libsrc/core/table.hpp index 34f40c93..2e6d37d6 100644 --- a/libsrc/core/table.hpp +++ b/libsrc/core/table.hpp @@ -9,6 +9,7 @@ #include #include +#include #include "array.hpp" #include "bitarray.hpp" @@ -347,6 +348,45 @@ namespace ngcore } }; + template + Table CreateTable( const TRange & range, const TFunc & func, std::optional< size_t > cnt ) + { + std::unique_ptr> pcreator; + + if(cnt) + pcreator = std::make_unique>(*cnt); + else + pcreator = std::make_unique>(); + + auto & creator = *pcreator; + + for ( ; !creator.Done(); creator++) + ParallelForRange + (range, [&] (auto myrange) + { + for (auto i : myrange) + func(creator, i); + }, TasksPerThread(4) + ); + + return creator.MoveTable(); + } + + template + Table CreateSortedTable( const TRange & range, const TFunc & func, std::optional< size_t > cnt ) + { + Table table = CreateTable(range, func, cnt); + ParallelForRange + (table.Range(), [&] (auto myrange) + { + for (auto i : myrange) + QuickSort(table[i]); + }, TasksPerThread(4) + ); + + return table; + } + class NGCORE_API FilteredTableCreator : public TableCreator { protected: diff --git a/libsrc/meshing/meshclass.cpp b/libsrc/meshing/meshclass.cpp index 38f614e7..d9fc8fb7 100644 --- a/libsrc/meshing/meshclass.cpp +++ b/libsrc/meshing/meshclass.cpp @@ -6534,70 +6534,36 @@ namespace netgen Table Mesh :: CreatePoint2ElementTable() const { - TableCreator creator(GetNP()); - for ( ; !creator.Done(); creator++) - ngcore::ParallelForRange - (Range(volelements), [&] (auto myrange) - { - for (ElementIndex ei : myrange) + return ngcore::CreateSortedTable( volelements.Range(), + [&](auto & table, ElementIndex ei) + { for (PointIndex pi : (*this)[ei].PNums()) - creator.Add (pi, ei); - }); - - auto elementsonnode = creator.MoveTable(); - ngcore::ParallelForRange - (elementsonnode.Range(), [&] (auto myrange) - { - for (PointIndex pi : myrange) - QuickSort(elementsonnode[pi]); - }, ngcore::TasksPerThread(4)); - - return move(elementsonnode); + table.Add (pi, ei); + }, GetNP()); } Table Mesh :: CreatePoint2SurfaceElementTable( int faceindex ) const { static Timer timer("Mesh::CreatePoint2SurfaceElementTable"); RegionTimer rt(timer); - TableCreator creator(GetNP()); - if(faceindex==0) { - for ( ; !creator.Done(); creator++) - ngcore::ParallelForRange - (Range(surfelements), [&] (auto myrange) - { - for (SurfaceElementIndex ei : myrange) + return ngcore::CreateSortedTable( surfelements.Range(), + [&](auto & table, SurfaceElementIndex ei) + { for (PointIndex pi : (*this)[ei].PNums()) - creator.Add (pi, ei); - }, - // ngcore::TasksPerThread(4)); - (surfelements.Size()>100) ? ngcore::TasksPerThread(4) : 1); + table.Add (pi, ei); + }, GetNP()); } - else - { - Array face_els; - GetSurfaceElementsOfFace(faceindex, face_els); - for ( ; !creator.Done(); creator++) - ngcore::ParallelForRange - (Range(face_els), [&] (auto myrange) - { - for (auto i : myrange) - for (PointIndex pi : (*this)[face_els[i]].PNums()) - creator.Add (pi, face_els[i]); - }, ngcore::TasksPerThread(4)); - } - - auto elementsonnode = creator.MoveTable(); - ngcore::ParallelForRange - (elementsonnode.Range(), [&] (auto myrange) - { - for (PointIndex pi : myrange) - QuickSort(elementsonnode[pi]); - }, - (surfelements.Size()>100) ? ngcore::TasksPerThread(1) : 1); - return move(elementsonnode); + Array face_els; + GetSurfaceElementsOfFace(faceindex, face_els); + return ngcore::CreateSortedTable( face_els.Range(), + [&](auto & table, size_t i) + { + for (PointIndex pi : (*this)[face_els[i]].PNums()) + table.Add (pi, face_els[i]); + }, GetNP()); }