Permalink
Browse files

Don't use polling -- let ising physics progress in background

  • Loading branch information...
1 parent 94d80f5 commit 180a8733abb5ee0fed8deddc9fa48b070bc0b2a3 @nickstenning committed Feb 9, 2011
Showing with 33 additions and 43 deletions.
  1. +1 −1 pysrc/simulate.py
  2. +21 −31 src/ising.cpp
  3. +8 −8 src/zhelpers.cpp
  4. +3 −3 src/zhelpers.h
View
@@ -41,7 +41,7 @@ def on_draw():
window.clear()
bitplane.draw()
- pyglet.clock.schedule_interval(update, 1/10.0)
+ pyglet.clock.schedule_interval(update, 1/20.0)
pyglet.app.run()
View
@@ -8,8 +8,8 @@
#include "zhelpers.h"
#include "util.h"
-int process_comm(zmq::socket_t&, Ising2D&);
-int process_data(zmq::socket_t&, Ising2D&);
+std::string process_comm(std::string&, Ising2D&);
+std::string process_data(std::string&, Ising2D&);
int main (unsigned int /*argc*/, char* const /*argv*/[])
{
@@ -21,34 +21,32 @@ int main (unsigned int /*argc*/, char* const /*argv*/[])
comm.connect("tcp://127.0.0.1:5000");
data.connect("tcp://127.0.0.1:5001");
- // Initialize poll set
- zmq::pollitem_t poll_items [] = {
- { comm, 0, ZMQ_POLLIN, 0 },
- { data, 0, ZMQ_POLLIN, 0 }
- };
-
- Ising2D ising(200, 200);
+ Ising2D ising(400, 400);
while (1) {
- zmq::poll(&poll_items[0], 2, -1);
+ std::string req, rep;
- // Got command
- if (poll_items[0].revents & ZMQ_POLLIN) {
- process_comm(comm, ising);
+ // Process any commands
+ if (s_recv(comm, req, ZMQ_NOBLOCK)) {
+ rep = process_comm(req, ising);
+ s_send(comm, rep);
}
- // Got request for data
- if (poll_items[1].revents & ZMQ_POLLIN) {
- process_data(data, ising);
+ // Process any requests for data
+ if (s_recv(data, req, ZMQ_NOBLOCK)) {
+ rep = process_data(req, ising);
+ s_send(data, rep);
}
+
+ ising.step();
}
return 0;
}
-int process_comm(zmq::socket_t& sock, Ising2D& ising)
+std::string process_comm(std::string& msg, Ising2D& ising)
{
- std::istringstream s_in( s_recv(sock) );
+ std::istringstream s_in( msg );
std::ostringstream s_out;
std::string cmd;
@@ -63,24 +61,16 @@ int process_comm(zmq::socket_t& sock, Ising2D& ising)
s_out << "OK";
}
- s_send(sock, s_out.str());
- return 0;
+ return s_out.str();
}
-int process_data(zmq::socket_t& sock, Ising2D& ising)
+std::string process_data(std::string& msg, Ising2D& ising)
{
- std::istringstream s_in( s_recv(sock) );
-
- int numSteps;
- s_in >> numSteps;
-
- ising.step(numSteps);
-
+ std::istringstream s_in( msg );
std::ostringstream s_out( std::ios::binary );
- ising.serialize(s_out);
- s_send(sock, s_out.str());
+ ising.serialize(s_out);
- return 0;
+ return s_out.str();
}
View
@@ -10,26 +10,26 @@
#define within(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))
-std::string s_recv (zmq::socket_t& socket) {
+bool s_recv (zmq::socket_t& socket, std::string& string, int flags) {
zmq::message_t message;
- socket.recv(&message);
+ bool rc = socket.recv(&message, flags);
- std::string string(static_cast<char*>(message.data()), message.size());
+ string.assign(static_cast<char*>(message.data()), message.size());
- return string;
+ return rc;
}
-bool s_send (zmq::socket_t& socket, std::string const& string) {
+bool s_send (zmq::socket_t& socket, std::string const& string, int flags) {
zmq::message_t message(string.size());
memcpy(message.data(), string.data(), string.size());
- bool rc = socket.send(message);
+ bool rc = socket.send(message, flags);
return rc;
}
-bool s_send (zmq::socket_t& socket, char const string[]) {
+bool s_send (zmq::socket_t& socket, char const string[], int flags) {
std::string sstring(string);
- return s_send(socket, sstring);
+ return s_send(socket, sstring, flags);
}
void s_set_id (zmq::socket_t& socket) {
View
@@ -11,13 +11,13 @@ namespace zmq {
/**
* Receive 0MQ string from socket and convert into string
**/
-std::string s_recv (zmq::socket_t& socket);
+bool s_recv (zmq::socket_t& socket, std::string& string, int flags = 0);
/**
* Convert string to 0MQ string and send to socket
**/
-bool s_send (zmq::socket_t& socket, std::string const& string);
-bool s_send (zmq::socket_t& socket, char const string[]);
+bool s_send (zmq::socket_t& socket, std::string const& string, int flags = 0);
+bool s_send (zmq::socket_t& socket, char const string[], int flags = 0);
/**
* Receives all message parts from socket, printing neatly

0 comments on commit 180a873

Please sign in to comment.