Main Page Class Hierarchy Compound List File List Compound Members Related Pages

# message.h

    00001 //
    00002 // message.h
    00003 //
    00004 // Copyright (C) 1996 Limit Point Systems, Inc.
    00005 //
    00006 // Author: Curtis Janssen <cljanss@limitpt.com>
    00007 // Maintainer: LPS
    00008 //
    00009 // This file is part of the SC Toolkit.
    00010 //
    00011 // The SC Toolkit is free software; you can redistribute it and/or modify
    00012 // it under the terms of the GNU Library General Public License as published by
    00013 // the Free Software Foundation; either version 2, or (at your option)
    00014 // any later version.
    00015 //
    00016 // The SC Toolkit is distributed in the hope that it will be useful,
    00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00019 // GNU Library General Public License for more details.
    00020 //
    00021 // You should have received a copy of the GNU Library General Public License
    00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
    00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
    00024 //
    00025 // The U.S. Government is granted a limited license as per AL 91-7.
    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 <map>
    00036
    00037 #include <math.h>
    00038 #include <util/class/class.h>
    00039 #include <util/state/state.h>
    00040 #include <util/keyval/keyval.h>
    00041 #include <util/group/topology.h>
    00042
    00043 namespace sc {
    00044
    00045 template <class T>
    00046 class GrpReduce {
    00047   public:
    00048     virtual ~GrpReduce() {};
    00049     virtual void reduce(T*target, T*data, int n) = 0;
    00050 };
    00051
    00052 template <class T>
    00053 class GrpSumReduce: public GrpReduce<T> {
    00054   public:
    00055     void reduce(T*target, T*data, int nelement);
    00056 };
    00057
    00058 template <class T>
    00059 class GrpMinReduce: public GrpReduce<T> {
    00060   public:
    00061     void reduce(T*target, T*data, int nelement);
    00062 };
    00063
    00064 template <class T>
    00065 class GrpMaxReduce: public GrpReduce<T> {
    00066   public:
    00067     void reduce(T*target, T*data, int nelement);
    00068 };
    00069
    00070 template <class T>
    00071 class GrpArithmeticAndReduce: public GrpReduce<T> {
    00072   public:
    00073     void reduce(T*target, T*data, int nelement);
    00074 };
    00075
    00076 template <class T>
    00077 class GrpArithmeticOrReduce: public GrpReduce<T> {
    00078   public:
    00079     void reduce(T*target, T*data, int nelement);
    00080 };
    00081
    00082 template <class T>
    00083 class GrpArithmeticXOrReduce: public GrpReduce<T> {
    00084   public:
    00085     void reduce(T*target, T*data, int nelement);
    00086 };
    00087
    00088 template <class T>
    00089 class GrpProductReduce: public GrpReduce<T> {
    00090   public:
    00091     void reduce(T*target, T*data, int nelement);
    00092 };
    00093
    00094 template <class T>
    00095 class GrpFunctionReduce: public GrpReduce<T> {
    00096   private:
    00097     void (*func_)(T*target,T*data,int nelement);
    00098   public:
    00099     GrpFunctionReduce(void(*func)(T*,T*,int)):func_(func) {}
    00100     void reduce(T*target, T*data, int nelement);
    00101 };
    00102
    00106 class MessageGrp: public DescribedClass {
    00107   private:
    00108     // These are initialized by the initialize() member (see below).
    00109     int me_;
    00110     int n_;
    00111     int nclass_;
    00112     int gop_max_;
    00113     std::map<ClassDescP,int> classdesc_to_index_;
    00114     ClassDescP *index_to_classdesc_;
    00115   protected:
    00122     void initialize(int me, int n);
    00123
    00124     Ref<MachineTopology> topology_;
    00125
    00126     int debug_;
    00127   public:
    00128     MessageGrp();
    00129     MessageGrp(const Ref<KeyVal>&);
    00130     virtual ~MessageGrp();
    00131
    00133     int n() { return n_; }
    00135     int me() { return me_; }
    00136
    00139     virtual Ref<MessageGrp> clone(void)=0;
    00140
    00143     static void set_default_messagegrp(const Ref<MessageGrp>&);
    00145     static MessageGrp* get_default_messagegrp();
    00146
    00153     static MessageGrp* initial_messagegrp(int &argc, char** &argv);
    00154
    00157     virtual void send(int target, double* data, int ndata);
    00158     virtual void send(int target, unsigned int* data, int ndata);
    00159     virtual void send(int target, int* data, int ndata);
    00160     virtual void send(int target, char* data, int nbyte);
    00161     virtual void send(int target, unsigned char* data, int nbyte);
    00162     virtual void send(int target, signed char* data, int nbyte);
    00163     virtual void send(int target, short* data, int ndata);
    00164     virtual void send(int target, long* data, int ndata);
    00165     virtual void send(int target, float* data, int ndata);
    00166     void send(int target, double data) { send(target,&data,1); }
    00167     void send(int target, int data) { send(target,&data,1); }
    00168     virtual void raw_send(int target, void* data, int nbyte) = 0;
    00169
    00172     virtual void sendt(int target, int type, double* data, int ndata);
    00173     virtual void sendt(int target, int type, unsigned int* data, int ndata);
    00174     virtual void sendt(int target, int type, int* data, int ndata);
    00175     virtual void sendt(int target, int type, char* data, int nbyte);
    00176     virtual void sendt(int target, int type, unsigned char* data, int nbyte);
    00177     virtual void sendt(int target, int type, signed char* data, int nbyte);
    00178     virtual void sendt(int target, int type, short* data, int ndata);
    00179     virtual void sendt(int target, int type, long* data, int ndata);
    00180     virtual void sendt(int target, int type, float* data, int ndata);
    00181     void sendt(int target, int type, double data) {sendt(target,type,&data,1);}
    00182     void sendt(int target, int type, int data) {sendt(target,type,&data,1);}
    00183     virtual void raw_sendt(int target, int type, void* data, int nbyte) = 0;
    00184
    00187     virtual void recv(int sender, double* data, int ndata);
    00188     virtual void recv(int sender, unsigned int* data, int ndata);
    00189     virtual void recv(int sender, int* data, int ndata);
    00190     virtual void recv(int sender, char* data, int nbyte);
    00191     virtual void recv(int sender, unsigned char* data, int nbyte);
    00192     virtual void recv(int sender, signed char* data, int nbyte);
    00193     virtual void recv(int sender, short* data, int ndata);
    00194     virtual void recv(int sender, long* data, int ndata);
    00195     virtual void recv(int sender, float* data, int ndata);
    00196     void recv(int sender, double& data) { recv(sender,&data,1); }
    00197     void recv(int sender, int& data) { recv(sender,&data,1); }
    00198     virtual void raw_recv(int sender, void* data, int nbyte) = 0;
    00199
    00202     virtual void recvt(int type, double* data, int ndata);
    00203     virtual void recvt(int type, unsigned int* data, int ndata);
    00204     virtual void recvt(int type, int* data, int ndata);
    00205     virtual void recvt(int type, char* data, int nbyte);
    00206     virtual void recvt(int type, unsigned char* data, int nbyte);
    00207     virtual void recvt(int type, signed char* data, int nbyte);
    00208     virtual void recvt(int type, short* data, int ndata);
    00209     virtual void recvt(int type, long* data, int ndata);
    00210     virtual void recvt(int type, float* data, int ndata);
    00211     void recvt(int type, double& data) { recvt(type,&data,1); }
    00212     void recvt(int type, int& data) { recvt(type,&data,1); }
    00213     virtual void raw_recvt(int type, void* data, int nbyte) = 0;
    00214
    00216     virtual int probet(int type) = 0;
    00217
    00220     virtual void bcast(double* data, int ndata, int from = 0);
    00221     virtual void bcast(unsigned int* data, int ndata, int from = 0);
    00222     virtual void bcast(int* data, int ndata, int from = 0);
    00223     virtual void bcast(char* data, int nbyte, int from = 0);
    00224     virtual void bcast(unsigned char* data, int nbyte, int from = 0);
    00225     virtual void bcast(signed char* data, int nbyte, int from = 0);
    00226     virtual void bcast(short* data, int ndata, int from = 0);
    00227     virtual void bcast(long* data, int ndata, int from = 0);
    00228     virtual void bcast(float* data, int ndata, int from = 0);
    00229     virtual void raw_bcast(void* data, int nbyte, int from = 0);
    00230     void bcast(double& data, int from = 0) { bcast(&data, 1, from); }
    00231     void bcast(int& data, int from = 0) { bcast(&data, 1, from); }
    00232
    00235     virtual void raw_collect(const void *part, const int *lengths,
    00236                              void *whole, int bytes_per_datum=1);
    00237     void collect(const double *part, const int *lengths, double *whole);
    00238
    00241     virtual void sum(double* data, int n, double* = 0, int target = -1);
    00242     virtual void sum(unsigned int* data, int n, unsigned int* = 0, int target = -1);
    00243     virtual void sum(int* data, int n, int* = 0, int target = -1);
    00244     virtual void sum(char* data, int n, char* = 0, int target = -1);
    00245     virtual void sum(unsigned char* data, int n,
    00246                      unsigned char* = 0, int target = -1);
    00247     virtual void sum(signed char* data, int n,
    00248                      signed char* = 0, int target = -1);
    00249     void sum(double& data) { sum(&data, 1); }
    00250     void sum(int& data) { sum(&data, 1); }
    00253     virtual void max(double* data, int n, double* = 0, int target = -1);
    00254     virtual void max(int* data, int n, int* = 0, int target = -1);
    00255     virtual void max(unsigned int* data, int n, unsigned int* = 0, int target = -1);
    00256     virtual void max(char* data, int n, char* = 0, int target = -1);
    00257     virtual void max(unsigned char* data, int n,
    00258                      unsigned char* = 0, int target = -1);
    00259     virtual void max(signed char* data, int n,
    00260                      signed char* = 0, int target = -1);
    00261     void max(double& data) { max(&data, 1); }
    00262     void max(int& data) { max(&data, 1); }
    00265     virtual void min(double* data, int n, double* = 0, int target = -1);
    00266     virtual void min(int* data, int n, int* = 0, int target = -1);
    00267     virtual void min(unsigned int* data, int n, unsigned int* = 0, int target = -1);
    00268     virtual void min(char* data, int n, char* = 0, int target = -1);
    00269     virtual void min(unsigned char* data, int n,
    00270                      unsigned char* = 0, int target = -1);
    00271     virtual void min(signed char* data, int n,
    00272                      signed char* = 0, int target = -1);
    00273     void min(double& data) { min(&data, 1); }
    00274     void min(int& data) { min(&data, 1); }
    00277     virtual void reduce(double*, int n, GrpReduce<double>&,
    00278                         double*scratch = 0, int target = -1);
    00279     virtual void reduce(int*, int n, GrpReduce<int>&,
    00280                         int*scratch = 0, int target = -1);
    00281     virtual void reduce(unsigned int*, int n, GrpReduce<unsigned int>&,
    00282                         unsigned int*scratch = 0, int target = -1);
    00283     virtual void reduce(char*, int n, GrpReduce<char>&,
    00284                         char*scratch = 0, int target = -1);
    00285     virtual void reduce(unsigned char*, int n, GrpReduce<unsigned char>&,
    00286                         unsigned char*scratch = 0, int target = -1);
    00287     virtual void reduce(signed char*, int n, GrpReduce<signed char>&,
    00288                         signed char*scratch = 0, int target = -1);
    00289     virtual void reduce(short*, int n, GrpReduce<short>&,
    00290                         short*scratch = 0, int target = -1);
    00291     virtual void reduce(float*, int n, GrpReduce<float>&,
    00292                         float*scratch = 0, int target = -1);
    00293     virtual void reduce(long*, int n, GrpReduce<long>&,
    00294                         long*scratch = 0, int target = -1);
    00295     void reduce(double& data, GrpReduce<double>& r) { reduce(&data, 1, r); }
    00296     void reduce(int& data, GrpReduce<int>& r) { reduce(&data, 1, r); }
    00297
    00299     virtual void sync();
    00300
    00306     int classdesc_to_index(const ClassDesc*);
    00307     const ClassDesc* index_to_classdesc(int);
    00308     int nclass() const { return nclass_; }
    00309 };
    00310
    00311 struct message_struct {
    00312   void *buf;
    00313   int size;
    00314   int type;
    00315   struct message_struct *p;
    00316   };
    00317 typedef struct message_struct message_t;
    00318
    00319
    00322 class ProcMessageGrp: public MessageGrp {
    00323   private:
    00324     // Messages are stored in these linked lists
    00325     message_t *sync_messages;
    00326     message_t *type_messages;
    00327
    00328     void sendit(message_t *& messages, int dest, int msgtype, void* buf, int bytes);
    00329     void recvit(message_t *& messages, int source, int type, void* buf, int bytes,
    00330                 int& last_size, int& last_type);
    00331
    00332   public:
    00333     ProcMessageGrp();
    00334     ProcMessageGrp(const Ref<KeyVal>&);
    00335     ~ProcMessageGrp();
    00336
    00337     Ref<MessageGrp> clone(void);
    00338
    00339     void raw_send(int target, void* data, int nbyte);
    00340     void raw_sendt(int target, int type, void* data, int nbyte);
    00341     void raw_recv(int sender, void* data, int nbyte);
    00342     void raw_recvt(int type, void* data, int nbyte);
    00343     void raw_bcast(void* data, int nbyte, int from);
    00344     int probet(int type);
    00345     void sync();
    00346 };
    00347
    00352 class intMessageGrp: public MessageGrp {
    00353   protected:
    00354     int msgtype_nbit; // the total number of bits available
    00355     int ctl_nbit; // control information bits
    00356     int seq_nbit; // sequence information bits
    00357     int typ_nbit; // type information bits
    00358     int src_nbit; // source information bits
    00359
    00360     // Masks for the fields in the type.
    00361     int ctl_mask;
    00362     int seq_mask;
    00363     int typ_mask;
    00364     int src_mask;
    00365
    00366     // Shifts to construct a message type.
    00367     int ctl_shift;
    00368     int seq_shift;
    00369     int typ_shift;
    00370     int src_shift;
    00371
    00372     int msgtype_typ(int msgtype);
    00373     int typ_msgtype(int usrtype);
    00374     int seq_msgtype(int source, int seq);
    00375
    00376     // The next sequence number for each node is stored in these.
    00377     int *source_seq;
    00378     int *target_seq;
    00379
    00381     virtual void basic_send(int target, int type, void* data, int nbyte) = 0;
    00383     virtual void basic_recv(int type, void* data, int nbyte) = 0;
    00385     virtual int basic_probe(int type) = 0;
    00386
    00387     intMessageGrp();
    00388     intMessageGrp(const Ref<KeyVal>&);
    00389
    00390     void initialize(int me, int n, int nbits);
    00391   public:
    00392     ~intMessageGrp();
    00393
    00394     void raw_send(int target, void* data, int nbyte);
    00395     void raw_recv(int sender, void* data, int nbyte);
    00396     void raw_sendt(int target, int type, void* data, int nbyte);
    00397     void raw_recvt(int type, void* data, int nbyte);
    00398
    00399     int probet(int);
    00400
    00401     int leftover_ctl_bits();
    00402 };
    00403
    00404 }
    00405
    00406 #include <util/group/messaget.h>
    00407
    00408 #endif
    00409
    00410
    00411 // Local Variables:
    00412 // mode: c++
    00413 // c-file-style: "CLJ"
    00414 // End: