4

I am making a test by modifying the code 'serialize.cpp' in '/apps/boost_1_56_0/libs/graph/test' and trying to send a boost graph object over the network via boost MPI.

It compiled fine, but when I ran the executable using mpirun, I got this error:

terminate called after throwing an instance of 'boost::archive::archive_exception'
    what(): input stream error.

I think I am reading an empty buffer, but not sure how to fix it.

#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/tuple/tuple.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <map>
#include <boost/graph/adj_list_serialize.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <vector>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>

std::stringstream ss;

struct vertex_properties {
  std::string name;

  template<class Archive>
  void serialize(Archive & ar, const unsigned int /*version*/) {
    ar & BOOST_SERIALIZATION_NVP(name);
  }  
};

struct edge_properties {
  std::string name;

  template<class Archive>
  void serialize(Archive & ar, const unsigned int /*version*/) {
    ar & BOOST_SERIALIZATION_NVP(name);
  }  
};

using namespace boost;

typedef adjacency_list<vecS, vecS, undirectedS, 
           vertex_properties, edge_properties> Graph;

typedef graph_traits<Graph>::vertex_descriptor vd_type;


typedef adjacency_list<vecS, vecS, undirectedS, 
           vertex_properties> Graph_no_edge_property;

int main()
{

  mpi::environment env;
  mpi::communicator world;
  using namespace std;

  if (world.rank() == 0) {

    archive::text_oarchive oa(ss);
    Graph g;
    vertex_properties vp;
    vp.name = "A";
    vd_type A = add_vertex( vp, g );
    vp.name = "B";
    vd_type B = add_vertex( vp, g );
    edge_properties ep;
    ep.name = "a";
    add_edge( A, B, ep, g);

    oa << BOOST_SERIALIZATION_NVP(g);

    world.send(1, 0, g);

  } else if (world.rank() == 1) { 

    archive::text_iarchive ia(ss);
    Graph g;
    ia >> BOOST_SERIALIZATION_NVP(g);

    if  (!( g[*(vertices( g ).first)].name == "A" )) return -1;

    world.recv(0, 0, g);
    cout << "number of vertices received from graph g is " <<  
     num_vertices(g) << " edges is " << num_edges(g) << endl; 
  }
  return 0;
}                                                    ^
sehe
  • 374,641
  • 47
  • 450
  • 633
user3658306
  • 217
  • 4
  • 15
  • 1
    I haven't used this library, but I can't see anything that links the stringstream `ss` to the `mpi::communicator`. This would leave `ss` empty, so you wont be able to read anything from it. – The Dark Sep 17 '15 at 23:40
  • I have tried to serialize it to xml, I got doubled vertices and edges, the output is "number of vertices received from graph g is 4 edges is 2". – user3658306 Sep 18 '15 at 00:09
  • Here is the answer. The " " has done all the work, all we need to do is to serialize the vertex, edge and graph properties. – user3658306 Sep 18 '15 at 17:41
  • @user3658306 In fact, not. I think the bundled properties are automatically serialized. – sehe Sep 18 '15 at 17:55
  • @sehe I use the bundled properties, they are not automatically serialized from what I have tested. – user3658306 Sep 18 '15 at 18:22
  • @sehe I thought you meant we do not need to serialize the properties of vertex and edge using serialize(). What I meant was when we send the graph object via MPI, we do not have to do this explicitly "oa << BOOST_SERIALIZATION_NVP(g);". – user3658306 Sep 18 '15 at 20:49
  • We don't! What `serialize` is doing is merely _making the properties serializable_. We're not calling it! And indeed, I expect that you should not have to _manuall_ serialize the thing. Why else would `send` take the `g` as data? – sehe Sep 18 '15 at 20:50
  • Yes, we do not call serialize() explicitly. – user3658306 Sep 18 '15 at 21:15
  • What I (obviously?!) mean is I expect MPI to call it implicitly, so you should never have to use an archive type. Not much experience with MPI though – sehe Sep 18 '15 at 21:27
  • @sehe I think your statement is correct. – user3658306 Sep 19 '15 at 18:36
  • I think the The Dark has a good point. I would think that MPI should communicate the archive as a string which would be decoded into a boost graph object on the other processes. – pbible Sep 22 '15 at 15:00

