CompressedTable(Creator)

This commit is contained in:
Joachim Schoeberl 2024-12-26 20:29:38 +01:00
parent a675c42d89
commit 1a610b060f
7 changed files with 153 additions and 9 deletions

View File

@ -1099,6 +1099,106 @@ namespace ngcore
return ost;
}
template <class T, class IndexType>
class CompressedTable
{
Table<T, size_t> table;
ClosedHashTable<IndexType, size_t> idmap;
public:
CompressedTable (Table<T, size_t> && atable, ClosedHashTable<IndexType, size_t> && aidmap)
: table(std::move(atable)), idmap(std::move(aidmap)) { }
FlatArray<T> operator[](IndexType id) const
{
if (auto nr = idmap.GetIfUsed(id))
return table[*nr];
else
return { 0, nullptr };
}
auto & Table() { return table; }
};
template <class T, typename IndexType>
class CompressedTableCreator
{
protected:
int mode; // 1 .. cnt, 2 .. cnt entries, 3 .. fill table
size_t nd; // number of entries;
ClosedHashTable<IndexType, size_t> idmap;
Array<int,size_t> cnt;
Table<T,size_t> table;
public:
CompressedTableCreator()
{ nd = 0; mode = 1; }
CompressedTable<T,IndexType> MoveTable()
{
return { std::move(table), std::move(idmap) };
}
bool Done () { return mode > 3; }
void operator++(int) { SetMode (mode+1); }
int GetMode () const { return mode; }
void SetMode (int amode)
{
mode = amode;
if (mode == 2)
{
cnt.SetSize(nd);
cnt = 0;
}
if (mode == 3)
{
table = Table<T,size_t> (cnt);
cnt = 0;
}
}
void Add (IndexType blocknr, const T & data)
{
switch (mode)
{
case 1:
{
if (!idmap.Used (blocknr))
idmap[blocknr] = nd++;
break;
}
case 2:
cnt[idmap.Get(blocknr)]++;
break;
case 3:
size_t cblock = idmap.Get(blocknr);
int ci = cnt[cblock]++;
table[cblock][ci] = data;
break;
}
}
};
} // namespace ngcore

View File

@ -17,6 +17,7 @@
#include "ngcore_api.hpp"
#include "profiler.hpp"
namespace ngcore
{
@ -454,6 +455,8 @@ namespace ngcore
/**
A dynamic table class.

View File

@ -212,7 +212,7 @@ namespace netgen
}
Array<Neighbour> neighbors(mesh.GetNSE());
auto elements_on_node = mesh.CreatePoint2SurfaceElementTable(faceindex);
auto elements_on_node = mesh.CreateCompressedPoint2SurfaceElementTable(faceindex);
Array<bool> swapped(mesh.GetNSE());
Array<int,PointIndex> pdef(mesh.GetNP());
@ -366,9 +366,9 @@ namespace netgen
template <typename T_PI2SEI>
double CombineImproveEdge( Mesh & mesh,
const Table<SurfaceElementIndex, PointIndex> & elementsonnode,
const T_PI2SEI & elementsonnode,
Array<Vec<3>, PointIndex> & normals,
Array<bool, PointIndex> & fixed,
PointIndex pi1, PointIndex pi2,
@ -601,7 +601,7 @@ namespace netgen
int np = mesh.GetNP();
auto elementsonnode = mesh.CreatePoint2SurfaceElementTable(faceindex);
auto elementsonnode = mesh.CreateCompressedPoint2SurfaceElementTable(faceindex);
// int ntasks = ngcore::TaskManager::GetMaxThreads();
Array<std::tuple<PointIndex, PointIndex>> edges;

View File

@ -36,10 +36,10 @@ inline void AppendEdges( const Element & elem, PointIndex pi, Array<std::tuple<P
}
}
template<typename TINDEX>
void BuildEdgeList( const Mesh & mesh, const Table<TINDEX, PointIndex> & elementsonnode, Array<std::tuple<PointIndex, PointIndex>> & edges )
template<typename T_PI2SEI>
void BuildEdgeList( const Mesh & mesh, const T_PI2SEI & elementsonnode, Array<std::tuple<PointIndex, PointIndex>> & edges )
{
static_assert(is_same_v<TINDEX, ElementIndex>||is_same_v<TINDEX,SurfaceElementIndex>, "Invalid type for TINDEX");
// static_assert(is_same_v<TINDEX, ElementIndex>||is_same_v<TINDEX,SurfaceElementIndex>, "Invalid type for TINDEX");
static Timer tbuild_edges("Build edges"); RegionTimer reg(tbuild_edges);
int ntasks = 4*ngcore::TaskManager::GetMaxThreads();

View File

@ -7016,6 +7016,8 @@ namespace netgen
Table<ElementIndex, PointIndex> Mesh :: CreatePoint2ElementTable(std::optional<BitArray> points, int domain) const
{
static Timer timer("Mesh::CreatePoint2VolumeElementTable"); RegionTimer rt(timer);
if(points)
{
const auto & free_points = *points;
@ -7075,6 +7077,42 @@ namespace netgen
}
CompressedTable<SurfaceElementIndex, PointIndex> Mesh :: CreateCompressedPoint2SurfaceElementTable( int faceindex ) const
{
static Timer timer("Mesh::CreatePoint2SurfaceElementTable"); RegionTimer rt(timer);
CompressedTableCreator<SurfaceElementIndex, PointIndex> creator;
if(faceindex==0)
{
for ( ; !creator.Done(); creator++)
for (auto sei : SurfaceElements().Range())
for (auto pi : (*this)[sei].PNums())
creator.Add(pi, sei);
}
else
{
Array<SurfaceElementIndex> face_els;
GetSurfaceElementsOfFace(faceindex, face_els);
for ( ; !creator.Done(); creator++)
for (auto sei : face_els)
for (auto pi : (*this)[sei].PNums())
creator.Add(pi, sei);
}
auto compressed_table = creator.MoveTable();
for (auto row : compressed_table.Table())
QuickSort (row);
return compressed_table;
}
/*
void Mesh :: BuildConnectedNodes ()

View File

@ -845,7 +845,9 @@ namespace netgen
DLL_HEADER Table<ElementIndex, PointIndex> CreatePoint2ElementTable(std::optional<BitArray> points = std::nullopt, int domain = 0) const;
// DLL_HEADER Table<SurfaceElementIndex, PointIndex> CreatePoint2SurfaceElementTable( int faceindex=0 ) const;
DLL_HEADER Table<SurfaceElementIndex, PointIndex> CreatePoint2SurfaceElementTable( int faceindex=0 ) const;
DLL_HEADER CompressedTable<SurfaceElementIndex, PointIndex> CreateCompressedPoint2SurfaceElementTable( int faceindex=0 ) const;
DLL_HEADER bool PureTrigMesh (int faceindex = 0) const;
DLL_HEADER bool PureTetMesh () const;

View File

@ -699,7 +699,8 @@ namespace netgen
int ncolors;
Array<int> colors;
bool mixed = false;
auto elementsonpoint = mesh.CreatePoint2SurfaceElementTable( faceindex );
// auto elementsonpoint = mesh.CreatePoint2SurfaceElementTable( faceindex );
auto elementsonpoint = mesh.CreateCompressedPoint2SurfaceElementTable( faceindex );
NgArray<MeshPoint, PointIndex::BASE> savepoints(mesh.GetNP());
Table<PointIndex> color_table;