Commit f469b905 authored by Julien Wintz's avatar Julien Wintz
Browse files

Proof reading distributed array framework. Perfect. We may however implement...

Proof reading distributed array framework. Perfect. We may however implement nested classes in template files to have a better look at the API.
parent db5b6a8d
/* dtkDistributedArray.h ---
/* dtkDistributedArray.h ---
*
* Author: Thibaud Kloczko
* Created: 2013 Wed Feb 13 22:49:34 (+0100)
* Created: Fri Mar 8 15:06:15 2013 (+0100)
* Version:
* Last-Updated: Fri Mar 8 16:08:12 2013 (+0100)
* By: Julien Wintz
* Update #: 25
*/
/* Commentary:
*
*/
/* Change log:
/* Change Log:
*
*/
......@@ -18,20 +18,20 @@
#include "dtkDistributedWorker.h"
// /////////////////////////////////////////////////////////////////
// dtkDistributedContainer interface
// dtkDistributedContainer
// /////////////////////////////////////////////////////////////////
class dtkDistributedContainer
{
public:
virtual ~dtkDistributedContainer(void) { ; }
virtual ~dtkDistributedContainer(void) {}
public:
virtual void setMode(const dtkDistributed::Mode& mode) = 0;
};
// /////////////////////////////////////////////////////////////////
// dtkDistributedArray interface
// dtkDistributedArray
// /////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedArray : public dtkDistributedContainer
......@@ -44,16 +44,25 @@ public:
inline dtkDistributedArray(const qlonglong& count, dtkDistributedWorker *worker);
inline ~dtkDistributedArray(void);
#pragma mark -
#pragma mark Handler nested class
public:
class handler
{
protected:
friend class dtkDistributedArray<T>;
protected:
qlonglong buffer_count;
protected:
T *buffer;
public:
inline handler(dtkDistributedArray<T>& array) : buffer(0), buffer_count(0) {;}
inline virtual ~handler(void) {;}
inline virtual ~handler(void) {}
public:
inline virtual bool empty(void) const { return !buffer_count; }
inline virtual qlonglong count(void) const { return buffer_count; }
......@@ -64,6 +73,9 @@ public:
};
friend class handler;
#pragma mark -
#pragma mark Handler nested class - global variation
private:
class global_handler : public handler
{
......@@ -74,48 +86,56 @@ private:
qlonglong m_count;
qlonglong m_wid;
qlonglong buffer_id;
public:
global_handler(dtkDistributedArray<T>& array) : handler(array) {;}
~global_handler(void) {;}
public:
inline bool empty(void) const { return !m_count; }
inline qlonglong count(void) const { return m_count; }
inline void setAt(const qlonglong& index, const T& value)
{
qint32 owner = static_cast<qint32>(m_mapper->owner(index));
qlonglong pos = m_mapper->globalToLocal(index);
m_comm->put(owner, pos, &(const_cast<T&>(value)), buffer_id);
}
{
qint32 owner = static_cast<qint32>(m_mapper->owner(index));
qlonglong pos = m_mapper->globalToLocal(index);
m_comm->put(owner, pos, &(const_cast<T&>(value)), buffer_id);
}
inline T at(const qlonglong& index) const
{
qint32 owner = static_cast<qint32>(m_mapper->owner(index));
qlonglong pos = m_mapper->globalToLocal(index);
if (m_wid == owner) {
{
qint32 owner = static_cast<qint32>(m_mapper->owner(index));
qlonglong pos = m_mapper->globalToLocal(index);
if (m_wid == owner) {
return buffer[pos];
} else {
T temp;
m_comm->get(owner, pos, &temp, buffer_id);
return temp;
}
}
} else {
T temp;
m_comm->get(owner, pos, &temp, buffer_id);
return temp;
}
}
inline T first(void) const { return this->at(0); }
inline T last(void) const { return this->at(m_count-1); }
};
friend class global_handler;
#pragma mark -
#pragma mark Item nested class
public:
class item
{
friend class dtkDistributedArray<T>::iterator;
dtkDistributedArray<T>::handler *h;
qlonglong id;
public:
inline item(void) : h(0), id(0) {;}
inline item(dtkDistributedArray<T>::handler *handler, const qlonglong& index) : h(handler), id(index) {;}
inline item(const item& o) : h(o.h), id(o.id) {;}
inline ~item(void) {;}
public:
inline item& operator = (const item& o) { h = o.h; id = o.id; return *this; }
public:
inline T operator * (void) const { return h->at(id); }
inline item& operator = (const T& value) { h->setAt(id, value); return *this; }
......@@ -129,6 +149,7 @@ public:
inline bool operator > (const item &o) const { return id > o.id; }
inline bool operator <= (const item &o) const { return id <= o.id; }
inline bool operator >= (const item &o) const { return id >= o.id; }
private:
item& operator ++ (void) { ++id; return *this; }
item& operator -- (void) { --id; return *this; }
......@@ -137,17 +158,23 @@ public:
};
friend class item;
#pragma mark -
#pragma mark Iterator nested class
class iterator
{
friend class const_iterator;
item it;
public:
inline iterator(void) : it(item()) {;}
inline iterator(dtkDistributedArray<T>::handler *handler, const qlonglong& index = 0) : it(handler, index) {;}
inline iterator(const iterator& o) : it(o.it) {;}
inline ~iterator(void) {;}
public:
inline iterator& operator = (const iterator& o) { it = o.it; }
public:
inline item operator * (void) const { return it; }
inline item operator [] (qlonglong j) const { return item(it.h, it.id + j); }
......@@ -169,19 +196,25 @@ public:
};
friend class iterator;
#pragma mark -
#pragma mark Iterator nested class - const variation
public:
class const_iterator
{
const dtkDistributedArray<T>::handler *h;
qlonglong id;
public:
inline const_iterator(void) : h(0), id(0) {;}
inline const_iterator(const dtkDistributedArray<T>::handler *handler, const qlonglong& index = 0) : h(handler), id(index) {;}
inline const_iterator(const const_iterator& o) : h(o.h), id(o.id) {;}
explicit inline const_iterator(const iterator& o) : h(o.h), id(o.id) {;}
inline ~const_iterator(void) {;}
public:
inline const_iterator& operator = (const const_iterator& o) { h = o.h; id = o.id; }
public:
inline T operator * (void) const { return h->at(id); }
inline T operator [] (qlonglong j) const { return h->at(id + j); }
......
......@@ -13,15 +13,10 @@
*/
#include "dtkDistributed.h"
#include "dtkDistributedCommunicator.h"
#include "dtkDistributedMapper.h"
#include "dtkDistributedWorker.h"
class dtkDistributedCommunicator;
// /////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////
template<typename T> dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& count, dtkDistributedWorker *worker) : m_handler(0), m_local_handler(*this), m_global_handler(*this), m_wid(worker->wid()), m_count(count), m_mapper(new dtkDistributedMapper), m_worker(worker), m_comm(worker->communicator())
{
this->initialize();
......
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