From 4a9188da61ccb7ef8262f2faa2233e70cd035617 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Sun, 29 Dec 2024 21:36:37 +0100 Subject: [PATCH] more use of ElementIndex, T_Range(size_t) is now explicit --- libsrc/core/array.hpp | 3 +- libsrc/core/taskmanager.hpp | 6 + libsrc/gprim/adtree.hpp | 4 +- libsrc/meshing/bisect.cpp | 150 +++++++++++-------- libsrc/meshing/boundarylayer.cpp | 2 +- libsrc/meshing/boundarylayer_interpolate.cpp | 6 +- libsrc/meshing/boundarylayer_limiter.hpp | 2 +- libsrc/meshing/curvedelems.cpp | 2 +- libsrc/meshing/improve2.cpp | 2 +- libsrc/meshing/meshclass.hpp | 2 +- libsrc/meshing/meshtype.hpp | 26 +++- libsrc/visualization/vssolution.cpp | 2 +- 12 files changed, 123 insertions(+), 84 deletions(-) diff --git a/libsrc/core/array.hpp b/libsrc/core/array.hpp index 4418ef6c..00859e55 100644 --- a/libsrc/core/array.hpp +++ b/libsrc/core/array.hpp @@ -278,7 +278,8 @@ namespace ngcore T first, next; public: NETGEN_INLINE T_Range () { ; } - NETGEN_INLINE T_Range (T n) : first(0), next(n) {;} + // NETGEN_INLINE T_Range (T n) : first(0), next(n) {;} + NETGEN_INLINE explicit T_Range (size_t n) : first(IndexBASE()), next(IndexBASE()+n) {;} NETGEN_INLINE T_Range (T f, T n) : first(f), next(n) {;} template NETGEN_INLINE T_Range(T_Range r2) : first(r2.First()), next(r2.Next()) { ; } diff --git a/libsrc/core/taskmanager.hpp b/libsrc/core/taskmanager.hpp index 430fea2b..9a37a247 100644 --- a/libsrc/core/taskmanager.hpp +++ b/libsrc/core/taskmanager.hpp @@ -317,6 +317,7 @@ namespace ngcore public: SharedLoop (IntRange ar) : r(ar) { cnt = r.begin(); } + SharedLoop (size_t s) : SharedLoop (IntRange{s}) { ; } SharedIterator begin() { return SharedIterator (cnt, r.end(), true); } SharedIterator end() { return SharedIterator (cnt, r.end(), false); } }; @@ -623,6 +624,8 @@ public: Reset (r); } + SharedLoop2 (size_t s) : SharedLoop2 (IntRange{s}) { } + void Reset (IntRange r) { for (size_t i = 0; i < ranges.Size(); i++) @@ -632,6 +635,9 @@ public: participants.store(0, std::memory_order_relaxed); processed.store(0, std::memory_order_release); } + + void Reset (size_t s) { Reset(IntRange{s}); } + SharedIterator begin() { diff --git a/libsrc/gprim/adtree.hpp b/libsrc/gprim/adtree.hpp index c0abcb47..21108630 100644 --- a/libsrc/gprim/adtree.hpp +++ b/libsrc/gprim/adtree.hpp @@ -970,7 +970,7 @@ public: Leaf *leaf1 = (Leaf*) ball_leaves.Alloc(); new (leaf1) Leaf(); Leaf *leaf2 = (Leaf*) ball_leaves.Alloc(); new (leaf2) Leaf(); - for (auto i : order.Range(isplit)) + for (auto i : order.Range(0, isplit)) leaf1->Add(leaf_index, leaf->p[i], leaf->index[i] ); for (auto i : order.Range(isplit, N)) leaf2->Add(leaf_index, leaf->p[i], leaf->index[i] ); @@ -1334,7 +1334,7 @@ public: leaves.Append(leaf2); leaves[leaf1->nr] = leaf1; - for (auto i : order.Range(isplit)) + for (auto i : order.Range(0,isplit)) leaf1->Add(leaves, leaf_index, leaf->p[i], leaf->index[i] ); for (auto i : order.Range(isplit, N)) leaf2->Add(leaves, leaf_index, leaf->p[i], leaf->index[i] ); diff --git a/libsrc/meshing/bisect.cpp b/libsrc/meshing/bisect.cpp index 9eeb9419..818958f2 100644 --- a/libsrc/meshing/bisect.cpp +++ b/libsrc/meshing/bisect.cpp @@ -1118,15 +1118,16 @@ namespace netgen for (int step = 1; step <= 2; step++) { - for (int i = 1; i <= mtets.Size(); i++) + // for (int i = 1; i <= mtets.Size(); i++) + for (ElementIndex ei : mtets.Range()) { double h = 0; for (int j = 0; j < 3; j++) for (int k = j+1; k < 4; k++) { - const Point<3> & p1 = mesh.Point (mtets[i-1].pnums[j]); - const Point<3> & p2 = mesh.Point (mtets[i-1].pnums[k]); + const Point<3> & p1 = mesh.Point (mtets[ei].pnums[j]); + const Point<3> & p2 = mesh.Point (mtets[ei].pnums[k]); double hh = Dist2 (p1, p2); if (hh > h) h = hh; } @@ -1135,7 +1136,7 @@ namespace netgen double hshould = 1e10; for (int j = 0; j < 4; j++) { - double hi = hv (mtets[i-1].pnums[j]-IndexBASE()); + double hi = hv (mtets[ei].pnums[j]-IndexBASE()); if (hi < hshould) hshould = hi; } @@ -1150,13 +1151,12 @@ namespace netgen { if (h > hshould * hfac) { - mtets[i-1].marked = 1; + mtets[ei].marked = 1; marked = 1; } else - mtets[i-1].marked = 0; + mtets[ei].marked = 0; } - } for (int i = 1; i <= mprisms.Size(); i++) { @@ -1701,13 +1701,15 @@ namespace netgen int hanging = 0; // for (int i = 1; i <= mtets.Size(); i++) - ParallelForRange - (tm, mtets.Size(), [&] (size_t begin, size_t end) + ngcore::ParallelForRange + // (tm, mtets.Size(), [&] (size_t begin, size_t end) + (mtets.Range(), [&] (auto myrange) { bool my_hanging = false; - for (size_t i = begin; i < end; i++) + // for (size_t i = begin; i < end; i++) + for (auto ei : myrange) { - MarkedTet & teti = mtets[i]; + MarkedTet & teti = mtets[ei]; if (teti.marked) { @@ -1894,8 +1896,8 @@ namespace netgen const auto& mtris = *mesh.bisectioninfo.mtris; const auto& mquads = *mesh.bisectioninfo.mquads; ost << mtets.Size() << "\n"; - for(int i=0; i> size; mtets.SetSize(size); constexpr auto PI0 = IndexBASE(); - for(int i=0; i(size) ) { - ist >> mtets[i]; - if(mtets[i].pnums[0] >= PI0+mesh.GetNV() || - mtets[i].pnums[1] >= PI0+mesh.GetNV() || - mtets[i].pnums[2] >= PI0+mesh.GetNV() || - mtets[i].pnums[3] >= PI0+mesh.GetNV()) + ist >> mtets[ei]; + if(mtets[ei].pnums[0] >= PI0+mesh.GetNV() || + mtets[ei].pnums[1] >= PI0+mesh.GetNV() || + mtets[ei].pnums[2] >= PI0+mesh.GetNV() || + mtets[ei].pnums[3] >= PI0+mesh.GetNV()) return false; } @@ -2530,9 +2533,10 @@ namespace netgen int maxnum = BTSortEdges (mesh, idmaps, edgenumber); - for(int m = 0; m < mtets_old.Size(); m++) + // for(int m = 0; m < mtets_old.Size(); m++) + for (auto mi : mtets_old.Range()) { - MarkedTet & mt = mtets_old[m]; + MarkedTet & mt = mtets_old[mi]; //(*testout) << "old mt " << mt; @@ -2926,8 +2930,12 @@ namespace netgen if(st == "refinementinfo") // new version { + /* for(int i=1; i<=mtets.Size(); i++) mtets[i-1].marked = 0; + */ + for(auto ei : mtets.Range()) + mtets[ei].marked = 0; for(int i=1; i<=mprisms.Size(); i++) mprisms.Elem(i).marked = 0; for(int i=1; i<=mtris.Size(); i++) @@ -2968,7 +2976,8 @@ namespace netgen while(inf && isint) { - mtets[atoi(st.c_str())-1].marked = 3; + // mtets[atoi(st.c_str())-1].marked = 3; + mtets[IndexBASE()+(atoi(st.c_str())-1)].marked = 3; marked = 1; inf >> st; @@ -3056,12 +3065,13 @@ namespace netgen inf.open(opt.refinementfilename); char ch; - for (int i = 1; i <= mtets.Size(); i++) + // for (int i = 1; i <= mtets.Size(); i++) + for (auto ei : mtets.Range()) { inf >> ch; if(!inf) throw NgException("something wrong with refinementinfo file (old format)"); - mtets[i-1].marked = (ch == '1'); + mtets[ei].marked = (ch == '1'); } marked = 1; } @@ -3075,18 +3085,18 @@ namespace netgen // all in one ! if (mprisms.Size()) { - int cnttet = 0; + ElementIndex cnttet = IndexBASE(); int cntprism = 0; for (int i = 1; i <= mesh.GetNE(); i++) { if (mesh.VolumeElement(i).GetType() == TET || mesh.VolumeElement(i).GetType() == TET10) { - cnttet++; - mtets[cnttet-1].marked = + mtets[cnttet].marked = (opt.onlyonce ? 3 : 1) * mesh.VolumeElement(i).TestRefinementFlag(); - if (mtets[cnttet-1].marked) + if (mtets[cnttet].marked) cntm++; + cnttet++; } else { @@ -3100,11 +3110,12 @@ namespace netgen } } else - for (int i = 1; i <= mtets.Size(); i++) + // for (int i = 1; i <= mtets.Size(); i++) + for (auto ei : mtets.Range()) { - mtets[i-1].marked = - (opt.onlyonce ? 1 : 3) * mesh.VolumeElement(i).TestRefinementFlag(); - if (mtets[i-1].marked) + mtets[ei].marked = + (opt.onlyonce ? 1 : 3) * mesh.VolumeElement(ei).TestRefinementFlag(); + if (mtets[ei].marked) cntm++; } @@ -3202,12 +3213,12 @@ namespace netgen { PrintMessage(3,"refine p"); - for (int i = 1; i <= mtets.Size(); i++) - mtets[i-1].incorder = mtets[i-1].marked ? 1 : 0; + for (auto ei : mtets.Range()) + mtets[ei].incorder = mtets[ei].marked ? 1 : 0; - for (int i = 1; i <= mtets.Size(); i++) - if (mtets[i-1].incorder) - mtets[i-1].marked = 0; + for (auto ei : mtets.Range()) + if (mtets[ei].incorder) + mtets[ei].marked = 0; for (int i = 1; i <= mprisms.Size(); i++) @@ -3272,19 +3283,22 @@ namespace netgen - for (int i = 1; i <= mtets.Size(); i++) - mtets[i-1].incorder = 1; - for (int i = 1; i <= mtets.Size(); i++) + // for (int i = 1; i <= mtets.Size(); i++) + for (auto ei : mtets.Range()) + mtets[ei].incorder = 1; + // for (int i = 1; i <= mtets.Size(); i++) + for (auto ei : mtets.Range()) { - if (!mtets[i-1].marked) - mtets[i-1].incorder = 0; + if (!mtets[ei].marked) + mtets[ei].incorder = 0; for (int j = 0; j < 4; j++) - if (singv.Test (mtets[i-1].pnums[j])) - mtets[i-1].incorder = 0; + if (singv.Test (mtets[ei].pnums[j])) + mtets[ei].incorder = 0; } - for (int i = 1; i <= mtets.Size(); i++) - if (mtets[i-1].incorder) - mtets[i-1].marked = 0; + // for (int i = 1; i <= mtets.Size(); i++) + for (auto ei : mtets.Range()) + if (mtets[ei].incorder) + mtets[ei].marked = 0; for (int i = 1; i <= mprisms.Size(); i++) @@ -3335,10 +3349,11 @@ namespace netgen NgProfiler::StartTimer (timer_bisecttet); (*opt.tracer)("bisecttet", false); size_t nel = mtets.Size(); - for (size_t i = 0; i < nel; i++) - if (mtets[i].marked) + // for (size_t i = 0; i < nel; i++) + for (auto ei : ngcore::T_Range(nel)) + if (mtets[ei].marked) { - MarkedTet oldtet = mtets[i]; + MarkedTet oldtet = mtets[ei]; SortedPointIndices<2> edge(oldtet.pnums[oldtet.tetedge1], oldtet.pnums[oldtet.tetedge2]); @@ -3358,9 +3373,9 @@ namespace netgen MarkedTet newtet1, newtet2; BTBisectTet (oldtet, newp, newtet1, newtet2); - mtets[i] = newtet1; + mtets[ei] = newtet1; mtets.Append (newtet2); - mesh.mlparentelement.Append (i+1); + mesh.mlparentelement.Append (ei-IndexBASE()+1); } NgProfiler::StopTimer (timer_bisecttet); (*opt.tracer)("bisecttet", true); @@ -3673,18 +3688,21 @@ namespace netgen v_order = 0; if (mesh.GetDimension() == 3) { - for (int i = 1; i <= mtets.Size(); i++) - if (mtets[i-1].incorder) - mtets[i-1].order++; + // for (int i = 1; i <= mtets.Size(); i++) + for (auto ei : mtets.Range()) + if (mtets[ei].incorder) + mtets[ei].order++; - for (int i = 0; i < mtets.Size(); i++) + // for (int i = 0; i < mtets.Size(); i++) + for (auto ei : mtets.Range()) for (int j = 0; j < 4; j++) - if (int(mtets[i].order) > v_order[mtets[i].pnums[j]]) - v_order[mtets[i].pnums[j]] = mtets[i].order; - for (int i = 0; i < mtets.Size(); i++) + if (int(mtets[ei].order) > v_order[mtets[ei].pnums[j]]) + v_order[mtets[ei].pnums[j]] = mtets[ei].order; + // for (int i = 0; i < mtets.Size(); i++) + for (auto ei : mtets.Range()) for (int j = 0; j < 4; j++) - if (int(mtets[i].order) < v_order[mtets[i].pnums[j]]-1) - mtets[i].order = v_order[mtets[i].pnums[j]]-1; + if (int(mtets[ei].order) < v_order[mtets[ei].pnums[j]]-1) + mtets[ei].order = v_order[mtets[ei].pnums[j]]-1; } else { @@ -3732,19 +3750,19 @@ namespace netgen mesh.AddVolumeElement (el); } */ - ParallelForRange - (opt.task_manager, mtets.Size(), [&] (size_t begin, size_t end) + ngcore::ParallelForRange + (mtets.Range(), [&] (auto myrange) { - for (size_t i = begin; i < end; i++) + for (auto ei : myrange) { Element el(TET); - auto & tet = mtets[i]; + auto & tet = mtets[ei]; el.SetIndex (tet.matindex); el.SetOrder (tet.order); for (int j = 0; j < 4; j++) el[j] = tet.pnums[j]; el.NewestVertex() = tet.newest_vertex; - mesh.SetVolumeElement (ElementIndex(i), el); + mesh.SetVolumeElement (ei, el); } }); diff --git a/libsrc/meshing/boundarylayer.cpp b/libsrc/meshing/boundarylayer.cpp index 645fa672..8f31669c 100644 --- a/libsrc/meshing/boundarylayer.cpp +++ b/libsrc/meshing/boundarylayer.cpp @@ -707,7 +707,7 @@ void BoundaryLayerTool ::InsertNewElements( s[0] = p0; s[1] = p1; s[2] = PointIndex::INVALID; - auto pair = + [[maybe_unused]] auto pair = s[0] < s[1] ? make_pair(s[0], s[1]) : make_pair(s[1], s[0]); if (extra_edge_nr) s.edgenr = ++edge_nr; diff --git a/libsrc/meshing/boundarylayer_interpolate.cpp b/libsrc/meshing/boundarylayer_interpolate.cpp index 00992cd5..ca38885e 100644 --- a/libsrc/meshing/boundarylayer_interpolate.cpp +++ b/libsrc/meshing/boundarylayer_interpolate.cpp @@ -282,7 +282,7 @@ void BoundaryLayerTool ::InterpolateSurfaceGrowthVectors() RegionTimer rtall(tall); static Timer tsmooth("InterpolateSurfaceGrowthVectors-Smoothing"); auto np_old = this->np; - auto np = mesh.GetNP(); + [[maybe_unused]] auto np = mesh.GetNP(); auto hasMoved = [&] (PointIndex pi) { return (pi - IndexBASE() >= np_old) || mapto[pi].Size() > 0 || special_boundary_points.count(pi); @@ -380,8 +380,8 @@ void BoundaryLayerTool ::FixSurfaceElements() { static Timer tall("FixSurfaceElements"); RegionTimer rtall(tall); - auto np_old = this->np; - auto np = mesh.GetNP(); + [[maybe_unused]] auto np_old = this->np; + [[maybe_unused]] auto np = mesh.GetNP(); non_bl_growth_vectors.clear(); diff --git a/libsrc/meshing/boundarylayer_limiter.hpp b/libsrc/meshing/boundarylayer_limiter.hpp index f79ff9ae..5ed04bfc 100644 --- a/libsrc/meshing/boundarylayer_limiter.hpp +++ b/libsrc/meshing/boundarylayer_limiter.hpp @@ -738,7 +738,7 @@ struct GrowthVectorLimiter LimitBoundaryLayer(safety); CheckLimits(__LINE__); - for (auto i : Range(10)) + for ([[maybe_unused]] auto i : Range(10)) EqualizeLimits(smoothing_factors[i_pass]); CheckLimits(__LINE__); diff --git a/libsrc/meshing/curvedelems.cpp b/libsrc/meshing/curvedelems.cpp index a3be91a8..5863cab8 100644 --- a/libsrc/meshing/curvedelems.cpp +++ b/libsrc/meshing/curvedelems.cpp @@ -614,7 +614,7 @@ namespace netgen if (aorder <= 1) { - for (ElementIndex ei = 0; ei < mesh.GetNE(); ei++) + for (ElementIndex ei : mesh.VolumeElements().Range()) if (mesh[ei].GetType() == TET10) ishighorder = 1; return; diff --git a/libsrc/meshing/improve2.cpp b/libsrc/meshing/improve2.cpp index a9d7f258..0d6d9b82 100644 --- a/libsrc/meshing/improve2.cpp +++ b/libsrc/meshing/improve2.cpp @@ -353,7 +353,7 @@ namespace netgen improvement_candidates[cnt++]= std::make_pair(t1,o1); }); - auto elements_with_improvement = improvement_candidates.Range(cnt.load()); + auto elements_with_improvement = improvement_candidates.Range(0, cnt.load()); QuickSort(elements_with_improvement); for (auto [t1,o1] : elements_with_improvement) diff --git a/libsrc/meshing/meshclass.hpp b/libsrc/meshing/meshclass.hpp index 68d25075..0aeb3f87 100644 --- a/libsrc/meshing/meshclass.hpp +++ b/libsrc/meshing/meshclass.hpp @@ -42,7 +42,7 @@ namespace netgen class MarkedTri; class MarkedQuad; - typedef Array T_MTETS; + typedef Array T_MTETS; typedef NgArray T_MPRISMS; typedef NgArray T_MIDS; typedef NgArray T_MTRIS; diff --git a/libsrc/meshing/meshtype.hpp b/libsrc/meshing/meshtype.hpp index 2598d056..7f25962d 100644 --- a/libsrc/meshing/meshtype.hpp +++ b/libsrc/meshing/meshtype.hpp @@ -468,14 +468,11 @@ namespace netgen int i; public: ElementIndex () = default; - // private: - constexpr ElementIndex (int ai) : i(ai) { ; } - public: + constexpr /* explicit */ ElementIndex (int ai) : i(ai) { ; } ElementIndex & operator= (const ElementIndex & ai) { i = ai.i; return *this; } ElementIndex & operator= (int ai) { i = ai; return *this; } - // private: - constexpr operator int () const { return i; } - public: + + constexpr /* explicit */ operator int () const { return i; } ElementIndex operator++ (int) { return ElementIndex(i++); } ElementIndex operator-- (int) { return ElementIndex(i--); } ElementIndex & operator++ () { ++i; return *this; } @@ -495,7 +492,24 @@ namespace netgen return (ost << ei-IndexBASE()); } + inline ElementIndex operator+ (ElementIndex ei, int i) { return ElementIndex { int(ei) + i }; } + inline ElementIndex operator+ (size_t s, ElementIndex ei) { return ElementIndex(int(ei) + s); } + inline ElementIndex operator+ (ElementIndex ei, size_t s) { return ElementIndex(int(ei) + s); } + inline bool operator== (ElementIndex ei1, ElementIndex ei2) { return int(ei1) == int(ei2); }; + inline bool operator!= (ElementIndex ei1, ElementIndex ei2) { return int(ei1) != int(ei2); }; + inline bool operator< (ElementIndex ei1, ElementIndex ei2) { return int(ei1) < int(ei2); }; + inline bool operator> (ElementIndex ei1, ElementIndex ei2) { return int(ei1) > int(ei2); }; + inline bool operator>= (ElementIndex ei1, ElementIndex ei2) { return int(ei1) >= int(ei2); }; + inline bool operator<= (ElementIndex ei1, ElementIndex ei2) { return int(ei1) <= int(ei2); }; + // these should not be needed: + inline bool operator== (ElementIndex ei1, int ei2) { return int(ei1) == int(ei2); }; + inline bool operator< (size_t s, ElementIndex ei2) { return int(s) < int(ei2); }; + inline bool operator< (ElementIndex ei1, size_t s) { return int(ei1) < int(s); }; // should not need + inline bool operator< (ElementIndex ei1, int s) { return int(ei1) < int(s); }; // should not need + inline bool operator>= (size_t s, ElementIndex ei2) { return int(s) >= int(ei2); }; + + class SurfaceElementIndex { int i; diff --git a/libsrc/visualization/vssolution.cpp b/libsrc/visualization/vssolution.cpp index 446488df..529f516d 100644 --- a/libsrc/visualization/vssolution.cpp +++ b/libsrc/visualization/vssolution.cpp @@ -4442,7 +4442,7 @@ namespace netgen for (int i = 0; i < trigs.Size(); i++) { const ClipPlaneTrig & trig = trigs[i]; - if (trig.elnr != lastelnr) + if (trig.elnr != ElementIndex(lastelnr)) { lastelnr = trig.elnr; nlp = -1;