Commit 3ebaff2e authored by Davis King's avatar Davis King

Added try_receive() routines to the bsp_context object.

parent cbb319fe
...@@ -420,16 +420,35 @@ namespace dlib ...@@ -420,16 +420,35 @@ namespace dlib
} }
template <typename T> template <typename T>
bool receive ( void receive (
T& item
)
{
if(!try_receive(item))
throw dlib::socket_error("bsp_context::receive(): no messages to receive, all nodes currently blocked.");
}
template <typename T>
bool try_receive (
T& item T& item
) )
{ {
unsigned long sending_node_id; unsigned long sending_node_id;
return receive(item, sending_node_id); return try_receive(item, sending_node_id);
}
template <typename T>
void receive (
T& item,
unsigned long& sending_node_id
)
{
if(!try_receive(item, sending_node_id))
throw dlib::socket_error("bsp_context::receive(): no messages to receive, all nodes currently blocked.");
} }
template <typename T> template <typename T>
bool receive ( bool try_receive (
T& item, T& item,
unsigned long& sending_node_id unsigned long& sending_node_id
) )
......
...@@ -102,7 +102,7 @@ namespace dlib ...@@ -102,7 +102,7 @@ namespace dlib
!*/ !*/
template <typename T> template <typename T>
bool receive ( bool try_receive (
T& item T& item
); );
/*! /*!
...@@ -114,17 +114,44 @@ namespace dlib ...@@ -114,17 +114,44 @@ namespace dlib
node. node.
- else - else
- The following must have been true for this function to return false: - The following must have been true for this function to return false:
- All other nodes were blocked on calls to receive() or terminated. - All other nodes were blocked on calls to receive(),
try_receive(), or have terminated.
- There were not any messages in flight between any nodes. - There were not any messages in flight between any nodes.
- That is, if all the nodes had continued to block on receive() - That is, if all the nodes had continued to block on receive
then they all would have blocked forever. Therefore, this methods then they all would have blocked forever. Therefore,
function only returns once there are no more messages to process this function only returns false once there are no more messages
by any any node and there is no possibility of more being to process by any node and there is no possibility of more being
generated until control is returned to the callers of receive(). generated until control is returned to the callers of receive
methods.
throws throws
- dlib::socket_error: - dlib::socket_error:
This exception is thrown if some error occurs which prevents us from This exception is thrown if some error occurs which prevents us from
communicating with other processing nodes. communicating with other processing nodes.
- dlib::serialization_error or any exception thrown by the global
deserialize(T) routine:
This is thrown if there is a problem in deserialize(). This might
happen if the message sent doesn't match the type T expected by
try_receive().
!*/
template <typename T>
void receive (
T& item
);
/*!
requires
- item is serializable
ensures
- #item == the next message which was sent to the calling processing
node.
- This function is just a wrapper around try_receive() that throws an
exception if a message is not received (i.e. if try_receive() returns
false).
throws
- dlib::socket_error:
This exception is thrown if some error occurs which prevents us from
communicating with other processing nodes or if there was not a message
to receive.
- dlib::serialization_error or any exception thrown by the global - dlib::serialization_error or any exception thrown by the global
deserialize(T) routine: deserialize(T) routine:
This is thrown if there is a problem in deserialize(). This might This is thrown if there is a problem in deserialize(). This might
...@@ -133,7 +160,7 @@ namespace dlib ...@@ -133,7 +160,7 @@ namespace dlib
!*/ !*/
template <typename T> template <typename T>
bool receive ( bool try_receive (
T& item, T& item,
unsigned long& sending_node_id unsigned long& sending_node_id
); );
...@@ -148,17 +175,46 @@ namespace dlib ...@@ -148,17 +175,46 @@ namespace dlib
- #sending_node_id < number_of_nodes() - #sending_node_id < number_of_nodes()
- else - else
- The following must have been true for this function to return false: - The following must have been true for this function to return false:
- All other nodes were blocked on calls to receive() or terminated. - All other nodes were blocked on calls to receive(),
try_receive(), or have terminated.
- There were not any messages in flight between any nodes. - There were not any messages in flight between any nodes.
- That is, if all the nodes had continued to block on receive() - That is, if all the nodes had continued to block on receive
then they all would have blocked forever. Therefore, this methods then they all would have blocked forever. Therefore,
function only returns once there are no more messages to process this function only returns false once there are no more messages
by any any node and there is no possibility of more being to process by any node and there is no possibility of more being
generated until control is returned to the callers of receive(). generated until control is returned to the callers of receive
methods.
throws throws
- dlib::socket_error: - dlib::socket_error:
This exception is thrown if some error occurs which prevents us from This exception is thrown if some error occurs which prevents us from
communicating with other processing nodes. communicating with other processing nodes.
- dlib::serialization_error or any exception thrown by the global
deserialize(T) routine:
This is thrown if there is a problem in deserialize(). This might
happen if the message sent doesn't match the type T expected by
try_receive().
!*/
template <typename T>
void receive (
T& item,
unsigned long& sending_node_id
);
/*!
requires
- item is serializable
ensures
- #item == the next message which was sent to the calling processing node.
- #sending_node_id == the node id of the node that sent this message.
- #sending_node_id < number_of_nodes()
- This function is just a wrapper around try_receive() that throws an
exception if a message is not received (i.e. if try_receive() returns
false).
throws
- dlib::socket_error:
This exception is thrown if some error occurs which prevents us from
communicating with other processing nodes or if there was not a message
to receive.
- dlib::serialization_error or any exception thrown by the global - dlib::serialization_error or any exception thrown by the global
deserialize(T) routine: deserialize(T) routine:
This is thrown if there is a problem in deserialize(). This might This is thrown if there is a problem in deserialize(). This might
...@@ -175,8 +231,8 @@ namespace dlib ...@@ -175,8 +231,8 @@ namespace dlib
- There are not any messages in flight between any nodes. - There are not any messages in flight between any nodes.
- That is, if all the nodes had continued to block on receive() then - That is, if all the nodes had continued to block on receive() then
they all would have blocked forever. Therefore, this function only they all would have blocked forever. Therefore, this function only
returns once there are no more messages to process by any any node returns once there are no more messages to process by any node and
and there is no possibility of more being generated until control is there is no possibility of more being generated until control is
returned to the callers of receive(). returned to the callers of receive().
throws throws
- dlib::socket_error: - dlib::socket_error:
......
...@@ -114,7 +114,7 @@ namespace ...@@ -114,7 +114,7 @@ namespace
result = 0; result = 0;
int val; int val;
while(obj.receive(val)) while(obj.try_receive(val))
result += val; result += val;
} }
...@@ -227,24 +227,24 @@ namespace ...@@ -227,24 +227,24 @@ namespace
int accum = 0; int accum = 0;
int temp = 0; int temp = 0;
while(obj.receive(temp)) while(obj.try_receive(temp))
accum += temp; accum += temp;
// send to node 1 so it can sum everything // send to node 1 so it can sum everything
if (obj.node_id() != 1) if (obj.node_id() != 1)
obj.send(accum, 1); obj.send(accum, 1);
while(obj.receive(temp)) while(obj.try_receive(temp))
accum += temp; accum += temp;
// Now hop the accum values along the nodes until the value from node 1 gets to // Now hop the accum values along the nodes until the value from node 1 gets to
// node 0. // node 0.
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
// this whole block is a noop since it doesn't end up doing anything. // this whole block is a noop since it doesn't end up doing anything.
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
...@@ -253,7 +253,7 @@ namespace ...@@ -253,7 +253,7 @@ namespace
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
} }
} }
...@@ -317,24 +317,24 @@ namespace ...@@ -317,24 +317,24 @@ namespace
int accum = 0; int accum = 0;
int temp = 0; int temp = 0;
while(obj.receive(temp)) while(obj.try_receive(temp))
accum += temp; accum += temp;
// send to node 1 so it can sum everything // send to node 1 so it can sum everything
if (obj.node_id() != 1) if (obj.node_id() != 1)
obj.send(accum, 1); obj.send(accum, 1);
while(obj.receive(temp)) while(obj.try_receive(temp))
accum += temp; accum += temp;
// Now hop the accum values along the nodes until the value from node 1 gets to // Now hop the accum values along the nodes until the value from node 1 gets to
// node 0. // node 0.
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST(obj.receive(accum)); obj.receive(accum);
// this whole block is a noop since it doesn't end up doing anything. // this whole block is a noop since it doesn't end up doing anything.
for (int k = 0; k < 40; ++k) for (int k = 0; k < 40; ++k)
...@@ -343,7 +343,7 @@ namespace ...@@ -343,7 +343,7 @@ namespace
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes()); obj.send(accum, (obj.node_id()+1)%obj.number_of_nodes());
DLIB_TEST_MSG(obj.receive(accum), obj.node_id()); obj.receive(accum);
obj.receive(); obj.receive();
} }
...@@ -415,12 +415,15 @@ namespace ...@@ -415,12 +415,15 @@ namespace
void perform_test ( void perform_test (
) )
{ {
dotest1(); for (int i = 0; i < 3; ++i)
dotest2<0>(); {
dotest2<1>(); dotest1();
dotest2<2>(); dotest2<0>();
dotest3(); dotest2<1>();
dotest4(); dotest2<2>();
dotest3();
dotest4();
}
} }
} a; } a;
......
Markdown is supported
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