From ff5df75297e1e10cd91cba869aff82818af19208 Mon Sep 17 00:00:00 2001 From: lasnier Date: Fri, 11 Jan 2013 00:20:09 +0100 Subject: [PATCH] adding custom topologies, src and test --- smp/src/CMakeLists.txt | 2 + smp/src/smp.h | 3 + smp/src/topology/customBooleanTopology.cpp | 119 ++++++++++++++++ smp/src/topology/customBooleanTopology.h | 77 +++++++++++ smp/src/topology/customStochasticTopology.cpp | 129 ++++++++++++++++++ smp/src/topology/customStochasticTopology.h | 78 +++++++++++ smp/test/CMakeLists.txt | 13 ++ smp/test/data-topo-bool | 4 + smp/test/data-topo-stoch | 3 + smp/test/t-smpCustomTopo.cpp | 118 ++++++++++++++++ smp/test/t-smpTopo.cpp | 1 + 11 files changed, 547 insertions(+) create mode 100644 smp/src/topology/customBooleanTopology.cpp create mode 100644 smp/src/topology/customBooleanTopology.h create mode 100644 smp/src/topology/customStochasticTopology.cpp create mode 100644 smp/src/topology/customStochasticTopology.h create mode 100644 smp/test/data-topo-bool create mode 100644 smp/test/data-topo-stoch create mode 100644 smp/test/t-smpCustomTopo.cpp diff --git a/smp/src/CMakeLists.txt b/smp/src/CMakeLists.txt index b658067c0..1135119a0 100644 --- a/smp/src/CMakeLists.txt +++ b/smp/src/CMakeLists.txt @@ -28,6 +28,8 @@ set (SMP_FILE topology/ring.cpp topology/hypercubic.cpp topology/mesh.cpp + topology/customBooleanTopology.cpp + topology/customStochasticTopology.cpp notifier.cpp islandModelWrapper.h ) diff --git a/smp/src/smp.h b/smp/src/smp.h index 3840f67d6..181cfc193 100644 --- a/smp/src/smp.h +++ b/smp/src/smp.h @@ -49,5 +49,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include #include #include +#include +#include + #endif diff --git a/smp/src/topology/customBooleanTopology.cpp b/smp/src/topology/customBooleanTopology.cpp new file mode 100644 index 000000000..8c066e0dc --- /dev/null +++ b/smp/src/topology/customBooleanTopology.cpp @@ -0,0 +1,119 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012 + +Alexandre Quemy, Thibault Lasnier - INSA Rouen + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +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 license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include + +paradiseo::smp::CustomBooleanTopology::CustomBooleanTopology(std::string filename) +{ + std :: ifstream f(filename); + if (f) + { + int temp; + unsigned size; + bool isNeighbor, isFirst = true; + std::string line; + std::vector lineVector; + + while(getline(f, line)) + { + lineVector.clear(); + + //line contains a line of text from the file + std::istringstream tokenizer(line); + std::string token; + + while(tokenizer>> temp >> std::skipws) + { + //white spaces are skipped, and the integer is converted to boolean, to be stored + isNeighbor = (bool) temp; + lineVector.push_back(isNeighbor); + } + + //if this is the first line, we must initiate the variable size + if(isFirst) + { + size = lineVector.size(); + isFirst=false; + } + + //for each vector non empty, if the size is not equal to the others, error + if(lineVector.size() != size && !lineVector.empty()) + throw std::runtime_error("Mistake in the topology, line "+ std::to_string(_matrix.size()+1) ); + + if(!lineVector.empty()) + _matrix.push_back(lineVector); + } + + //for each vector, verify if the size is equal to the size of the final matrix + for(auto& line : _matrix) + if(line.size() != _matrix.size()) + throw std::runtime_error("Mistake in the topology, matrix is not square" ); + + f.close () ; + } + else + { + throw std::runtime_error("Error occured while reading the topology file " + filename); + } +} + +std::vector paradiseo::smp::CustomBooleanTopology::getIdNeighbors(unsigned idNode) const +{ + std::vector neighbors; + for(unsigned j=0; j<_matrix.size();j++) + if(_matrix[idNode][j]) + neighbors.push_back(j); + + return neighbors; +} + +void paradiseo::smp::CustomBooleanTopology::construct(unsigned nbNode) +{ + assert(nbNode==_matrix.size()); +} + +void paradiseo::smp::CustomBooleanTopology::isolateNode(unsigned idNode) +{ + for(int i=0;i<_matrix.size();i++) + { + //Line of idNode to false : no connection FROM this node + _matrix[idNode][i]=false; + + //Column of idNode to false : no connection TO this node + _matrix[i][idNode]=false; + } +} diff --git a/smp/src/topology/customBooleanTopology.h b/smp/src/topology/customBooleanTopology.h new file mode 100644 index 000000000..6fc9b4a49 --- /dev/null +++ b/smp/src/topology/customBooleanTopology.h @@ -0,0 +1,77 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012 + +Alexandre Quemy, Thibault Lasnier - INSA Rouen + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +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 license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef CUST_BOOL_TOPO_H_ +#define CUST_BOOL_TOPO_H_ + +#include +#include +#include + +namespace paradiseo +{ +namespace smp +{ + +class CustomBooleanTopology : public AbstractTopology +{ +public : + /** + *Constructor for a custom topology from a file + *@param filename Name of the file, must be in the same directory as the executable + */ + CustomBooleanTopology(std::string filename); + + /** + * Inherited from AbstractTopology + * @see smp::topology::AbstractTopology::getIdNeighbors + */ + std::vector getIdNeighbors(unsigned idNode) const; + + /** + * Inherited from AbstractTopology : a custom topology cannot be reconstructed in a different way, so the method throws an error if nbNode doesn't match with the actual number of nodes. + * @param nbNode number of nodes for the topology + */ + void construct(unsigned nbNode); + + /** + *Inherited from AbstractTopology : changes the topology : removes any connection from/to the given node. + *@param idNode index of the node to be isolated + */ + void isolateNode(unsigned idNode); + +private : + std::vector> _matrix; +}; + +} + +} + +#endif diff --git a/smp/src/topology/customStochasticTopology.cpp b/smp/src/topology/customStochasticTopology.cpp new file mode 100644 index 000000000..ee6d43db3 --- /dev/null +++ b/smp/src/topology/customStochasticTopology.cpp @@ -0,0 +1,129 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012 + +Alexandre Quemy, Thibault Lasnier - INSA Rouen + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +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 license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string filename) +{ + std :: ifstream f(filename); + if (f) + { + double temp; + unsigned size; + double isNeighbor, isFirst = true; + std::string line; + std::vector lineVector; + + while(getline(f, line)) + { + lineVector.clear(); + + //line contains a line of text from the file + std::istringstream tokenizer(line); + std::string token; + + while(tokenizer>> temp >> std::skipws) + { + //white spaces are skipped, and the integer is converted to boolean, to be stored + + if(temp<0) + isNeighbor = 0; + else if(temp>1) + isNeighbor = 1; + else + isNeighbor = (double) temp; + lineVector.push_back(isNeighbor); + } + + //if this is the first line, we must initiate the variable size + if(isFirst) + { + size = lineVector.size(); + isFirst=false; + } + + //for each vector non empty, if the size is not equal to the others, error + if(lineVector.size() != size && !lineVector.empty()) + throw std::runtime_error("Mistake in the topology, line "+ std::to_string(_matrix.size()+1) ); + + if(!lineVector.empty()) + _matrix.push_back(lineVector); + } + + //for each vector, verify if the size is equal to the size of the final matrix + for(auto& line : _matrix) + if(line.size() != _matrix.size()) + throw std::runtime_error("Mistake in the topology, matrix is not square" ); + + f.close () ; + } + else + { + throw std::runtime_error("Error occured while reading the topology file " + filename); + } +} + +std::vector paradiseo::smp::CustomStochasticTopology::getIdNeighbors(unsigned idNode) const +{ + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0,1); + + std::vector neighbors; + for(unsigned j=0; j<_matrix.size();j++) + if(_matrix[idNode][j] > dis(gen)) + neighbors.push_back(j); + return neighbors; +} + +void paradiseo::smp::CustomStochasticTopology::construct(unsigned nbNode) +{ + assert(nbNode==_matrix.size()); +} + +void paradiseo::smp::CustomStochasticTopology::isolateNode(unsigned idNode) +{ + for(int i=0;i<_matrix.size();i++) + { + //Line of idNode to false : no connection FROM this node + _matrix[idNode][i]=0; + + //Column of idNode to false : no connection TO this node + _matrix[i][idNode]=0; + } +} diff --git a/smp/src/topology/customStochasticTopology.h b/smp/src/topology/customStochasticTopology.h new file mode 100644 index 000000000..d220e9765 --- /dev/null +++ b/smp/src/topology/customStochasticTopology.h @@ -0,0 +1,78 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012 + +Alexandre Quemy, Thibault Lasnier - INSA Rouen + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +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 license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef CUST_STOCH_TOPO_H_ +#define CUST_STOCH_TOPO_H_ + +#include +#include +#include + +namespace paradiseo +{ +namespace smp +{ + +class CustomStochasticTopology : public AbstractTopology +{ +public : + + /** + *Constructor for a custom topology from a file + *@param filename Name of the file, must be in the same directory as the executable + */ + CustomStochasticTopology(std::string filename); + + /** + * Inherited from AbstractTopology + * @see smp::topology::AbstractTopology::getIdNeighbors + */ + std::vector getIdNeighbors(unsigned idNode) const; + + /** + * Inherited from AbstractTopology : a custom topology cannot be reconstructed in a different way, so the method throws an error if nbNode doesn't match with the actual number of nodes. + * @param nbNode number of nodes for the topology + */ + void construct(unsigned nbNode); + + /** + *Inherited from AbstractTopology : changes the topology : removes any connection from/to the given node. + *@param idNode index of the node to be isolated + */ + void isolateNode(unsigned idNode); + +private : + std::vector> _matrix; +}; + +} + +} + +#endif diff --git a/smp/test/CMakeLists.txt b/smp/test/CMakeLists.txt index d56ab97c1..b855a1e4f 100644 --- a/smp/test/CMakeLists.txt +++ b/smp/test/CMakeLists.txt @@ -20,6 +20,7 @@ set (TEST_LIST t-smpMI_Homogeneous t-smpMI_Heterogeneous t-smpMI_Wrapper + t-smpCustomTopo ) ###################################################################################### @@ -39,3 +40,15 @@ execute_process( ${CMAKE_CURRENT_SOURCE_DIR}/t-data.dat ${CMAKE_CURRENT_BINARY_DIR}/t-data.dat) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/t-data.dat DESTINATION share${INSTALL_SUB_DIR}/smp/test COMPONENT tests) + +execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/data-topo-bool + ${CMAKE_CURRENT_BINARY_DIR}/data-topo-bool) +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data-topo-bool DESTINATION share${INSTALL_SUB_DIR}/smp/test COMPONENT tests) + +execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/data-topo-stoch + ${CMAKE_CURRENT_BINARY_DIR}/data-topo-stoch) +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data-topo-stoch DESTINATION share${INSTALL_SUB_DIR}/smp/test COMPONENT tests) diff --git a/smp/test/data-topo-bool b/smp/test/data-topo-bool new file mode 100644 index 000000000..0974a1e0b --- /dev/null +++ b/smp/test/data-topo-bool @@ -0,0 +1,4 @@ +0 1 0 0 +1 0 1 1 +0 1 0 1 +1 0 1 0 diff --git a/smp/test/data-topo-stoch b/smp/test/data-topo-stoch new file mode 100644 index 000000000..291400038 --- /dev/null +++ b/smp/test/data-topo-stoch @@ -0,0 +1,3 @@ +.25 .5 .75 +.2 .1 .05 +.9 .2 .03 diff --git a/smp/test/t-smpCustomTopo.cpp b/smp/test/t-smpCustomTopo.cpp new file mode 100644 index 000000000..7315b41a1 --- /dev/null +++ b/smp/test/t-smpCustomTopo.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace paradiseo::smp; + +int main() +{ + + std::vector value; + + //TEST OF CUSTOM BOOLEAN TOPOLOGY + CustomBooleanTopology topo_bool("data-topo-bool"); + std::vector neighbors = topo_bool.getIdNeighbors(0); + + value.clear(); + value.push_back(1); + assert(neighbors == value); +/////////////////////////////////////////////////////// + neighbors = topo_bool.getIdNeighbors(1); + + value.clear(); + value.push_back(0); + value.push_back(2); + value.push_back(3); + assert(neighbors == value); +////////////////////////////////////////////////////// + neighbors = topo_bool.getIdNeighbors(2); + + value.clear(); + value.push_back(1); + value.push_back(3); + assert(neighbors == value); +////////////////////////////////////////////////////// + neighbors = topo_bool.getIdNeighbors(3); + + value.clear(); + value.push_back(0); + value.push_back(2); + assert(neighbors == value); + + + //TEST OF CUSTOM STOCHASTIC TOPOLOGY + CustomStochasticTopology topo_stoch("data-topo-stoch"); + std::vector> matrix; + matrix.resize(3); + for(auto& line : matrix) + line.resize(3); + + //Computation of the mean probability + int it_nb = 1000; + for(int i = 0 ; i < it_nb ; i++) + { + for(int j = 0; j < 3; j++) + { + neighbors = topo_stoch.getIdNeighbors(j); + for(auto& node : neighbors) + { + matrix[j][node]++; + } + } + } + + for(auto& line : matrix) + { + for(auto& edge : line) + edge = edge/it_nb; + } + + //Reading the actual matrix + std :: ifstream f("data-topo-stoch"); + std::vector> _matrix; + if (f) + { + double temp; + unsigned size; + double isNeighbor, isFirst = true; + std::string line; + std::vector lineVector; + + while(getline(f, line)) + { + lineVector.clear(); + + //line contains a line of text from the file + std::istringstream tokenizer(line); + std::string token; + + while(tokenizer>> temp >> std::skipws) + { + //white spaces are skipped, and the integer is converted to boolean, to be stored + + if(temp<0) + isNeighbor = 0; + else if(temp>1) + isNeighbor = 1; + else + isNeighbor = (double) temp; + lineVector.push_back(isNeighbor); + } + + if(!lineVector.empty()) + _matrix.push_back(lineVector); + } + + f.close () ; + } + + //Comparison to the actual matrix : _matrix + for(int i = 0; i < matrix.size(); i++) + for(int j = 0; j < matrix.size(); j++) + assert(std::abs(_matrix[i][j] - matrix[i][j]) < .05); +} diff --git a/smp/test/t-smpTopo.cpp b/smp/test/t-smpTopo.cpp index f5c2cb1c5..075c7c6d7 100644 --- a/smp/test/t-smpTopo.cpp +++ b/smp/test/t-smpTopo.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace paradiseo::smp;