- Main Page
- Classes
- Files
- 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     ~GrpSumReduce() {};
    00056     void reduce(T*target, T*data, int nelement);
    00057 };
    00058
    00059 template <class T>
    00060 class GrpMinReduce: public GrpReduce<T> {
    00061   public:
    00062     ~GrpMinReduce() {};
    00063     void reduce(T*target, T*data, int nelement);
    00064 };
    00065
    00066 template <class T>
    00067 class GrpMaxReduce: public GrpReduce<T> {
    00068   public:
    00069     ~GrpMaxReduce() {};
    00070     void reduce(T*target, T*data, int nelement);
    00071 };
    00072
    00073 template <class T>
    00074 class GrpArithmeticAndReduce: public GrpReduce<T> {
    00075   public:
    00076     void reduce(T*target, T*data, int nelement);
    00077 };
    00078
    00079 template <class T>
    00080 class GrpArithmeticOrReduce: public GrpReduce<T> {
    00081   public:
    00082     void reduce(T*target, T*data, int nelement);
    00083 };
    00084
    00085 template <class T>
    00086 class GrpArithmeticXOrReduce: public GrpReduce<T> {
    00087   public:
    00088     void reduce(T*target, T*data, int nelement);
    00089 };
    00090
    00091 template <class T>
    00092 class GrpProductReduce: public GrpReduce<T> {
    00093   public:
    00094     void reduce(T*target, T*data, int nelement);
    00095 };
    00096
    00097 template <class T>
    00098 class GrpFunctionReduce: public GrpReduce<T> {
    00099   private:
    00100     void (*func_)(T*target,T*data,int nelement);
    00101   public:
    00102     GrpFunctionReduce(void(*func)(T*,T*,int)):func_(func) {}
    00103     void reduce(T*target, T*data, int nelement);
    00104 };
    00105
    00109 class MessageGrp: public DescribedClass {
    00110   private:
    00111     // These are initialized by the initialize() member (see below).
    00112     int me_;
    00113     int n_;
    00114     int nclass_;
    00115     int gop_max_;
    00116     std::map<ClassDescP,int> classdesc_to_index_;
    00117     ClassDescP *index_to_classdesc_;
    00118   protected:
    00125     void initialize(int me, int n);
    00126
    00127     Ref<MachineTopology> topology_;
    00128
    00129     int debug_;
    00130   public:
    00131     MessageGrp();
    00132     MessageGrp(const Ref<KeyVal>&);
    00133     virtual ~MessageGrp();
    00134
    00136     int n() { return n_; }
    00138     int me() { return me_; }
    00139
    00142     virtual Ref<MessageGrp> clone(void)=0;
    00143
    00146     static void set_default_messagegrp(const Ref<MessageGrp>&);
    00148     static MessageGrp* get_default_messagegrp();
    00149
    00156     static MessageGrp* initial_messagegrp(int &argc, char** &argv);
    00157
    00160     virtual void send(int target, const double* data, int ndata);
    00161     virtual void send(int target, const unsigned int* data, int ndata);
    00162     virtual void send(int target, const int* data, int ndata);
    00163     virtual void send(int target, const char* data, int nbyte);
    00164     virtual void send(int target, const unsigned char* data, int nbyte);
    00165     virtual void send(int target, const signed char* data, int nbyte);
    00166     virtual void send(int target, const short* data, int ndata);
    00167     virtual void send(int target, const long* data, int ndata);
    00168     virtual void send(int target, const float* data, int ndata);
    00169     void send(int target, double data) { send(target,&data,1); }
    00170     void send(int target, int data) { send(target,&data,1); }
    00171     virtual void raw_send(int target, const void* data, int nbyte) = 0;
    00172
    00175     virtual void sendt(int target, int type, const double* data, int ndata);
    00176     virtual void sendt(int target, int type, const unsigned int* data, int ndata);
    00177     virtual void sendt(int target, int type, const int* data, int ndata);
    00178     virtual void sendt(int target, int type, const char* data, int nbyte);
    00179     virtual void sendt(int target, int type, const unsigned char* data, int nbyte);
    00180     virtual void sendt(int target, int type, const signed char* data, int nbyte);
    00181     virtual void sendt(int target, int type, const short* data, int ndata);
    00182     virtual void sendt(int target, int type, const long* data, int ndata);
    00183     virtual void sendt(int target, int type, const float* data, int ndata);
    00184     void sendt(int target, int type, double data) {sendt(target,type,&data,1);}
    00185     void sendt(int target, int type, int data) {sendt(target,type,&data,1);}
    00186     virtual void raw_sendt(int target, int type, const void* data, int nbyte) = 0;
    00187
    00190     virtual void recv(int sender, double* data, int ndata);
    00191     virtual void recv(int sender, unsigned int* data, int ndata);
    00192     virtual void recv(int sender, int* data, int ndata);
    00193     virtual void recv(int sender, char* data, int nbyte);
    00194     virtual void recv(int sender, unsigned char* data, int nbyte);
    00195     virtual void recv(int sender, signed char* data, int nbyte);
    00196     virtual void recv(int sender, short* data, int ndata);
    00197     virtual void recv(int sender, long* data, int ndata);
    00198     virtual void recv(int sender, float* data, int ndata);
    00199     void recv(int sender, double& data) { recv(sender,&data,1); }
    00200     void recv(int sender, int& data) { recv(sender,&data,1); }
    00201     virtual void raw_recv(int sender, void* data, int nbyte) = 0;
    00202
    00205     virtual void recvt(int type, double* data, int ndata);
    00206     virtual void recvt(int type, unsigned int* data, int ndata);
    00207     virtual void recvt(int type, int* data, int ndata);
    00208     virtual void recvt(int type, char* data, int nbyte);
    00209     virtual void recvt(int type, unsigned char* data, int nbyte);
    00210     virtual void recvt(int type, signed char* data, int nbyte);
    00211     virtual void recvt(int type, short* data, int ndata);
    00212     virtual void recvt(int type, long* data, int ndata);
    00213     virtual void recvt(int type, float* data, int ndata);
    00214     void recvt(int type, double& data) { recvt(type,&data,1); }
    00215     void recvt(int type, int& data) { recvt(type,&data,1); }
    00216     virtual void raw_recvt(int type, void* data, int nbyte) = 0;
    00217
    00219     virtual int probet(int type) = 0;
    00220
    00223     virtual void bcast(double* data, int ndata, int from = 0);
    00224     virtual void bcast(unsigned int* data, int ndata, int from = 0);
    00225     virtual void bcast(int* data, int ndata, int from = 0);
    00226     virtual void bcast(char* data, int nbyte, int from = 0);
    00227     virtual void bcast(unsigned char* data, int nbyte, int from = 0);
    00228     virtual void bcast(signed char* data, int nbyte, int from = 0);
    00229     virtual void bcast(short* data, int ndata, int from = 0);
    00230     virtual void bcast(long* data, int ndata, int from = 0);
    00231     virtual void bcast(float* data, int ndata, int from = 0);
    00232     virtual void raw_bcast(void* data, int nbyte, int from = 0);
    00233     void bcast(double& data, int from = 0) { bcast(&data, 1, from); }
    00234     void bcast(int& data, int from = 0) { bcast(&data, 1, from); }
    00235
    00238     virtual void raw_collect(const void *part, const int *lengths,
    00239                              void *whole, int bytes_per_datum=1);
    00240     void collect(const double *part, const int *lengths, double *whole);
    00241
    00244     virtual void sum(double* data, int n, double* = 0, int target = -1);
    00245     virtual void sum(unsigned int* data, int n, unsigned int* = 0, int target = -1);
    00246     virtual void sum(int* data, int n, int* = 0, int target = -1);
    00247     virtual void sum(char* data, int n, char* = 0, int target = -1);
    00248     virtual void sum(unsigned char* data, int n,
    00249                      unsigned char* = 0, int target = -1);
    00250     virtual void sum(signed char* data, int n,
    00251                      signed char* = 0, int target = -1);
    00252     void sum(double& data) { sum(&data, 1); }
    00253     void sum(int& data) { sum(&data, 1); }
    00256     virtual void max(double* data, int n, double* = 0, int target = -1);
    00257     virtual void max(int* data, int n, int* = 0, int target = -1);
    00258     virtual void max(unsigned int* data, int n, unsigned int* = 0, int target = -1);
    00259     virtual void max(char* data, int n, char* = 0, int target = -1);
    00260     virtual void max(unsigned char* data, int n,
    00261                      unsigned char* = 0, int target = -1);
    00262     virtual void max(signed char* data, int n,
    00263                      signed char* = 0, int target = -1);
    00264     void max(double& data) { max(&data, 1); }
    00265     void max(int& data) { max(&data, 1); }
    00268     virtual void min(double* data, int n, double* = 0, int target = -1);
    00269     virtual void min(int* data, int n, int* = 0, int target = -1);
    00270     virtual void min(unsigned int* data, int n, unsigned int* = 0, int target = -1);
    00271     virtual void min(char* data, int n, char* = 0, int target = -1);
    00272     virtual void min(unsigned char* data, int n,
    00273                      unsigned char* = 0, int target = -1);
    00274     virtual void min(signed char* data, int n,
    00275                      signed char* = 0, int target = -1);
    00276     void min(double& data) { min(&data, 1); }
    00277     void min(int& data) { min(&data, 1); }
    00280     virtual void reduce(double*, int n, GrpReduce<double>&,
    00281                         double*scratch = 0, int target = -1);
    00282     virtual void reduce(int*, int n, GrpReduce<int>&,
    00283                         int*scratch = 0, int target = -1);
    00284     virtual void reduce(unsigned int*, int n, GrpReduce<unsigned int>&,
    00285                         unsigned int*scratch = 0, int target = -1);
    00286     virtual void reduce(char*, int n, GrpReduce<char>&,
    00287                         char*scratch = 0, int target = -1);
    00288     virtual void reduce(unsigned char*, int n, GrpReduce<unsigned char>&,
    00289                         unsigned char*scratch = 0, int target = -1);
    00290     virtual void reduce(signed char*, int n, GrpReduce<signed char>&,
    00291                         signed char*scratch = 0, int target = -1);
    00292     virtual void reduce(short*, int n, GrpReduce<short>&,
    00293                         short*scratch = 0, int target = -1);
    00294     virtual void reduce(float*, int n, GrpReduce<float>&,
    00295                         float*scratch = 0, int target = -1);
    00296     virtual void reduce(long*, int n, GrpReduce<long>&,
    00297                         long*scratch = 0, int target = -1);
    00298     void reduce(double& data, GrpReduce<double>& r) { reduce(&data, 1, r); }
    00299     void reduce(int& data, GrpReduce<int>& r) { reduce(&data, 1, r); }
    00300
    00302     virtual void sync();
    00303
    00305     Ref<MachineTopology> topology() { return topology_; }
    00306
    00312     int classdesc_to_index(const ClassDesc*);
    00313     const ClassDesc* index_to_classdesc(int);
    00314     int nclass() const { return nclass_; }
    00315 };
    00316
    00317 struct message_struct {
    00318   void *buf;
    00319   int size;
    00320   int type;
    00321   struct message_struct *p;
    00322   };
    00323 typedef struct message_struct message_t;
    00324
    00325
    00328 class ProcMessageGrp: public MessageGrp {
    00329   private:
    00330     // Messages are stored in these linked lists
    00331     message_t *sync_messages;
    00332     message_t *type_messages;
    00333
    00334     void sendit(message_t *& messages, int dest, int msgtype, const void* buf, int bytes);
    00335     void recvit(message_t *& messages, int source, int type, void* buf, int bytes,
    00336                 int& last_size, int& last_type);
    00337
    00338   public:
    00339     ProcMessageGrp();
    00340     ProcMessageGrp(const Ref<KeyVal>&);
    00341     ~ProcMessageGrp();
    00342
    00343     Ref<MessageGrp> clone(void);
    00344
    00345     void raw_send(int target, const void* data, int nbyte);
    00346     void raw_sendt(int target, int type, const void* data, int nbyte);
    00347     void raw_recv(int sender, void* data, int nbyte);
    00348     void raw_recvt(int type, void* data, int nbyte);
    00349     void raw_bcast(void* data, int nbyte, int from);
    00350     int probet(int type);
    00351     void sync();
    00352 };
    00353
    00358 class intMessageGrp: public MessageGrp {
    00359   protected:
    00360     int msgtype_nbit; // the total number of bits available
    00361     int ctl_nbit; // control information bits
    00362     int seq_nbit; // sequence information bits
    00363     int typ_nbit; // type information bits
    00364     int src_nbit; // source information bits
    00365
    00366     // Masks for the fields in the type.
    00367     int ctl_mask;
    00368     int seq_mask;
    00369     int typ_mask;
    00370     int src_mask;
    00371
    00372     // Shifts to construct a message type.
    00373     int ctl_shift;
    00374     int seq_shift;
    00375     int typ_shift;
    00376     int src_shift;
    00377
    00378     int msgtype_typ(int msgtype);
    00379     int typ_msgtype(int usrtype);
    00380     int seq_msgtype(int source, int seq);
    00381
    00382     // The next sequence number for each node is stored in these.
    00383     int *source_seq;
    00384     int *target_seq;
    00385
    00387     virtual void basic_send(int target, int type, const void* data, int nbyte) = 0;
    00389     virtual void basic_recv(int type, void* data, int nbyte) = 0;
    00391     virtual int basic_probe(int type) = 0;
    00392
    00393     intMessageGrp();
    00394     intMessageGrp(const Ref<KeyVal>&);
    00395
    00396     void initialize(int me, int n, int nbits);
    00397   public:
    00398     ~intMessageGrp();
    00399
    00400     void raw_send(int target, const void* data, int nbyte);
    00401     void raw_recv(int sender, void* data, int nbyte);
    00402     void raw_sendt(int target, int type, const void* data, int nbyte);
    00403     void raw_recvt(int type, void* data, int nbyte);
    00404
    00405     int probet(int);
    00406
    00407     int leftover_ctl_bits();
    00408 };
    00409
    00410 }
    00411
    00412 #include <util/group/messaget.h>
    00413
    00414 #endif
    00415
    00416
    00417 // Local Variables:
    00418 // mode: c++
    00419 // c-file-style: "CLJ"
    00420 // End: