Commit 28febb3c authored by Sylvain Thery's avatar Sylvain Thery
Browse files

Parallelisation d'algorithme et foreach

ajout de GLM pour GL3 (bientot !)
parent 1b8ad6f6
......@@ -5,8 +5,8 @@ project(Tutos)
#SET (COMMON_LIBS ${GLUT_LIBRARY} ${OPENGL_LIBRARY} ${GLEW_LIBRARY} ${DEVIL_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBXML2_LIBRARIES} gzstream AntTweakBar openctm)
#SET (CMAKE_BUILD_TYPE Debug)
SET(CMAKE_BUILD_TYPE Release)
SET (CMAKE_BUILD_TYPE Debug)
#SET(CMAKE_BUILD_TYPE Release)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOTOPOWARNING")
......@@ -54,5 +54,5 @@ target_link_libraries( tp_master
add_executable( tuto_mt tuto_mt.cpp)
target_link_libraries( tuto_mt
# containerD topologyD utilsD algoD ${COMMON_LIBS} boost_thread)
container topology utils algo ${COMMON_LIBS} boost_thread)
containerD topologyD utilsD algoD ${COMMON_LIBS} boost_thread)
# container topology utils algo ${COMMON_LIBS} boost_thread)
......@@ -635,7 +635,6 @@ void myGlutWin::myKeyboard(unsigned char keycode, int x, int y)
/// calcul du rayon
getOrthoScreenRay(x,y,rayA,rayB);
PFP::VEC3 AB = rayB-rayA;
float AB2 = AB.norm2();
d_faces.clear();
d_edges.clear();
......@@ -679,7 +678,6 @@ void myGlutWin::myKeyboard(unsigned char keycode, int x, int y)
/// Rayon
getOrthoScreenRay(x,y,rayA,rayB);
PFP::VEC3 AB = rayB-rayA;
float AB2 = AB.norm2();
d_faces.clear();
d_edges.clear();
......
......@@ -41,6 +41,7 @@
#include "Algo/Parallel/parallel_foreach.h"
#include "averaging.h"
using namespace CGoGN ;
......@@ -68,9 +69,10 @@ public:
Algo::Render::VBO::MapRender_VBO* m_render;
MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWin(argc, argv, winX, winY) {}
MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWin(argc, argv, winX, winY), m_render(NULL) {}
~MyGlutWin()
{
if (m_render !=NULL)
delete m_render ;
}
};
......@@ -103,585 +105,167 @@ void MyGlutWin::myRedraw()
glPopMatrix();
}
AttributeHandler<PFP::VEC3> position;
AttributeHandler<PFP::VEC3> facesnormals;
//
//template<typename PFP>
//void fonction_exemple(typename PFP::MAP& map, const AttributeHandler<typename PFP::VEC3>& position, Dart d)
//{
// std::cout << "Position brin"<<d.index<< " = "<< position[d]<< std::endl;
//}
// NEED VERTEX_THREAD
template<typename XXX>
class Thread1
template <typename XXX>
class ThreadArea: public Algo::Parallel::CGoGNThread<typename XXX::MAP>
{
protected:
typename XXX::MAP& m_map;
unsigned int m_th;
public:
Thread1(typename XXX::MAP& map, unsigned int th):
m_map(map), m_th(th) {}
void operator()()
{
typename XXX::VEC3 total(0.);
unsigned int nb=0;
{
CellMarker mv(m_map,VERTEX_CELL, m_th);
for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
{
if (! mv.isMarked(d)) // si d non marque:
{
typename XXX::VEC3 p = position[d];
total+=p;
nb++;
mv.mark(d);
}
}
}
{
CellMarker mv(m_map,VERTEX_CELL, m_th);
for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
{
if (! mv.isMarked(d)) // si d non marque:
{
typename XXX::VEC3 p = position[d];
total+=p;
nb++;
mv.mark(d);
}
}
}
{
CellMarker mv(m_map,VERTEX_CELL, m_th);
for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
{
if (! mv.isMarked(d)) // si d non marque:
{
typename XXX::VEC3 p = position[d];
total+=p;
nb++;
mv.mark(d);
}
}
}
{
CellMarker mv(m_map,VERTEX_CELL, m_th);
for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
{
if (! mv.isMarked(d)) // si d non marque:
{
typename XXX::VEC3 p = position[d];
total+=p;
nb++;
mv.mark(d);
}
}
}
total /= typename XXX::REAL(nb);
std::cout << "Thread "<< m_th << "-> median pos: "<<total<< std::endl;
}
};
// NEED DART_THREAD
template<typename XXX>
class Thread2
{
protected:
typename XXX::MAP& m_map;
unsigned int m_th;
SelectorTrue m_selt;
const typename XXX::TVEC3& m_positions;
float area;
public:
Thread2(typename XXX::MAP& map, unsigned int th):
m_map(map), m_th(th) {}
ThreadArea(typename XXX::MAP& map, const typename XXX::TVEC3& pos, unsigned int th):
Algo::Parallel::CGoGNThread<typename XXX::MAP>(map,th),
m_positions(pos),
area(0.0f) {}
void operator()()
{
area = Algo::Geometry::totalArea<XXX>(m_map,position,m_selt,m_th);
area = Algo::Geometry::totalArea<XXX>(m_map,position,m_selt,m_th);
area = Algo::Geometry::totalArea<XXX>(m_map,position,m_selt,m_th);
area = Algo::Geometry::totalArea<XXX>(m_map,position,m_selt,m_th);
std::cout << "Thread "<< m_th<<" Total Area= "<< area<< std::endl;
// 3 times just for fun !!!
area += Algo::Geometry::totalArea<XXX>(this->m_map, m_positions, SelectorTrue(), this->m_threadId);
area += Algo::Geometry::totalArea<XXX>(this->m_map, m_positions, SelectorTrue(), this->m_threadId);
area += Algo::Geometry::totalArea<XXX>(this->m_map, m_positions, SelectorTrue(), this->m_threadId);
}
};
// NEED FACE_THREAD
template<typename XXX>
class Thread3
{
protected:
typename XXX::MAP& m_map;
unsigned int m_th;
SelectorTrue m_selt;
public:
Thread3(typename XXX::MAP& map, unsigned int th):
m_map(map), m_th(th) {}
void operator()()
{
Algo::Geometry::computeNormalFaces<XXX>(m_map,position,facesnormals,m_selt,m_th);
typename XXX::VEC3 total(0.);
unsigned int nb=0;
for (unsigned int i=facesnormals.begin(); i != facesnormals.end(); facesnormals.next(i))
{
typename XXX::VEC3 f = facesnormals[i]/10000.0f;
total+=f;
nb++;
}
for (unsigned int i=facesnormals.begin(); i != facesnormals.end(); facesnormals.next(i))
{
typename XXX::VEC3 f = facesnormals[i]/10000.0f;
total+=f;
nb++;
}
for (unsigned int i=facesnormals.begin(); i != facesnormals.end(); facesnormals.next(i))
{
typename XXX::VEC3 f = facesnormals[i]/10000.0f;
total+=f;
nb++;
}
for (unsigned int i=facesnormals.begin(); i != facesnormals.end(); facesnormals.next(i))
{
typename XXX::VEC3 f = facesnormals[i]/10000.0f;
total+=f;
nb++;
}
total /= typename XXX::REAL(nb);
std::cout << "Thread "<< m_th<<" Face normal :"<<total << std::endl;
}
float getTripleValue() { return area;}
};
// NEED DART_THREAD
template<typename XXX>
class Thread0
template <typename XXX>
class ThreadNormals: public Algo::Parallel::CGoGNThread<typename XXX::MAP>
{
protected:
typename XXX::MAP& m_map;
MyGlutWin& m_mgw;
unsigned int m_th;
SelectorTrue m_selt;
const typename XXX::TVEC3& m_positions;
typename XXX::TVEC3& m_normals;
public:
Thread0(typename XXX::MAP& map,MyGlutWin& mgw, unsigned int th):
m_map(map), m_mgw(mgw), m_th(th) {}
ThreadNormals(typename XXX::MAP& map, const typename XXX::TVEC3& pos, typename XXX::TVEC3& norm, unsigned int th):
Algo::Parallel::CGoGNThread<typename XXX::MAP>(map,th),
m_positions(pos),
m_normals(norm) {}
void operator()()
{
std::cout << "Begin render init"<<std::endl;
m_mgw.useContext();
// instanciation of the renderer (here using VBOs)
m_mgw.m_render = new Algo::Render::VBO::MapRender_VBO();
// update the renderer (geometry and primitives)
m_mgw.m_render->updateData(Algo::Render::VBO::POSITIONS, position);
m_mgw.m_render->initPrimitives<PFP>(m_map, m_selt, Algo::Render::VBO::TRIANGLES,m_th);
m_mgw.m_render->initPrimitives<PFP>(m_map, m_selt, Algo::Render::VBO::LINES,m_th);
m_mgw.releaseContext();
std::cout<< "Render OK "<< std::endl;
Algo::Geometry::computeNormalVertices<XXX>(this->m_map, m_positions, m_normals, SelectorTrue(), this->m_threadId);
}
};
boost::mutex mutexio;
unsigned int cpt1=0;
unsigned int cpt2=0;
};
//class ThreadFunc
//{
//protected:
// FunctorType& m_functor;
// std::vector<Dart>& m_darts;
// boost::mutex& m_exclu;
//
//public:
// ThreadFunc(FunctorType& func, std::vector<Dart>& vd, boost::mutex& mut ):
// m_functor(func), m_darts(vd), m_exclu(mut) {}
//
// void operator()()
// {
// bool m_finished=false;
//
// while(!m_finished)
// {
// Dart d;
// bool ok=false;
// do
// {
// m_exclu.lock();
// if (!m_darts.empty())
// {
// d = m_darts.back();
// m_darts.pop_back();
// std::cout << "Lecture "<< " = "<< m_darts.size()<< std::endl;
// ok=true;
// }
// else std::cout << "PB vecteur vide"<< std::endl;
// m_exclu.unlock();
// } while (!ok);
//
// if (!d.isNil())
// m_functor(d);
// else
// m_finished=true;
// }
//// std::cout << "Finished "<< std::endl;
// }
//};
//#define NBTH 7
//template <typename PFP>
//void parallel_foreach_orbit(typename PFP::MAP& map, unsigned int orbit, FunctorType& func)
//{
// std::vector<Dart> vd[NBTH];
//
// boost::thread* threads[NBTH];
// boost::mutex muts[NBTH];
//
//
// DartMarker dm(map);
//
// Dart d=map.begin();
//
// // NBTH new functions, new thread (with good darts !)
// for (unsigned int i=0; i<NBTH; ++i)
// {
// while (dm.isMarked(d))
// map.next(d);
// dm.markOrbit(orbit,d);
// vd[i].reserve(16);
// vd[i].push_back(d);
//// threads[i] = new boost::thread(ThreadFunc(func, vd[i], muts[i]));
// map.next(d);
// }
//// std::cout << "OK1" << std::endl;
//
// while ( d != map.end())
// {
// // store thre NBTH next darts in tempo v2d
// for (unsigned int i=0; i<NBTH; ++i)
// {
// while ((d !=map.end()) && (dm.isMarked(d)))
// map.next(d);
//
// if (d !=map.end())
// {
// dm.markOrbit(orbit,d);
//// muts[i].lock();
// vd[i].push_back(d);
//// std::cout << "Size of"<< i << " = "<< vd[i].size()<< std::endl;
//// muts[i].unlock();
// map.next(d);
// }
// else
// {
//// muts[i].lock();
// vd[i].push_back(Dart::nil());
//// muts[i].unlock();
// }
// }
// }
//
//
// //wait for all theads to be finished
// for (unsigned int i=0; i< NBTH; ++i)
// {
//// threads[i]->join();
//// delete threads[i];
// }
// std::cout << "------------------------"<<std::endl;
//}
//
//class ThreadFunc
//template<typename XXX>
//class Thread0
//{
//protected:
// FunctorType& m_functor;
// const std::vector<Dart>& m_darts;
//
// typename XXX::MAP& m_map;
// MyGlutWin& m_mgw;
// unsigned int m_th;
// SelectorTrue m_selt;
//public:
// ThreadFunc(FunctorType& func, const std::vector<Dart>& vd):
// m_functor(func), m_darts(vd) {}
// Thread0(typename XXX::MAP& map,MyGlutWin& mgw, unsigned int th):
// m_map(map), m_mgw(mgw), m_th(th) {}
//
// void operator()()
// {
// for (std::vector<Dart>::const_iterator it = m_darts.begin(); it != m_darts.end(); ++it)
// m_functor(*it);
// }
//};
//template <typename PFP>
//void parallel_foreach_orbit(typename PFP::MAP& map, unsigned int orbit, FunctorType& func)
//{
// std::vector<Dart> vd[NBTH];
// boost::thread* threads[NBTH];
//
// DartMarker dm(map);
// Dart d=map.begin();
//
// // NBTH new functions, new thread (with good darts !)
// for (unsigned int i=0; i<NBTH; ++i)
// vd[i].reserve(4096);
//
// std::cout << "Begin render init"<<std::endl;
// m_mgw.useContext();
//
// while ( d != map.end())
// {
// for (unsigned int i=0; i<NBTH; ++i)
// {
// while ((d !=map.end()) && (dm.isMarked(d)))
// map.next(d);
// // instanciation of the renderer (here using VBOs)
// m_mgw.m_render = new Algo::Render::VBO::MapRender_VBO();
//
// if (d !=map.end())
// {
// dm.markOrbit(orbit,d);
// vd[i].push_back(d);
// map.next(d);
// }
// }
// }
// // update the renderer (geometry and primitives)
// m_mgw.m_render->updateData(Algo::Render::VBO::POSITIONS, position);
//
// for (unsigned int i=0; i<NBTH; ++i)
// {
// threads[i] = new boost::thread(ThreadFunc(func, vd[i]));
// }
// m_mgw.m_render->initPrimitives<PFP>(m_map, m_selt, Algo::Render::VBO::TRIANGLES,m_th);
// m_mgw.m_render->initPrimitives<PFP>(m_map, m_selt, Algo::Render::VBO::LINES,m_th);
//
// m_mgw.releaseContext();
// std::cout<< "Render OK "<< std::endl;
//
// //wait for all theads to be finished
// for (unsigned int i=0; i< NBTH; ++i)
// {
// threads[i]->join();
// delete threads[i];
// }
//}
//
//class ThreadFunc
//{
//protected:
// FunctorType& m_functor;
// const std::vector<Dart>& m_darts;
// boost::barrier& m_sync1;
// boost::barrier& m_sync2;
// bool& m_finished;
//public:
// ThreadFunc(FunctorType& func, const std::vector<Dart>& vd, boost::barrier& s1, boost::barrier& s2, bool& finished):
// m_functor(func), m_darts(vd), m_sync1(s1), m_sync2(s2), m_finished(finished) {}
//
// void operator()()
// {
// while (!m_finished)
// {
// for (std::vector<Dart>::const_iterator it = m_darts.begin(); it != m_darts.end(); ++it)
// m_functor(*it);
// m_sync1.wait();
// m_sync2.wait();
// }
// }
//};
//
//#define SZBUFF 2048
//
//template <typename PFP, int NBTH >
//void parallel_foreach_orbit(typename PFP::MAP& map, const FunctorSelect& good, unsigned int orbit, FunctorType& func)
//{
// std::vector<Dart> vd[NBTH];
// boost::thread* threads[NBTH];
//
// DartMarker dm(map);
// Dart d=map.begin();
//
// // NBTH new functions, new thread (with good darts !)
// for (unsigned int i=0; i<NBTH; ++i)
// vd[i].reserve(SZBUFF);
//
// // fill each vd buffers with 4096 darts
// unsigned int nb =0;
// while (( d != map.end()) && (nb < NBTH*SZBUFF) )
// {
// if (good(d) && (!dm.isMarked(d)))
// {
// dm.markOrbit(orbit,d);
// vd[nb%NBTH].push_back(d);
// nb++;
// }
// map.next(d);
// }
//
// boost::barrier sync1(NBTH+1);
// boost::barrier sync2(NBTH+1);
// bool finished=false;
// // lauch threads
// for (unsigned int i=0; i<NBTH; ++i)
// threads[i] = new boost::thread(ThreadFunc(func, vd[i],sync1,sync2, finished));
//
// // and continue to traverse the map
// std::vector<Dart> tempo[NBTH];
// for (unsigned int i=0; i<NBTH; ++i)
// tempo[i].reserve(SZBUFF);
//
// while ( d != map.end())
// {
// for (unsigned int i=0; i<NBTH; ++i)
// tempo[i].clear();
//
// unsigned int nb =0;
// while (( d != map.end()) && (nb < NBTH*SZBUFF) )
// {
// if (good(d) && (!dm.isMarked(d)))
// {
// dm.markOrbit(orbit,d);
// tempo[nb%NBTH].push_back(d);
// nb++;
// }
// map.next(d);
// }
//
// sync1.wait();
// for (unsigned int i=0; i<NBTH; ++i)
// vd[i].swap(tempo[i]);
// sync2.wait();
// }
//
// sync1.wait();
// finished = true;
// sync2.wait();
//
// //wait for all theads to be finished
// for (unsigned int i=0; i< NBTH; ++i)
// {
// threads[i]->join();
// delete threads[i];
// }
//}
//
//
//template <typename PFP, int NBTH >
//void parallel_foreach_cell(typename PFP::MAP& map, const FunctorSelect& good, unsigned int cell, FunctorType& func, unsigned int szbuff)
//{
// std::vector<Dart> vd[NBTH];
// boost::thread* threads[NBTH];
//
// CellMarker cm(map,cell);
// Dart d=map.begin();
//
// // NBTH new functions, new thread (with good darts !)
// for (unsigned int i=0; i<NBTH; ++i)
// vd[i].reserve(szbuff);
//
// // fill each vd buffers with 4096 darts
// unsigned int nb =0;
// while (( d != map.end()) && (nb < NBTH*szbuff) )
// {
// if (good(d) && (!cm.isMarked(d)))
// {
// cm.mark(d);
// vd[nb%NBTH].push_back(d);
// nb++;
// }
// map.next(d);
// }
//
// boost::barrier sync1(NBTH+1);
// boost::barrier sync2(NBTH+1);
// bool finished=false;
// // lauch threads
// for (unsigned int i=0; i<NBTH; ++i)
// threads[i] = new boost::thread(ThreadFunc(func, vd[i],sync1,sync2, finished));
//
// // and continue to traverse the map
// std::vector<Dart> tempo[NBTH];
// for (unsigned int i=0; i<NBTH; ++i)
// tempo[i].reserve(SZBUFF);
//
// while ( d != map.end())
// {
// for (unsigned int i=0; i<NBTH; ++i)
// tempo[i].clear();
//
// unsigned int nb =0;
// while (( d != map.end()) && (nb < NBTH*szbuff) )
// {
// if (good(d) && (!cm.isMarked(d)))
// {
// cm.mark(d);
// tempo[nb%NBTH].push_back(d);
// nb++;
// }
// map.next(d);
// }
//
// sync1.wait();
// for (unsigned int i=0; i<NBTH; ++i)