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     GrpReduce() {}
00049     virtual ~GrpReduce() {};
00050     virtual void reduce(T*target, T*data, int n) = 0;
00051 };
00052 
00053 template <class T>
00054 class GrpSumReduce: public GrpReduce<T> {
00055   public:
00056     GrpSumReduce() : GrpReduce<T>() {}
00057     ~GrpSumReduce() {};
00058     void reduce(T*target, T*data, int nelement);
00059 };
00060 
00061 template <class T>
00062 class GrpMinReduce: public GrpReduce<T> {
00063   public:
00064     GrpMinReduce() : GrpReduce<T>() {}
00065     ~GrpMinReduce() {};
00066     void reduce(T*target, T*data, int nelement);
00067 };
00068 
00069 template <class T>
00070 class GrpMaxReduce: public GrpReduce<T> {
00071   public:
00072     GrpMaxReduce() : GrpReduce<T>() {}
00073     ~GrpMaxReduce() {};
00074     void reduce(T*target, T*data, int nelement);
00075 };
00076 
00077 template <class T>
00078 class GrpArithmeticAndReduce: public GrpReduce<T> {
00079   public:
00080     void reduce(T*target, T*data, int nelement);
00081 };
00082 
00083 template <class T>
00084 class GrpArithmeticOrReduce: public GrpReduce<T> {
00085   public:
00086     void reduce(T*target, T*data, int nelement);
00087 };
00088 
00089 template <class T>
00090 class GrpArithmeticXOrReduce: public GrpReduce<T> {
00091   public:
00092     void reduce(T*target, T*data, int nelement);
00093 };
00094 
00095 template <class T>
00096 class GrpProductReduce: public GrpReduce<T> {
00097   public:
00098     void reduce(T*target, T*data, int nelement);
00099 };
00100 
00101 template <class T>
00102 class GrpFunctionReduce: public GrpReduce<T> {
00103   private:
00104     void (*func_)(T*target,T*data,int nelement);
00105   public:
00106     GrpFunctionReduce(void(*func)(T*,T*,int)):func_(func) {}
00107     void reduce(T*target, T*data, int nelement);
00108 };
00109 
00113 class MessageGrp: public DescribedClass {
00114   public:
00115     class MessageInfo {
00116         friend class MessageGrp;
00117       private:
00118         int sender_;
00119         int type_;
00120         int nbyte_;
00121       public:
00122         int sender() const { return sender_; }
00123         int type() const { return type_; }
00124         int nbyte() const { return nbyte_; }
00125     };
00126     enum { AnyType = -1 };
00127     enum { AnySender = -1 };
00128     class MessageHandle {
00129         friend class MessageGrp;
00130       private:
00131         void *id_;
00132       public:
00133         MessageHandle(): id_(0) {}
00134         MessageHandle(const MessageHandle &h): id_(h.id_) {}
00135     };
00136   private:
00137     // These are initialized by the initialize() member (see below).
00138     int me_;
00139     int n_;
00140     int nclass_;
00141     int gop_max_;
00142     std::map<ClassDescP,int> classdesc_to_index_;
00143     ClassDescP *index_to_classdesc_;
00144   protected:
00151     void initialize(int me, int n);
00152 
00153     Ref<MachineTopology> topology_;
00154 
00155     int debug_;
00156 
00157     void set_sender(MessageInfo *info,int sender) {
00158       if (info) info->sender_ = sender;
00159     }
00160     void set_type(MessageInfo *info,int type) {
00161       if (info) info->type_ = type;
00162     }
00163     void set_nbyte(MessageInfo *info,int nbyte) {
00164       if (info) info->nbyte_ = nbyte;
00165     }
00166 
00167     void set_id(MessageHandle *handle,void *id) {
00168       handle->id_ = id;
00169     }
00170     void *get_id(const MessageHandle *handle) {
00171       return handle->id_;
00172     }
00173   public:
00174 
00175     MessageGrp();
00176     MessageGrp(const Ref<KeyVal>&);
00177     virtual ~MessageGrp();
00178     
00180     int n() { return n_; }
00182     int me() { return me_; }
00183 
00186     virtual Ref<MessageGrp> clone(void)=0;
00187     
00190     static void set_default_messagegrp(const Ref<MessageGrp>&);
00192     static MessageGrp* get_default_messagegrp();
00193 
00200     static MessageGrp* initial_messagegrp(int &argc, char** &argv);
00201 
00204     virtual void send(int target, const double* data, int ndata);
00205     virtual void send(int target, const unsigned int* data, int ndata);
00206     virtual void send(int target, const int* data, int ndata);
00207     virtual void send(int target, const char* data, int nbyte);
00208     virtual void send(int target, const unsigned char* data, int nbyte);
00209     virtual void send(int target, const signed char* data, int nbyte);
00210     virtual void send(int target, const short* data, int ndata);
00211     virtual void send(int target, const long* data, int ndata);
00212     virtual void send(int target, const float* data, int ndata);
00213     void send(int target, double data) { send(target,&data,1); }
00214     void send(int target, int data) { send(target,&data,1); }
00215     virtual void raw_send(int target, const void* data, int nbyte) = 0;
00216 
00219     virtual void sendt(int target, int type, const double* data, int ndata,
00220                        bool rcvrdy=false);
00221     virtual void sendt(int target, int type, const unsigned int* data, int ndata,
00222                        bool rcvrdy=false);
00223     virtual void sendt(int target, int type, const int* data, int ndata,
00224                        bool rcvrdy=false);
00225     virtual void sendt(int target, int type, const char* data, int nbyte,
00226                        bool rcvrdy=false);
00227     virtual void sendt(int target, int type, const unsigned char* data, int nbyte,
00228                        bool rcvrdy=false);
00229     virtual void sendt(int target, int type, const signed char* data, int nbyte,
00230                        bool rcvrdy=false);
00231     virtual void sendt(int target, int type, const short* data, int ndata,
00232                        bool rcvrdy=false);
00233     virtual void sendt(int target, int type, const long* data, int ndata,
00234                        bool rcvrdy=false);
00235     virtual void sendt(int target, int type, const float* data, int ndata,
00236                        bool rcvrdy=false);
00237     void sendt(int target, int type, double data,
00238                bool rcvrdy=false) {sendt(target,type,&data,1,rcvrdy);}
00239     void sendt(int target, int type, int data,
00240                bool rcvrdy=false) {sendt(target,type,&data,1,rcvrdy);}
00241     virtual void raw_sendt(int target, int type, const void* data, int nbyte,
00242                            bool rcvrdy=false) = 0;
00243 
00246     virtual void recv(int sender, double* data, int ndata);
00247     virtual void recv(int sender, unsigned int* data, int ndata);
00248     virtual void recv(int sender, int* data, int ndata);
00249     virtual void recv(int sender, char* data, int nbyte);
00250     virtual void recv(int sender, unsigned char* data, int nbyte);
00251     virtual void recv(int sender, signed char* data, int nbyte);
00252     virtual void recv(int sender, short* data, int ndata);
00253     virtual void recv(int sender, long* data, int ndata);
00254     virtual void recv(int sender, float* data, int ndata);
00255     void recv(int sender, double& data) { recv(sender,&data,1); }
00256     void recv(int sender, int& data) { recv(sender,&data,1); }
00257     virtual void raw_recv(int sender, void* data, int nbyte,
00258                           MessageInfo *info=0) = 0;
00259 
00262     virtual void recvt(int sender, int type, double* data, int ndata);
00263     virtual void recvt(int sender, int type, unsigned int* data, int ndata);
00264     virtual void recvt(int sender, int type, int* data, int ndata);
00265     virtual void recvt(int sender, int type, char* data, int nbyte);
00266     virtual void recvt(int sender, int type, unsigned char* data, int nbyte);
00267     virtual void recvt(int sender, int type, signed char* data, int nbyte);
00268     virtual void recvt(int sender, int type, short* data, int ndata);
00269     virtual void recvt(int sender, int type, long* data, int ndata);
00270     virtual void recvt(int sender, int type, float* data, int ndata);
00271     void recvt(int sender, int type, double& data) {
00272       recvt(sender,type,&data,1);
00273     }
00274     void recvt(int sender, int type, int& data) {
00275       recvt(sender,type,&data,1);
00276     }
00277     virtual void raw_recvt(int sender, int type, void* data, int nbyte,
00278                            MessageInfo *info=0) = 0;
00279 
00280     virtual void nb_sendt(int target, int type,
00281                           const double* data, int ndata,
00282                           MessageHandle&,
00283                           bool rcvrdy=false);
00284     virtual void nb_sendt(int target, int type,
00285                           const unsigned int* data, int ndata,
00286                           MessageHandle&,
00287                           bool rcvrdy=false);
00288     virtual void nb_sendt(int target, int type,
00289                           const int* data, int ndata,
00290                           MessageHandle&,
00291                           bool rcvrdy=false);
00292     virtual void nb_sendt(int target, int type,
00293                           const char* data, int nbyte,
00294                           MessageHandle&,
00295                           bool rcvrdy=false);
00296     virtual void nb_sendt(int target, int type,
00297                           const unsigned char* data, int nbyte,
00298                           MessageHandle&,
00299                           bool rcvrdy=false);
00300     virtual void nb_sendt(int target, int type,
00301                           const signed char* data, int nbyte,
00302                           MessageHandle&,
00303                           bool rcvrdy=false);
00304     virtual void nb_sendt(int target, int type,
00305                           const short* data, int ndata,
00306                           MessageHandle&,
00307                           bool rcvrdy=false);
00308     virtual void nb_sendt(int target, int type,
00309                           const long* data, int ndata,
00310                           MessageHandle&,
00311                           bool rcvrdy=false);
00312     virtual void nb_sendt(int target, int type,
00313                           const float* data, int ndata,
00314                           MessageHandle&,
00315                           bool rcvrdy=false);
00316     void nb_sendt(int target, int type, double data,
00317                   MessageHandle&mh,
00318                   bool rcvrdy=false) {
00319       nb_sendt(target,type,&data,1,mh,rcvrdy);
00320     }
00321     void nb_sendt(int target, int type, int data,
00322                   MessageHandle&mh,
00323                   bool rcvrdy=false) {
00324       nb_sendt(target,type,&data,1,mh,rcvrdy);
00325     }
00326     virtual void raw_nb_sendt(int target, int type,
00327                               const void* data, int nbyte,
00328                               MessageHandle&,
00329                               bool rcvrdy=false) = 0;
00330 
00331     virtual void nb_recvt(int sender, int type, double* data, int ndata,
00332                           MessageHandle&);
00333     virtual void nb_recvt(int sender, int type, unsigned int* data, int ndata,
00334                           MessageHandle&);
00335     virtual void nb_recvt(int sender, int type, int* data, int ndata,
00336                           MessageHandle&);
00337     virtual void nb_recvt(int sender, int type, char* data, int nbyte,
00338                           MessageHandle&);
00339     virtual void nb_recvt(int sender, int type, unsigned char* data, int nbyte,
00340                           MessageHandle&);
00341     virtual void nb_recvt(int sender, int type, signed char* data, int nbyte,
00342                           MessageHandle&);
00343     virtual void nb_recvt(int sender, int type, short* data, int ndata,
00344                           MessageHandle&);
00345     virtual void nb_recvt(int sender, int type, long* data, int ndata,
00346                           MessageHandle&);
00347     virtual void nb_recvt(int sender, int type, float* data, int ndata,
00348                           MessageHandle&);
00349     void nb_recvt(int sender, int type, double& data,
00350                   MessageHandle&mh) {
00351       nb_recvt(sender,type,&data,1,mh);
00352     }
00353     void nb_recvt(int sender, int type, int& data,
00354                   MessageHandle&mh) {
00355       nb_recvt(sender,type,&data,1,mh);
00356     }
00357     virtual void raw_nb_recvt(int sender, int type,
00358                               void* data, int nbyte,
00359                               MessageHandle&) = 0;
00360 
00361     virtual void wait(const MessageHandle&,
00362                       MessageInfo *info=0) = 0;
00363 
00365     virtual int probet(int sender, int type, MessageInfo*info=0) = 0;
00366 
00369     virtual void bcast(double* data, int ndata, int from = 0);
00370     virtual void bcast(unsigned int* data, int ndata, int from = 0);
00371     virtual void bcast(int* data, int ndata, int from = 0);
00372     virtual void bcast(char* data, int nbyte, int from = 0);
00373     virtual void bcast(unsigned char* data, int nbyte, int from = 0);
00374     virtual void bcast(signed char* data, int nbyte, int from = 0);
00375     virtual void bcast(short* data, int ndata, int from = 0);
00376     virtual void bcast(long* data, int ndata, int from = 0);
00377     virtual void bcast(float* data, int ndata, int from = 0);
00378     virtual void raw_bcast(void* data, int nbyte, int from = 0);
00379     void bcast(double& data, int from = 0) { bcast(&data, 1, from); }
00380     void bcast(int& data, int from = 0) { bcast(&data, 1, from); }
00381 
00384     virtual void raw_collect(const void *part, const int *lengths,
00385                              void *whole, int bytes_per_datum=1);
00386     void collect(const double *part, const int *lengths, double *whole);
00387 
00390     virtual void sum(double* data, int n, double* = 0, int target = -1);
00391     virtual void sum(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00392     virtual void sum(int* data, int n, int* = 0, int target = -1);
00393     virtual void sum(long* data, int n, long* = 0, int target = -1);
00394     virtual void sum(char* data, int n, char* = 0, int target = -1);
00395     virtual void sum(unsigned char* data, int n,
00396                      unsigned char* = 0, int target = -1);
00397     virtual void sum(signed char* data, int n,
00398                      signed char* = 0, int target = -1);
00399     void sum(double& data) { sum(&data, 1); }
00400     void sum(int& data) { sum(&data, 1); }
00403     virtual void max(double* data, int n, double* = 0, int target = -1);
00404     virtual void max(int* data, int n, int* = 0, int target = -1);
00405     virtual void max(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00406     virtual void max(char* data, int n, char* = 0, int target = -1);
00407     virtual void max(unsigned char* data, int n,
00408                      unsigned char* = 0, int target = -1);
00409     virtual void max(signed char* data, int n,
00410                      signed char* = 0, int target = -1);
00411     void max(double& data) { max(&data, 1); }
00412     void max(int& data) { max(&data, 1); }
00415     virtual void min(double* data, int n, double* = 0, int target = -1);
00416     virtual void min(int* data, int n, int* = 0, int target = -1);
00417     virtual void min(unsigned int* data, int n, unsigned int* = 0, int target = -1);
00418     virtual void min(char* data, int n, char* = 0, int target = -1);
00419     virtual void min(unsigned char* data, int n,
00420                      unsigned char* = 0, int target = -1);
00421     virtual void min(signed char* data, int n,
00422                      signed char* = 0, int target = -1);
00423     void min(double& data) { min(&data, 1); }
00424     void min(int& data) { min(&data, 1); }
00427     virtual void reduce(double*, int n, GrpReduce<double>&,
00428                         double*scratch = 0, int target = -1);
00429     virtual void reduce(int*, int n, GrpReduce<int>&,
00430                         int*scratch = 0, int target = -1);
00431     virtual void reduce(unsigned int*, int n, GrpReduce<unsigned int>&,
00432                         unsigned int*scratch = 0, int target = -1);
00433     virtual void reduce(char*, int n, GrpReduce<char>&,
00434                         char*scratch = 0, int target = -1);
00435     virtual void reduce(unsigned char*, int n, GrpReduce<unsigned char>&,
00436                         unsigned char*scratch = 0, int target = -1);
00437     virtual void reduce(signed char*, int n, GrpReduce<signed char>&,
00438                         signed char*scratch = 0, int target = -1);
00439     virtual void reduce(short*, int n, GrpReduce<short>&,
00440                         short*scratch = 0, int target = -1);
00441     virtual void reduce(float*, int n, GrpReduce<float>&,
00442                         float*scratch = 0, int target = -1);
00443     virtual void reduce(long*, int n, GrpReduce<long>&,
00444                         long*scratch = 0, int target = -1);
00445     void reduce(double& data, GrpReduce<double>& r) { reduce(&data, 1, r); }
00446     void reduce(int& data, GrpReduce<int>& r) { reduce(&data, 1, r); }
00447 
00449     virtual void sync();
00450 
00452     Ref<MachineTopology> topology() { return topology_; }
00453 
00459     int classdesc_to_index(const ClassDesc*);
00460     const ClassDesc* index_to_classdesc(int);
00461     int nclass() const { return nclass_; }
00462 };
00463 
00464 struct message_struct {
00465   void *buf;
00466   int size;
00467   int type;
00468   struct message_struct *p;
00469   };
00470 typedef struct message_struct message_t;
00471 
00472 
00475 class ProcMessageGrp: public MessageGrp {
00476   private:
00477     // Messages are stored in these linked lists
00478     message_t *sync_messages;
00479     message_t *type_messages;
00480 
00481     void sendit(message_t *& messages, int dest, int msgtype, const void* buf, int bytes);
00482     void recvit(message_t *& messages, int source, int type, void* buf, int bytes,
00483                 int& last_size, int& last_type);
00484         
00485   public:
00486     ProcMessageGrp();
00487     ProcMessageGrp(const Ref<KeyVal>&);
00488     ~ProcMessageGrp();
00489 
00490     Ref<MessageGrp> clone(void);
00491     
00492     void raw_send(int target, const void* data, int nbyte);
00493     void raw_sendt(int target, int type, const void* data, int nbyte,
00494                    bool rcvrdy=false);
00495     void raw_recv(int sender, void* data, int nbyte,
00496                   MessageInfo *info=0);
00497     void raw_recvt(int sender, int type, void* data, int nbyte,
00498                    MessageInfo *info=0);
00499     void raw_bcast(void* data, int nbyte, int from);
00500 
00501     void raw_nb_sendt(int sender, int type,
00502                       const void* data, int nbyte,
00503                       MessageHandle&,
00504                       bool rcvrdy=false);
00505     void raw_nb_recvt(int sender, int type,
00506                       void* data, int nbyte,
00507                       MessageHandle&);
00508     void wait(const MessageHandle&,
00509               MessageInfo *info=0);
00510 
00511     int probet(int sender, int type, MessageInfo *info=0);
00512     void sync();
00513 };
00514 
00515 }
00516 
00517 #include <util/group/messaget.h>
00518 
00519 #endif
00520 
00521 
00522 // Local Variables:
00523 // mode: c++
00524 // c-file-style: "CLJ"
00525 // End:

Generated at Wed Sep 5 14:02:30 2007 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.5.2.
These pages are hosted on SourceForge.net