From 2d925f1e0391e32e3b4c39301fc13d2faf99d546 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 21 Oct 2012 14:33:34 -0400 Subject: [PATCH] Made BSP interfaces more explicit by using network_address rather than std::pair to represent hostname/port combos. --- dlib/bsp/bsp.cpp | 10 +++++----- dlib/bsp/bsp.h | 43 ++++++++++++++++++----------------------- dlib/bsp/bsp_abstract.h | 40 +++++++++++++++++++------------------- dlib/test/bsp.cpp | 32 +++++++++++++++--------------- 4 files changed, 60 insertions(+), 65 deletions(-) diff --git a/dlib/bsp/bsp.cpp b/dlib/bsp/bsp.cpp index 2df758a64..bcebe3d54 100644 --- a/dlib/bsp/bsp.cpp +++ b/dlib/bsp/bsp.cpp @@ -15,7 +15,7 @@ namespace dlib void connect_all ( map_id_to_con& cons, - const std::vector >& hosts, + const std::vector& hosts, unsigned long node_id ) { @@ -41,7 +41,7 @@ namespace dlib { try { - scoped_ptr con(new bsp_con(make_pair(hosts[i].ip,hosts[i].port))); + scoped_ptr con(new bsp_con(hosts[i].addr)); dlib::serialize(node_id, con->stream); // tell the other end our node_id con->stream.flush(); unsigned long id = hosts[i].node_id; @@ -50,7 +50,7 @@ namespace dlib catch (std::exception&) { std::ostringstream sout; - sout << "Could not connect to " << hosts[i].ip << ":" << hosts[i].port; + sout << "Could not connect to " << hosts[i].addr; error_string = sout.str(); break; } @@ -60,7 +60,7 @@ namespace dlib void send_out_connection_orders ( map_id_to_con& cons, - const std::vector >& hosts + const std::vector& hosts ) { // tell everyone their node ids @@ -74,7 +74,7 @@ namespace dlib std::vector targets; for (unsigned long i = 0; i < hosts.size(); ++i) { - hostinfo info(hosts[i].first, hosts[i].second, i+1); + hostinfo info(hosts[i], i+1); dlib::serialize(targets, cons[info.node_id]->stream); targets.push_back(info); diff --git a/dlib/bsp/bsp.h b/dlib/bsp/bsp.h index 4a17f2019..f6b6ad5d8 100644 --- a/dlib/bsp/bsp.h +++ b/dlib/bsp/bsp.h @@ -29,9 +29,9 @@ namespace dlib struct bsp_con { bsp_con( - const std::pair& dest + const network_address& dest ) : - con(connect(dest.first,dest.second)), + con(connect(dest)), buf(con), stream(&buf), terminated(false) @@ -62,7 +62,7 @@ namespace dlib void connect_all ( map_id_to_con& cons, - const std::vector >& hosts, + const std::vector& hosts, unsigned long node_id ); /*! @@ -72,7 +72,7 @@ namespace dlib void send_out_connection_orders ( map_id_to_con& cons, - const std::vector >& hosts + const std::vector& hosts ); // ------------------------------------------------------------------------------------ @@ -81,18 +81,15 @@ namespace dlib { hostinfo() {} hostinfo ( - const std::string& ip_, - unsigned short port_, + const network_address& addr_, unsigned long node_id_ ) : - ip(ip_), - port(port_), + addr(addr_), node_id(node_id_) { } - std::string ip; - unsigned short port; + network_address addr; unsigned long node_id; }; @@ -101,8 +98,7 @@ namespace dlib std::ostream& out ) { - dlib::serialize(item.ip, out); - dlib::serialize(item.port, out); + dlib::serialize(item.addr, out); dlib::serialize(item.node_id, out); } @@ -111,8 +107,7 @@ namespace dlib std::istream& in ) { - dlib::deserialize(item.ip, in); - dlib::deserialize(item.port, in); + dlib::deserialize(item.addr, in); dlib::deserialize(item.node_id, in); } @@ -531,7 +526,7 @@ namespace dlib typename funct_type > friend void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct ); @@ -540,7 +535,7 @@ namespace dlib typename ARG1 > friend void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1 ); @@ -551,7 +546,7 @@ namespace dlib typename ARG2 > friend void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2 @@ -564,7 +559,7 @@ namespace dlib typename ARG3 > friend void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2, @@ -579,7 +574,7 @@ namespace dlib typename ARG4 > friend void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2, @@ -671,7 +666,7 @@ namespace dlib typename funct_type > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct ) { @@ -691,7 +686,7 @@ namespace dlib typename ARG1 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1 ) @@ -713,7 +708,7 @@ namespace dlib typename ARG2 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2 @@ -737,7 +732,7 @@ namespace dlib typename ARG3 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2, @@ -763,7 +758,7 @@ namespace dlib typename ARG4 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2, diff --git a/dlib/bsp/bsp_abstract.h b/dlib/bsp/bsp_abstract.h index 08fdc837c..77da585a9 100644 --- a/dlib/bsp/bsp_abstract.h +++ b/dlib/bsp/bsp_abstract.h @@ -251,7 +251,7 @@ namespace dlib typename funct_type > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct ); /*! @@ -265,9 +265,9 @@ namespace dlib calling bsp_connect(). In particular, this node will execute funct(CONTEXT), which is expected to carry out this node's portion of the BSP computation. - The other processing nodes are executed on the hosts indicated by the input - argument. In particular, this function interprets hosts as a list of IP - addresses and port numbers identifying machines running the bsp_listen() or - bsp_listen_dynamic_port() routines. + argument. In particular, this function interprets hosts as a list addresses + identifying machines running the bsp_listen() or bsp_listen_dynamic_port() + routines. - This call to bsp_connect() blocks until the BSP computation has completed on all processing nodes. throws @@ -285,7 +285,7 @@ namespace dlib typename ARG1 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1 ); @@ -300,9 +300,9 @@ namespace dlib calling bsp_connect(). In particular, this node will execute funct(CONTEXT,arg1), which is expected to carry out this node's portion of the BSP computation. - The other processing nodes are executed on the hosts indicated by the input - argument. In particular, this function interprets hosts as a list of IP - addresses and port numbers identifying machines running the bsp_listen() or - bsp_listen_dynamic_port() routines. + argument. In particular, this function interprets hosts as a list addresses + identifying machines running the bsp_listen() or bsp_listen_dynamic_port() + routines. - This call to bsp_connect() blocks until the BSP computation has completed on all processing nodes. throws @@ -321,7 +321,7 @@ namespace dlib typename ARG2 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2 @@ -337,9 +337,9 @@ namespace dlib calling bsp_connect(). In particular, this node will execute funct(CONTEXT,arg1,arg2), which is expected to carry out this node's portion of the BSP computation. - The other processing nodes are executed on the hosts indicated by the input - argument. In particular, this function interprets hosts as a list of IP - addresses and port numbers identifying machines running the bsp_listen() or - bsp_listen_dynamic_port() routines. + argument. In particular, this function interprets hosts as a list addresses + identifying machines running the bsp_listen() or bsp_listen_dynamic_port() + routines. - This call to bsp_connect() blocks until the BSP computation has completed on all processing nodes. throws @@ -359,7 +359,7 @@ namespace dlib typename ARG3 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2, @@ -376,9 +376,9 @@ namespace dlib calling bsp_connect(). In particular, this node will execute funct(CONTEXT,arg1,arg2,arg3), which is expected to carry out this node's portion of the BSP computation. - The other processing nodes are executed on the hosts indicated by the input - argument. In particular, this function interprets hosts as a list of IP - addresses and port numbers identifying machines running the bsp_listen() or - bsp_listen_dynamic_port() routines. + argument. In particular, this function interprets hosts as a list addresses + identifying machines running the bsp_listen() or bsp_listen_dynamic_port() + routines. - This call to bsp_connect() blocks until the BSP computation has completed on all processing nodes. throws @@ -399,7 +399,7 @@ namespace dlib typename ARG4 > void bsp_connect ( - const std::vector >& hosts, + const std::vector& hosts, funct_type funct, ARG1 arg1, ARG2 arg2, @@ -417,9 +417,9 @@ namespace dlib calling bsp_connect(). In particular, this node will execute funct(CONTEXT,arg1,arg2,arg3,arg4), which is expected to carry out this node's portion of the BSP computation. - The other processing nodes are executed on the hosts indicated by the input - argument. In particular, this function interprets hosts as a list of IP - addresses and port numbers identifying machines running the bsp_listen() or - bsp_listen_dynamic_port() routines. + argument. In particular, this function interprets hosts as a list addresses + identifying machines running the bsp_listen() or bsp_listen_dynamic_port() + routines. - This call to bsp_connect() blocks until the BSP computation has completed on all processing nodes. throws diff --git a/dlib/test/bsp.cpp b/dlib/test/bsp.cpp index ef4d8a8ef..7bc1b7900 100644 --- a/dlib/test/bsp.cpp +++ b/dlib/test/bsp.cpp @@ -158,10 +158,10 @@ namespace try { int result; - std::vector > hosts; - hosts.push_back(make_pair("127.0.0.1",12345)); - hosts.push_back(make_pair("127.0.0.1",12346)); - hosts.push_back(make_pair("127.0.0.1",12347)); + std::vector hosts; + hosts.push_back(network_address("127.0.0.1",12345)); + hosts.push_back(network_address("127.0.0.1",12346)); + hosts.push_back(network_address("127.0.0.1",12347)); bsp_connect(hosts, sum_array_driver, dlib::ref(v), dlib::ref(result)); dlog << LINFO << "result: "<< result; @@ -202,10 +202,10 @@ namespace try { - std::vector > hosts; - hosts.push_back(make_pair("127.0.0.1",12345)); - hosts.push_back(make_pair("127.0.0.1",12346)); - hosts.push_back(make_pair("127.0.0.1",12347)); + std::vector hosts; + hosts.push_back(network_address("127.0.0.1",12345)); + hosts.push_back(network_address("127.0.0.1",12346)); + hosts.push_back(network_address("127.0.0.1",12347)); bsp_connect(hosts, test2_job); } catch (std::exception& e) @@ -285,11 +285,11 @@ namespace try { - std::vector > hosts; + std::vector hosts; unsigned short port; - ports.dequeue(port); hosts.push_back(make_pair("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; - ports.dequeue(port); hosts.push_back(make_pair("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; - ports.dequeue(port); hosts.push_back(make_pair("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; + ports.dequeue(port); hosts.push_back(network_address("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; + ports.dequeue(port); hosts.push_back(network_address("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; + ports.dequeue(port); hosts.push_back(network_address("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; int result = 0; const int expected = 1+2+3 + 0+2+3 + 0+1+3 + 0+1+2; bsp_connect(hosts, test3_job_driver, dlib::ref(result)); @@ -377,11 +377,11 @@ namespace try { - std::vector > hosts; + std::vector hosts; unsigned short port; - ports.dequeue(port); hosts.push_back(make_pair("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; - ports.dequeue(port); hosts.push_back(make_pair("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; - ports.dequeue(port); hosts.push_back(make_pair("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; + ports.dequeue(port); hosts.push_back(network_address("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; + ports.dequeue(port); hosts.push_back(network_address("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; + ports.dequeue(port); hosts.push_back(network_address("127.0.0.1",port)); dlog << LINFO << "PORT: " << port; int result = 0; const int expected = 1+2+3 + 0+2+3 + 0+1+3 + 0+1+2; bsp_connect(hosts, test4_job_driver, dlib::ref(result));