triskele/test/TestDeal.cpp
2018-08-27 13:46:51 +02:00

93 lines
2.6 KiB
C++

// faire test 10000 + lamba + 1...coreCount
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include "triskeleDealThreads.hpp"
#include "TestThread.hpp"
using namespace std;
using namespace boost::chrono;
using namespace triskele;
using namespace otb;
using namespace otb::triskele;
inline void
fDirect (const DimImg &nbItem, const vector<DimImg> &global) {
DimImg sum = 0;
for (DimImg x = 0; x < nbItem; ++x)
sum += global[x];
}
template<typename FunctId>
inline void
fLambda (DimImg &nbItem, const FunctId &functId/* functId (id) */) {
for (DimImg x = 0; x < nbItem; ++x)
functId (x);
}
template<typename FunctId>
inline void
fThread (double &inDuration, const DimImg &nbItem, const FunctId &functId/* functId (id) */) {
// One thread
#if INTEL_TBB_THREAD
using namespace tbb;
#pragma warning(disable: 588)
parallel_for (size_t (0), size_t (1), [&nbItem, &functId, &inDuration] (size_t idCopyValInThread) {
auto start = high_resolution_clock::now ();
for (DimImg x = 0; x < nbItem; ++x)
functId (x);
auto end = high_resolution_clock::now ();
inDuration = duration_cast<duration<double> > (end-start).count ();
});
#else /* BOOST thread */
std::vector<boost::thread> tasks;
for (unsigned int idCopyValInThread = 0; idCopyValInThread < 1; ++idCopyValInThread) {
tasks.push_back (boost::thread ([/*no ref!!!*/idCopyValInThread, &nbItem, &functId, &inDuration] () {
auto start = high_resolution_clock::now ();
for (DimImg x = 0; x < nbItem; ++x)
functId (x);
auto end = high_resolution_clock::now ();
inDuration += duration_cast<duration<double> > (end-start).count ();
}));
}
for (unsigned int i = 0; i < 1; ++i)
tasks[i].join ();
#endif
}
void
TestThread::testDeal () {
DimImg nbItem = global.size ();
double inDuration = 0;
// XXX direct
auto startDirect = high_resolution_clock::now ();
fDirect (nbItem, global);
// XXX lambda
auto startLamba = high_resolution_clock::now ();
DimImg sum = 0;
fLambda (nbItem, [this, &sum] (const DimImg &x) {
sum += global [x];
});
// XXX thread
auto startThread = high_resolution_clock::now ();
sum = 0;
fThread (inDuration, nbItem, [this, &sum] (const DimImg &x) {
sum += global [x];
});
// XXX n direct
// XXX n lambda
// XXX n thread
auto end = high_resolution_clock::now ();
addTime (directDealStats, duration_cast<duration<double> > (startLamba-startDirect).count ());
addTime (lambdaDealStats, duration_cast<duration<double> > (startThread-startLamba).count ());
addTime (threadDealStats, duration_cast<duration<double> > (end-startThread).count ());
addTime (inThreadDealStats, inDuration);
}