Commit 1a5d8eb5 authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud
Browse files

Improve dtkDistributedItem concept.

parent 0892be22
......@@ -48,11 +48,18 @@ public:
inline T at(const qlonglong& index) const;
public:
inline T first(void) const;
inline T last(void) const;
//inline dtkDistributedItem<T> first(void);
inline T first(void) const;
//inline dtkDistributedItem<T> last(void);
inline T last(void) const;
public:
inline dtkDistributedItem<T> operator [] (const qlonglong& index);
//inline T operator [] (const qlonglong& index) const;
public:
dtkDistributedArrayItem<T> operator [] (const qlonglong& index);
//dtkDistributedIterator<T>& constIterator(void) const;
public:
inline dtkDistributedMapper *mapper(void);
......
......@@ -67,13 +67,13 @@ template<typename T> T dtkDistributedArray<T>::last(void) const
return m_handler->last();
}
template<typename T> dtkDistributedMapper *dtkDistributedArray<T>::mapper(void)
template<typename T> dtkDistributedItem<T> dtkDistributedArray<T>::operator [] (const qlonglong& index)
{
return m_handler->m_mapper;
return dtkDistributedItem<T>(new dtkDistributedArrayItem<T>(const_cast<dtkDistributedArray<T>*>(this), index));
}
template<typename T> dtkDistributedArrayItem<T> dtkDistributedArray<T>::operator [] (const qlonglong& index)
template<typename T> dtkDistributedMapper *dtkDistributedArray<T>::mapper(void)
{
return dtkDistributedArrayItem<T>(this, index);
return m_handler->m_mapper;
}
......@@ -55,6 +55,9 @@ public:
inline T first(void) const;
inline T last(void) const;
public:
inline dtkDistributedIterator<T> iterator(void);
// /////////////////////////////////////////////////////////////////
protected:
......
......@@ -14,122 +14,33 @@
#pragma once
#include "dtkDistributedItem.h"
// /////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////
template <typename T> class dtkDistributedArrayItem
template <typename T> class dtkDistributedArrayItem : public dtkDistributedContainerItem<T>
{
public:
inline dtkDistributedArrayItem(void);
inline dtkDistributedArrayItem(const dtkDistributedArrayItem<T>& other);
public:
inline ~dtkDistributedArrayItem(void);
inline dtkDistributedArrayItem(dtkDistributedArray<T> *array, const qlonglong& index) : m_index(index), m_array(array) {;}
public:
dtkDistributedArrayItem& operator = (const dtkDistributedArrayItem<T>& other);
inline ~dtkDistributedArrayItem(void) { m_index = -1; m_array = 0; }
public:
inline dtkDistributedArrayItem& operator = (const T& value);
inline void assign(const T& value) { m_array->set(m_index, value); }
public:
inline dtkDistributedArrayItem& operator ++ (void);
inline dtkDistributedArrayItem& operator -- (void);
inline void increment(const T& value) { m_array->set(m_index, m_array->at(m_index) + value); }
inline void decrement(const T& value) { m_array->set(m_index, m_array->at(m_index) - value); }
inline dtkDistributedArrayItem& operator += (const T& value);
inline dtkDistributedArrayItem& operator -= (const T& value);
inline dtkDistributedArrayItem& operator *= (const T& value);
inline dtkDistributedArrayItem& operator /= (const T& value);
inline void multiply(const T& value) { m_array->set(m_index, m_array->at(m_index) * value); }
inline void divide(const T& value) { m_array->set(m_index, m_array->at(m_index) / value); }
public:
inline T value(void) const;
inline T value(void) const { return m_array->at(m_index); }
private:
friend class dtkDistributedArray<T>;
inline dtkDistributedArrayItem(dtkDistributedArray<T> *array, const qlonglong& index);
private:
public:
qlonglong m_index;
dtkDistributedArray<T> *m_array;
dtkDistributedArray<T> *m_array;
};
// /////////////////////////////////////////////////////////////////
template <typename T> dtkDistributedArrayItem<T>::dtkDistributedArrayItem(void) : m_index(-1), m_array(0)
{
}
template <typename T> dtkDistributedArrayItem<T>::dtkDistributedArrayItem(const dtkDistributedArrayItem& other) : m_index(other.m_index), m_array(other.m_array)
{
}
template <typename T> dtkDistributedArrayItem<T>::dtkDistributedArrayItem(dtkDistributedArray<T> *array, const qlonglong& index) : m_index(index), m_array(array)
{
}
template <typename T> dtkDistributedArrayItem<T>::~dtkDistributedArrayItem(void)
{
m_index = -1;
m_array = NULL;
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator = (const dtkDistributedArrayItem<T>& other)
{
m_index = other.m_index;
m_array = other.m_array;
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator = (const T& value)
{
m_array.set(m_index, value);
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator ++ (void)
{
m_array->set(m_index, m_array->at(m_index)++);
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator -- (void)
{
m_array->set(m_index, m_array->at(m_index)--);
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator += (const T& value)
{
m_array->set(m_index, m_array->at(m_index) + value);
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator -= (const T& value)
{
m_array->set(m_index, m_array->at(m_index) - value);
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator *= (const T& value)
{
m_array->set(m_index, m_array->at(m_index) * value);
return (*this);
}
template <typename T> dtkDistributedArrayItem<T>& dtkDistributedArrayItem<T>::operator /= (const T& value)
{
m_array->set(m_index, m_array->at(m_index) / value);
return (*this);
}
template <typename T> T dtkDistributedArrayItem<T>::value(void) const
{
return m_array->at(m_index);
}
/* dtkDistributedItem.h ---
*
* Author: Thibaud Kloczko
* Created: 2013 Wed Feb 27 23:11:32 (+0100)
*/
/* Commentary:
*
*/
/* Change log:
*
*/
#pragma once
// /////////////////////////////////////////////////////////////////
// dtkDistributedContainerItem interface
// /////////////////////////////////////////////////////////////////
template <typename T> class dtkDistributedContainerItem
{
public:
virtual ~dtkDistributedContainerItem(void) {;}
public:
virtual void assign(const T& value) = 0;
virtual void increment(const T& value) = 0;
virtual void decrement(const T& value) = 0;
virtual void multiply(const T& value) = 0;
virtual void divide(const T& value) = 0;
public:
virtual T value(void) const = 0;
};
// /////////////////////////////////////////////////////////////////
// dtkDistributedItem interface
// /////////////////////////////////////////////////////////////////
template <typename T> class dtkDistributedItem
{
public:
inline dtkDistributedItem(dtkDistributedContainerItem<T> *d_ptr);
public:
inline ~dtkDistributedItem(void);
public:
inline dtkDistributedItem& operator = (const T& value);
inline dtkDistributedItem& operator += (const T& value);
inline dtkDistributedItem& operator -= (const T& value);
inline dtkDistributedItem& operator *= (const T& value);
inline dtkDistributedItem& operator /= (const T& value);
public:
inline T value(void) const;
private:
dtkDistributedContainerItem<T> *d;
private:
friend class dtkDistributedArray<T>;
};
// /////////////////////////////////////////////////////////////////
template <typename T> dtkDistributedItem<T>::dtkDistributedItem(dtkDistributedContainerItem<T> *d_ptr) : d(d_ptr)
{
}
template <typename T> dtkDistributedItem<T>::~dtkDistributedItem(void)
{
if (d) {
delete d;
}
d = NULL;
}
template <typename T> dtkDistributedItem<T>& dtkDistributedItem<T>::operator = (const T& value)
{
d->assign(value);
return (*this);
}
template <typename T> dtkDistributedItem<T>& dtkDistributedItem<T>::operator += (const T& value)
{
d->increment(value);
return (*this);
}
template <typename T> dtkDistributedItem<T>& dtkDistributedItem<T>::operator -= (const T& value)
{
d->decrement(value);
return (*this);
}
template <typename T> dtkDistributedItem<T>& dtkDistributedItem<T>::operator *= (const T& value)
{
d->multiply(value);
return (*this);
}
template <typename T> dtkDistributedItem<T>& dtkDistributedItem<T>::operator /= (const T& value)
{
d->divide(value);
return (*this);
}
template <typename T> T dtkDistributedItem<T>::value(void) const
{
return d->value();
}
......@@ -14,6 +14,7 @@
#include "dtkDistributedContainerTest.h"
#include <dtkDistributed>
#include <dtkDistributed/dtkDistributedArray.h>
#include <dtkDistributed/dtkDistributedPolicy.h>
#include <dtkDistributed/dtkDistributedWork.h>
#include <dtkDistributed/dtkDistributedWorker.h>
......@@ -87,91 +88,91 @@ sumWork *sumWork::clone(void)
void sumWork::run(void)
{
qDebug()<< "run!!!!";
// qDebug()<< "run!!!!";
qlonglong N = 10000000;
qlonglong sum = 0;
// qlonglong N = 10000000;
// 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();
dtkDistributedContainer<qlonglong>& c = *(new dtkDistributedContainer<qlonglong>(N,dtkDistributedWork::worker() ));
// dtkDistributedArray<qlonglong>& c = *(new dtkDistributedArray<qlonglong>(N,dtkDistributedWork::worker() ));
QVERIFY(N == c.size());
// QVERIFY(N == c.count());
qDebug()<< "allocation time:" <<time.elapsed() << "ms"; time.restart();
// qDebug()<< "allocation time:" <<time.elapsed() << "ms"; time.restart();
DTK_DISTRIBUTED_BEGIN_LOCAL
// DTK_DISTRIBUTED_BEGIN_LOCAL
dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
// dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
// Fill the container in parallel
while(it.hasNext()) {
c.setLocal(it.index(), it.globalIndex());
it.next();
}
comm->barrier();
it.toFront();
// // Fill the container in parallel
// while(it.hasNext()) {
// c.set(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();
// dtkDistributedArray<qlonglong>& partial_sum = *(new dtkDistributedARray<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();
// // 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_END_LOCAL
// DTK_DISTRIBUTED_END_LOCAL
DTK_DISTRIBUTED_BEGIN_GLOBAL
// DTK_DISTRIBUTED_BEGIN_GLOBAL
// Sum the partial sums in sequential mode
// // Sum the partial sums in sequential mode
check_sum = 0;
// check_sum = 0;
while(it_partial.hasNext()) {
// 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.next();
// }
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;
// delete &c;
// delete &partial_sum;
}
void dtkDistributedContainerTestCase::initTestCase(void)
......@@ -186,43 +187,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