From 0c249b1a7bd4b8076301d8fa3ed20f2898e3e919 Mon Sep 17 00:00:00 2001 From: Sylvain Thery Date: Mon, 12 May 2014 15:16:38 +0200 Subject: [PATCH] automatic using of // versions --- Apps/Examples/viewer.cpp | 41 ++++- Apps/Examples/volumeExplorer.cpp | 3 - Apps/Tuto/Traversals/traverse_cells.cpp | 17 +- include/Algo/Geometry/area.h | 15 ++ include/Algo/Geometry/area.hpp | 58 ++++-- include/Algo/Geometry/centroid.h | 21 +-- include/Algo/Geometry/centroid.hpp | 74 +++++--- include/Algo/Geometry/curvature.h | 19 +- include/Algo/Geometry/curvature.hpp | 99 ++++++++-- include/Algo/Geometry/normal.h | 20 ++- include/Algo/Geometry/normal.hpp | 34 +++- include/Algo/Geometry/volume.h | 2 +- include/Algo/Geometry/volume.hpp | 18 +- include/Algo/Render/GL2/topo3Render.hpp | 2 +- include/Topology/generic/attributeHandler.h | 2 +- include/Topology/generic/attributeHandler.hpp | 7 +- include/Topology/generic/genericmap.h | 26 +++ .../generic/traversor/traversorCell.h | 21 +-- .../generic/traversor/traversorCell.hpp | 169 ++---------------- src/Topology/generic/genericmap.cpp | 6 + 20 files changed, 367 insertions(+), 287 deletions(-) diff --git a/Apps/Examples/viewer.cpp b/Apps/Examples/viewer.cpp index 4616bd3d..b2494e19 100644 --- a/Apps/Examples/viewer.cpp +++ b/Apps/Examples/viewer.cpp @@ -457,19 +457,56 @@ void Viewer::cb_keyPress(int keycode) case'Z': { + Utils::Chrono ch; ch.start(); for (unsigned int i=0; i<4; ++i) - Algo::Surface::Geometry::Parallel::computeNormalVertices(myMap, position, normal) ; + Algo::Surface::Geometry::computeNormalVertices(myMap, position, normal) ; std::cout << "Algo::Surface::Geometry::Parallel::computeNormalVertices "<< ch.elapsed()<< " ms "<< std::endl; + ch.start(); + + CGoGN::Parallel::NumberOfThreads = 1; + for (unsigned int i=0; i<4; ++i) + Algo::Surface::Geometry::Parallel::computeNormalVertices(myMap, position, normal) ; + std::cout << "Algo::Surface::Geometry::Parallel::computeNormalVertices1 "<< ch.elapsed()<< " ms "<< std::endl; + + ch.start(); + + CGoGN::Parallel::NumberOfThreads = 2; + for (unsigned int i=0; i<4; ++i) + Algo::Surface::Geometry::Parallel::computeNormalVertices(myMap, position, normal) ; + std::cout << "Algo::Surface::Geometry::Parallel::computeNormalVertices2 "<< ch.elapsed()<< " ms "<< std::endl; + + ch.start(); + + CGoGN::Parallel::NumberOfThreads = 3; + for (unsigned int i=0; i<4; ++i) + Algo::Surface::Geometry::Parallel::computeNormalVertices(myMap, position, normal) ; + std::cout << "Algo::Surface::Geometry::Parallel::computeNormalVertices3 "<< ch.elapsed()<< " ms "<< std::endl; + + ch.start(); + + CGoGN::Parallel::NumberOfThreads = 4; + for (unsigned int i=0; i<4; ++i) + Algo::Surface::Geometry::Parallel::computeNormalVertices(myMap, position, normal) ; + std::cout << "Algo::Surface::Geometry::Parallel::computeNormalVertices4 "<< ch.elapsed()<< " ms "<< std::endl; + + ch.start(); + + CGoGN::Parallel::NumberOfThreads = 8; + for (unsigned int i=0; i<4; ++i) + Algo::Surface::Geometry::Parallel::computeNormalVertices(myMap, position, normal) ; + std::cout << "Algo::Surface::Geometry::Parallel::computeNormalVertices8 "<< ch.elapsed()<< " ms "<< std::endl; + + ch.start(); for (unsigned int i=0; i<4; ++i) Parallel::foreach_cell(myMap,[&](Vertex v, unsigned int th) { normal[v] = Algo::Surface::Geometry::vertexNormal(myMap,v,position); - },4,false,FORCE_CELL_MARKING); + },false,FORCE_CELL_MARKING); std::cout << "Parallel::foreach_cell "<< ch.elapsed()<< " ms "<< std::endl; diff --git a/Apps/Examples/volumeExplorer.cpp b/Apps/Examples/volumeExplorer.cpp index a4e67dc6..a05fa47e 100644 --- a/Apps/Examples/volumeExplorer.cpp +++ b/Apps/Examples/volumeExplorer.cpp @@ -498,8 +498,6 @@ int main(int argc, char **argv) Utils::Chrono ch; - - Vertex v(myMap.begin()); VEC3 p = Algo::Volume::Geometry::vertexNeighborhoodCentroid(myMap,v,position); @@ -529,7 +527,6 @@ int main(int argc, char **argv) vol += Algo::Geometry::totalVolume(myMap, position); std::cout << ch.elapsed()<< " ms val="<(myMap,[&](Face f, unsigned int thr) { - // for each face add surface to accumulator (-1 because counter between 1-4 not 0-3) + // for each face add surface to accumulator (-1 because counter between 1-3 not 0-3) surf[thr-1] += Algo::Surface::Geometry::convexFaceArea(myMap,f,position); - },4,false); + },false); - std::cout << surf[0]<< "/"<< surf[1]<< "/"<< surf[2]<< "/"<< surf[3]<< "/"<< std::endl; - std::cout << "Total="<(mapx, positions, centerVolumes,3); + Algo::Volume::Geometry::Parallel::computeCentroidELWVolumes(mapx, positions, centerVolumes); // debut phi1 DartAutoAttribute fv1(mapx); diff --git a/include/Topology/generic/attributeHandler.h b/include/Topology/generic/attributeHandler.h index aa9436f8..226daabd 100644 --- a/include/Topology/generic/attributeHandler.h +++ b/include/Topology/generic/attributeHandler.h @@ -338,7 +338,7 @@ inline void foreach_attribute(ATTR& attr, FUNC func) namespace Parallel { template -void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbth=0); +void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbth=NumberOfThreads); } } // namespace CGoGN diff --git a/include/Topology/generic/attributeHandler.hpp b/include/Topology/generic/attributeHandler.hpp index d773183a..a477ecaa 100644 --- a/include/Topology/generic/attributeHandler.hpp +++ b/include/Topology/generic/attributeHandler.hpp @@ -305,13 +305,12 @@ public: template -void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbth) +void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbthread) { - if (nbth==0) - nbth = boost::thread::hardware_concurrency(); + // thread 0 is for attribute traversal + unsigned int nbth = nbthread -1; std::vector< unsigned int >* vd = new std::vector< unsigned int >[nbth]; - for (unsigned int i = 0; i < nbth; ++i) vd[i].reserve(SIZE_BUFFER_THREAD); diff --git a/include/Topology/generic/genericmap.h b/include/Topology/generic/genericmap.h index 34998680..049424f4 100644 --- a/include/Topology/generic/genericmap.h +++ b/include/Topology/generic/genericmap.h @@ -43,9 +43,33 @@ #include "Topology/generic/marker.h" #include "Topology/generic/functor.h" +#include + namespace CGoGN { +namespace Parallel +{ +/** + * @brief Number of threads used for // traversal foreach + */ +extern int NumberOfThreads; + +/** + * @brief get number of cores of computer (threads, /2 if hyperthreading) + * @param hyperthreading + * @return nb of core + */ +inline int getSystemNumberOfCores(bool hyperthreading=false) +{ + if (hyperthreading) + return boost::thread::hardware_concurrency()/2; + return boost::thread::hardware_concurrency(); +} + +} + + class AttributeHandlerGen ; class DartMarkerGen ; class CellMarkerGen ; @@ -389,6 +413,8 @@ public: void compact() ; } ; + + // //template //bool foreach_dart_of_orbit_in_parent(MAP* ptrMap, unsigned int orbit, Dart d, FunctorType& f, unsigned int thread = 0) diff --git a/include/Topology/generic/traversor/traversorCell.h b/include/Topology/generic/traversor/traversorCell.h index 76ba3cf2..21d19814 100644 --- a/include/Topology/generic/traversor/traversorCell.h +++ b/include/Topology/generic/traversor/traversorCell.h @@ -124,22 +124,19 @@ inline void foreach_cell(const MAP& map, FUNC f, TraversalOptim opt = AUTO, unsi template inline void foreach_cell_until(const MAP& map, FUNC f, TraversalOptim opt = AUTO, unsigned int thread = 0); -/* - * Executes function f on each ORBIT, then - * execute function g on each ORBIT - * Optimization on marking (even pass marking, odd pass unmarking) - */ -//template -//void foreach_cell_EvenOddd(const MAP& map, FUNC f, FUNC2 g, unsigned int nbpasses=1, TraversalOptim opt = AUTO, unsigned int thread = 0); - namespace Parallel { +/** + * @brief foreach_cell + * @param map + * @param func function to apply on cells + * @param needMarkers func need markers ? + * @param opt optimization param of traversal + * @param nbth number of used thread (0:for traversal, [1,nbth-1] for func computing +*/ template -void foreach_cell(MAP& map, FUNC func, unsigned int nbth=0, bool needMarkers=true, TraversalOptim opt=AUTO); - -//template -//void foreach_cell_EO(MAP& map, FUNC_E funcEven, FUNC_O funcOdd, unsigned int nbPasses=1, unsigned int nbth=0, bool needMarkers=true, TraversalOptim opt = AUTO); +void foreach_cell(MAP& map, FUNC func, bool needMarkers=true, TraversalOptim opt=AUTO, unsigned int nbth=NumberOfThreads); } diff --git a/include/Topology/generic/traversor/traversorCell.hpp b/include/Topology/generic/traversor/traversorCell.hpp index 546b55a4..843fbc21 100644 --- a/include/Topology/generic/traversor/traversorCell.hpp +++ b/include/Topology/generic/traversor/traversorCell.hpp @@ -655,17 +655,13 @@ public: template -void foreach_cell_tmpl(MAP& map, FUNC func, unsigned int nbth, bool needMarkers) +void foreach_cell_tmpl(MAP& map, FUNC func, bool needMarkers, unsigned int nbth) { - if (nbth==0) - nbth = boost::thread::hardware_concurrency(); - + // buffer for cell traversing std::vector< Cell >* vd = new std::vector< Cell >[nbth]; - for (unsigned int i = 0; i < nbth; ++i) vd[i].reserve(SIZE_BUFFER_THREAD); - // TraversorCell trav(map,false,th_orig); unsigned int nb = 0; TraversorCell trav(map); Cell cell = trav.begin(); @@ -738,173 +734,32 @@ void foreach_cell_tmpl(MAP& map, FUNC func, unsigned int nbth, bool needMarkers) } -//template -//void foreach_cell_EO_tmpl(MAP& map, FUNC_E funcEven, FUNC_O funcOdd, unsigned int nbPasses, unsigned int nbth, bool needMarkers=true) -//{ -// if (nbth==0) -// nbth = boost::thread::hardware_concurrency(); - -// std::vector< Cell >* vd = new std::vector< Cell >[nbth]; - -// for (unsigned int i = 0; i < nbth; ++i) -// vd[i].reserve(SIZE_BUFFER_THREAD); - -// bool finished_even=false; -// bool finished_odd=false; -// // lauch threads -// if (needMarkers) -// { -// unsigned int nbth_prec = map.getNbThreadMarkers(); -// if (nbth_prec < nbth+1) -// map.addThreadMarker(nbth+1-nbth_prec); -// } - -// boost::thread** threads = new boost::thread*[2*nbth]; -// ThreadFunction** tfse = new ThreadFunction*[nbth]; -// ThreadFunction** tfso = new ThreadFunction*[nbth]; - -// boost::barrier sync1(nbth+1); -// boost::barrier sync2(nbth+1); -// boost::barrier sync3(nbth+1); -// boost::barrier sync4(nbth+1); - -// for (unsigned int i = 0; i < nbth; ++i) -// { -// tfse[i] = new ThreadFunction(funcEven, vd[i],sync1,sync2, finished_even,1+i); -// threads[i] = new boost::thread( boost::ref( *(tfse[i]) ) ); - -// tfso[i] = new ThreadFunction(funcOdd, vd[i],sync3,sync4, finished_odd,1+i); -// threads[nbth+i] = new boost::thread( boost::ref( *(tfso[i]) ) ); -// } - -// // and continue to traverse the map -// std::vector< Cell >* tempo = new std::vector< Cell >[nbth]; -// for (unsigned int i = 0; i < nbth; ++i) -// tempo[i].reserve(SIZE_BUFFER_THREAD); - -// TraversorCell trav(map); - -// for (unsigned i=0; i cell = trav.begin(); -// Cell c_end = trav.end(); -// while (cell.dart != c_end.dart) -// { -// for (unsigned int i = 0; i < nbth; ++i) -// tempo[i].clear(); - -// unsigned int nb = 0; -// while ((cell.dart != c_end.dart) && (nb < nbth*SIZE_BUFFER_THREAD) ) -// { -// tempo[nb%nbth].push_back(cell); -// nb++; -// cell = trav.next(); -// } -// sync1.wait();// wait for all thread to finish its vector -// for (unsigned int i = 0; i < nbth; ++i) -// vd[i].swap(tempo[i]); -// sync2.wait();// everybody refilled then go -// } -// sync1.wait();// wait for all thread to finish its vector -// sync2.wait(); // just wait for last barrier wait ! - -// cell = trav.begin(); -// c_end = trav.end(); -// while (cell.dart != c_end.dart) -// { -// for (unsigned int i = 0; i < nbth; ++i) -// tempo[i].clear(); -// unsigned int nb = 0; - -// while ((cell.dart != c_end.dart) && (nb < nbth*SIZE_BUFFER_THREAD) ) -// { -// tempo[nb%nbth].push_back(cell); -// nb++; -// cell = trav.next(); -// } -// sync3.wait();// wait for all thread to finish its vector -// for (unsigned int i = 0; i < nbth; ++i) -// vd[i].swap(tempo[i]); -// sync4.wait();// everybody refilled then go -// } -// sync3.wait();// wait for all thread to finish its vector -// sync4.wait(); // just wait for last barrier wait ! -// } - -// sync1.wait();// wait for all thread to finish its vector -// finished_even = true;// say finsih to everyone -// sync2.wait(); // just wait for last barrier wait ! -// sync3.wait();// wait for all thread to finish its vector -// finished_odd = true;// say finsih to everyone -// sync4.wait(); // just wait for last barrier wait ! - -// //wait for all theads to be finished -// for (unsigned int i = 0; i < nbth; ++i) -// { -// threads[i]->join(); -// delete threads[i]; -// delete tfse[i]; -// threads[i+nbth]->join(); -// delete threads[i+nbth]; -// delete tfso[i]; -// } - -// delete[] tfse; -// delete[] tfso; -// delete[] threads; -// delete[] vd; -// delete[] tempo; -//} - - - - - template -void foreach_cell(MAP& map, FUNC func, unsigned int nbth, bool needMarkers, TraversalOptim opt) +void foreach_cell(MAP& map, FUNC func, bool needMarkers, TraversalOptim opt, unsigned int nbth) { + if (nbth < 2) + { + CGoGNerr << "Warning number of threads must be > 1 for //" << CGoGNendl; + nbth =2; + } switch(opt) { case FORCE_DART_MARKING: - foreach_cell_tmpl(map,func,nbth,needMarkers); + foreach_cell_tmpl(map,func,needMarkers,nbth-1); break; case FORCE_CELL_MARKING: - foreach_cell_tmpl(map,func,nbth,needMarkers); + foreach_cell_tmpl(map,func,needMarkers,nbth-1); break; case FORCE_QUICK_TRAVERSAL: - foreach_cell_tmpl(map,func,nbth,needMarkers); + foreach_cell_tmpl(map,func,needMarkers,nbth-1); break; case AUTO: default: - foreach_cell_tmpl(map,func,nbth,needMarkers); + foreach_cell_tmpl(map,func,needMarkers,nbth-1); break; } } - -//template -//void foreach_cell_EO(MAP& map, FUNC_E funcEven, FUNC_O funcOdd, unsigned int nbPasses, unsigned int nbth, bool needMarkers, TraversalOptim opt) -//{ -// switch(opt) -// { -// case FORCE_DART_MARKING: -// foreach_cell_EO_tmpl(map,funcEven,funcOdd,nbPasses,nbth,needMarkers); -// break; -// case FORCE_CELL_MARKING: -// foreach_cell_EO_tmpl(map,funcEven,funcOdd,nbPasses,nbth,needMarkers); -// break; -// case FORCE_QUICK_TRAVERSAL: -// foreach_cell_EO_tmpl(map,funcEven,funcOdd,nbPasses,nbth,needMarkers); -// break; -// case AUTO: -// default: -// foreach_cell_EO_tmpl(map,funcEven,funcOdd,nbPasses,nbth,needMarkers); -// break; -// } -//} - - - }// namespace Parallel diff --git a/src/Topology/generic/genericmap.cpp b/src/Topology/generic/genericmap.cpp index fa3d4ba5..a4a777dd 100644 --- a/src/Topology/generic/genericmap.cpp +++ b/src/Topology/generic/genericmap.cpp @@ -33,6 +33,12 @@ namespace CGoGN { +namespace Parallel +{ +//int NumberOfThreads=1; +int NumberOfThreads = getSystemNumberOfCores(); +} + std::map* GenericMap::m_attributes_registry_map = NULL; int GenericMap::m_nbInstances = 0; -- GitLab