Page MenuHomec4science

duo_distributed_vector.hh
No OneTemporary

File Metadata

Created
Tue, Nov 12, 16:35

duo_distributed_vector.hh

/**
* @file duo_distributed_vector.hh
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Mon Oct 28 19:23:14 2013
*
* @brief Migration safe representation of array of references
*
* @section LICENSE
*
* Copyright INRIA and CEA
*
* The LibMultiScale is a C++ parallel framework for the multiscale
* coupling methods dedicated to material simulations. This framework
* provides an API which makes it possible to program coupled simulations
* and integration of already existing codes.
*
* This Project was initiated in a collaboration between INRIA Futurs Bordeaux
* within ScAlApplix team and CEA/DPTA Ile de France.
* The project is now continued at the Ecole Polytechnique Fédérale de Lausanne
* within the LSMS/ENAC laboratory.
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*
*/
#ifndef __DUO_DISTRIBUTED_VECTOR_H__
#define __DUO_DISTRIBUTED_VECTOR_H__
/* -------------------------------------------------------------------------- */
#include <list>
/* -------------------------------------------------------------------------- */
#include "ref_subset.hh"
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
/* -------------------------------------------------------------------------- */
/** Class duodistributedvector that allow redistribution of vectors,
with possiblity to adapt the migration procedures (creating holes
in the commuincation scheme) and eventually unfragmenting it */
class DuoDistributedVector : public AttachedObject {
public:
/* ------------------------------------------------------------------------ */
/* Typedefs */
/* ------------------------------------------------------------------------ */
typedef std::map<UInt,std::vector<UInt> > MapUIntToUIntList;
typedef std::map<UInt,UInt> MapUIntToUInt;
typedef std::vector<std::pair<UInt,UInt> > PairUIntList;
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
~DuoDistributedVector();
DuoDistributedVector(UInt lsize,UInt tsize, const std::string & my_name);
/* ------------------------------------------------------------------------ */
/* Methods (low level) */
/* ------------------------------------------------------------------------ */
//! add an index for the communication to a given proc
inline void addIndex(UInt index,UInt proc);
//! remove an index for the communication : return associated duo proc
inline UInt removeIndex(UInt index);
//! find the real index given proc and com_index
inline UInt findRealIndex(UInt com_index,UInt duo_proc);
//! do set the duo proc of a given index (return 1 if success 0 otherwise)
inline UInt setDuoProc(UInt index, UInt proc);
/* ------------------------------------------------------------------------ */
/* Methods (high level) */
/* ------------------------------------------------------------------------ */
//! update both sides of the migrated operations
UInt synchronizeMigration(CommGroup groupAtomic,CommGroup groupFE);
//! function used to synchronize vectors (by summing them)
void synchronizeVectorBySum(const std::string & name_vec,std::vector<Real> & vec,
CommGroup group1,CommGroup group2,UInt stride=1);
//! function used to redistribute a vector
void distributeVector(const std::string & name_vec,std::vector<Real> & vec,
CommGroup group1,CommGroup group2,UInt stride=1);
//! print to file the current redistribution scheme (with positions info)
void print(const std::string & prefix);
//! print to screen summary of com scheme
void printSummary();
//! set the size of the duo vector
void setSize(UInt lsize,UInt tsize);
//! clear the duo (for reuse)
void clear();
private:
//! update the communication list for the sent indexes
void updateSent();
//! update the communication list for the recv indexes
void updateRecv();
//! update the communication list for the moved indexes
void updateMoved();
public:
/* ------------------------------------------------------------------------ */
/* Communication Methods */
/* ------------------------------------------------------------------------ */
inline void packData(UInt index,PackBuffer & buffer,UInt toproc);
inline void unpackData(UInt index,PackBuffer & buffer,UInt fromproc);
inline void moveAttachedValues(UInt i_src,UInt i_dest);
inline void resize(UInt sz);
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
private:
//! map proc to communication indexes list
MapUIntToUIntList com_list;
//! map index to communication indexes
MapUIntToUInt duo_index_map;
//! map index to duo proc
MapUIntToUInt duo_proc_map;
//! list of indexes sent by migration
MapUIntToUIntList sent;
//! list of processors indexes were sent to
MapUIntToUIntList sent_procs;
//! list of indexes received by migration
MapUIntToUIntList received;
//! moved indexes in the container
PairUIntList moved;
//! total size of vectors that can be redistributed
UInt totalsize;
//! actual local size of vecotr that can be redistributed
UInt localsize;
//! flag used to say that migration have occured
bool something_changed;
//! descriptive name
std::string name;
};
/* -------------------------------------------------------------------------- */
inline UInt DuoDistributedVector::setDuoProc(UInt index, UInt proc){
if (duo_index_map.count(index)){
DUMP("index " << index << " was already associated with duo proc "
<< duo_proc_map[index],DBG_INFO);
return 0;
}
addIndex(index,proc);
return 1;
};
/* -------------------------------------------------------------------------- */
inline UInt DuoDistributedVector::findRealIndex(UInt com_index,UInt duo_proc){
LM_ASSERT(com_list.count(duo_proc),"processor " << duo_proc
<< " not part of redistribution scheme: abort");
LM_ASSERT(com_index < com_list[duo_proc].size(),
"invalid com_index: abort "
<< com_index << " "
<< com_list[duo_proc].size());
return com_list[duo_proc][com_index];
}
/* -------------------------------------------------------------------------- */
inline UInt DuoDistributedVector::removeIndex(UInt index){
LM_ASSERT(duo_index_map.count(index),"index not present in communication tab");
UInt com_index = duo_index_map[index];
UInt duo_proc = duo_proc_map[index];
duo_index_map.erase(index);
duo_proc_map.erase(index);
std::vector<UInt> & coms = com_list[duo_proc];
UInt last_index = coms.back();
coms.pop_back();
//put the last com to the position that have been removed
DUMP("remove index " << index
<< " com index " << com_index
<< " from proc " << duo_proc,DBG_DETAIL);
if (com_index < coms.size()){
coms[com_index] = last_index;
duo_index_map[last_index] = com_index;
DUMP("displace index " << last_index
<< " from com index " << coms.size()
<< " to com index " << com_index
<< " for proc " << duo_proc
<< " duo is " << name,DBG_DETAIL);
}
return duo_proc;
}
/* -------------------------------------------------------------------------- */
inline void DuoDistributedVector::addIndex(UInt index,UInt duo_proc){
LM_ASSERT(!duo_index_map.count(index),
"index " << index << " already present in communication tab"
<< " of proc " << duo_proc_map[index]);
std::vector<UInt> & coms = com_list[duo_proc];
UInt com_index = coms.size();
coms.push_back(index);
duo_proc_map[index] = duo_proc;
duo_index_map[index] = com_index;
DUMP("add index " << index
<< " com index " << com_index
<< " from proc " << duo_proc
<< " duo is " << name,DBG_DETAIL);
}
/* -------------------------------------------------------------------------- */
inline void DuoDistributedVector::packData(UInt index,PackBuffer & buffer,
UInt proc){
LM_ASSERT(duo_index_map.count(index),"not found index : abort");
LM_ASSERT(duo_proc_map.count(index),"not found index : abort");
UInt fem_proc = duo_proc_map[index];
buffer << fem_proc;
sent[fem_proc].push_back(index);
sent_procs[fem_proc].push_back(proc);
DUMP("register index " << index << " to sent_list"
<< " from proc " << fem_proc
<< " giving new owner as " << proc
<< " duo is " << name,DBG_DETAIL);
}
/* -------------------------------------------------------------------------- */
inline void DuoDistributedVector::unpackData(UInt index,PackBuffer & buffer,
UInt proc){
UInt fem_proc;
buffer >> fem_proc;
received[fem_proc].push_back(index);
DUMP("register index " << index << " to receive_list"
<< " from proc " << fem_proc
<< " giving old owner as " << proc
<< " duo is " << name,DBG_DETAIL);
}
/* -------------------------------------------------------------------------- */
inline void DuoDistributedVector::moveAttachedValues(UInt i_src,UInt i_dest){
std::pair<UInt,UInt> p(i_src,i_dest);
moved.push_back(p);
}
/* -------------------------------------------------------------------------- */
inline void DuoDistributedVector::resize(UInt sz){
localsize = sz;
}
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__
#endif /* __DUO_DISTRIBUTED_VECTOR_H__ */

Event Timeline