[0b990d] | 1 | //
|
---|
| 2 | // mstate.h
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
| 5 | //
|
---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
| 7 | // Maintainer: LPS
|
---|
| 8 | //
|
---|
| 9 | // This file is part of the SC Toolkit.
|
---|
| 10 | //
|
---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
| 12 | // it under the terms of the GNU Library General Public License as published by
|
---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
| 14 | // any later version.
|
---|
| 15 | //
|
---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU Library General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU Library General Public License
|
---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | //
|
---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
| 26 | //
|
---|
| 27 |
|
---|
| 28 | #ifdef __GNUC__
|
---|
| 29 | #pragma interface
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | #ifndef _util_group_mstate_h
|
---|
| 33 | #define _util_group_mstate_h
|
---|
| 34 |
|
---|
| 35 | #include <util/state/state.h>
|
---|
| 36 | #include <util/state/statein.h>
|
---|
| 37 | #include <util/state/stateout.h>
|
---|
| 38 | #include <util/group/message.h>
|
---|
| 39 |
|
---|
| 40 | namespace sc {
|
---|
| 41 |
|
---|
| 42 | /** The MsgStateSend is an abstract base class that sends objects
|
---|
| 43 | to nodes in a MessageGrp.
|
---|
| 44 | */
|
---|
| 45 | class MsgStateSend: public StateOut {
|
---|
| 46 | private:
|
---|
| 47 | // do not allow copy constructor or assignment
|
---|
| 48 | MsgStateSend(const MsgStateSend&);
|
---|
| 49 | void operator=(const MsgStateSend&);
|
---|
| 50 | protected:
|
---|
| 51 | Ref<MessageGrp> grp;
|
---|
| 52 | int nbuf; // the number of bytes used in the buffer
|
---|
| 53 | int bufsize; // the allocated size of the data buffer
|
---|
| 54 | char* buffer; // the data buffer
|
---|
| 55 | char* send_buffer; // the buffer used to send data (includes nbuf)
|
---|
| 56 | int nheader; // nbuf + nheader = the number of bytes in send_buffer to send
|
---|
| 57 | int* nbuf_buffer; // the pointer to the nbuf stored in the buffer
|
---|
| 58 |
|
---|
| 59 | int put_array_void(const void*, int);
|
---|
| 60 | public:
|
---|
| 61 | MsgStateSend(const Ref<MessageGrp>&);
|
---|
| 62 | virtual ~MsgStateSend();
|
---|
| 63 |
|
---|
| 64 | /// Specializations must implement flush().
|
---|
| 65 | virtual void flush() = 0;
|
---|
| 66 |
|
---|
| 67 | /** The buffer size of statein and stateout objects that communicate
|
---|
| 68 | with each other must match. */
|
---|
| 69 | void set_buffer_size(int);
|
---|
| 70 |
|
---|
| 71 | /** I only need to override put(const ClassDesc*) but C++ will
|
---|
| 72 | hide all of the other put's so I must override everything. */
|
---|
| 73 | int put(const ClassDesc*);
|
---|
| 74 | int put(char r);
|
---|
| 75 | int put(unsigned int r);
|
---|
| 76 | int put(int r);
|
---|
| 77 | int put(float r);
|
---|
| 78 | int put(double r);
|
---|
| 79 | int put(const char*,int);
|
---|
| 80 | int put(const int*,int);
|
---|
| 81 | int put(const unsigned int*,int);
|
---|
| 82 | int put(const float*,int);
|
---|
| 83 | int put(const double*,int);
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | /** The MsgStateBufRecv is an abstract base class that
|
---|
| 87 | buffers objects sent through a MessageGrp.
|
---|
| 88 | */
|
---|
| 89 | class MsgStateBufRecv: public StateIn {
|
---|
| 90 | private:
|
---|
| 91 | // do not allow copy constructor or assignment
|
---|
| 92 | MsgStateBufRecv(const MsgStateBufRecv&);
|
---|
| 93 | void operator=(const MsgStateBufRecv&);
|
---|
| 94 | protected:
|
---|
| 95 | Ref<MessageGrp> grp;
|
---|
| 96 | int nbuf; // the number of bytes used in the buffer
|
---|
| 97 | int ibuf; // the current pointer withing the buffer
|
---|
| 98 | int bufsize; // the allocated size of the buffer
|
---|
| 99 | char* buffer; // the data buffer
|
---|
| 100 | char* send_buffer; // the buffer used to send data (includes nbuf)
|
---|
| 101 | int nheader; // nbuf + nheader = the number of bytes in send_buffer to send
|
---|
| 102 | int* nbuf_buffer; // the pointer to the nbuf stored in the buffer
|
---|
| 103 |
|
---|
| 104 | int get_array_void(void*,int);
|
---|
| 105 |
|
---|
| 106 | /// Specializations must implement next_buffer().
|
---|
| 107 | virtual void next_buffer() = 0;
|
---|
| 108 | public:
|
---|
| 109 | /// MsgStateBufRecv can be initialized with a MessageGrp.
|
---|
| 110 | MsgStateBufRecv(const Ref<MessageGrp>&);
|
---|
| 111 | /// Use the default MessageGrp.
|
---|
| 112 | MsgStateBufRecv();
|
---|
| 113 |
|
---|
| 114 | virtual ~MsgStateBufRecv();
|
---|
| 115 |
|
---|
| 116 | /** The buffer size of statein and stateout objects that communicate
|
---|
| 117 | with each other must match. */
|
---|
| 118 | void set_buffer_size(int);
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | /** The MsgStateRecv is an abstract base class that receives
|
---|
| 122 | objects from nodes in a MessageGrp. */
|
---|
| 123 | class MsgStateRecv: public MsgStateBufRecv {
|
---|
| 124 | private:
|
---|
| 125 | // do not allow copy constructor or assignment
|
---|
| 126 | MsgStateRecv(const MsgStateRecv&);
|
---|
| 127 | void operator=(const MsgStateRecv&);
|
---|
| 128 | public:
|
---|
| 129 | /// MsgStateRecv must be initialized with a MessageGrp.
|
---|
| 130 | MsgStateRecv(const Ref<MessageGrp>&);
|
---|
| 131 |
|
---|
| 132 | virtual ~MsgStateRecv();
|
---|
| 133 |
|
---|
| 134 | /** Returns the version of the ClassDesc. This assumes that
|
---|
| 135 | the version of the remote class is the same as that of
|
---|
| 136 | the local class. */
|
---|
| 137 | int version(const ClassDesc*);
|
---|
| 138 |
|
---|
| 139 | /** I only need to override get(ClassDesc**) but C++ will hide
|
---|
| 140 | all of the other get's so I must override everything. */
|
---|
| 141 | int get(const ClassDesc**);
|
---|
| 142 | int get(char&r, const char *key = 0);
|
---|
| 143 | int get(unsigned int&r, const char *key = 0);
|
---|
| 144 | int get(int&r, const char *key = 0);
|
---|
| 145 | int get(float&r, const char *key = 0);
|
---|
| 146 | int get(double&r, const char *key = 0);
|
---|
| 147 | int get(char*&);
|
---|
| 148 | int get(unsigned int*&);
|
---|
| 149 | int get(int*&);
|
---|
| 150 | int get(float*&);
|
---|
| 151 | int get(double*&);
|
---|
| 152 | };
|
---|
| 153 |
|
---|
| 154 | /** StateSend is a concrete specialization of
|
---|
| 155 | MsgStateSend that does the send part of point to
|
---|
| 156 | point communication in a MessageGrp. */
|
---|
| 157 | class StateSend: public MsgStateSend {
|
---|
| 158 | private:
|
---|
| 159 | // do not allow copy constructor or assignment
|
---|
| 160 | StateSend(const StateSend&);
|
---|
| 161 | void operator=(const StateSend&);
|
---|
| 162 | private:
|
---|
| 163 | int target_;
|
---|
| 164 | public:
|
---|
| 165 | /// Create a StateSend given a MessageGrp.
|
---|
| 166 | StateSend(const Ref<MessageGrp>&);
|
---|
| 167 |
|
---|
| 168 | ~StateSend();
|
---|
| 169 | /// Specify the target node.
|
---|
| 170 | void target(int);
|
---|
| 171 | /// Flush the buffer.
|
---|
| 172 | void flush();
|
---|
| 173 | };
|
---|
| 174 |
|
---|
| 175 | /** StateRecv is a concrete specialization of
|
---|
| 176 | MsgStateRecv that does the receive part of point to
|
---|
| 177 | point communication in a MessageGrp. */
|
---|
| 178 | class StateRecv: public MsgStateRecv {
|
---|
| 179 | private:
|
---|
| 180 | // do not allow copy constructor or assignment
|
---|
| 181 | StateRecv(const StateRecv&);
|
---|
| 182 | void operator=(const StateRecv&);
|
---|
| 183 | private:
|
---|
| 184 | int source_;
|
---|
| 185 | protected:
|
---|
| 186 | void next_buffer();
|
---|
| 187 | public:
|
---|
| 188 | /// Create a StateRecv given a MessageGrp.
|
---|
| 189 | StateRecv(const Ref<MessageGrp>&);
|
---|
| 190 | /// Specify the source node.
|
---|
| 191 | void source(int);
|
---|
| 192 | };
|
---|
| 193 |
|
---|
| 194 | /** BcastStateSend does the send part of a broadcast of an object
|
---|
| 195 | to all nodes. Only one node uses a BcastStateSend and the rest
|
---|
| 196 | must use a BcastStateRecv. */
|
---|
| 197 | class BcastStateSend: public MsgStateSend {
|
---|
| 198 | private:
|
---|
| 199 | // do not allow copy constructor or assignment
|
---|
| 200 | BcastStateSend(const BcastStateSend&);
|
---|
| 201 | void operator=(const BcastStateSend&);
|
---|
| 202 | public:
|
---|
| 203 | /// Create the BcastStateSend.
|
---|
| 204 | BcastStateSend(const Ref<MessageGrp>&);
|
---|
| 205 |
|
---|
| 206 | ~BcastStateSend();
|
---|
| 207 | /// Flush the data remaining in the buffer.
|
---|
| 208 | void flush();
|
---|
| 209 | };
|
---|
| 210 |
|
---|
| 211 | /** BcastStateRecv does the receive part of a broadcast of an
|
---|
| 212 | object to all nodes. Only one node uses a BcastStateSend and
|
---|
| 213 | the rest must use a BcastStateRecv. */
|
---|
| 214 | class BcastStateRecv: public MsgStateRecv {
|
---|
| 215 | private:
|
---|
| 216 | // do not allow copy constructor or assignment
|
---|
| 217 | BcastStateRecv(const BcastStateRecv&);
|
---|
| 218 | void operator=(const BcastStateRecv&);
|
---|
| 219 | protected:
|
---|
| 220 | int source_;
|
---|
| 221 | void next_buffer();
|
---|
| 222 | public:
|
---|
| 223 | /// Create the BcastStateRecv.
|
---|
| 224 | BcastStateRecv(const Ref<MessageGrp>&, int source = 0);
|
---|
| 225 | /// Set the source node.
|
---|
| 226 | void source(int s);
|
---|
| 227 | };
|
---|
| 228 |
|
---|
| 229 | /** This creates and forwards/retrieves data from either a BcastStateRecv
|
---|
| 230 | or a BcastStateSend depending on the value of the argument to
|
---|
| 231 | constructor. */
|
---|
| 232 | class BcastState {
|
---|
| 233 | private:
|
---|
| 234 | BcastStateRecv *recv_;
|
---|
| 235 | BcastStateSend *send_;
|
---|
| 236 | public:
|
---|
| 237 | /// Create a BcastState object. The default source is node 0.
|
---|
| 238 | BcastState(const Ref<MessageGrp> &, int source = 0);
|
---|
| 239 |
|
---|
| 240 | ~BcastState();
|
---|
| 241 |
|
---|
| 242 | /** Broadcast data to all nodes. After these are called
|
---|
| 243 | for a group of data the flush member must be called
|
---|
| 244 | to force the source node to actually write the data. */
|
---|
| 245 | void bcast(int &);
|
---|
| 246 | void bcast(double &);
|
---|
| 247 | void bcast(int *&, int);
|
---|
| 248 | void bcast(double *&, int);
|
---|
| 249 | template <class T> void bcast(Ref<T>&a)
|
---|
| 250 | {
|
---|
| 251 | if (recv_) {
|
---|
| 252 | a << SavableState::restore_state(*recv_);
|
---|
| 253 | }
|
---|
| 254 | else if (send_) {
|
---|
| 255 | SavableState::save_state(a.pointer(),*send_);
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /** Force data to be written. Data is not otherwise written
|
---|
| 260 | until the buffer is full. */
|
---|
| 261 | void flush();
|
---|
| 262 |
|
---|
| 263 | /** Call the StateOut or StateIn
|
---|
| 264 | forget_references member. */
|
---|
| 265 | void forget_references();
|
---|
| 266 |
|
---|
| 267 | /// Controls the amount of data that is buffered before it is sent.
|
---|
| 268 | void set_buffer_size(int);
|
---|
| 269 | };
|
---|
| 270 |
|
---|
| 271 | /** BcastStateBin reads a file in written by
|
---|
| 272 | StateInBin on node 0 and broadcasts it to all nodes
|
---|
| 273 | so state can be simultaneously restored on all nodes. */
|
---|
| 274 | class BcastStateInBin: public MsgStateBufRecv {
|
---|
| 275 | private:
|
---|
| 276 | // do not allow copy constructor or assignment
|
---|
| 277 | BcastStateInBin(const BcastStateRecv&);
|
---|
| 278 | void operator=(const BcastStateRecv&);
|
---|
| 279 | protected:
|
---|
| 280 | int opened_;
|
---|
| 281 | int file_position_;
|
---|
| 282 | std::streambuf *buf_;
|
---|
| 283 |
|
---|
| 284 | void next_buffer();
|
---|
| 285 | int get_array_void(void*, int);
|
---|
| 286 | public:
|
---|
| 287 | /// Create the BcastStateRecv using the default MessageGrp.
|
---|
| 288 | BcastStateInBin(const Ref<KeyVal> &);
|
---|
| 289 | /// Create the BcastStateRecv.
|
---|
| 290 | BcastStateInBin(const Ref<MessageGrp>&, const char *filename);
|
---|
| 291 |
|
---|
| 292 | ~BcastStateInBin();
|
---|
| 293 |
|
---|
| 294 | virtual int open(const char *name);
|
---|
| 295 | virtual void close();
|
---|
| 296 |
|
---|
| 297 | void seek(int loc);
|
---|
| 298 | int seekable();
|
---|
| 299 | int tell();
|
---|
| 300 | int use_directory();
|
---|
| 301 | };
|
---|
| 302 |
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | #endif
|
---|
| 306 |
|
---|
| 307 | // Local Variables:
|
---|
| 308 | // mode: c++
|
---|
| 309 | // c-file-style: "CLJ"
|
---|
| 310 | // End:
|
---|