Commit c5be02f7 authored by Sylvain Thery's avatar Sylvain Thery

Optimize & simplify parallel foreach

- use automatically orbit or cell & quicktraversal
- DartMarker & CellMarker now thread safe
parent 0c4ea341
This diff is collapsed.
...@@ -27,8 +27,20 @@ ...@@ -27,8 +27,20 @@
#include <iostream> #include <iostream>
#include "Utils/Qt/qtSimple.h"
#include "Topology/generic/parameters.h"
#include "Topology/map/embeddedMap3.h"
#include "Geometry/vector_gen.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/GL2/mapRender.h"
#include "Utils/Shaders/shaderSimpleColor.h"
#include "Utils/Shaders/shaderVectorPerVertex.h"
#include "Utils/cgognStream.h" #include "Utils/cgognStream.h"
#include "Utils/Qt/qtSimple.h"
// forward definitions (minimize includes) // forward definitions (minimize includes)
namespace CGoGN { namespace Algo { namespace Render { namespace GL1 { class MapRender; } } } } namespace CGoGN { namespace Algo { namespace Render { namespace GL1 { class MapRender; } } } }
...@@ -45,21 +57,67 @@ class MyQT: public Utils::QT::SimpleQT ...@@ -45,21 +57,67 @@ class MyQT: public Utils::QT::SimpleQT
Q_OBJECT Q_OBJECT
public: public:
MyQT() Algo::Render::GL2::MapRender* m_render;
Utils::VBO* m_positionVBO;
Utils::VBO* m_normalVBO;
Utils::ShaderSimpleColor* m_shader;
Utils::ShaderVectorPerVertex* m_lines;
MyQT():
m_render(NULL),
m_positionVBO(NULL),
m_normalVBO(NULL),
m_shader(NULL),
m_lines(NULL)
{} {}
// callbacks of simpleQT to overdefine: protected:
void cb_redraw(); void cb_redraw();
void cb_initGL(); void cb_initGL();
void cb_Open(); void threadSimple();
void threadAttrib();
void threadStorage();
void cb_New(); void cb_keyPress(int code);
// callbacks (slots) locally defined
public slots:
void menu_slot1();
}; };
//class MyQT: public Utils::QT::SimpleQT
//{
// Q_OBJECT
//
//public:
// MyQT()
// {}
//
// // callbacks of simpleQT to overdefine:
// void cb_redraw();
//
// void cb_initGL();
//
// void cb_Open();
//
// void cb_New();
//
//// callbacks (slots) locally defined
//public slots:
// void menu_slot1();
//};
#endif #endif
This diff is collapsed.
This diff is collapsed.
...@@ -95,7 +95,7 @@ public: ...@@ -95,7 +95,7 @@ public:
map.addEmbedding<CELL>() ; map.addEmbedding<CELL>() ;
m_mark = m_map.getMarkerSet<CELL>(m_thread).getNewMark() ; m_mark = m_map.getMarkerSet<CELL>(m_thread).getNewMark() ;
m_markVector = m_map.getMarkVector<CELL>(m_thread) ; m_markVector = m_map.getMarkVector<CELL>(m_thread) ;
m_map.cellMarkers.push_back(this) ; m_map.cellMarkers[m_thread].push_back(this) ;
} }
virtual ~CellMarkerBase() virtual ~CellMarkerBase()
...@@ -103,12 +103,14 @@ public: ...@@ -103,12 +103,14 @@ public:
if(releaseOnDestruct) if(releaseOnDestruct)
{ {
m_map.getMarkerSet<CELL>(m_thread).releaseMark(m_mark) ; m_map.getMarkerSet<CELL>(m_thread).releaseMark(m_mark) ;
for(std::vector<CellMarkerGen*>::iterator it = m_map.cellMarkers.begin(); it != m_map.cellMarkers.end(); ++it)
std::vector<CellMarkerGen*>& cmg = m_map.cellMarkers[m_thread];
for(std::vector<CellMarkerGen*>::iterator it = cmg.begin(); it != cmg.end(); ++it)
{ {
if(*it == this) if(*it == this)
{ {
*it = m_map.cellMarkers.back(); *it = cmg.back();
m_map.cellMarkers.pop_back(); cmg.pop_back();
return; return;
} }
} }
......
...@@ -46,6 +46,7 @@ protected: ...@@ -46,6 +46,7 @@ protected:
Mark m_mark ; Mark m_mark ;
AttributeMultiVector<Mark>* m_markVector ; AttributeMultiVector<Mark>* m_markVector ;
unsigned int m_thread ; unsigned int m_thread ;
unsigned int m_idReg ;
bool releaseOnDestruct ; bool releaseOnDestruct ;
public: public:
...@@ -57,7 +58,7 @@ public: ...@@ -57,7 +58,7 @@ public:
{ {
m_mark = m_map.getMarkerSet<DART>(m_thread).getNewMark() ; m_mark = m_map.getMarkerSet<DART>(m_thread).getNewMark() ;
m_markVector = m_map.getMarkVector<DART>(m_thread) ; m_markVector = m_map.getMarkVector<DART>(m_thread) ;
m_map.dartMarkers.push_back(this) ; m_map.dartMarkers[m_thread].push_back(this) ;
} }
virtual ~DartMarkerGen() virtual ~DartMarkerGen()
...@@ -65,12 +66,13 @@ public: ...@@ -65,12 +66,13 @@ public:
if(releaseOnDestruct) if(releaseOnDestruct)
{ {
m_map.getMarkerSet<DART>(m_thread).releaseMark(m_mark) ; m_map.getMarkerSet<DART>(m_thread).releaseMark(m_mark) ;
for(std::vector<DartMarkerGen*>::iterator it = m_map.dartMarkers.begin(); it != m_map.dartMarkers.end(); ++it) std::vector<DartMarkerGen*>& dmg = m_map.dartMarkers[m_thread];
for(std::vector<DartMarkerGen*>::iterator it = dmg.begin(); it != dmg.end(); ++it)
{ {
if(*it == this) if(*it == this)
{ {
*it = m_map.dartMarkers.back(); *it = dmg.back();
m_map.dartMarkers.pop_back(); dmg.pop_back();
return; return;
} }
} }
......
...@@ -432,6 +432,65 @@ public: ...@@ -432,6 +432,65 @@ public:
} ; } ;
//
// FOR PARALLEL TRAVERSALS
//
/**
* Functor class for parallel::foreach_orbit/cell/dart
* Overload bool parallelDo
* Overload duplicate if necessary (no sharing of functors)
*/
template<typename MAP>
class FunctorMapThreaded
{
protected:
MAP& m_map ;
public:
FunctorMapThreaded(MAP& m): m_map(m) {}
virtual ~FunctorMapThreaded() {}
/**
* @return a pointer on a copy of the object.
*/
virtual FunctorMapThreaded<MAP>* duplicate() const { return NULL;}
/**
* insert your code here:
* @param d the dart on which apply functor
* @param threadID the id of thread currently running your code
*/
virtual void parallelDo(Dart d, unsigned int threadID) = 0;
};
/**
* Functor class for parallel::foreach_attrib
* Overload parallelDo
* Overload duplicate if necessary (no sharing of functors)
*/
class FunctorAttribThreaded
{
public:
virtual ~FunctorAttribThreaded() {}
/**
* @return a pointer on a copy of the object.
*/
virtual FunctorAttribThreaded* duplicate() const { return NULL;}
/**
* insert your code here:
* @param d the dart on which apply functor
* @param threadID the id of thread currently running your code
*/
virtual void parallelDo(unsigned int i, unsigned int threadID) = 0;
};
} //namespace CGoGN } //namespace CGoGN
#endif #endif
...@@ -112,9 +112,11 @@ protected: ...@@ -112,9 +112,11 @@ protected:
/** /**
* Store links to created AttributeHandlers, DartMarkers and CellMarkers * Store links to created AttributeHandlers, DartMarkers and CellMarkers
*/ */
std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*> attributeHandlers ; std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*> attributeHandlers ; // TODO think of MT (AttributeHandler creation & release are not thread safe!
std::vector<DartMarkerGen*> dartMarkers ; std::vector<DartMarkerGen*> dartMarkers[NB_THREAD] ;
std::vector<CellMarkerGen*> cellMarkers ; std::vector<CellMarkerGen*> cellMarkers[NB_THREAD] ;
/** /**
* is map a multiresolution map * is map a multiresolution map
......
...@@ -43,16 +43,22 @@ void AttribMap::init() ...@@ -43,16 +43,22 @@ void AttribMap::init()
} }
} }
for(unsigned int i = 0; i < cellMarkers.size(); ++i) for (unsigned int j=0; j<NB_THREAD; ++j)
{ {
CellMarkerGen* cm = cellMarkers[i] ; std::vector<CellMarkerGen*>& cmg = cellMarkers[j];
cm->updateMarkVector(m_markTables[cm->getCell()][cm->getThread()]) ;
}
for(unsigned int i = 0; i < dartMarkers.size(); ++i) for(unsigned int i = 0; i < cmg.size(); ++i)
{ {
DartMarkerGen* cm = dartMarkers[i] ; CellMarkerGen* cm = cmg[i] ;
cm->updateMarkVector(m_markTables[DART][cm->getThread()]) ; cm->updateMarkVector(m_markTables[cm->getCell()][cm->getThread()]) ;
}
std::vector<DartMarkerGen*>& dmg = dartMarkers[j];
for(unsigned int i = 0; i < dmg.size(); ++i)
{
DartMarkerGen* cm = dmg[i] ;
cm->updateMarkVector(m_markTables[DART][cm->getThread()]) ;
}
} }
} }
......
...@@ -84,8 +84,11 @@ GenericMap::GenericMap() : m_nbThreads(1) ...@@ -84,8 +84,11 @@ GenericMap::GenericMap() : m_nbThreads(1)
} }
} }
dartMarkers.reserve(16) ; for (unsigned int i=0; i<NB_THREAD; ++i)
cellMarkers.reserve(16) ; {
dartMarkers[i].reserve(16) ;
cellMarkers[i].reserve(16) ;
}
// get & lock marker for boundary // get & lock marker for boundary
m_boundaryMarker = m_marksets[DART][0].getNewMark(); m_boundaryMarker = m_marksets[DART][0].getNewMark();
...@@ -106,23 +109,21 @@ GenericMap::~GenericMap() ...@@ -106,23 +109,21 @@ GenericMap::~GenericMap()
m_attribs[i].clear(true) ; m_attribs[i].clear(true) ;
} }
for(std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*>::iterator it = attributeHandlers.begin(); it != attributeHandlers.end(); ++it) for(std::multimap<AttributeMultiVectorGen*, AttributeHandlerGen*>::iterator it = attributeHandlers.begin(); it != attributeHandlers.end(); ++it)
(*it).second->setInvalid() ; (*it).second->setInvalid() ;
attributeHandlers.clear() ; attributeHandlers.clear() ;
for(std::vector<DartMarkerGen*>::iterator it = dartMarkers.begin(); it != dartMarkers.end(); ++it) for (unsigned int i=0; i<NB_THREAD; ++i)
(*it)->setReleaseOnDestruct(false) ; {
dartMarkers.clear() ; for(std::vector<DartMarkerGen*>::iterator it = dartMarkers[i].begin(); it != dartMarkers[i].end(); ++it)
(*it)->setReleaseOnDestruct(false) ;
for(std::vector<CellMarkerGen*>::iterator it = cellMarkers.begin(); it != cellMarkers.end(); ++it) dartMarkers[i].clear() ;
(*it)->setReleaseOnDestruct(false) ;
cellMarkers.clear() ;
// if(m_attributes_registry_map) for(std::vector<CellMarkerGen*>::iterator it = cellMarkers[i].begin(); it != cellMarkers[i].end(); ++it)
// { (*it)->setReleaseOnDestruct(false) ;
// delete m_attributes_registry_map; cellMarkers[i].clear() ;
// m_attributes_registry_map = NULL; }
// }
// clean type registry if necessary // clean type registry if necessary
m_nbInstances--; m_nbInstances--;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment