00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef __GNUC__
00029 #pragma interface
00030 #endif
00031
00032 #ifndef _util_group_message_h
00033 #define _util_group_message_h
00034
00035 #include <math.h>
00036 #include <util/class/class.h>
00037 #include <util/state/state.h>
00038 #include <util/keyval/keyval.h>
00039 #include <util/group/topology.h>
00040 #include <util/container/avlmap.h>
00041
00042 template <class T>
00043 class GrpReduce {
00044 public:
00045 virtual ~GrpReduce() {};
00046 virtual void reduce(T*target, T*data, int n) = 0;
00047 };
00048
00049 template <class T>
00050 class GrpSumReduce: public GrpReduce<T> {
00051 public:
00052 void reduce(T*target, T*data, int nelement);
00053 };
00054
00055 template <class T>
00056 class GrpMinReduce: public GrpReduce<T> {
00057 public:
00058 void reduce(T*target, T*data, int nelement);
00059 };
00060
00061 template <class T>
00062 class GrpMaxReduce: public GrpReduce<T> {
00063 public:
00064 void reduce(T*target, T*data, int nelement);
00065 };
00066
00067 template <class T>
00068 class GrpArithmeticAndReduce: public GrpReduce<T> {
00069 public:
00070 void reduce(T*target, T*data, int nelement);
00071 };
00072
00073 template <class T>
00074 class GrpArithmeticOrReduce: public GrpReduce<T> {
00075 public:
00076 void reduce(T*target, T*data, int nelement);
00077 };
00078
00079 template <class T>
00080 class GrpArithmeticXOrReduce: public GrpReduce<T> {
00081 public:
00082 void reduce(T*target, T*data, int nelement);
00083 };
00084
00085 template <class T>
00086 class GrpProductReduce: public GrpReduce<T> {
00087 public:
00088 void reduce(T*target, T*data, int nelement);
00089 };
00090
00091 template <class T>
00092 class GrpFunctionReduce: public GrpReduce<T> {
00093 private:
00094 void (*func_)(T*target,T*data,int nelement);
00095 public:
00096 GrpFunctionReduce(void(*func)(T*,T*,int)):func_(func) {}
00097 void reduce(T*target, T*data, int nelement);
00098 };
00099
00103 class MessageGrp: public DescribedClass {
00104 private:
00105
00106 int me_;
00107 int n_;
00108 int nclass_;
00109 int gop_max_;
00110 AVLMap<ClassDescP,int> classdesc_to_index_;
00111 ClassDescP *index_to_classdesc_;
00112 protected:
00119 void initialize(int me, int n);
00120
00121 Ref<MachineTopology> topology_;
00122
00123 int debug_;
00124 public:
00125 MessageGrp();
00126 MessageGrp(const Ref<KeyVal>&);
00127 virtual ~MessageGrp();
00128
00130 int n() { return n_; }
00132 int me() { return me_; }
00133
00136 static void set_default_messagegrp(const Ref<MessageGrp>&);
00138 static MessageGrp* get_default_messagegrp();
00139
00146 static MessageGrp* initial_messagegrp(int &argc, char** argv);
00147
00150 virtual void send(int target, double* data, int ndata);
00151 virtual void send(int target, unsigned int* data, int ndata);
00152 virtual void send(int target, int* data, int ndata);
00153 virtual void send(int target, char* data, int nbyte);
00154 virtual void send(int target, unsigned char* data, int nbyte);
00155 virtual void send(int target, signed char* data, int nbyte);
00156 virtual void send(int target, short* data, int ndata);
00157 virtual void send(int target, long* data, int ndata);
00158 virtual void send(int target, float* data, int ndata);
00159 void send(int target, double data) { send(target,&data,1); }
00160 void send(int target, int data) { send(target,&data,1); }
00161 virtual void raw_send(int target, void* data, int nbyte) = 0;
00162
00165 virtual void sendt(int target, int type, double* data, int ndata);
00166 virtual void sendt(int target, int type, unsigned int* data, int ndata);
00167 virtual void sendt(int target, int type, int* data, int ndata);
00168 virtual void sendt(int target, int type, char* data, int nbyte);
00169 virtual void sendt(int target, int type, unsigned char* data, int nbyte);
00170 virtual void sendt(int target, int type, signed char* data, int nbyte);
00171 virtual void sendt(int target, int type, short* data, int ndata);
00172 virtual void sendt(int target, int type, long* data, int ndata);
00173 virtual void sendt(int target, int type, float* data, int ndata);
00174 void sendt(int target, int type, double data) {sendt(target,type,&data,1);}
00175 void sendt(int target, int type, int data) {sendt(target,type&data,1);}
00176 virtual void raw_sendt(int target, int type, void* data, int nbyte) = 0;
00177
00180 virtual void recv(int sender, double* data, int ndata);
00181 virtual void recv(int sender, unsigned int* data, int ndata);
00182 virtual void recv(int sender, int* data, int ndata);
00183 virtual void recv(int sender, char* data, int nbyte);
00184 virtual void recv(int sender, unsigned char* data, int nbyte);
00185 virtual void recv(int sender, signed char* data, int nbyte);
00186 virtual void recv(int sender, short* data, int ndata);
00187 virtual void recv(int sender, long* data, int ndata);
00188 virtual void recv(int sender, float* data, int ndata);
00189 void recv(int sender, double& data) { recv(sender,&data,1); }
00190 void recv(int sender, int& data) { recv(sender,&data,1); }
00191 virtual void raw_recv(int sender, void* data, int nbyte) = 0;
00192
00195 virtual void recvt(int type, double* data, int ndata);
00196 virtual void recvt(int type, unsigned int* data, int ndata);
00197 virtual void recvt(int type, int* data, int ndata);
00198 virtual void recvt(int type, char* data, int nbyte);
00199 virtual void recvt(int type, unsigned char* data, int nbyte);
00200 virtual void recvt(int type, signed char* data, int nbyte);
00201 virtual void recvt(int type, short* data, int ndata);
00202 virtual void recvt(int type, long* data, int ndata);
00203 virtual void recvt(int type, float* data, int ndata);
00204 void recvt(int type, double& data) { recvt(type,&data,1); }
00205 void recvt(int type, int& data) { recvt(type,&data,1); }
00206 virtual void raw_recvt(int type, void* data, int nbyte) = 0;
00207
00209 virtual int probet(int type) = 0;
00210
00213 virtual void bcast(double* data, int ndata, int from = 0);
00214 virtual void bcast(unsigned int* data, int ndata, int from = 0);
00215 virtual void bcast(int* data, int ndata, int from = 0);
00216 virtual void bcast(char* data, int nbyte, int from = 0);
00217 virtual void bcast(unsigned char* data, int nbyte, int from = 0);
00218 virtual void bcast(signed char* data, int nbyte, int from = 0);
00219 virtual void bcast(short* data, int ndata, int from = 0);
00220 virtual void bcast(long* data, int ndata, int from = 0);
00221 virtual void bcast(float* data, int ndata, int from = 0);
00222 virtual void raw_bcast(void* data, int nbyte, int from = 0);
00223 void bcast(double& data, int from = 0) { bcast(&data, 1, from); }
00224 void bcast(int& data, int from = 0) { bcast(&data, 1, from); }
00225
00228 virtual void raw_collect(const void *part, const int *lengths,
00229 void *whole, int bytes_per_datum=1);
00230 void collect(const double *part, const int *lengths, double *whole);
00231
00234 virtual void sum(double* data, int n, double* = 0, int target = -1);
00235 virtual void sum(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00236 virtual void sum(int* data, int n, int* = 0, int target = -1);
00237 virtual void sum(char* data, int n, char* = 0, int target = -1);
00238 virtual void sum(unsigned char* data, int n,
00239 unsigned char* = 0, int target = -1);
00240 virtual void sum(signed char* data, int n,
00241 signed char* = 0, int target = -1);
00242 void sum(double& data) { sum(&data, 1); }
00243 void sum(int& data) { sum(&data, 1); }
00246 virtual void max(double* data, int n, double* = 0, int target = -1);
00247 virtual void max(int* data, int n, int* = 0, int target = -1);
00248 virtual void max(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00249 virtual void max(char* data, int n, char* = 0, int target = -1);
00250 virtual void max(unsigned char* data, int n,
00251 unsigned char* = 0, int target = -1);
00252 virtual void max(signed char* data, int n,
00253 signed char* = 0, int target = -1);
00254 void max(double& data) { max(&data, 1); }
00255 void max(int& data) { max(&data, 1); }
00258 virtual void min(double* data, int n, double* = 0, int target = -1);
00259 virtual void min(int* data, int n, int* = 0, int target = -1);
00260 virtual void min(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00261 virtual void min(char* data, int n, char* = 0, int target = -1);
00262 virtual void min(unsigned char* data, int n,
00263 unsigned char* = 0, int target = -1);
00264 virtual void min(signed char* data, int n,
00265 signed char* = 0, int target = -1);
00266 void min(double& data) { min(&data, 1); }
00267 void min(int& data) { min(&data, 1); }
00270 virtual void reduce(double*, int n, GrpReduce<double>&,
00271 double*scratch = 0, int target = -1);
00272 virtual void reduce(int*, int n, GrpReduce<int>&,
00273 int*scratch = 0, int target = -1);
00274 virtual void reduce(unsigned int*, int n, GrpReduce<unsigned int>&,
00275 unsigned int*scratch = 0, int target = -1);
00276 virtual void reduce(char*, int n, GrpReduce<char>&,
00277 char*scratch = 0, int target = -1);
00278 virtual void reduce(unsigned char*, int n, GrpReduce<unsigned char>&,
00279 unsigned char*scratch = 0, int target = -1);
00280 virtual void reduce(signed char*, int n, GrpReduce<signed char>&,
00281 signed char*scratch = 0, int target = -1);
00282 virtual void reduce(short*, int n, GrpReduce<short>&,
00283 short*scratch = 0, int target = -1);
00284 virtual void reduce(float*, int n, GrpReduce<float>&,
00285 float*scratch = 0, int target = -1);
00286 virtual void reduce(long*, int n, GrpReduce<long>&,
00287 long*scratch = 0, int target = -1);
00288 void reduce(double& data, GrpReduce<double>& r) { reduce(&data, 1, r); }
00289 void reduce(int& data, GrpReduce<int>& r) { reduce(&data, 1, r); }
00290
00292 virtual void sync();
00293
00295 virtual int last_source() = 0;
00296 virtual int last_size() = 0;
00297 virtual int last_type() = 0;
00298
00304 int classdesc_to_index(const ClassDesc*);
00305 const ClassDesc* index_to_classdesc(int);
00306 int nclass() const { return nclass_; }
00307 };
00308
00309
00312 class ProcMessageGrp: public MessageGrp {
00313 private:
00314
00315 int last_type_;
00316 int last_source_;
00317 int last_size_;
00318
00319 void set_last_type(int a) { last_type_ = a; }
00320 void set_last_source(int a) { last_source_ = a; }
00321 void set_last_size(int a) { last_size_ = a; }
00322 public:
00323 ProcMessageGrp();
00324 ProcMessageGrp(const Ref<KeyVal>&);
00325 ~ProcMessageGrp();
00326 void raw_send(int target, void* data, int nbyte);
00327 void raw_sendt(int target, int type, void* data, int nbyte);
00328 void raw_recv(int sender, void* data, int nbyte);
00329 void raw_recvt(int type, void* data, int nbyte);
00330 void raw_bcast(void* data, int nbyte, int from);
00331 int probet(int type);
00332 void sync();
00333
00334 int last_source();
00335 int last_size();
00336 int last_type();
00337 };
00338
00343 class intMessageGrp: public MessageGrp {
00344 protected:
00345 int msgtype_nbit;
00346 int ctl_nbit;
00347 int seq_nbit;
00348 int typ_nbit;
00349 int src_nbit;
00350
00351
00352 int ctl_mask;
00353 int seq_mask;
00354 int typ_mask;
00355 int src_mask;
00356
00357
00358 int ctl_shift;
00359 int seq_shift;
00360 int typ_shift;
00361 int src_shift;
00362
00363 int msgtype_typ(int msgtype);
00364 int typ_msgtype(int usrtype);
00365 int seq_msgtype(int source, int seq);
00366
00367
00368 int *source_seq;
00369 int *target_seq;
00370
00372 virtual void basic_send(int target, int type, void* data, int nbyte) = 0;
00374 virtual void basic_recv(int type, void* data, int nbyte) = 0;
00376 virtual int basic_probe(int type) = 0;
00377
00378 intMessageGrp();
00379 intMessageGrp(const Ref<KeyVal>&);
00380 void initialize(int me, int n, int nbits);
00381 public:
00382 ~intMessageGrp();
00383
00384 void raw_send(int target, void* data, int nbyte);
00385 void raw_recv(int sender, void* data, int nbyte);
00386 void raw_sendt(int target, int type, void* data, int nbyte);
00387 void raw_recvt(int type, void* data, int nbyte);
00388
00389 int probet(int);
00390
00391 int leftover_ctl_bits();
00392 };
00393
00394 #include <util/group/messaget.h>
00395
00396 #endif
00397
00398
00399
00400
00401
00402