Google

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 <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 namespace sc {
00043 
00044 template <class T>
00045 class GrpReduce {
00046   public:
00047     virtual ~GrpReduce() {};
00048     virtual void reduce(T*target, T*data, int n) = 0;
00049 };
00050 
00051 template <class T>
00052 class GrpSumReduce: public GrpReduce<T> {
00053   public:
00054     void reduce(T*target, T*data, int nelement);
00055 };
00056 
00057 template <class T>
00058 class GrpMinReduce: public GrpReduce<T> {
00059   public:
00060     void reduce(T*target, T*data, int nelement);
00061 };
00062 
00063 template <class T>
00064 class GrpMaxReduce: public GrpReduce<T> {
00065   public:
00066     void reduce(T*target, T*data, int nelement);
00067 };
00068 
00069 template <class T>
00070 class GrpArithmeticAndReduce: public GrpReduce<T> {
00071   public:
00072     void reduce(T*target, T*data, int nelement);
00073 };
00074 
00075 template <class T>
00076 class GrpArithmeticOrReduce: public GrpReduce<T> {
00077   public:
00078     void reduce(T*target, T*data, int nelement);
00079 };
00080 
00081 template <class T>
00082 class GrpArithmeticXOrReduce: public GrpReduce<T> {
00083   public:
00084     void reduce(T*target, T*data, int nelement);
00085 };
00086 
00087 template <class T>
00088 class GrpProductReduce: public GrpReduce<T> {
00089   public:
00090     void reduce(T*target, T*data, int nelement);
00091 };
00092 
00093 template <class T>
00094 class GrpFunctionReduce: public GrpReduce<T> {
00095   private:
00096     void (*func_)(T*target,T*data,int nelement);
00097   public:
00098     GrpFunctionReduce(void(*func)(T*,T*,int)):func_(func) {}
00099     void reduce(T*target, T*data, int nelement);
00100 };
00101 
00105 class MessageGrp: public DescribedClass {
00106   private:
00107     // These are initialized by the initialize() member (see below).
00108     int me_;
00109     int n_;
00110     int nclass_;
00111     int gop_max_;
00112     AVLMap<ClassDescP,int> classdesc_to_index_;
00113     ClassDescP *index_to_classdesc_;
00114   protected:
00121     void initialize(int me, int n);
00122 
00123     Ref<MachineTopology> topology_;
00124 
00125     int debug_;
00126   public:
00127     MessageGrp();
00128     MessageGrp(const Ref<KeyVal>&);
00129     virtual ~MessageGrp();
00130     
00132     int n() { return n_; }
00134     int me() { return me_; }
00135 
00138     virtual Ref<MessageGrp> clone(void)=0;
00139     
00142     static void set_default_messagegrp(const Ref<MessageGrp>&);
00144     static MessageGrp* get_default_messagegrp();
00145 
00152     static MessageGrp* initial_messagegrp(int &argc, char** &argv);
00153 
00156     virtual void send(int target, double* data, int ndata);
00157     virtual void send(int target, unsigned int* data, int ndata);
00158     virtual void send(int target, int* data, int ndata);
00159     virtual void send(int target, char* data, int nbyte);
00160     virtual void send(int target, unsigned char* data, int nbyte);
00161     virtual void send(int target, signed char* data, int nbyte);
00162     virtual void send(int target, short* data, int ndata);
00163     virtual void send(int target, long* data, int ndata);
00164     virtual void send(int target, float* data, int ndata);
00165     void send(int target, double data) { send(target,&data,1); }
00166     void send(int target, int data) { send(target,&data,1); }
00167     virtual void raw_send(int target, void* data, int nbyte) = 0;
00168 
00171     virtual void sendt(int target, int type, double* data, int ndata);
00172     virtual void sendt(int target, int type, unsigned int* data, int ndata);
00173     virtual void sendt(int target, int type, int* data, int ndata);
00174     virtual void sendt(int target, int type, char* data, int nbyte);
00175     virtual void sendt(int target, int type, unsigned char* data, int nbyte);
00176     virtual void sendt(int target, int type, signed char* data, int nbyte);
00177     virtual void sendt(int target, int type, short* data, int ndata);
00178     virtual void sendt(int target, int type, long* data, int ndata);
00179     virtual void sendt(int target, int type, float* data, int ndata);
00180     void sendt(int target, int type, double data) {sendt(target,type,&data,1);}
00181     void sendt(int target, int type, int data) {sendt(target,type,&data,1);}
00182     virtual void raw_sendt(int target, int type, void* data, int nbyte) = 0;
00183 
00186     virtual void recv(int sender, double* data, int ndata);
00187     virtual void recv(int sender, unsigned int* data, int ndata);
00188     virtual void recv(int sender, int* data, int ndata);
00189     virtual void recv(int sender, char* data, int nbyte);
00190     virtual void recv(int sender, unsigned char* data, int nbyte);
00191     virtual void recv(int sender, signed char* data, int nbyte);
00192     virtual void recv(int sender, short* data, int ndata);
00193     virtual void recv(int sender, long* data, int ndata);
00194     virtual void recv(int sender, float* data, int ndata);
00195     void recv(int sender, double& data) { recv(sender,&data,1); }
00196     void recv(int sender, int& data) { recv(sender,&data,1); }
00197     virtual void raw_recv(int sender, void* data, int nbyte) = 0;
00198 
00201     virtual void recvt(int type, double* data, int ndata);
00202     virtual void recvt(int type, unsigned int* data, int ndata);
00203     virtual void recvt(int type, int* data, int ndata);
00204     virtual void recvt(int type, char* data, int nbyte);
00205     virtual void recvt(int type, unsigned char* data, int nbyte);
00206     virtual void recvt(int type, signed char* data, int nbyte);
00207     virtual void recvt(int type, short* data, int ndata);
00208     virtual void recvt(int type, long* data, int ndata);
00209     virtual void recvt(int type, float* data, int ndata);
00210     void recvt(int type, double& data) { recvt(type,&data,1); }
00211     void recvt(int type, int& data) { recvt(type,&data,1); }
00212     virtual void raw_recvt(int type, void* data, int nbyte) = 0;
00213 
00215     virtual int probet(int type) = 0;
00216 
00219     virtual void bcast(double* data, int ndata, int from = 0);
00220     virtual void bcast(unsigned int* data, int ndata, int from = 0);
00221     virtual void bcast(int* data, int ndata, int from = 0);
00222     virtual void bcast(char* data, int nbyte, int from = 0);
00223     virtual void bcast(unsigned char* data, int nbyte, int from = 0);
00224     virtual void bcast(signed char* data, int nbyte, int from = 0);
00225     virtual void bcast(short* data, int ndata, int from = 0);
00226     virtual void bcast(long* data, int ndata, int from = 0);
00227     virtual void bcast(float* data, int ndata, int from = 0);
00228     virtual void raw_bcast(void* data, int nbyte, int from = 0);
00229     void bcast(double& data, int from = 0) { bcast(&data, 1, from); }
00230     void bcast(int& data, int from = 0) { bcast(&data, 1, from); }
00231 
00234     virtual void raw_collect(const void *part, const int *lengths,
00235                              void *whole, int bytes_per_datum=1);
00236     void collect(const double *part, const int *lengths, double *whole);
00237 
00240     virtual void sum(double* data, int n, double* = 0, int target = -1);
00241     virtual void sum(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00242     virtual void sum(int* data, int n, int* = 0, int target = -1);
00243     virtual void sum(char* data, int n, char* = 0, int target = -1);
00244     virtual void sum(unsigned char* data, int n,
00245                      unsigned char* = 0, int target = -1);
00246     virtual void sum(signed char* data, int n,
00247                      signed char* = 0, int target = -1);
00248     void sum(double& data) { sum(&data, 1); }
00249     void sum(int& data) { sum(&data, 1); }
00252     virtual void max(double* data, int n, double* = 0, int target = -1);
00253     virtual void max(int* data, int n, int* = 0, int target = -1);
00254     virtual void max(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00255     virtual void max(char* data, int n, char* = 0, int target = -1);
00256     virtual void max(unsigned char* data, int n,
00257                      unsigned char* = 0, int target = -1);
00258     virtual void max(signed char* data, int n,
00259                      signed char* = 0, int target = -1);
00260     void max(double& data) { max(&data, 1); }
00261     void max(int& data) { max(&data, 1); }
00264     virtual void min(double* data, int n, double* = 0, int target = -1);
00265     virtual void min(int* data, int n, int* = 0, int target = -1);
00266     virtual void min(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00267     virtual void min(char* data, int n, char* = 0, int target = -1);
00268     virtual void min(unsigned char* data, int n,
00269                      unsigned char* = 0, int target = -1);
00270     virtual void min(signed char* data, int n,
00271                      signed char* = 0, int target = -1);
00272     void min(double& data) { min(&data, 1); }
00273     void min(int& data) { min(&data, 1); }
00276     virtual void reduce(double*, int n, GrpReduce<double>&,
00277                         double*scratch = 0, int target = -1);
00278     virtual void reduce(int*, int n, GrpReduce<int>&,
00279                         int*scratch = 0, int target = -1);
00280     virtual void reduce(unsigned int*, int n, GrpReduce<unsigned int>&,
00281                         unsigned int*scratch = 0, int target = -1);
00282     virtual void reduce(char*, int n, GrpReduce<char>&,
00283                         char*scratch = 0, int target = -1);
00284     virtual void reduce(unsigned char*, int n, GrpReduce<unsigned char>&,
00285                         unsigned char*scratch = 0, int target = -1);
00286     virtual void reduce(signed char*, int n, GrpReduce<signed char>&,
00287                         signed char*scratch = 0, int target = -1);
00288     virtual void reduce(short*, int n, GrpReduce<short>&,
00289                         short*scratch = 0, int target = -1);
00290     virtual void reduce(float*, int n, GrpReduce<float>&,
00291                         float*scratch = 0, int target = -1);
00292     virtual void reduce(long*, int n, GrpReduce<long>&,
00293                         long*scratch = 0, int target = -1);
00294     void reduce(double& data, GrpReduce<double>& r) { reduce(&data, 1, r); }
00295     void reduce(int& data, GrpReduce<int>& r) { reduce(&data, 1, r); }
00296 
00298     virtual void sync();
00299 
00305     int classdesc_to_index(const ClassDesc*);
00306     const ClassDesc* index_to_classdesc(int);
00307     int nclass() const { return nclass_; }
00308 };
00309 
00310 struct message_struct {
00311   void *buf;
00312   int size;
00313   int type;
00314   struct message_struct *p;
00315   };
00316 typedef struct message_struct message_t;
00317 
00318 
00321 class ProcMessageGrp: public MessageGrp {
00322   private:
00323     // Messages are stored in these linked lists
00324     message_t *sync_messages;
00325     message_t *type_messages;
00326 
00327     void sendit(message_t *& messages, int dest, int msgtype, void* buf, int bytes);
00328     void recvit(message_t *& messages, int source, int type, void* buf, int bytes,
00329                 int& last_size, int& last_type);
00330         
00331   public:
00332     ProcMessageGrp();
00333     ProcMessageGrp(const Ref<KeyVal>&);
00334     ~ProcMessageGrp();
00335 
00336     Ref<MessageGrp> clone(void);
00337     
00338     void raw_send(int target, void* data, int nbyte);
00339     void raw_sendt(int target, int type, void* data, int nbyte);
00340     void raw_recv(int sender, void* data, int nbyte);
00341     void raw_recvt(int type, void* data, int nbyte);
00342     void raw_bcast(void* data, int nbyte, int from);
00343     int probet(int type);
00344     void sync();
00345 };
00346 
00351 class intMessageGrp: public MessageGrp {
00352   protected:
00353     int msgtype_nbit; // the total number of bits available
00354     int ctl_nbit; // control information bits
00355     int seq_nbit; // sequence information bits
00356     int typ_nbit; // type information bits
00357     int src_nbit; // source information bits
00358 
00359     // Masks for the fields in the type.
00360     int ctl_mask;
00361     int seq_mask;
00362     int typ_mask;
00363     int src_mask;
00364 
00365     // Shifts to construct a message type.
00366     int ctl_shift;
00367     int seq_shift;
00368     int typ_shift;
00369     int src_shift;
00370 
00371     int msgtype_typ(int msgtype);
00372     int typ_msgtype(int usrtype);
00373     int seq_msgtype(int source, int seq);
00374 
00375     // The next sequence number for each node is stored in these.
00376     int *source_seq;
00377     int *target_seq;
00378     
00380     virtual void basic_send(int target, int type, void* data, int nbyte) = 0;
00382     virtual void basic_recv(int type, void* data, int nbyte) = 0;
00384     virtual int basic_probe(int type) = 0;
00385 
00386     intMessageGrp();
00387     intMessageGrp(const Ref<KeyVal>&);
00388 
00389     void initialize(int me, int n, int nbits);
00390   public:
00391     ~intMessageGrp();
00392 
00393     void raw_send(int target, void* data, int nbyte);
00394     void raw_recv(int sender, void* data, int nbyte);
00395     void raw_sendt(int target, int type, void* data, int nbyte);
00396     void raw_recvt(int type, void* data, int nbyte);
00397 
00398     int probet(int);
00399 
00400     int leftover_ctl_bits();
00401 };
00402 
00403 }
00404 
00405 #include <util/group/messaget.h>
00406 
00407 #endif
00408 
00409 
00410 // Local Variables:
00411 // mode: c++
00412 // c-file-style: "CLJ"
00413 // End:

Generated at Fri Jan 10 08:14:09 2003 for MPQC 2.1.3 using the documentation package Doxygen 1.2.14.