Commit 98ec03b4 authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud
Browse files

Introduce generic iterator for array.

parent 1a5d8eb5
......@@ -30,7 +30,7 @@ template<typename T> class dtkDistributedArray : public dtkDistributedContainerI
{
public:
inline dtkDistributedArray(const qlonglong& count, dtkDistributedWorker *worker);
~dtkDistributedArray(void);
inline ~dtkDistributedArray(void);
public:
inline void clear(void);
......@@ -48,21 +48,17 @@ public:
inline T at(const qlonglong& index) const;
public:
//inline dtkDistributedItem<T> first(void);
inline T first(void) const;
//inline dtkDistributedItem<T> last(void);
inline T last(void) const;
inline T first(void) const;
inline T last(void) const;
public:
inline dtkDistributedItem<T> operator [] (const qlonglong& index);
//inline T operator [] (const qlonglong& index) const;
public:
//dtkDistributedIterator<T>& constIterator(void) const;
inline dtkDistributedIterator<T> iterator(void);
public:
inline dtkDistributedMapper *mapper(void);
inline qlonglong localToGlobal(const qlonglong& index);
public:
dtkDistributedArrayHandler<T> *m_handler;
......
......@@ -72,8 +72,13 @@ template<typename T> dtkDistributedItem<T> dtkDistributedArray<T>::operator [] (
return dtkDistributedItem<T>(new dtkDistributedArrayItem<T>(const_cast<dtkDistributedArray<T>*>(this), index));
}
template<typename T> dtkDistributedMapper *dtkDistributedArray<T>::mapper(void)
template<typename T> dtkDistributedIterator<T> dtkDistributedArray<T>::iterator(void)
{
return m_handler->m_mapper;
return m_handler->iterator();
}
template<typename T> qlonglong dtkDistributedArray<T>::localToGlobal(const qlonglong& index)
{
return m_handler->localToGlobal(index);
}
......@@ -58,6 +58,9 @@ public:
public:
inline dtkDistributedIterator<T> iterator(void);
public:
inline qlonglong localToGlobal(const qlonglong& index);
// /////////////////////////////////////////////////////////////////
protected:
......@@ -128,6 +131,43 @@ private:
friend class dtkDistributedArray<T>;
};
// ///////////////////////////////////////////////////////////////////
// dtkDistributedIteratorArrayLocal interface
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedIteratorArrayPrivate : public dtkDistributedIteratorPrivate<T>
{
public:
dtkDistributedArrayHandler<T>& h;
qlonglong id;
public:
inline dtkDistributedIteratorArrayPrivate(dtkDistributedArrayHandler<T>& handler) : h(handler), id(0) {;}
inline ~dtkDistributedIteratorArrayPrivate(void) {;}
public:
inline void toFirst(void) { id = 0; }
inline void toLast(void) { id = h.count(); }
inline void toNext(void) { ++id; }
inline void toPrevious(void) { --id; }
public:
inline bool hasNext(void) { return ( id < h.count() ); }
inline bool hasPrevious(void) { return ( id > 0); }
public:
inline qlonglong index(void) { return id; }
public:
inline T current(void) { return h.at(id); }
inline T next(void) { return h.at(id + 1); }
inline T previous(void) { return h.at(id - 1); }
public:
inline bool findBackward(const T& value) { while(id > 0) if (value == h.at(id--)) return true; return false; }
inline bool findForward(const T& value) { qlonglong count = h.count(); while(id < count) if (value == h.at(id++)) return true; return false; }
};
// /////////////////////////////////////////////////////////////////
#include "dtkDistributedArrayHandler.tpp"
......@@ -29,6 +29,8 @@ template <typename T> dtkDistributedArrayHandler<T>::dtkDistributedArrayHandler(
template <typename T> dtkDistributedArrayHandler<T>::~dtkDistributedArrayHandler(void)
{
m_comm->deallocate( m_wid, m_buffer_id);
if (m_mapper)
delete m_mapper;
}
......@@ -127,3 +129,13 @@ template<typename T> T dtkDistributedArrayHandler<T>::last(void) const
{
return (this->*lastMethod)();
}
template<typename T> dtkDistributedIterator<T> dtkDistributedArrayHandler<T>::iterator(void)
{
return dtkDistributedIterator<T>(new dtkDistributedIteratorArrayPrivate<T>(*this));
}
template<typename T> qlonglong dtkDistributedArrayHandler<T>::localToGlobal(const qlonglong& index)
{
return m_mapper->localToGlobal(index, m_wid);
}
......@@ -59,13 +59,10 @@ public:
// dtkDistributedIterator interface
// ///////////////////////////////////////////////////////////////////
// template<typename T> class dtkFuture;
template<typename T> class dtkDistributedIterator
template<typename T> class dtkDistributedIteratorPrivate
{
public:
dtkDistributedIterator(void) {;}
virtual ~dtkDistributedIterator(void) {;}
virtual ~dtkDistributedIteratorPrivate(void) {;}
public:
virtual void toFirst(void) = 0;
......@@ -89,3 +86,41 @@ public:
virtual bool findBackward(const T& value) { return false; }
virtual bool findForward(const T& value) { return false; }
};
// ///////////////////////////////////////////////////////////////////
// dtkDistributedIterator interface
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedIterator
{
public:
inline dtkDistributedIterator(dtkDistributedIteratorPrivate<T> *d_ptr) : d(d_ptr) {;}
public:
inline ~dtkDistributedIterator(void) { delete d; d = NULL; }
public:
inline void toFirst(void) { d->toFirst(); }
inline void toLast(void) { d->toLast(); }
inline void toNext(void) { d->toNext(); }
inline void toPrevious(void) { d->toPrevious(); }
public:
inline bool hasNext(void) { return d->hasNext(); }
inline bool hasPrevious(void) { return d->hasPrevious(); }
public:
inline qlonglong index(void) { return d->index(); }
public:
inline T current(void) { return d->current(); }
inline T next(void) { return d->next(); }
inline T previous(void) { return d->previous(); }
public:
inline bool findBackward(const T& value) { return d->findBackward(); }
inline bool findForward(const T& value) { return d->findForward(); }
private:
dtkDistributedIteratorPrivate<T> *d;
};
......@@ -35,7 +35,7 @@ class containerWork : public dtkDistributedWork
array.m_handler->setLocalMode();
for (qlonglong i = 0; i < array.count(); ++i)
array.set(i, array.mapper()->localToGlobal(i, dtkDistributedWork::worker()->wid()));
array.set(i, array.localToGlobal(i));
qDebug() << array.first() << array.last() << dtkDistributedWork::worker()->wid();
......@@ -56,7 +56,7 @@ class containerWork : public dtkDistributedWork
array.m_handler->setLocalMode();
for (qlonglong i = 0; i < array.count(); ++i)
array[i] += array.mapper()->localToGlobal(i, dtkDistributedWork::worker()->wid());
array[i] += array.localToGlobal(i);
DTK_DISTRIBUTED_END_LOCAL
......@@ -88,88 +88,94 @@ sumWork *sumWork::clone(void)
void sumWork::run(void)
{
// qDebug()<< "run!!!!";
qDebug()<< "run!!!!";
// qlonglong N = 10000000;
// qlonglong sum = 0;
qlonglong N = 1000001;
qlonglong sum = 0;
// for (qlonglong i = 0; i < N; ++i)
// sum += 2*i;
for (qlonglong i = 0; i < N; ++i)
sum += 2*i;
// dtkDistributedCommunicator *comm = dtkDistributedWork::worker()->communicator();
dtkDistributedCommunicator *comm = dtkDistributedWork::worker()->communicator();
// QTime time, maintime;
// maintime.start();
// time.start();
QTime time, maintime;
maintime.start();
time.start();
// dtkDistributedArray<qlonglong>& c = *(new dtkDistributedArray<qlonglong>(N,dtkDistributedWork::worker() ));
qlonglong check_sum = 0;
// QVERIFY(N == c.count());
dtkDistributedArray<qlonglong> c(N, dtkDistributedWork::worker());
c.m_handler->setGlobalMode();
// qDebug()<< "allocation time:" <<time.elapsed() << "ms"; time.restart();
dtkDistributedArray<qlonglong> partial_sum(dtkDistributedWork::worker()->wct(), dtkDistributedWork::worker() );
partial_sum.m_handler->setGlobalMode();
// DTK_DISTRIBUTED_BEGIN_LOCAL
QVERIFY(N == c.count());
// dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
qDebug()<< "allocation time:" <<time.elapsed() << "ms"; time.restart();
// // Fill the container in parallel
// while(it.hasNext()) {
// c.set(it.index(), it.globalIndex());
// it.next();
// }
DTK_DISTRIBUTED_BEGIN_LOCAL
c.m_handler->setLocalMode();
partial_sum.m_handler->setLocalMode();
// comm->barrier();
dtkDistributedIterator<qlonglong> it = c.iterator();
// it.toFront();
// Fill the container in parallel
while(it.hasNext()) {
c.set(it.index(), c.localToGlobal(it.index()));
it.toNext();
}
// // Do the computation in parallel
// while(it.hasNext()) {
// c.setLocal(it.index(), 2 * it.peekNext() );
// it.next();
// }
comm->barrier();
// // it.toFront();
// // while(it.hasNext()) {
// // qDebug() << dtkDistributedWork::worker()->wid() << it.index() << it.peekNext();
// // it.next();
// // }
it.toFirst();
// comm->barrier();
// Do the computation in parallel
while(it.hasNext()) {
c.set(it.index(), 2 * it.current() );
it.toNext();
}
// qlonglong check_sum = 0;
// it.toFirst();
// while(it.hasNext()) {
// qDebug() << dtkDistributedWork::worker()->wid() << it.index() << it.current();
// it.toNext();
// }
// dtkDistributedArray<qlonglong>& partial_sum = *(new dtkDistributedARray<qlonglong>(dtkDistributedWork::worker()->wct(), dtkDistributedWork::worker() ));
comm->barrier();
// dtkDistributedIterator<qlonglong>& it_partial = partial_sum.iterator();
// comm->barrier();
// it.toFront();
comm->barrier();
it.toFirst();
// // Do the partial sum in parallel, and put the result in a parallel container (of size = number of process/threads)
// while(it.hasNext()) {
// check_sum += c.localAt(it.index());
// it.next();
// }
// partial_sum.setLocal(0,check_sum);
// Do the partial sum in parallel, and put the result in a parallel container (of size = number of process/threads)
while(it.hasNext()) {
check_sum += c.at(it.index());
it.toNext();
}
partial_sum.set(0, check_sum);
// DTK_DISTRIBUTED_END_LOCAL
DTK_DISTRIBUTED_END_LOCAL
// DTK_DISTRIBUTED_BEGIN_GLOBAL
DTK_DISTRIBUTED_BEGIN_GLOBAL
c.m_handler->setGlobalMode();
partial_sum.m_handler->setGlobalMode();
// // Sum the partial sums in sequential mode
// Sum the partial sums in sequential mode
// check_sum = 0;
check_sum = 0;
// while(it_partial.hasNext()) {
dtkDistributedIterator<qlonglong> it_partial = partial_sum.iterator();
while(it_partial.hasNext()) {
// check_sum += partial_sum.at(it_partial.index());
// it_partial.next();
// }
check_sum += partial_sum.at(it_partial.index());
it_partial.toNext();
}
// qDebug() << "TOTAL SUM" << check_sum << sum << maintime.elapsed() << "ms";
qDebug() << "TOTAL SUM" << check_sum << sum << maintime.elapsed() << "ms";
// QVERIFY(sum == check_sum);
QVERIFY(sum == check_sum);
// DTK_DISTRIBUTED_END_GLOBAL
DTK_DISTRIBUTED_END_GLOBAL
// delete &c;
// delete &partial_sum;
......@@ -187,43 +193,43 @@ void dtkDistributedContainerTestCase::init(void)
void dtkDistributedContainerTestCase::testSum(void)
{
// dtkDistributedPolicy policy;
dtkDistributedPolicy policy;
// QByteArray numprocs = qgetenv("DTK_NUM_THREADS");
// QByteArray policyEnv = qgetenv("DTK_DISTRIBUTED_POLICY");
QByteArray numprocs = qgetenv("DTK_NUM_THREADS");
QByteArray policyEnv = qgetenv("DTK_DISTRIBUTED_POLICY");
// int np = 2;
int np = 2;
// policy.setType(dtkDistributedPolicy::MP);
policy.setType(dtkDistributedPolicy::MP);
// if (!numprocs.isEmpty()) {
// np = numprocs.toInt();
// qDebug() << "got num procs from env" << np;
// }
if (!numprocs.isEmpty()) {
np = numprocs.toInt();
qDebug() << "got num procs from env" << np;
}
// if (!policyEnv.isEmpty()) {
// qDebug() << "got policy from env" << policyEnv;
// if (QString(policyEnv) == "MT"){
// policy.setType(dtkDistributedPolicy::MT);
// } else if (QString(policyEnv) == "MP") {
// policy.setType(dtkDistributedPolicy::MP);
// } else {
// qDebug() << "unknown policy" << policyEnv;
// }
// }
if (!policyEnv.isEmpty()) {
qDebug() << "got policy from env" << policyEnv;
if (QString(policyEnv) == "MT"){
policy.setType(dtkDistributedPolicy::MT);
} else if (QString(policyEnv) == "MP") {
policy.setType(dtkDistributedPolicy::MP);
} else {
qDebug() << "unknown policy" << policyEnv;
}
}
// for (int i=0; i < np; ++i)
// policy.addHost("localhost");
for (int i=0; i < np; ++i)
policy.addHost("localhost");
// dtkDistributedWorkerManager manager;
// sumWork *work = new sumWork();
dtkDistributedWorkerManager manager;
sumWork *work = new sumWork();
// manager.setPolicy(&policy);
// manager.setWork(work);
// qDebug() << "spawn";
// manager.spawn();
// manager.exec();
// manager.unspawn();
manager.setPolicy(&policy);
manager.setWork(work);
qDebug() << "spawn";
manager.spawn();
manager.exec();
manager.unspawn();
}
void dtkDistributedContainerTestCase::testContainer(void)
......
Supports Markdown
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