helper functions for table creation

This commit is contained in:
Matthias Hochsteger 2021-05-10 11:17:59 +02:00 committed by mhochsteger
parent 955eaa682c
commit 4b53c63fba
2 changed files with 58 additions and 52 deletions

View File

@ -9,6 +9,7 @@
#include <atomic> #include <atomic>
#include <iostream> #include <iostream>
#include <optional>
#include "array.hpp" #include "array.hpp"
#include "bitarray.hpp" #include "bitarray.hpp"
@ -347,6 +348,45 @@ namespace ngcore
} }
}; };
template <typename TEntry, typename TIndex, typename TRange, typename TFunc>
Table<TEntry, TIndex> CreateTable( const TRange & range, const TFunc & func, std::optional< size_t > cnt )
{
std::unique_ptr<TableCreator<TEntry, TIndex>> pcreator;
if(cnt)
pcreator = std::make_unique<TableCreator<TEntry, TIndex>>(*cnt);
else
pcreator = std::make_unique<TableCreator<TEntry, TIndex>>();
auto & creator = *pcreator;
for ( ; !creator.Done(); creator++)
ParallelForRange
(range, [&] (auto myrange)
{
for (auto i : myrange)
func(creator, i);
}, TasksPerThread(4)
);
return creator.MoveTable();
}
template <typename TEntry, typename TIndex, typename TRange, typename TFunc>
Table<TEntry, TIndex> CreateSortedTable( const TRange & range, const TFunc & func, std::optional< size_t > cnt )
{
Table<TEntry, TIndex> table = CreateTable<TEntry, TIndex>(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<int> class NGCORE_API FilteredTableCreator : public TableCreator<int>
{ {
protected: protected:

View File

@ -6534,70 +6534,36 @@ namespace netgen
Table<ElementIndex, PointIndex> Mesh :: CreatePoint2ElementTable() const Table<ElementIndex, PointIndex> Mesh :: CreatePoint2ElementTable() const
{ {
TableCreator<ElementIndex, PointIndex> creator(GetNP()); return ngcore::CreateSortedTable<ElementIndex, PointIndex>( volelements.Range(),
for ( ; !creator.Done(); creator++) [&](auto & table, ElementIndex ei)
ngcore::ParallelForRange {
(Range(volelements), [&] (auto myrange)
{
for (ElementIndex ei : myrange)
for (PointIndex pi : (*this)[ei].PNums()) for (PointIndex pi : (*this)[ei].PNums())
creator.Add (pi, ei); table.Add (pi, ei);
}); }, GetNP());
auto elementsonnode = creator.MoveTable();
ngcore::ParallelForRange
(elementsonnode.Range(), [&] (auto myrange)
{
for (PointIndex pi : myrange)
QuickSort(elementsonnode[pi]);
}, ngcore::TasksPerThread(4));
return move(elementsonnode);
} }
Table<SurfaceElementIndex, PointIndex> Mesh :: CreatePoint2SurfaceElementTable( int faceindex ) const Table<SurfaceElementIndex, PointIndex> Mesh :: CreatePoint2SurfaceElementTable( int faceindex ) const
{ {
static Timer timer("Mesh::CreatePoint2SurfaceElementTable"); RegionTimer rt(timer); static Timer timer("Mesh::CreatePoint2SurfaceElementTable"); RegionTimer rt(timer);
TableCreator<SurfaceElementIndex, PointIndex> creator(GetNP());
if(faceindex==0) if(faceindex==0)
{ {
for ( ; !creator.Done(); creator++) return ngcore::CreateSortedTable<SurfaceElementIndex, PointIndex>( surfelements.Range(),
ngcore::ParallelForRange [&](auto & table, SurfaceElementIndex ei)
(Range(surfelements), [&] (auto myrange) {
{
for (SurfaceElementIndex ei : myrange)
for (PointIndex pi : (*this)[ei].PNums()) for (PointIndex pi : (*this)[ei].PNums())
creator.Add (pi, ei); table.Add (pi, ei);
}, }, GetNP());
// ngcore::TasksPerThread(4));
(surfelements.Size()>100) ? ngcore::TasksPerThread(4) : 1);
}
else
{
Array<SurfaceElementIndex> 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(); Array<SurfaceElementIndex> face_els;
ngcore::ParallelForRange GetSurfaceElementsOfFace(faceindex, face_els);
(elementsonnode.Range(), [&] (auto myrange) return ngcore::CreateSortedTable<SurfaceElementIndex, PointIndex>( face_els.Range(),
{ [&](auto & table, size_t i)
for (PointIndex pi : myrange) {
QuickSort(elementsonnode[pi]); for (PointIndex pi : (*this)[face_els[i]].PNums())
}, table.Add (pi, face_els[i]);
(surfelements.Size()>100) ? ngcore::TasksPerThread(1) : 1); }, GetNP());
return move(elementsonnode);
} }