1 Answers1

2

This is only partly addressing the question, perhaps, but here goes:

Q. @sehe I use the bundled properties, they are not automatically serialized from what I have tested. – user3658306 1 hour ago

A. Not true:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <random>

static std::mt19937 prng { std::random_device{} () };

std::string random_word(int len=7) {
    std::string s;
    static std::uniform_int_distribution<> chars('a', 'z');
    generate_n(back_inserter(s), len, bind(chars, ref(prng)));
    return s;
}

struct Vertex {
    std::string name = random_word();
    template <typename Ar> void serialize(Ar& ar, unsigned) { ar & BOOST_SERIALIZATION_NVP(name); }
};

struct Edge {
    std::string label = random_word();
    template <typename Ar> void serialize(Ar& ar, unsigned) { ar & BOOST_SERIALIZATION_NVP(label); }
};

using namespace boost;

using Graph = adjacency_list<vecS, vecS, undirectedS, Vertex, Edge>;

#include <boost/graph/adj_list_serialize.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <iostream>

int main() {
    Graph g;
    generate_random_graph(g, 20, 40, prng);

    boost::archive::xml_oarchive oa(std::cout);
    oa << BOOST_SERIALIZATION_NVP(g);
}

Prints a random graph in XML form complete with properties like:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="12">
<g class_id="0" tracking_level="0" version="0">
    <V>20</V>
    <E>40</E>
    <vertex_property class_id="1" tracking_level="0" version="0">
        <name>pjfpjir</name>
    </vertex_property>
    <vertex_property>
        <name>yimnobr</name>
    </vertex_property>
    <vertex_property>
        <name>wshvrpl</name>
    </vertex_property>
    <vertex_property>
        <name>lckoymw</name>
    </vertex_property>
    <vertex_property>
        <name>bqovgzi</name>
    </vertex_property>
    <vertex_property>
        <name>thxvqzm</name>
    </vertex_property>
    <vertex_property>
        <name>aczdbtl</name>
    </vertex_property>
    <vertex_property>
        <name>mqwayyk</name>
    </vertex_property>
    <vertex_property>
        <name>xarqhaq</name>
    </vertex_property>
    <vertex_property>
        <name>qsismjs</name>
    </vertex_property>
    <vertex_property>
        <name>makkpqi</name>
    </vertex_property>
    <vertex_property>
        <name>ekklaes</name>
    </vertex_property>
    <vertex_property>
        <name>cxyzrhz</name>
    </vertex_property>
    <vertex_property>
        <name>xetrxco</name>
    </vertex_property>
    <vertex_property>
        <name>flwqnns</name>
    </vertex_property>
    <vertex_property>
        <name>dlzoglu</name>
    </vertex_property>
    <vertex_property>
        <name>viumusv</name>
    </vertex_property>
    <vertex_property>
        <name>bktdobe</name>
    </vertex_property>
    <vertex_property>
        <name>pxsoxdj</name>
    </vertex_property>
    <vertex_property>
        <name>cldlbtp</name>
    </vertex_property>
    <u>15</u>
    <v>4</v>
    <edge_property class_id="2" tracking_level="0" version="0">
        <label>zssnhpu</label>
    </edge_property>
    <u>17</u>
    <v>0</v>
    <edge_property>
        <label>pzivbjs</label>
    </edge_property>
    <u>1</u>
    <v>0</v>
    <edge_property>
        <label>ueeoqft</label>
    </edge_property>
    <u>9</u>
    <v>7</v>
    <edge_property>
        <label>jdidntq</label>
    </edge_property>
    <u>8</u>
    <v>4</v>
    <edge_property>
        <label>tfpvuqx</label>
    </edge_property>
    <u>2</u>
    <v>10</v>
    <edge_property>
        <label>ccycjyf</label>
    </edge_property>
    <u>19</u>
    <v>12</v>
    <edge_property>
        <label>bcuuatp</label>
    </edge_property>
    <u>5</u>
    <v>12</v>
    <edge_property>
        <label>okezxdc</label>
    </edge_property>
    <u>0</u>
    <v>5</v>
    <edge_property>
        <label>qxugnbw</label>
    </edge_property>
    <u>5</u>
    <v>1</v>
    <edge_property>
        <label>sxerbxx</label>
    </edge_property>
    <u>1</u>
    <v>4</v>
    <edge_property>
        <label>diziick</label>
    </edge_property>
    <u>19</u>
    <v>7</v>
    <edge_property>
        <label>lufsvfb</label>
    </edge_property>
    <u>19</u>
    <v>3</v>
    <edge_property>
        <label>siwrshv</label>
    </edge_property>
    <u>13</u>
    <v>3</v>
    <edge_property>
        <label>rtiihnd</label>
    </edge_property>
    <u>18</u>
    <v>17</v>
    <edge_property>
        <label>nllpszz</label>
    </edge_property>
    <u>15</u>
    <v>7</v>
    <edge_property>
        <label>qbvxdtx</label>
    </edge_property>
    <u>12</u>
    <v>14</v>
    <edge_property>
        <label>zyclorw</label>
    </edge_property>
    <u>10</u>
    <v>3</v>
    <edge_property>
        <label>xdjhxht</label>
    </edge_property>
    <u>10</u>
    <v>0</v>
    <edge_property>
        <label>hwanuvt</label>
    </edge_property>
    <u>5</u>
    <v>12</v>
    <edge_property>
        <label>nzrwayu</label>
    </edge_property>
    <u>4</u>
    <v>12</v>
    <edge_property>
        <label>nselbtl</label>
    </edge_property>
    <u>7</u>
    <v>8</v>
    <edge_property>
        <label>eflgiho</label>
    </edge_property>
    <u>19</u>
    <v>6</v>
    <edge_property>
        <label>kadepsb</label>
    </edge_property>
    <u>9</u>
    <v>4</v>
    <edge_property>
        <label>xfpiijv</label>
    </edge_property>
    <u>15</u>
    <v>19</v>
    <edge_property>
        <label>aooxlov</label>
    </edge_property>
    <u>8</u>
    <v>1</v>
    <edge_property>
        <label>kaemqfa</label>
    </edge_property>
    <u>14</u>
    <v>12</v>
    <edge_property>
        <label>fxkcjma</label>
    </edge_property>
    <u>10</u>
    <v>2</v>
    <edge_property>
        <label>urewqkq</label>
    </edge_property>
    <u>0</u>
    <v>18</v>
    <edge_property>
        <label>vdskttr</label>
    </edge_property>
    <u>6</u>
    <v>9</v>
    <edge_property>
        <label>ejlncwe</label>
    </edge_property>
    <u>2</u>
    <v>3</v>
    <edge_property>
        <label>upsbqeq</label>
    </edge_property>
    <u>1</u>
    <v>9</v>
    <edge_property>
        <label>hdsmlsg</label>
    </edge_property>
    <u>17</u>
    <v>2</v>
    <edge_property>
        <label>okiovwn</label>
    </edge_property>
    <u>10</u>
    <v>16</v>
    <edge_property>
        <label>pbutvzu</label>
    </edge_property>
    <u>5</u>
    <v>4</v>
    <edge_property>
        <label>szuhnuf</label>
    </edge_property>
    <u>10</u>
    <v>11</v>
    <edge_property>
        <label>sztnmxa</label>
    </edge_property>
    <u>7</u>
    <v>16</v>
    <edge_property>
        <label>pxknohn</label>
    </edge_property>
    <u>1</u>
    <v>2</v>
    <edge_property>
        <label>ezmuunb</label>
    </edge_property>
    <u>6</u>
    <v>16</v>
    <edge_property>
        <label>jtzsxfv</label>
    </edge_property>
    <u>16</u>
    <v>15</v>
    <edge_property>
        <label>ijnaybi</label>
    </edge_property>
    <graph_property class_id="3" tracking_level="0" version="0"></graph_property>
</g>
</boost_serialization>

What Next

Now that we have debunked one of the assumptions, maybe we can figure out how MPI send/receive should be used (hint: likely not using manual serialization)

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633