Commit ca5c7742 authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud
Browse files

Add mapper.

parent 1faa3ba2
......@@ -3,9 +3,9 @@
## Author: Julien Wintz
## Created: Mon Feb 4 12:16:39 2013 (+0100)
## Version:
## Last-Updated: Tue Feb 5 14:06:26 2013 (+0100)
## By: Julien Wintz
## Update #: 31
## Last-Updated: 2013 Thu Feb 7 12:25:52 (+0100)
## By: Thibaud Kloczko
## Update #: 32
######################################################################
##
### Change Log:
......@@ -24,10 +24,12 @@ set(${PROJECT_NAME}_HEADERS
dtkDistributedCommunicatorPluginManager
dtkDistributedCommunicatorPluginManager.h
dtkDistributedContainer.h
dtkDistributedContainer.tpp)
dtkDistributedContainer.tpp
dtkDistributedMapper.h)
set(${PROJECT_NAME}_SOURCES
dtkDistributedCommunicatorPluginManager.cpp)
dtkDistributedCommunicatorPluginManager.cpp
dtkDistributedMapper.cpp)
## #################################################################
## Build rules
......
......@@ -3,9 +3,9 @@
* Author: Thibaud Kloczko
* Created: Tue Feb 5 14:08:23 2013 (+0100)
* Version:
* Last-Updated: Tue Feb 5 14:09:34 2013 (+0100)
* By: Julien Wintz
* Update #: 7
* Last-Updated: 2013 Thu Feb 7 15:06:29 (+0100)
* By: Thibaud Kloczko
* Update #: 29
*/
/* Change Log:
......@@ -14,6 +14,8 @@
#pragma once
#include <QtCore/QVector>
// ///////////////////////////////////////////////////////////////////
// dtkDistributedContainer
// ///////////////////////////////////////////////////////////////////
......@@ -27,7 +29,7 @@ template<typename T> class dtkDistributedContainer;
template<typename T> class dtkDistributedLocalIterator
{
public:
dtkDistributedLocalIterator(dtkDistributedContainer<T> *container);
dtkDistributedLocalIterator(dtkDistributedContainer<T>& container);
~dtkDistributedLocalIterator(void);
public:
......@@ -51,7 +53,7 @@ public:
qlonglong globalIndex(void) const;
private:
dtkDistributedContainer<T> *c;
dtkDistributedContainer<T>& c;
typedef typename QVector<T>::const_iterator const_iterator;
const_iterator i;
qlonglong m_index;
......@@ -61,10 +63,16 @@ private:
// dtkDistributedContainer interface
// /////////////////////////////////////////////////////////////////
class dtkDistributedCommunicator;
class dtkDistributedMapper;
template<typename T> class dtkDistributedContainer
{
public:
dtkDistributedContainer(void);
dtkDistributedContainer(const qlonglong& size, dtkDistributedCommunicator *communicator);
public:
~dtkDistributedContainer(void);
public:
......@@ -79,10 +87,14 @@ public:
const T& at(const qlonglong& index);
public:
dtkDistributedLocalIterator<T> *localIterator(void);
dtkDistributedLocalIterator<T>& localIterator(void);
private:
dtkDistributedMapper *m_mapper;
dtkDistributedCommunicator *m_comm;
private:
QVector<T> m_array;
QVector<T> m_array;
dtkDistributedLocalIterator<T> *m_iterator;
public:
......
......@@ -3,9 +3,9 @@
* Author: Thibaud Kloczko
* Created: Tue Feb 5 14:12:49 2013 (+0100)
* Version:
* Last-Updated: Tue Feb 5 14:13:21 2013 (+0100)
* By: Julien Wintz
* Update #: 5
* Last-Updated: 2013 Thu Feb 7 15:07:57 (+0100)
* By: Thibaud Kloczko
* Update #: 56
*/
/* Change Log:
......@@ -14,11 +14,13 @@
#pragma once
#include "dtkDistributedMapper.h"
// /////////////////////////////////////////////////////////////////
// dtkDistributedLocalIterator implementation
// /////////////////////////////////////////////////////////////////
template<typename T> dtkDistributedLocalIterator<T>::dtkDistributedLocalIterator(dtkDistributedContainer<T> *container) : c(container), i(c->m_array.constBegin()), m_index(0)
template<typename T> dtkDistributedLocalIterator<T>::dtkDistributedLocalIterator(dtkDistributedContainer<T>& container) : c(container), i(c.m_array.constBegin()), m_index(0)
{
};
......@@ -30,24 +32,24 @@ template<typename T> dtkDistributedLocalIterator<T>::~dtkDistributedLocalIterato
template <typename T> void dtkDistributedLocalIterator<T>::toBack(void)
{
i = c->m_array.constEnd();
m_index = c->m_array.size()-1;
i = c.m_array.constEnd();
m_index = c.m_array.size()-1;
};
template <typename T> void dtkDistributedLocalIterator<T>::toFront(void)
{
i = c->m_array.constBegin();
i = c.m_array.constBegin();
m_index = 0;
};
template <typename T> bool dtkDistributedLocalIterator<T>::hasNext(void)
{
return (i != c->m_array.constEnd());
return (i != c.m_array.constEnd());
};
template <typename T> bool dtkDistributedLocalIterator<T>::hasPrevious(void)
{
return (i != c->m_array.constBegin());
return (i != c.m_array.constBegin());
};
template <typename T> const T& dtkDistributedLocalIterator<T>::next(void)
......@@ -89,17 +91,22 @@ template <typename T> qlonglong dtkDistributedLocalIterator<T>::globalIndex(void
// dtkDistributedContainer implementation
// /////////////////////////////////////////////////////////////////
template<typename T> dtkDistributedContainer<T>::dtkDistributedContainer(void)
template<typename T> dtkDistributedContainer<T>::dtkDistributedContainer(void) : m_iterator(0), m_mapper(new dtkDistributedMapper), m_comm(0)
{
m_iterator = NULL;
};
template<typename T> dtkDistributedContainer<T>::~dtkDistributedContainer(void)
template<typename T> dtkDistributedContainer<T>::dtkDistributedContainer(const qlonglong& size, dtkDistributedCommunicator *communicator) : m_iterator(0), m_mapper(new dtkDistributedMapper), m_comm(communicator)
{
if (m_iterator)
delete m_iterator;
this->resize(size);
//m_mapper->setMapping(size, m_comm->size());
};
m_iterator = NULL;
template<typename T> dtkDistributedContainer<T>::~dtkDistributedContainer(void)
{
delete m_iterator;
delete m_mapper;
};
template<typename T> void dtkDistributedContainer<T>::resize(const qlonglong& size)
......@@ -122,10 +129,10 @@ template <typename T> const T&dtkDistributedContainer<T>:: at(const qlonglong& i
return m_array.at(index);
};
template <typename T> dtkDistributedLocalIterator<T> *dtkDistributedContainer<T>::localIterator(void)
template <typename T> dtkDistributedLocalIterator<T>& dtkDistributedContainer<T>::localIterator(void)
{
if (!m_iterator)
m_iterator = new dtkDistributedLocalIterator<T>(this);
m_iterator = new dtkDistributedLocalIterator<T>(*this);
return m_iterator;
return *m_iterator;
};
/* dtkDistributedMapper.cpp ---
*
* Author: Thibaud Kloczko
* Created: 2013 Thu Feb 7 10:55:57 (+0100)
*/
/* Commentary:
*
*/
/* Change log:
*
*/
#include "dtkDistributedMapper.h"
#include <QtCore>
// /////////////////////////////////////////////////////////////////
// dtkDistributedMapperPrivate interface
// /////////////////////////////////////////////////////////////////
class dtkDistributedMapperPrivate
{
public:
dtkDistributedMapperPrivate(void) {;}
~dtkDistributedMapperPrivate(void) {;}
public:
void setMapping(const qlonglong& id_number, const qlonglong& pu_number);
public:
qlonglong localToGlobal(const qlonglong& local_id, const qlonglong& pu_id) const;
qlonglong globalToLocal(const qlonglong& global_id) const;
qlonglong count(const qlonglong& pu_id) const;
qlonglong owner(const qlonglong& global_id) const;
QVector<qlonglong> readers(const qlonglong& global_id) const;
public:
qlonglong id_count;
qlonglong pu_count;
qlonglong step;
QVector<qlonglong> map;
};
// /////////////////////////////////////////////////////////////////
// dtkDistributedMapperPrivate implementation
// /////////////////////////////////////////////////////////////////
void dtkDistributedMapperPrivate::setMapping(const qlonglong& id_number, const qlonglong& pu_number)
{
this->id_count = id_number;
this->pu_count = pu_number;
this->map.reserve(this->pu_count);
this->step = qRound(this->id_count / (1. * this->pu_count));
qlonglong remain = this->id_count - this->step * (this->pu_count - 1);
for (qlonglong i = 0; i < this->pu_count - 1; ++i)
this->map << i * this->step;
this->map << (this->map.at(this->pu_count - 2) + remain);
}
qlonglong dtkDistributedMapperPrivate::localToGlobal(const qlonglong& local_id, const qlonglong& pu_id) const
{
return ( local_id + this->map.at(pu_id) );
}
qlonglong dtkDistributedMapperPrivate::globalToLocal(const qlonglong& global_id) const
{
qlonglong pu_id = this->owner(global_id);
return ( global_id - this->map.at(pu_id) );
}
qlonglong dtkDistributedMapperPrivate::count(const qlonglong& pu_id) const
{
if ( pu_id != (this->pu_count - 1) )
return ( this->map.at(pu_id + 1) - this->map.at(pu_id) );
else
return ( this->id_count - this->map.at(pu_id) );
}
qlonglong dtkDistributedMapperPrivate::owner(const qlonglong& global_id) const
{
return qFloor(global_id / this->step);
}
// /////////////////////////////////////////////////////////////////
// dtkDistributedMapper implementation
// /////////////////////////////////////////////////////////////////
dtkDistributedMapper::dtkDistributedMapper(void) : QObject(), d(new dtkDistributedMapperPrivate)
{
}
dtkDistributedMapper::~dtkDistributedMapper(void)
{
delete d;
}
void dtkDistributedMapper::setMapping(const qlonglong& id_count, const qlonglong& pu_count)
{
d->setMapping(id_count, pu_count);
}
qlonglong dtkDistributedMapper::localToGlobal(const qlonglong& local_id, const qlonglong& pu_id) const
{
return d->localToGlobal(local_id, pu_id);
}
qlonglong dtkDistributedMapper::globalToLocal(const qlonglong& global_id) const
{
return d->globalToLocal(global_id);
}
qlonglong dtkDistributedMapper::count(const qlonglong& pu_id) const
{
return d->count(pu_id);
}
qlonglong dtkDistributedMapper::owner(const qlonglong& global_id) const
{
return d->owner(global_id);
}
/* dtkDistributedMapper.h ---
*
* Author: Thibaud Kloczko
* Created: 2013 Thu Feb 7 10:40:37 (+0100)
*/
/* Commentary:
*
*/
/* Change log:
*
*/
#pragma once
#include <QtCore/QObject>
template <typename T> class QVector;
class dtkDistributedMapperPrivate;
// /////////////////////////////////////////////////////////////////
// dtkDistributedMapper interface
// /////////////////////////////////////////////////////////////////
class dtkDistributedMapper : public QObject
{
Q_OBJECT
public:
dtkDistributedMapper(void);
~dtkDistributedMapper(void);
public:
void setMapping(const qlonglong& id_count, const qlonglong& pu_count);
public:
qlonglong localToGlobal(const qlonglong& local_id, const qlonglong& pu_id) const;
qlonglong globalToLocal(const qlonglong& global_id) const;
qlonglong count(const qlonglong& pu_id) const;
qlonglong owner(const qlonglong& global_id) const;
private:
dtkDistributedMapperPrivate *d;
};
......@@ -25,9 +25,9 @@ void dtkDistributedContainerTestCase::init(void)
}
void dtkDistributedContainerTestCase::test(void)
void dtkDistributedContainerTestCase::test1(void)
{
qlonglong N = 1000;
qlonglong N = 1001;
qlonglong sum = 0;
for (qlonglong i = 0; i < N; ++i)
......@@ -38,19 +38,50 @@ void dtkDistributedContainerTestCase::test(void)
QVERIFY(N == c.size());
dtkDistributedLocalIterator<qlonglong> *it = c.localIterator();
dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
while(it->hasNext()) {
c.set(it->globalIndex(), it->localIndex());
it->next();
while(it.hasNext()) {
c.set(it.globalIndex(), it.localIndex());
it.next();
}
qlonglong check_sum = 0;
it->toFront();
while(it->hasNext()) {
check_sum += c.at(it->localIndex());
it->next();
it.toFront();
while(it.hasNext()) {
check_sum += c.at(it.localIndex());
it.next();
}
QVERIFY(sum == check_sum);
}
void dtkDistributedContainerTestCase::test2(void)
{
qlonglong N = 1001;
qlonglong W = 9;
qlonglong sum = 0;
for (qlonglong i = 0; i < N; ++i)
sum += i;
dtkDistributedContainer<qlonglong> c;// = dtkDistributedContainer<qlonglong>(N, W);
QVERIFY(N == c.size());
dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
while(it.hasNext()) {
c.set(it.globalIndex(), it.localIndex());
it.next();
}
qlonglong check_sum = 0;
it.toFront();
while(it.hasNext()) {
check_sum += c.at(it.localIndex());
it.next();
}
QVERIFY(sum == check_sum);
......
......@@ -25,7 +25,8 @@ private slots:
void init(void);
private slots:
void test(void);
void test1(void);
void test2(void);
private slots:
void cleanupTestCase(void);
......
Markdown is supported
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