MPQC  2.3.1
message.h
1 //
2 // message.h
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifdef __GNUC__
29 #pragma interface
30 #endif
31 
32 #ifndef _util_group_message_h
33 #define _util_group_message_h
34 
35 #include <map>
36 
37 #include <math.h>
38 #include <util/class/class.h>
39 #include <util/state/state.h>
40 #include <util/keyval/keyval.h>
41 #include <util/group/topology.h>
42 
43 namespace sc {
44 
45 template <class T>
46 class GrpReduce {
47  public:
48  virtual ~GrpReduce() {};
49  virtual void reduce(T*target, T*data, int n) = 0;
50 };
51 
52 template <class T>
53 class GrpSumReduce: public GrpReduce<T> {
54  public:
55  ~GrpSumReduce() {};
56  void reduce(T*target, T*data, int nelement);
57 };
58 
59 template <class T>
60 class GrpMinReduce: public GrpReduce<T> {
61  public:
62  ~GrpMinReduce() {};
63  void reduce(T*target, T*data, int nelement);
64 };
65 
66 template <class T>
67 class GrpMaxReduce: public GrpReduce<T> {
68  public:
69  ~GrpMaxReduce() {};
70  void reduce(T*target, T*data, int nelement);
71 };
72 
73 template <class T>
75  public:
76  void reduce(T*target, T*data, int nelement);
77 };
78 
79 template <class T>
80 class GrpArithmeticOrReduce: public GrpReduce<T> {
81  public:
82  void reduce(T*target, T*data, int nelement);
83 };
84 
85 template <class T>
87  public:
88  void reduce(T*target, T*data, int nelement);
89 };
90 
91 template <class T>
92 class GrpProductReduce: public GrpReduce<T> {
93  public:
94  void reduce(T*target, T*data, int nelement);
95 };
96 
97 template <class T>
98 class GrpFunctionReduce: public GrpReduce<T> {
99  private:
100  void (*func_)(T*target,T*data,int nelement);
101  public:
102  GrpFunctionReduce(void(*func)(T*,T*,int)):func_(func) {}
103  void reduce(T*target, T*data, int nelement);
104 };
105 
109 class MessageGrp: public DescribedClass {
110  private:
111  // These are initialized by the initialize() member (see below).
112  int me_;
113  int n_;
114  int nclass_;
115  int gop_max_;
116  std::map<ClassDescP,int> classdesc_to_index_;
117  ClassDescP *index_to_classdesc_;
118  protected:
125  void initialize(int me, int n);
126 
127  Ref<MachineTopology> topology_;
128 
129  int debug_;
130  public:
131  MessageGrp();
132  MessageGrp(const Ref<KeyVal>&);
133  virtual ~MessageGrp();
134 
136  int n() { return n_; }
138  int me() { return me_; }
139 
142  virtual Ref<MessageGrp> clone(void)=0;
143 
146  static void set_default_messagegrp(const Ref<MessageGrp>&);
149 
156  static MessageGrp* initial_messagegrp(int &argc, char** &argv);
157 
160  virtual void send(int target, const double* data, int ndata);
161  virtual void send(int target, const unsigned int* data, int ndata);
162  virtual void send(int target, const int* data, int ndata);
163  virtual void send(int target, const char* data, int nbyte);
164  virtual void send(int target, const unsigned char* data, int nbyte);
165  virtual void send(int target, const signed char* data, int nbyte);
166  virtual void send(int target, const short* data, int ndata);
167  virtual void send(int target, const long* data, int ndata);
168  virtual void send(int target, const float* data, int ndata);
169  void send(int target, double data) { send(target,&data,1); }
170  void send(int target, int data) { send(target,&data,1); }
171  virtual void raw_send(int target, const void* data, int nbyte) = 0;
172 
175  virtual void sendt(int target, int type, const double* data, int ndata);
176  virtual void sendt(int target, int type, const unsigned int* data, int ndata);
177  virtual void sendt(int target, int type, const int* data, int ndata);
178  virtual void sendt(int target, int type, const char* data, int nbyte);
179  virtual void sendt(int target, int type, const unsigned char* data, int nbyte);
180  virtual void sendt(int target, int type, const signed char* data, int nbyte);
181  virtual void sendt(int target, int type, const short* data, int ndata);
182  virtual void sendt(int target, int type, const long* data, int ndata);
183  virtual void sendt(int target, int type, const float* data, int ndata);
184  void sendt(int target, int type, double data) {sendt(target,type,&data,1);}
185  void sendt(int target, int type, int data) {sendt(target,type,&data,1);}
186  virtual void raw_sendt(int target, int type, const void* data, int nbyte) = 0;
187 
190  virtual void recv(int sender, double* data, int ndata);
191  virtual void recv(int sender, unsigned int* data, int ndata);
192  virtual void recv(int sender, int* data, int ndata);
193  virtual void recv(int sender, char* data, int nbyte);
194  virtual void recv(int sender, unsigned char* data, int nbyte);
195  virtual void recv(int sender, signed char* data, int nbyte);
196  virtual void recv(int sender, short* data, int ndata);
197  virtual void recv(int sender, long* data, int ndata);
198  virtual void recv(int sender, float* data, int ndata);
199  void recv(int sender, double& data) { recv(sender,&data,1); }
200  void recv(int sender, int& data) { recv(sender,&data,1); }
201  virtual void raw_recv(int sender, void* data, int nbyte) = 0;
202 
205  virtual void recvt(int type, double* data, int ndata);
206  virtual void recvt(int type, unsigned int* data, int ndata);
207  virtual void recvt(int type, int* data, int ndata);
208  virtual void recvt(int type, char* data, int nbyte);
209  virtual void recvt(int type, unsigned char* data, int nbyte);
210  virtual void recvt(int type, signed char* data, int nbyte);
211  virtual void recvt(int type, short* data, int ndata);
212  virtual void recvt(int type, long* data, int ndata);
213  virtual void recvt(int type, float* data, int ndata);
214  void recvt(int type, double& data) { recvt(type,&data,1); }
215  void recvt(int type, int& data) { recvt(type,&data,1); }
216  virtual void raw_recvt(int type, void* data, int nbyte) = 0;
217 
219  virtual int probet(int type) = 0;
220 
223  virtual void bcast(double* data, int ndata, int from = 0);
224  virtual void bcast(unsigned int* data, int ndata, int from = 0);
225  virtual void bcast(int* data, int ndata, int from = 0);
226  virtual void bcast(char* data, int nbyte, int from = 0);
227  virtual void bcast(unsigned char* data, int nbyte, int from = 0);
228  virtual void bcast(signed char* data, int nbyte, int from = 0);
229  virtual void bcast(short* data, int ndata, int from = 0);
230  virtual void bcast(long* data, int ndata, int from = 0);
231  virtual void bcast(float* data, int ndata, int from = 0);
232  virtual void raw_bcast(void* data, int nbyte, int from = 0);
233  void bcast(double& data, int from = 0) { bcast(&data, 1, from); }
234  void bcast(int& data, int from = 0) { bcast(&data, 1, from); }
235 
238  virtual void raw_collect(const void *part, const int *lengths,
239  void *whole, int bytes_per_datum=1);
240  void collect(const double *part, const int *lengths, double *whole);
241 
244  virtual void sum(double* data, int n, double* = 0, int target = -1);
245  virtual void sum(unsigned int* data, int n, unsigned int* = 0, int target = -1);
246  virtual void sum(int* data, int n, int* = 0, int target = -1);
247  virtual void sum(char* data, int n, char* = 0, int target = -1);
248  virtual void sum(unsigned char* data, int n,
249  unsigned char* = 0, int target = -1);
250  virtual void sum(signed char* data, int n,
251  signed char* = 0, int target = -1);
252  void sum(double& data) { sum(&data, 1); }
253  void sum(int& data) { sum(&data, 1); }
256  virtual void max(double* data, int n, double* = 0, int target = -1);
257  virtual void max(int* data, int n, int* = 0, int target = -1);
258  virtual void max(unsigned int* data, int n, unsigned int* = 0, int target = -1);
259  virtual void max(char* data, int n, char* = 0, int target = -1);
260  virtual void max(unsigned char* data, int n,
261  unsigned char* = 0, int target = -1);
262  virtual void max(signed char* data, int n,
263  signed char* = 0, int target = -1);
264  void max(double& data) { max(&data, 1); }
265  void max(int& data) { max(&data, 1); }
268  virtual void min(double* data, int n, double* = 0, int target = -1);
269  virtual void min(int* data, int n, int* = 0, int target = -1);
270  virtual void min(unsigned int* data, int n, unsigned int* = 0, int target = -1);
271  virtual void min(char* data, int n, char* = 0, int target = -1);
272  virtual void min(unsigned char* data, int n,
273  unsigned char* = 0, int target = -1);
274  virtual void min(signed char* data, int n,
275  signed char* = 0, int target = -1);
276  void min(double& data) { min(&data, 1); }
277  void min(int& data) { min(&data, 1); }
280  virtual void reduce(double*, int n, GrpReduce<double>&,
281  double*scratch = 0, int target = -1);
282  virtual void reduce(int*, int n, GrpReduce<int>&,
283  int*scratch = 0, int target = -1);
284  virtual void reduce(unsigned int*, int n, GrpReduce<unsigned int>&,
285  unsigned int*scratch = 0, int target = -1);
286  virtual void reduce(char*, int n, GrpReduce<char>&,
287  char*scratch = 0, int target = -1);
288  virtual void reduce(unsigned char*, int n, GrpReduce<unsigned char>&,
289  unsigned char*scratch = 0, int target = -1);
290  virtual void reduce(signed char*, int n, GrpReduce<signed char>&,
291  signed char*scratch = 0, int target = -1);
292  virtual void reduce(short*, int n, GrpReduce<short>&,
293  short*scratch = 0, int target = -1);
294  virtual void reduce(float*, int n, GrpReduce<float>&,
295  float*scratch = 0, int target = -1);
296  virtual void reduce(long*, int n, GrpReduce<long>&,
297  long*scratch = 0, int target = -1);
298  void reduce(double& data, GrpReduce<double>& r) { reduce(&data, 1, r); }
299  void reduce(int& data, GrpReduce<int>& r) { reduce(&data, 1, r); }
300 
302  virtual void sync();
303 
305  Ref<MachineTopology> topology() { return topology_; }
306 
312  int classdesc_to_index(const ClassDesc*);
313  const ClassDesc* index_to_classdesc(int);
314  int nclass() const { return nclass_; }
315 };
316 
318  void *buf;
319  int size;
320  int type;
321  struct message_struct *p;
322  };
323 typedef struct message_struct message_t;
324 
325 
328 class ProcMessageGrp: public MessageGrp {
329  private:
330  // Messages are stored in these linked lists
331  message_t *sync_messages;
332  message_t *type_messages;
333 
334  void sendit(message_t *& messages, int dest, int msgtype, const void* buf, int bytes);
335  void recvit(message_t *& messages, int source, int type, void* buf, int bytes,
336  int& last_size, int& last_type);
337 
338  public:
339  ProcMessageGrp();
340  ProcMessageGrp(const Ref<KeyVal>&);
341  ~ProcMessageGrp();
342 
343  Ref<MessageGrp> clone(void);
344 
345  void raw_send(int target, const void* data, int nbyte);
346  void raw_sendt(int target, int type, const void* data, int nbyte);
347  void raw_recv(int sender, void* data, int nbyte);
348  void raw_recvt(int type, void* data, int nbyte);
349  void raw_bcast(void* data, int nbyte, int from);
350  int probet(int type);
351  void sync();
352 };
353 
358 class intMessageGrp: public MessageGrp {
359  protected:
360  int msgtype_nbit; // the total number of bits available
361  int ctl_nbit; // control information bits
362  int seq_nbit; // sequence information bits
363  int typ_nbit; // type information bits
364  int src_nbit; // source information bits
365 
366  // Masks for the fields in the type.
367  int ctl_mask;
368  int seq_mask;
369  int typ_mask;
370  int src_mask;
371 
372  // Shifts to construct a message type.
373  int ctl_shift;
374  int seq_shift;
375  int typ_shift;
376  int src_shift;
377 
378  int msgtype_typ(int msgtype);
379  int typ_msgtype(int usrtype);
380  int seq_msgtype(int source, int seq);
381 
382  // The next sequence number for each node is stored in these.
383  int *source_seq;
384  int *target_seq;
385 
387  virtual void basic_send(int target, int type, const void* data, int nbyte) = 0;
389  virtual void basic_recv(int type, void* data, int nbyte) = 0;
391  virtual int basic_probe(int type) = 0;
392 
393  intMessageGrp();
394  intMessageGrp(const Ref<KeyVal>&);
395 
396  void initialize(int me, int n, int nbits);
397  public:
398  ~intMessageGrp();
399 
400  void raw_send(int target, const void* data, int nbyte);
401  void raw_recv(int sender, void* data, int nbyte);
402  void raw_sendt(int target, int type, const void* data, int nbyte);
403  void raw_recvt(int type, void* data, int nbyte);
404 
405  int probet(int);
406 
407  int leftover_ctl_bits();
408 };
409 
410 }
411 
412 #include <util/group/messaget.h>
413 
414 #endif
415 
416 
417 // Local Variables:
418 // mode: c++
419 // c-file-style: "CLJ"
420 // End:
sc::GrpArithmeticAndReduce
Definition: message.h:74
sc::MessageGrp::get_default_messagegrp
static MessageGrp * get_default_messagegrp()
Returns the default message group.
sc::intMessageGrp::probet
int probet(int)
Ask if a given typed message has been received.
sc::MessageGrp::clone
virtual Ref< MessageGrp > clone(void)=0
Returns a copy of this MessageGrp specialization that provides an independent communication context.
sc::GrpArithmeticXOrReduce
Definition: message.h:86
sc::MessageGrp::min
virtual void min(double *data, int n, double *=0, int target=-1)
Global minimization.
sc::Ref
A template class that maintains references counts.
Definition: ref.h:332
sc::ProcMessageGrp::probet
int probet(int type)
Ask if a given typed message has been received.
sc::intMessageGrp
Uses integer message types to send and receive messages.
Definition: message.h:358
sc::intMessageGrp::basic_probe
virtual int basic_probe(int type)=0
Must be implemented by specializations.
sc::GrpFunctionReduce
Definition: message.h:98
sc::GrpMinReduce
Definition: message.h:60
sc::MessageGrp::sum
virtual void sum(double *data, int n, double *=0, int target=-1)
Global sum reduction.
sc::ClassDesc
This class is used to contain information about classes.
Definition: class.h:158
sc::MessageGrp::me
int me()
Returns my processor number. In the range [0,n()).
Definition: message.h:138
sc::GrpReduce
Definition: message.h:46
sc::MessageGrp::probet
virtual int probet(int type)=0
Ask if a given typed message has been received.
sc::MessageGrp::n
int n()
Returns the number of processors.
Definition: message.h:136
sc::MessageGrp::recv
virtual void recv(int sender, double *data, int ndata)
Receive messages sent sequentually from the sender.
sc::MessageGrp::set_default_messagegrp
static void set_default_messagegrp(const Ref< MessageGrp > &)
The default message group contains the primary message group to be used by an application.
sc::MessageGrp::topology
Ref< MachineTopology > topology()
Return the MachineTopology object.
Definition: message.h:305
sc::ProcMessageGrp::clone
Ref< MessageGrp > clone(void)
Returns a copy of this MessageGrp specialization that provides an independent communication context.
sc::message_struct
Definition: message.h:317
sc::MessageGrp::raw_collect
virtual void raw_collect(const void *part, const int *lengths, void *whole, int bytes_per_datum=1)
Collect data distributed on the nodes to a big array replicated on each node.
sc::GrpMaxReduce
Definition: message.h:67
sc::MessageGrp::reduce
virtual void reduce(double *, int n, GrpReduce< double > &, double *scratch=0, int target=-1)
Global generic reduction.
sc::MessageGrp::initial_messagegrp
static MessageGrp * initial_messagegrp(int &argc, char **&argv)
Create a message group.
sc::intMessageGrp::basic_send
virtual void basic_send(int target, int type, const void *data, int nbyte)=0
Must be implemented by specializations.
sc::GrpSumReduce
Definition: message.h:53
sc::MessageGrp::max
virtual void max(double *data, int n, double *=0, int target=-1)
Global maximization.
sc::MessageGrp::classdesc_to_index
int classdesc_to_index(const ClassDesc *)
Each message group maintains an association of ClassDesc with a global index so SavableState informat...
sc::MessageGrp::sync
virtual void sync()
Synchronize all of the processors.
sc::DescribedClass
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition: class.h:244
sc::GrpArithmeticOrReduce
Definition: message.h:80
sc::MessageGrp::bcast
virtual void bcast(double *data, int ndata, int from=0)
Do broadcasts of various types of data.
sc::MessageGrp::sendt
virtual void sendt(int target, int type, const double *data, int ndata)
Send typed messages to the target processor.
sc::GrpProductReduce
Definition: message.h:92
sc::ProcMessageGrp
ProcMessageGrp provides a concrete specialization of MessageGrp that supports only one node.
Definition: message.h:328
sc::MessageGrp::initialize
void initialize(int me, int n)
The classdesc_to_index_ and index_to_classdesc_ arrays cannot be initialized by the MessageGrp CTOR,...
sc::ProcMessageGrp::sync
void sync()
Synchronize all of the processors.
sc::MessageGrp
The MessageGrp abstract class provides a mechanism for moving data and objects between nodes in a par...
Definition: message.h:109
sc::intMessageGrp::basic_recv
virtual void basic_recv(int type, void *data, int nbyte)=0
Must be implemented by specializations.
sc::MessageGrp::recvt
virtual void recvt(int type, double *data, int ndata)
Receive messages sent by type.
sc::MessageGrp::send
virtual void send(int target, const double *data, int ndata)
Send messages sequentially to the target processor.

Generated at Sun Jan 26 2020 23:33:04 for MPQC 2.3.1 using the documentation package Doxygen 1.8.16.