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

Local and global iterator work for sequential case.

parent 1006d34f
......@@ -3,9 +3,9 @@
* Author: Thibaud Kloczko
* Created: Tue Feb 5 14:08:23 2013 (+0100)
* Version:
* Last-Updated: 2013 Thu Feb 7 17:13:21 (+0100)
* Last-Updated: 2013 Fri Feb 8 16:10:23 (+0100)
* By: Thibaud Kloczko
* Update #: 56
* Update #: 122
*/
/* Change Log:
......@@ -14,67 +14,8 @@
#pragma once
#include <QtCore/QVector>
// ///////////////////////////////////////////////////////////////////
// dtkDistributedContainer
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedContainer;
// ///////////////////////////////////////////////////////////////////
// dtkDistributedLocalIterator
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedGlobalIterator
{
public:
qlonglong globalIndex(void) const {return 0;}
public:
bool hasNext(void) {return false;}
public:
const T& next(void) {return *new T();}
};
// ///////////////////////////////////////////////////////////////////
// dtkDistributedLocalIterator
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedLocalIterator
{
public:
dtkDistributedLocalIterator(dtkDistributedContainer<T>& container);
~dtkDistributedLocalIterator(void);
public:
void toBack(void);
void toFront(void);
public:
bool hasNext(void);
bool hasPrevious(void);
public:
const T& next(void);
const T& previous(void);
public:
const T& peekNext(void);
const T& peekPrevious(void);
public:
qlonglong localIndex(void) const;
qlonglong globalIndex(void) const;
private:
dtkDistributedContainer<T>& c;
typedef typename QVector<T>::const_iterator const_iterator;
const_iterator i;
qlonglong m_index;
};
template<typename T> class dtkDistributedLocalIterator;
template<typename T> class dtkDistributedGlobalIterator;
// /////////////////////////////////////////////////////////////////
// dtkDistributedContainer interface
......@@ -86,7 +27,6 @@ class dtkDistributedMapper;
template<typename T> class dtkDistributedContainer
{
public:
dtkDistributedContainer(void);
dtkDistributedContainer(const qlonglong& size, dtkDistributedCommunicator *communicator);
public:
......@@ -96,8 +36,6 @@ private:
void allocate(void);
public:
void resize(const qlonglong& size);
qlonglong size(void) const;
public:
......@@ -110,8 +48,8 @@ public:
const T& localAt(const qlonglong& local_id);
public:
dtkDistributedLocalIterator<T>& localIterator(void);
dtkDistributedGlobalIterator<T>& globalIterator(void) { return *new dtkDistributedGlobalIterator<T>();}
dtkDistributedLocalIterator<T>& localIterator(void);
dtkDistributedGlobalIterator<T>& globalIterator(void);
private:
dtkDistributedMapper *m_mapper;
......@@ -119,15 +57,70 @@ private:
private:
T *m_buffer;
qlonglong m_buffer_size;
qlonglong m_buffer_id;
qlonglong m_global_size;
T *m_temp;
QVector<T> m_array;
dtkDistributedLocalIterator<T> *m_iterator;
public:
dtkDistributedLocalIterator<T> *m_loc_it;
dtkDistributedGlobalIterator<T> *m_glob_it;
public:
friend class dtkDistributedLocalIterator<T>;
friend class dtkDistributedGlobalIterator<T>;
};
// ///////////////////////////////////////////////////////////////////
// dtkDistributedLocalIterator
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedGlobalIterator
{
dtkDistributedContainer<T>& c;
qlonglong gid;
public:
dtkDistributedGlobalIterator(dtkDistributedContainer<T>& container) : c(container), gid(0) {;}
~dtkDistributedGlobalIterator(void) {;}
public:
void toBack(void) { gid = c.m_global_size; }
void toFront(void) { gid = 0; }
bool hasNext(void) { return ( gid < c.m_global_size ); }
bool hasPrevious(void) { return ( gid > 0 ); }
T next(void) { return c.at(gid++); }
T previous(void) { return c.at(--gid); }
T peekNext(void) { return c.at(gid); }
T peekPrevious(void) { return c.at(gid-1); }
qlonglong globalIndex(void) { return gid; }
};
// ///////////////////////////////////////////////////////////////////
// dtkDistributedLocalIterator
// ///////////////////////////////////////////////////////////////////
template<typename T> class dtkDistributedLocalIterator
{
dtkDistributedContainer<T>& c;
const T *i;
public:
dtkDistributedLocalIterator(dtkDistributedContainer<T>& container) : c(container), i(c.m_buffer) {;}
~dtkDistributedLocalIterator(void) {;}
public:
friend class dtkDistributedLocalIterator<T>;
void toBack(void) { i = c.m_buffer + c.m_buffer_size; }
void toFront(void) { i = c.m_buffer; }
bool hasNext(void) { return ( i != (c.m_buffer + c.m_buffer_size) ); }
bool hasPrevious(void) { return ( i != c.m_buffer ); }
const T& next(void) { return *(i++); }
const T& previous(void) { return *(--i); }
const T& peekNext(void) { return *i; }
const T& peekPrevious(void) { const T *p = i; return *(--p); }
qlonglong localIndex(void) { return ( i - c.m_buffer ); }
};
// ///////////////////////////////////////////////////////////////////
......
......@@ -3,9 +3,9 @@
* Author: Thibaud Kloczko
* Created: Tue Feb 5 14:12:49 2013 (+0100)
* Version:
* Last-Updated: 2013 Thu Feb 7 17:05:30 (+0100)
* Last-Updated: 2013 Fri Feb 8 15:56:49 (+0100)
* By: Thibaud Kloczko
* Update #: 139
* Update #: 174
*/
/* Change Log:
......@@ -17,126 +17,45 @@
#include "dtkDistributedMapper.h"
#include "dtkDistributedCommunicator.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(void)
{
};
template <typename T> void dtkDistributedLocalIterator<T>::toBack(void)
{
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();
m_index = 0;
};
template <typename T> bool dtkDistributedLocalIterator<T>::hasNext(void)
{
return (i != c.m_array.constEnd());
};
template <typename T> bool dtkDistributedLocalIterator<T>::hasPrevious(void)
{
return (i != c.m_array.constBegin());
};
template <typename T> const T& dtkDistributedLocalIterator<T>::next(void)
{
++m_index;
return (*i++);
};
template <typename T> const T& dtkDistributedLocalIterator<T>::previous(void)
{
--m_index;
return *(--i);
};
template <typename T> const T& dtkDistributedLocalIterator<T>::peekNext(void)
{
return *i;
};
template <typename T> const T& dtkDistributedLocalIterator<T>::peekPrevious(void)
{
const_iterator p = i;
return (*--p);
};
template <typename T> qlonglong dtkDistributedLocalIterator<T>::localIndex(void) const
{
return m_index;
};
template <typename T> qlonglong dtkDistributedLocalIterator<T>::globalIndex(void) const
{
return m_index;
};
// /////////////////////////////////////////////////////////////////
// dtkDistributedContainer implementation
// /////////////////////////////////////////////////////////////////
template<typename T> dtkDistributedContainer<T>::dtkDistributedContainer(void) : m_buffer(0), m_iterator(0), m_mapper(new dtkDistributedMapper), m_comm(0), m_temp(0)
template<typename T> dtkDistributedContainer<T>::dtkDistributedContainer(const qlonglong& size, dtkDistributedCommunicator *communicator) : m_buffer(0), m_temp(0), m_loc_it(0), m_glob_it(0), m_global_size(size), m_mapper(new dtkDistributedMapper), m_comm(communicator)
{
};
template<typename T> dtkDistributedContainer<T>::dtkDistributedContainer(const qlonglong& size, dtkDistributedCommunicator *communicator) : m_buffer(0), m_temp(0), m_iterator(0), m_mapper(new dtkDistributedMapper), m_comm(communicator)
{
this->resize(size);
m_comm->initialize();
m_mapper->setMapping(size, m_comm->size());
m_mapper->setMapping(m_global_size, m_comm->size());
this->allocate();
};
template<typename T> dtkDistributedContainer<T>::~dtkDistributedContainer(void)
{
delete m_iterator;
if (m_loc_it)
delete m_loc_it;
if (m_glob_it)
delete m_glob_it;
delete m_mapper;
};
template<typename T> void dtkDistributedContainer<T>::allocate(void)
{
qDebug() << __func__;
m_buffer = new T[m_mapper->count(m_comm->rank())];
m_buffer_size = m_mapper->count(m_comm->rank());
m_buffer = new T[m_buffer_size];
// m_buffer_id = m_comm->allocate(m_mapper->count(m_comm->rank()), sizeof(T), m_buffer);
};
template<typename T> void dtkDistributedContainer<T>::resize(const qlonglong& size)
{
m_mapper->setMapping(size, 1);
m_buffer = new T[size];
m_array.resize(size);
};
template <typename T> qlonglong dtkDistributedContainer<T>::size(void) const
{
return m_array.size();
return m_global_size;
};
template <typename T> void dtkDistributedContainer<T>::set(const qlonglong& global_id, const T& value)
{
m_buffer[m_mapper->globalToLocal(global_id)] = value;
//m_array.replace(global_id, value);
};
template <typename T> void dtkDistributedContainer<T>::setLocal(const qlonglong& local_id, const T& value)
......@@ -171,8 +90,16 @@ template <typename T> const T& dtkDistributedContainer<T>::localAt(const qlonglo
template <typename T> dtkDistributedLocalIterator<T>& dtkDistributedContainer<T>::localIterator(void)
{
if (!m_iterator)
m_iterator = new dtkDistributedLocalIterator<T>(*this);
if (!m_loc_it)
m_loc_it = new dtkDistributedLocalIterator<T>(*this);
return *m_loc_it;
};
template <typename T> dtkDistributedGlobalIterator<T>& dtkDistributedContainer<T>::globalIterator(void)
{
if (!m_glob_it)
m_glob_it = new dtkDistributedGlobalIterator<T>(*this);
return *m_iterator;
return *m_glob_it;
};
......@@ -61,6 +61,7 @@ void dtkDistributedMapperPrivate::setMapping(const qlonglong& id_number, const q
if (this->pu_count == 1) {
this->map << 0;
this->step = this->id_count;
} else {
......
......@@ -26,41 +26,46 @@ void dtkDistributedContainerTestCase::init(void)
}
void dtkDistributedContainerTestCase::test1(void)
void dtkDistributedContainerTestCase::testLocal(void)
{
// qlonglong N = 1001;
qlonglong N = 11;
// qlonglong sum = 0;
// for (qlonglong i = 0; i < N; ++i)
// sum += i;
dtkDistributedCommunicator comm;
// dtkDistributedContainer<qlonglong> c;
// c.resize(N);
qlonglong sum = 0;
for (qlonglong i = 0; i < N; ++i)
sum += i;
// QVERIFY(N == c.size());
dtkDistributedContainer<qlonglong> c = dtkDistributedContainer<qlonglong>(N, &comm);
// dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
QVERIFY(N == c.size());
// while(it.hasNext()) {
// c.set(it.globalIndex(), it.localIndex());
// it.next();
// }
dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
// qlonglong check_sum = 0;
while(it.hasNext()) {
c.setLocal(it.localIndex(), it.localIndex());
it.next();
}
// it.toFront();
// while(it.hasNext()) {
// check_sum += c.at(it.localIndex());
// it.next();
// }
qlonglong check_sum = 0;
it.toFront();
while(it.hasNext()) {
check_sum += c.localAt(it.localIndex());
it.next();
}
// QVERIFY(sum == check_sum);
QVERIFY(sum == check_sum);
}
void dtkDistributedContainerTestCase::test2(void)
void dtkDistributedContainerTestCase::testGlobal(void)
{
qlonglong N = 1001;
qlonglong sum = 0;
for (qlonglong i = 0; i < N; ++i)
sum += i;
dtkDistributedCommunicator comm;
dtkDistributedContainer<qlonglong> c = dtkDistributedContainer<qlonglong>(N, &comm);
......@@ -74,12 +79,44 @@ void dtkDistributedContainerTestCase::test2(void)
g_it.next();
}
dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
qlonglong check_sum = 0;
while(it.hasNext()) {
c.setLocal(it.localIndex(), 2 * c.localAt(it.localIndex()));
it.next();
g_it.toFront();
while(g_it.hasNext()) {
check_sum += c.at(g_it.globalIndex());
g_it.next();
}
QVERIFY(sum == check_sum);
}
void dtkDistributedContainerTestCase::testGlobalLocal(void)
{
qlonglong N = 1001;
qlonglong sum = 0;
for (qlonglong i = 0; i < N; ++i)
sum += i;
dtkDistributedCommunicator comm;
dtkDistributedContainer<qlonglong> c = dtkDistributedContainer<qlonglong>(N, &comm);
QVERIFY(N == c.size());
dtkDistributedGlobalIterator<qlonglong>& g_it = c.globalIterator();
while(g_it.hasNext()) {
c.set(g_it.globalIndex(), g_it.globalIndex());
g_it.next();
}
// dtkDistributedLocalIterator<qlonglong>& it = c.localIterator();
// while(it.hasNext()) {
// c.setLocal(it.localIndex(), 2 * c.localAt(it.localIndex()));
// it.next();
// }
}
void dtkDistributedContainerTestCase::cleanupTestCase(void)
......
......@@ -25,8 +25,9 @@ private slots:
void init(void);
private slots:
void test1(void);
void test2(void);
void testLocal(void);
void testGlobal(void);
void testGlobalLocal(void);
private slots:
void cleanupTestCase(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