00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef MESSAGE_H
00012 #define MESSAGE_H
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Utility/Inform.h"
00039 #include "Utility/Pstring.h"
00040 #include "AppTypes/dcomplex.h"
00041 #include <stddef.h>
00042
00043 #ifdef IPPL_STDSTL
00044 #include <vector>
00045 using std::vector;
00046 #else
00047 #include <vector.h>
00048 #endif // IPPL_STDSTL
00049
00050 #ifdef IPPL_USE_STANDARD_HEADERS
00051 #include <iostream>
00052 using namespace std;
00053 #else
00054 #include <iostream.h>
00055 #endif
00056
00057 #include <string.h>
00058 #include <stdlib.h>
00059
00060
00061 class Communicate;
00062 class Message;
00063 ostream& operator<<(ostream& o, const Message& m);
00064
00065
00067
00068
00069
00070 template <class T>
00071 struct MessageTypeIntrinsic {
00072 enum { builtin = 0 };
00073 enum { pointer = 0 };
00074 };
00075
00076 #define DEFINE_BUILTIN_TRAIT_CLASS(T) \
00077 template <> \
00078 struct MessageTypeIntrinsic<T> { \
00079 enum { builtin = 1 }; \
00080 enum { pointer = 0 }; \
00081 }; \
00082 template <> \
00083 struct MessageTypeIntrinsic<T *> { \
00084 enum { builtin = 1 }; \
00085 enum { pointer = 1 }; \
00086 }; \
00087 template <int N> \
00088 struct MessageTypeIntrinsic<T[N]> { \
00089 enum { builtin = 1 }; \
00090 enum { pointer = 1 }; \
00091 };
00092
00093 #define DEFINE_ALL_BUILTIN_TRAIT_CLASS(T) \
00094 DEFINE_BUILTIN_TRAIT_CLASS(T) \
00095 DEFINE_BUILTIN_TRAIT_CLASS(const T)
00096
00097 DEFINE_ALL_BUILTIN_TRAIT_CLASS(bool)
00098 DEFINE_ALL_BUILTIN_TRAIT_CLASS(char)
00099 DEFINE_ALL_BUILTIN_TRAIT_CLASS(unsigned char)
00100 DEFINE_ALL_BUILTIN_TRAIT_CLASS(short)
00101 DEFINE_ALL_BUILTIN_TRAIT_CLASS(unsigned short)
00102 DEFINE_ALL_BUILTIN_TRAIT_CLASS(int)
00103 DEFINE_ALL_BUILTIN_TRAIT_CLASS(unsigned int)
00104 DEFINE_ALL_BUILTIN_TRAIT_CLASS(long)
00105 DEFINE_ALL_BUILTIN_TRAIT_CLASS(unsigned long)
00106 #if defined(IPPL_LONGLONG)
00107 DEFINE_ALL_BUILTIN_TRAIT_CLASS(long long)
00108 #endif
00109 DEFINE_ALL_BUILTIN_TRAIT_CLASS(float)
00110 DEFINE_ALL_BUILTIN_TRAIT_CLASS(double)
00111 DEFINE_ALL_BUILTIN_TRAIT_CLASS(dcomplex)
00112 #if ( defined(IPPL_HAS_TEMPLATED_COMPLEX) && \
00113 !defined(IPPL_USE_SINGLE_PRECISION) )
00114 DEFINE_ALL_BUILTIN_TRAIT_CLASS(fComplex)
00115 #endif
00116
00117
00118
00119
00120
00122
00123
00124
00125 template <class T, bool builtin, bool pointer>
00126 struct PutSingleItem { };
00127
00128
00129 template <class T>
00130 struct PutSingleItem<T, false, false> {
00131
00132 static Message& put(Message&, const T&);
00133
00134 static Message& get(Message&, T&);
00135
00136 static Message& put(Message&, T, T);
00137
00138 static Message& put(Message&, const vector<size_t>&, T);
00139
00140 static Message& get_iter(Message&, T);
00141 };
00142
00143
00144 template <class T>
00145 struct PutSingleItem<T, true, false> {
00146
00147 static Message& put(Message&, const T&);
00148
00149 static Message& get(Message&, T&);
00150 };
00151
00152
00153
00154 template <class T>
00155 struct PutSingleItem<T, true, true> {
00156
00157 static Message& put(Message&, T, T);
00158
00159 static Message& get(Message&, T);
00160
00161 static Message& put(Message&, const vector<size_t>&, T);
00162
00163 static Message& get_iter(Message&, T);
00164 };
00165
00166
00167 class Message {
00168
00169 public:
00170
00171
00172
00173
00174 class MsgItem {
00175 private:
00176
00177 struct MsgItemBuf {
00178 #if defined(IPPL_LONGLONG)
00179 unsigned long long d1, d2, d3, d4;
00180 #else
00181 unsigned long d1, d2, d3, d4;
00182 #endif
00183 MsgItemBuf() { }
00184 MsgItemBuf(const MsgItemBuf& m) : d1(m.d1),d2(m.d2),d3(m.d3),d4(m.d4) {}
00185 ~MsgItemBuf() { }
00186 };
00187
00188 public:
00189
00190 MsgItem() : item(0), elements(0), bytesize(0), needDelete(false) { }
00191
00192
00193 MsgItem(void *d, unsigned int elems, unsigned int totbytes,
00194 bool needcopy, bool needdel) : item(&defbuf), elements(elems),
00195 bytesize(totbytes), needDelete(needdel) {
00196 if (totbytes > 0 && d != 0) {
00197 if (needcopy) {
00198 if (totbytes <= sizeof(MsgItemBuf)) {
00199
00200 needDelete = false;
00201 } else {
00202
00203 item = malloc(totbytes);
00204 needDelete = true;
00205 }
00206 memcpy(item, d, totbytes);
00207 } else {
00208
00209 item = d;
00210 }
00211 } else {
00212
00213 item = 0;
00214 elements = 0;
00215 needDelete = false;
00216 }
00217 }
00218
00219
00220 MsgItem(const MsgItem &m) : item(&defbuf), defbuf(m.defbuf),
00221 elements(m.elements), bytesize(m.bytesize), needDelete(m.needDelete) {
00222
00223 if (m.item != &(m.defbuf))
00224 item = m.item;
00225 }
00226
00227
00228 ~MsgItem() { }
00229
00230
00231 unsigned int numBytes() const { return bytesize; }
00232 unsigned int numElems() const { return elements; }
00233 unsigned int elemSize() const { return (elements>0?bytesize/elements:0); }
00234
00235
00236 bool willNeedDelete() const { return (needDelete && item != 0); }
00237
00238
00239 void cancelDelete() { needDelete = false; }
00240
00241
00242 void *data() { return item; }
00243
00244
00245 void deleteData() {
00246 if (willNeedDelete())
00247 free(item);
00248 }
00249
00250 private:
00251
00252 void *item;
00253
00254
00255 unsigned int elements, bytesize;
00256
00257
00258 MsgItemBuf defbuf;
00259
00260
00261 bool needDelete;
00262 };
00263
00264 public:
00265
00266
00267
00268
00269
00270
00271 Message(unsigned int numelems = 8)
00272 : numRemoved(0), comm(0), commdata(0), DoCopy(true), DoDelete(true) {
00273 MsgItemList.reserve(numelems);
00274 }
00275
00276
00277
00278 ~Message();
00279
00280
00281
00282
00283
00284
00285 size_t size() const { return (MsgItemList.size() - numRemoved); }
00286 size_t removed() const { return numRemoved; }
00287 bool empty() const { return (size() == 0); }
00288
00289
00290
00291
00292 MsgItem& item(size_t n) { return MsgItemList[n + numRemoved]; }
00293 const MsgItem& item(size_t n) const {
00294 return MsgItemList[n+numRemoved];
00295 }
00296
00297
00298
00299 Message& setCopy(const bool c) {
00300 DoCopy = c;
00301 return *this;
00302 }
00303 bool willCopy() const { return DoCopy; }
00304
00305
00306
00307 Message& setDelete(const bool c) {
00308 DoDelete = c;
00309 return *this;
00310 }
00311 bool willDelete() const { return DoDelete; }
00312
00313
00314 Message& clear();
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 void *remove();
00325
00326
00327
00328
00329
00330 void useCommunicate(Communicate *c, void *d) {
00331 comm = c;
00332 commdata = d;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 template <class T>
00377 Message& put(const T& val) {
00378 return PutSingleItem<T,
00379 MessageTypeIntrinsic<T>::builtin,
00380 MessageTypeIntrinsic<T>::pointer>::put(*this, val);
00381 }
00382
00383
00384
00385
00386
00387
00388
00389 Message &put(const char *d) {
00390 return putmsg((void *)d, sizeof(char), strlen(d) + 1);
00391 }
00392
00393 Message& put(const string& s) {
00394 int len = s.length() + 1;
00395 put(len);
00396 put(s.c_str());
00397 return *this;
00398 }
00399
00400
00401
00402
00403
00404 template <class ForwardIterator>
00405 Message& put(ForwardIterator beg, ForwardIterator end) {
00406 return PutSingleItem<ForwardIterator,
00407 MessageTypeIntrinsic<ForwardIterator>::builtin,
00408 MessageTypeIntrinsic<ForwardIterator>::pointer>::put(
00409 *this, beg, end);
00410 }
00411
00412
00413
00414
00415
00416 template <class RandomAccessIterator>
00417 Message& put(const vector<size_t>& indices,
00418 RandomAccessIterator beg) {
00419 return PutSingleItem<RandomAccessIterator,
00420 MessageTypeIntrinsic<RandomAccessIterator>::builtin,
00421 MessageTypeIntrinsic<RandomAccessIterator>::pointer>::put(*this,
00422 indices, beg);
00423 }
00424
00425
00426
00427
00428 Message &putmsg(void *, int, int = 0);
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00440
00441 template <class T>
00442 Message& get(const T& cval) {
00443 T& val = const_cast<T&>(cval);
00444 return PutSingleItem<T,
00445 MessageTypeIntrinsic<T>::builtin,
00446 MessageTypeIntrinsic<T>::pointer>::get(*this, val);
00447 }
00448
00449
00450 Message& get(const string& s) {
00451 string& ncs = const_cast<string&>(s);
00452 int len;
00453 get(len);
00454 char* cstring = new char[len];
00455 get(cstring);
00456 ncs = cstring;
00457 delete [] cstring;
00458 return *this;
00459 }
00460
00461
00462 Message &get() { return getmsg(0); }
00463
00464
00465
00466
00468
00469
00470
00471 template <class OutputIterator>
00472 Message& get_iter(OutputIterator o) {
00473 return PutSingleItem<OutputIterator,
00474 MessageTypeIntrinsic<OutputIterator>::builtin,
00475 MessageTypeIntrinsic<OutputIterator>::pointer>::get_iter(*this, o);
00476 }
00477
00478
00479
00480
00481 Message &getmsg(void *);
00482
00483 private:
00484
00485 vector<MsgItem> MsgItemList;
00486
00487
00488 size_t numRemoved;
00489
00490
00491 bool DoCopy;
00492
00493
00494 bool DoDelete;
00495
00496
00497
00498 Communicate *comm;
00499 void *commdata;
00500
00501
00502
00503 void deleteMsgItem();
00504 };
00505
00506
00507
00508 template<class T>
00509 inline void putMessage(Message &m, const T &t) {
00510 m.put(t);
00511 }
00512
00513
00514 template<class ForwardIterator>
00515 inline void putMessage(Message &m, ForwardIterator beg, ForwardIterator end) {
00516 m.put(beg, end);
00517 }
00518
00519
00520 template <class RandomAccessIterator>
00521 inline void putMessage(Message &m, const vector<size_t> &v,
00522 RandomAccessIterator r) {
00523 m.put(v, r);
00524 }
00525
00526
00527
00528 template<class T>
00529 inline void getMessage(Message &m, T &t) {
00530 m.get(t);
00531 }
00532
00533
00534 template<class T>
00535 inline void getMessage(Message &m, T *t) {
00536 m.get_iter(t);
00537 }
00538
00539
00540 template<class T>
00541 inline void getMessage(Message &m, T *t, T *) {
00542 m.get_iter(t);
00543 }
00544
00545
00546
00547
00548 template<class OutputIterator>
00549 void getMessage_iter(Message &m, OutputIterator o) {
00550 m.get_iter(o);
00551 }
00552
00553
00554 #if ( defined(IPPL_MPIXX) || defined(IPPL_PM) )
00555 #define main mpi_main
00556 extern "C" {
00557 int mpi_main(int, char**);
00558 }
00559 #endif // IPPL_MPIXX || IPPL_PM
00560
00561 #include "Message/Message.cpp"
00562
00563 #endif // MESSAGE_H
00564
00565
00566
00567
00568
00569