Commit 761d9036 authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud
Browse files

Converging toward Nico version.

parent a3e9b34f
......@@ -18,7 +18,8 @@
#include "dtkDistributedContainerSequence.h"
#include "dtkDistributedArrayHandler.h"
class dtkDistributedCommunicator;
class dtkDistributedMapper;
class dtkDistributedWorker;
// /////////////////////////////////////////////////////////////////
// dtkDistributedArray interface
......@@ -27,33 +28,9 @@ class dtkDistributedCommunicator;
template<typename T> class dtkDistributedArray : public dtkDistributedContainerIndexed<T>, public dtkDistributedContainerSequence<T>
{
public:
dtkDistributedArray(const qlonglong& count);
dtkDistributedArray(const dtkDistributedArray<T>& other);
dtkDistributedArray(const qlonglong& count, dtkDistributedWorker *worker);
~dtkDistributedArray(void);
public:
QString identifier(void) const;
public:
dtkDistributedArray<T> *clone(void);
// public:
// dtkDistributedArray<T>& operator = (const dtkDistributedArray<T>& other) { this->copy(other); return *this; }
// protected:
// void copy(const dtkDistributedContainer<T>& other);
// public:
// bool operator == (const dtkDistributedArray<T>& other) const;
// bool operator != (const dtkDistributedArray<T>& other) const;
// protected:
// bool equal(const dtkDistributedContainer<T>& other) const;
// public:
// void setMode(dtkDistributedMode *mode); // register data manager to a given mode
// void setPolicy(dtkDistributedPolicy *policy); // register data manager to a given policy
public:
void clear(void);
......@@ -63,21 +40,12 @@ public:
public:
qlonglong count(void) const;
// public:
// void set(const qlonglong& global_id, const T& value);
// public:
// T at(const qlonglong& global_id) const;
// public:
// dtkDistributedIterator<T>& iterator(void);
private:
qlonglong m_count;
qlonglong m_buffer_count;
dtkDistributedMapper *m_mapper;
dtkDistributedWorker *m_worker;
public:
dtkDistributedArrayHandler<T> handler;
dtkDistributedArrayHandler<T> m_handler;
friend class dtkDistributedArrayHandler<T>;
......
......@@ -14,18 +14,20 @@
//#prama once
#include "dtkDistributedMapper.h"
#include "dtkDistributedWorker.h"
class dtkDistributedCommunicator;
// /////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////
template<typename T> dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& count) : dtkDistributedContainerIndexed<T>(), dtkDistributedContainerSequence<T>(), m_count(count), m_buffer_count(count/2)
{
handler.setArray(this);
}
template<typename T> dtkDistributedArray<T>::dtkDistributedArray(const dtkDistributedArray<T>& other)
template<typename T> dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& count, dtkDistributedWorker *worker) : m_mapper(new dtkDistributedMapper), m_worker(worker)
{
m_mapper->setMapping(count, m_worker->communicator()->size());
m_handler.initialize(m_worker->communicator(), m_worker->wid(), count, m_mapper->count(m_worker->wid()), m_worker->containerId());
}
template<typename T> dtkDistributedArray<T>::~dtkDistributedArray(void)
......@@ -33,28 +35,18 @@ template<typename T> dtkDistributedArray<T>::~dtkDistributedArray(void)
}
template<typename T> QString dtkDistributedArray<T>::identifier(void) const
{
return "dtkDistributedArray";
}
template<typename T> dtkDistributedArray<T> *dtkDistributedArray<T>::clone(void)
{
return new dtkDistributedArray<T>(*this);
}
template<typename T> void dtkDistributedArray<T>::clear(void)
{
return m_handler.clear();
}
template<typename T> bool dtkDistributedArray<T>::empty(void) const
{
return false;
return m_handler.empty();
}
template<typename T> qlonglong dtkDistributedArray<T>::count(void) const
{
return handler.count();
return m_handler.count();
}
......@@ -21,42 +21,63 @@ template <typename T> class dtkDistributedArrayHandler;
//
// /////////////////////////////////////////////////////////////////
template <typename T> class dtkDistributedArrayHandler : public QObject
template <typename T> class dtkDistributedArrayHandler
{
public:
void setArray(dtkDistributedArray<T> *array) { m_array = array; }
dtkDistributedArrayHandler(void) : m_wid(-1), m_count(-1), m_buffer_id(-1), m_buffer_count(-1), m_buffer(0), m_comm(0) {;}
public:
void initialize(dtkDistributedCommunicator *communicator, const qlonglong& wid, const qlonglong& count, const qlonglong& buffer_count, const qlonglong& buffer_id);
public:
void setLocalMode(void) {
countMethod = &dtkDistributedArrayHandler<T>::countLocal;
}
inline void setLocalMode(void);
inline void setGlobalMode(void);
void setGlobalMode(void) {
countMethod = &dtkDistributedArrayHandler<T>::countGlobal;
}
public:
inline void clear(void);
// /////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////
inline bool empty(void) const;
public:
qlonglong count(void) const { return (this->*countMethod)(); }
inline qlonglong count(void) const;
protected:
qlonglong countLocal(void) const { return m_array->m_buffer_count; }
qlonglong countGlobal(void) const { return m_array->m_count; }
// /////////////////////////////////////////////////////////////////
protected:
typedef void (dtkDistributedArrayHandler<T>::*clearMethodPointer)(void);
typedef bool (dtkDistributedArrayHandler<T>::*emptyMethodPointer)(void) const;
typedef qlonglong (dtkDistributedArrayHandler<T>::*countMethodPointer)(void) const;
protected:
clearMethodPointer clearMethod;
emptyMethodPointer emptyMethod;
countMethodPointer countMethod;
protected:
void clearLocal(void) { qDebug() << "I am locally cleared"; }
void clearGlobal(void) { qDebug() << "I am globally cleared"; }
bool emptyLocal(void) const { return !static_cast<bool>(m_buffer_count); }
bool emptyGlobal(void) const { return !static_cast<bool>(m_count); }
qlonglong countLocal(void) const { return m_buffer_count; }
qlonglong countGlobal(void) const { return m_count; }
// /////////////////////////////////////////////////////////////////
//
private:
qlonglong m_wid;
qlonglong m_count;
qlonglong m_buffer_id;
qlonglong m_buffer_count;
T *m_buffer;
// /////////////////////////////////////////////////////////////////
private:
dtkDistributedArray<T> *m_array;
dtkDistributedCommunicator *m_comm;
};
// /////////////////////////////////////////////////////////////////
#include "dtkDistributedArrayHandler.tpp"
/* dtkDistributedArrayHandler.tpp ---
*
* Author: Thibaud Kloczko
* Created: 2013 Tue Feb 26 15:04:40 (+0100)
*/
/* Commentary:
*
*/
/* Change log:
*
*/
#pragma once
// /////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////
template <typename T> void dtkDistributedArrayHandler<T>::initialize(dtkDistributedCommunicator *communicator, const qlonglong& wid, const qlonglong& count, const qlonglong& buffer_count, const qlonglong& buffer_id)
{
m_comm = communicator;
m_wid = wid;
m_count = count;
m_buffer_count = buffer_count;
m_buffer_id = buffer_id;
m_buffer = static_cast<T*>(m_comm->allocate(m_buffer_count, sizeof(T), m_wid, m_buffer_id));
}
template <typename T> void dtkDistributedArrayHandler<T>::setLocalMode(void)
{
clearMethod = &dtkDistributedArrayHandler<T>::clearLocal;
emptyMethod = &dtkDistributedArrayHandler<T>::emptyLocal;
countMethod = &dtkDistributedArrayHandler<T>::countLocal;
}
template <typename T> void dtkDistributedArrayHandler<T>::setGlobalMode(void)
{
clearMethod = &dtkDistributedArrayHandler<T>::clearGlobal;
emptyMethod = &dtkDistributedArrayHandler<T>::emptyGlobal;
countMethod = &dtkDistributedArrayHandler<T>::countGlobal;
}
template <typename T> void dtkDistributedArrayHandler<T>::clear(void)
{
return (this->*clearMethod)();
}
template <typename T> bool dtkDistributedArrayHandler<T>::empty(void) const
{
return (this->*emptyMethod)();
}
template <typename T> qlonglong dtkDistributedArrayHandler<T>::count(void) const
{
return (this->*countMethod)();
}
......@@ -15,7 +15,6 @@
#pragma once
#include <dtkDistributed>
#include "dtkObject.h"
class dtkDistributedMode;
class dtkDistributedPolicy;
......@@ -29,47 +28,27 @@ template<typename T> class dtkDistributedIterator;
class dtkDistributedCommunicator;
class dtkDistributedMapper;
template<typename T> class dtkDistributedContainer : public dtkObject
template<typename T> class dtkDistributedContainer
{
public:
dtkDistributedContainer(void) : dtkObject() {;}
dtkDistributedContainer(const dtkDistributedContainer<T>& other) : dtkObject(other) {;}
dtkDistributedContainer(void) {;}
virtual ~dtkDistributedContainer(void) {;}
public:
virtual dtkDistributedContainer<T> *clone(void) = 0;
public:
/* dtkDistributedContainer<T>& operator = (const dtkDistributedContainer<T>& other) { this->copy(other); return *this; } */
/* protected: */
/* virtual void copy(const dtkDistributedContainer<T>& other) = 0; */
public:
/* bool operator == (const dtkDistributedContainer<T>& other) const { return this->equal(other) ; } */
/* bool operator != (const dtkDistributedContainer<T>& other) const { return !(this->equal(other)); } */
/* protected: */
/* virtual bool equal(const dtkDistributedContainer<T>& other) const = 0; */
// virtual void setMode(dtkDistributedMode *mode) { Q_UNUSED(mode); }
// virtual void setPolicy(dtkDistributedPolicy *policy) { Q_UNUSED(policy); }
public:
virtual void clear(void) = 0;
// virtual void localClear(void) = 0;
virtual void clear(void) = 0;
public:
virtual bool empty(void) const = 0;
// virtual bool localEmpty(void) const = 0;
virtual bool empty(void) const = 0;
public:
virtual qlonglong count(void) const = 0;
// virtual qlonglong localCount(void) const = 0;
virtual qlonglong count(void) const = 0;
public:
// virtual dtkDistributedIterator<T>& iterator(void) = 0;
// virtual dtkDistributedIterator<T>& localIterator(void) = 0;
// virtual dtkDistributedIterator<T>& iterator(void) = 0;
};
// ///////////////////////////////////////////////////////////////////
......@@ -102,17 +81,7 @@ public:
virtual T next(void) = 0;
virtual T previous(void) = 0;
// public:
// virtual dtkFuture<T> futureCurrent(void) = 0;
// virtual dtkFuture<T> futureNext(void) = 0;
// virtual dtkFuture<T> futurePrevious(void) = 0;
public:
virtual bool findBackward(const T& value) { return false; }
virtual bool findForward(const T& value) { return false; }
};
// ///////////////////////////////////////////////////////////////////
//#include "dtkDistributedContainer.tpp"
......@@ -16,8 +16,6 @@
#include "dtkDistributedContainer.h"
template<typename T> class dtkFuture;
// /////////////////////////////////////////////////////////////////
// dtkDistributedContainerIndexed interface
// /////////////////////////////////////////////////////////////////
......@@ -26,26 +24,16 @@ template<typename T> class dtkDistributedContainerIndexed : public virtual dtkDi
{
public:
dtkDistributedContainerIndexed(void) : dtkDistributedContainer<T>() {;}
dtkDistributedContainerIndexed(const dtkDistributedContainerIndexed<T>& other) : dtkDistributedContainer<T>(other) {;}
virtual ~dtkDistributedContainerIndexed(void) {;}
public:
// virtual void resize(const qlonglong& size) = 0;
// virtual void set(const qlonglong& global_id, const T& value) = 0;
// virtual void localSet(const qlonglong& local_id, const T& value) = 0;
// virtual void insert(const qlonglong& global_id, const T& value) = 0;
// virtual void localInsert(const qlonglong& local_id, const T& value) = 0;
// virtual void removeAt(const qlonglong& global_id) = 0;
// virtual void set(const qlonglong& index, const T& value) = 0;
// virtual void insert(const qlonglong& index, const T& value) = 0;
// virtual void futureInsert(const qlonglong& global_id, const T& value) = 0;
public:
// virtual T at(const qlonglong& global_id) const = 0;
// virtual const T& localAt(const qlonglong& local_id) const = 0;
// virtual void removeAt(const qlonglong& index) = 0;
public:
//virtual dtkFuture<T> futureAt(const qlonglong& global_id) const = 0;
// virtual T at(const qlonglong& index) const = 0;
};
......@@ -24,38 +24,21 @@ template<typename T> class dtkDistributedContainerSequence : public virtual dtkD
{
public:
dtkDistributedContainerSequence(void) : dtkDistributedContainer<T>() {;}
dtkDistributedContainerSequence(const dtkDistributedContainerSequence<T>& other) : dtkDistributedContainer<T>(other) {;}
virtual ~dtkDistributedContainerSequence(void) {;}
/* public: */
/* virtual void append(const T& value) = 0; */
/* virtual void localAppend(const T& value) = 0; */
/* virtual void append(const T& value) = 0; */
/* virtual void prepend(const T& value) = 0; */
/* virtual void prepend(const T& value) = 0; */
/* virtual void localPrepend(const T& value) = 0; */
/* virtual void removeFirst(void) = 0; */
/* virtual void removeLast(void) = 0; */
/* virtual void removeFirst(void) = 0; */
/* virtual void localRemoveFirst(void) = 0; */
/* virtual void removeLast(void) = 0; */
/* virtual void localRemoveLast(void) = 0; */
/* virtual bool remove(const T& value) = 0; */
/* virtual bool localRemove(const T& value) = 0; */
/* virtual bool remove(const T& value) = 0; */
/* public: */
/* virtual T first(void) const = 0; */
/* virtual const T& localFirst(void) const = 0; */
/* virtual T last(void) const = 0; */
/* virtual const T& localLast(void) const = 0; */
/* virtual T first(void) const = 0; */
/* virtual T last(void) const = 0; */
/* virtual T takeFirst(void) = 0; */
/* virtual T localTakeFirst(void) = 0; */
/* virtual T takeLast(void) = 0; */
/* virtual T localTakeLast(void) = 0; */
public:
//virtual dtkFuture<T> futureAt(const qlonglong& global_id) const = 0;
/* virtual T takeFirst(void) = 0; */
/* virtual T takeLast(void) = 0; */
};
......@@ -14,103 +14,124 @@
#include "dtkDistributedContainerTest.h"
#include <dtkDistributed>
<<<<<<< Updated upstream
#include <dtkDistributed/dtkDistributedPolicy.h>
#include <dtkDistributed/dtkDistributedWork.h>
#include <dtkDistributed/dtkDistributedWorker.h>
#include <dtkDistributed/dtkDistributedWorkerManager.h>
=======
#include <dtkDistributed/dtkDistributedArray.h>
#include <dtkDistributed/dtkDistributedArrayHandler.h>
>>>>>>> Stashed changes
class myWork : public dtkDistributedWork
{
myWork *clone(void) {return new myWork(*this); };
myWork *clone(void) { return new myWork(*this); };
void run(void)
{
qDebug()<< "run!!!!";
qlonglong N = 10000000;
QTime time;
dtkDistributedCommunicator *comm = dtkDistributedWork::worker()->communicator();
dtkDistributedArray<qlonglong> array(1001, dtkDistributedWork::worker());
DTK_DISTRIBUTED_BEGIN_LOCAL
array.m_handler.setLocalMode();
array.clear();
qDebug() << array.empty() << dtkDistributedWork::worker()->wid();
qDebug() << array.count() << dtkDistributedWork::worker()->wid();
DTK_DISTRIBUTED_END_LOCAL
<<<<<<< Updated upstream
qlonglong sum = 0;
for (qlonglong i = 0; i < N; ++i)
sum += 2*i;
DTK_DISTRIBUTED_BEGIN_GLOBAL
array.m_handler.setGlobalMode();
array.clear();
qDebug() << array.empty() << dtkDistributedWork::worker()->wid();
qDebug() << array.count() << dtkDistributedWork::worker()->wid();
DTK_DISTRIBUTED_END_GLOBAL
DTK_DISTRIBUTED_BEGIN_LOCAL
array.m_handler.setLocalMode();
array.clear();
qDebug() << array.empty() << dtkDistributedWork::worker()->wid();
qDebug() << array.count() << dtkDistributedWork::worker()->wid();
DTK_DISTRIBUTED_END_LOCAL
dtkDistributedCommunicator *comm = dtkDistributedWork::worker()->communicator();
// qDebug()<< "run!!!!";
// qlonglong N = 10000000;
QTime time, maintime;
maintime.start();
time.start();
// qlonglong sum = 0;
// for (qlonglong i = 0; i < N; ++i)
// sum += 2*i;
dtkDistributedContainer<qlonglong>& c = *(new dtkDistributedContainer<qlonglong>(N,dtkDistributedWork::worker() ));
// dtkDistributedCommunicator *comm = dtkDistributedWork::worker()->communicator();
QVERIFY(N == c.size());
// QTime time, maintime;
// maintime.start();
// time.start();
qDebug()<< "allocation time:" <<time.elapsed() << "ms"; time.restart();
DTK_DISTRIBUTED_BEGIN_LOCAL
// dtkDistributedContainer<qlonglong>& c = *(new dtkDistributedContainer<qlonglong>(N,dtkDistributedWork::worker() ));
dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
// QVERIFY(N == c.size());
// Fill the container in parallel
while(it.hasNext()) {
c.setLocal(it.index(), it.globalIndex());
it.next();
}
// qDebug()<< "allocation time:" <<time.elapsed() << "ms"; time.restart();
// DTK_DISTRIBUTED_BEGIN_LOCAL
comm->barrier();
// dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
it.toFront();
// // Fill the container in parallel
// while(it.hasNext()) {
// c.setLocal(it.index(), it.globalIndex());
// it.next();
// }
// Do the computation in parallel
while(it.hasNext()) {
c.setLocal(it.index(), 2 * it.peekNext() );
it.next();
}
// comm->barrier();
// it.toFront();
// // Do the computation in parallel
// while(it.hasNext()) {
// qDebug() << dtkDistributedWork::worker()->wid() << it.index() << it.peekNext();
// 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();
// // }
qlonglong check_sum = 0;
// comm->barrier();
dtkDistributedContainer<qlonglong>& partial_sum = *(new dtkDistributedContainer<qlonglong>(dtkDistributedWork::worker()->wct(), dtkDistributedWork::worker() ));
// qlonglong check_sum = 0;
dtkDistributedIterator<qlonglong>& it_partial = partial_sum.iterator();
comm->barrier();
it.toFront();
// dtkDistributedContainer<qlonglong>& partial_sum = *(new dtkDistributedContainer<qlonglong>(dtkDistributedWork::worker()->wct(), dtkDistributedWork::worker() ));
// 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);
// dtkDistributedIterator<qlonglong>& it_partial = partial_sum.iterator();
// comm->barrier();
// it.toFront();
DTK_DISTRIBUTED_END_LOCAL
// // 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);
DTK_DISTRIBUTED_BEGIN_GLOBAL
// DTK_DISTRIBUTED_END_LOCAL
// Sum the partial sums in sequential mode
check_sum = 0;
while(it_partial.hasNext()) {
// DTK_DISTRIBUTED_BEGIN_GLOBAL
check_sum += partial_sum.at(it_partial.index());
it_partial.next();
}
// // Sum the partial sums in sequential mode
// check_sum = 0;
// while(it_partial.hasNext()) {
qDebug() << "TOTAL SUM" << check_sum << sum << maintime.