00001 /* 00002 * Equation Cruncher 00003 * Copyright (C) 2004 Dhruv Matani(dhruvbird@yahoo.com) 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License as 00008 * published by the Free Software Foundation; either version 2 of the 00009 * License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00019 * USA 00020 */ 00021 00022 00023 #if !defined EC_NODE_MANAGER_HPP 00024 #define EC_NODE_MANAGER_HPP 00025 00026 #include <list> 00027 #include <vector> 00028 #include <stdexcept> 00029 00030 #include "concurrence.hpp" 00031 #include "ports.hpp" 00032 #include "socket.hpp" 00033 #include "udefs.hpp" 00034 #include "glob_log.hpp" 00035 #include "atomic_i686.hpp" 00036 #include "config.hpp" 00037 00038 #include <iostream> 00039 00058 namespace eqn_cruncher 00059 { 00074 struct pkt_header 00075 { 00081 int size; 00089 int type; 00096 int code; 00102 double reserved; 00103 00104 pkt_header() : reserved(0) 00105 { } 00106 }; 00107 00115 class packet_error : public std::runtime_error 00116 { 00117 public: 00122 packet_error(std::string const& str = "packet_error") 00123 : std::runtime_error(str) 00124 { } 00125 }; 00126 00127 00136 class node_manager 00137 { 00138 std::list<eqn_cruncher::socket> sock_list; 00139 #if USE_PTHREAD_MUTEX 00140 pthr_lock sl_lock; 00141 #else 00142 Mutex sl_lock; 00143 #endif 00144 00145 static const unsigned int buff_size = 4092; 00146 static const unsigned int max_size = buff_size - sizeof(pkt_header); 00147 static const unsigned int max_doubles = max_size / sizeof(double); 00148 00149 public: 00150 enum { 00154 data, 00158 command }; 00159 00160 enum { 00166 quit, 00175 start, 00182 result, 00191 reset_client, 00197 reset_subclient }; 00198 00199 00208 int 00209 test_and_get() 00210 { 00211 int ret = -1; 00212 sl_lock.lock(); 00213 if (!sock_list.empty()) 00214 { 00215 ret = sock_list.back().get_sockid(); 00216 sock_list.pop_back(); 00217 } 00218 sl_lock.unlock(); 00219 return ret; 00220 } 00221 00228 void 00229 recycle_socket(int sock_id) 00230 { 00231 sl_lock.lock(); 00232 sock_list.push_back(eqn_cruncher::socket(sock_id)); 00233 sl_lock.unlock(); 00234 } 00235 00257 void 00258 send_vector(eqn_cruncher::matrix_t const& dv, eqn_cruncher::socket s); 00259 00260 00273 void 00274 server(server_data* psd); 00275 00276 00301 double 00302 get_vector(eqn_cruncher::socket s, unsigned int mat_size, 00303 double(*det_proc)(matrix_t const&)); 00304 00305 00313 void 00314 close_sockets(); 00315 00316 00325 void 00326 send_message(eqn_cruncher::pkt_header ph); 00327 00328 00333 void 00334 send_reset_client_msg(); 00335 00336 00341 void 00342 send_reset_subclient_msg(); 00343 00344 00349 void 00350 send_quit_msg(); 00351 }; 00352 } 00353 00354 #endif // EC_NODE_MANAGER_HPP
1.2.18