OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Message.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
27 #include "Message/Message.h"
28 #include "Utility/Inform.h"
29 #include "Utility/IpplInfo.h"
30 
31 
32 #include <iterator>
33 // Rogue Wave STL for PGI compiler doesn't have iterator_traits!!
34 
35 // By now they have 2002 andreas
36 #ifdef OLD_IPPL_PGI
37 namespace std
38 {
39 
40 template <class _Iterator>
41 struct iterator_traits
42 {
44  typedef typename _Iterator::value_type value_type;
45  typedef typename _Iterator::difference_type difference_type;
46  typedef typename _Iterator::pointer pointer;
47  typedef typename _Iterator::reference reference;
48 };
49 
50 template <class _Tp>
51 struct iterator_traits<_Tp*>
52 {
53  typedef random_access_iterator_tag iterator_category;
54  typedef _Tp value_type;
55  typedef ptrdiff_t difference_type;
56  typedef _Tp* pointer;
57  typedef _Tp& reference;
58 };
59 
60 template <class _Tp>
61 struct iterator_traits<const _Tp*>
62 {
63  typedef random_access_iterator_tag iterator_category;
64  typedef _Tp value_type;
65  typedef ptrdiff_t difference_type;
66  typedef const _Tp* pointer;
67  typedef const _Tp& reference;
68 };
69 
70 }
71 #endif // IPPL_PGI
72 
73 
74 
75 #include <new>
76 #include <memory>
77 #include <cstdlib>
78 #include <cstddef>
79 
80 // MWERKS: moved macro definitions for MessageTypeIntrinsic to Message.h
81 
82 // MWERKS: moved PutSingleItem definitions to Message.h
83 
84 // definitions for non-templated static member functions of PutSingleItem
85 // can remain here. JCC
86 
87 // specialization to a non-built-in type, which is never assumed to be a ptr
88 
89 template <class T>
90 Message&
92 {
93  T& value = const_cast<T&>(cvalue);
94  value.putMessage(msg);
95  return msg;
96 }
97 
98 template <class T>
99 Message&
101 {
102  value.getMessage(msg);
103  return msg;
104 }
105 
106 template <class T>
107 Message&
109 {
110  // get the value type using iterator traits
111  typedef typename std::iterator_traits<T>::value_type T2;
112 
113  // make sure this is not empty ... if so, just create an empty item
114  if (beg == end)
115  {
116  m.putmsg(0, sizeof(T2), 0);
117  }
118  else
119  {
120  // find the number of elements in this iterator range
121  unsigned int d = 0;
122  T f;
123  for (f = beg; f != end; ++d, ++f);
124 
125  // if DoCopy is false, we must assume the iterators are pointers
126  // to simple data types, and so we can just cast the pointer to
127  // void and store it
128  if (!m.willCopy())
129  {
130 #ifdef __MWERKS__
131  m.putmsg((void*) (&*beg), sizeof(T2), d);
132 #else
133  // Is this bad C++ or mwerks bug? Certain STL implems this might be wrong:
134  m.putmsg((void*) (&*beg), sizeof(T2), d);
135  //m.putmsg((void*) beg, sizeof(T2), d);
136 #endif // __MWERKS__
137  }
138  else
139  {
140  // make a copy ourselves
141  // use malloc to get block, placement new for each element
142  T2* cpydata = static_cast<T2*>( malloc(sizeof(T2) * d) );
143  T2* cpy = cpydata;
144  T i;
145  for (i = beg; i != end; ++i, ++cpy)
146  new (cpy) T2(*i);
147 
148  // put data into this message
149  m.setCopy(false);
150  m.setDelete(true);
151  m.putmsg( (void*) cpydata, sizeof(T2), d );
152  }
153  }
154  return m;
155 }
156 
157 template <class T>
158 Message&
160  const std::vector<size_t>& indices, T beg)
161 {
162  // get the value type using iterator traits
163  typedef typename std::iterator_traits<T>::value_type T2;
164 
165  // make sure this is not empty ... if so, just create an empty item
166  if (indices.begin() == indices.end())
167  {
168  m.putmsg(0, sizeof(T2), 0);
169  }
170  else
171  {
172  // find the number of elements in this iterator range
173  std::vector<size_t>::size_type d = indices.size();
174 
175  // make a copy of the data ourselves
176  // use malloc to get block, placement new for each element
177  T2* cpydata = static_cast<T2*>( malloc(sizeof(T2) * d) );
178  T2* cpy = cpydata;
179  std::vector<size_t>::const_iterator i, iend = indices.end();
180  for (i = indices.begin(); i != iend; ++i, ++cpy)
181  new (cpy) T2(beg[*i]);
182 
183  // put data into this message
184  m.setCopy(false);
185  m.setDelete(true);
186  m.putmsg( (void*) cpydata, sizeof(T2), d );
187  }
188  return m;
189 }
190 
191 template <class T>
192 Message&
194 {
195  // get the value type using iterator traits
196  typedef typename std::iterator_traits<T>::value_type T2;
197 
198  // check to see if there is an item
199  if ( m.empty() )
200  {
201  ERRORMSG("get_iter(): no more items in Message" << endl);
202  }
203  else
204  {
205  // get the next MsgItem off the top of the list
206  Message::MsgItem& mitem = m.item(0);
207 
208  // copy the data pointer
209  T2* data = static_cast<T2*>( mitem.data() );
210  int i;
211  for (i = mitem.numElems(); i > 0; i--)
212  *o++ = *data++;
213 
214  // delete this MsgItem
215  m.get();
216  }
217  return m;
218 }
219 
220 
221 // specialization to a built-in type that is not a pointer
222 
223 template <class T>
224 Message&
226 {
227  T& ncval = const_cast<T&>(value);
228  return msg.putmsg( (void*) &ncval, sizeof(T) );
229 }
230 
231 template <class T>
232 Message&
234 {
235  return msg.getmsg( (void*) &value );
236 }
237 
238 
239 // specialization to a pointer to a built-in type. In this class, we
240 // know that 'T' is a pointer type.
241 
242 
243 template <class T>
244 Message&
246 {
248  return msg.putmsg( (void*) beg,
249  sizeof(value_type),
250  (end - beg) );
251 }
252 
253 template <class T>
254 Message&
256 {
257  return msg.getmsg( (void*) ptr );
258 }
259 
260 template <class T>
261 Message&
263  const std::vector<size_t>& indices, T beg)
264 {
265  return PutSingleItem<T, false, false>::put(m, indices, beg);
266 }
267 
268 template <class T>
269 Message&
271 {
273 }
Definition: rbendmap.h:8
bool willCopy() const
Definition: Message.h:332
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
MsgItem & item(size_t n)
Definition: Message.h:316
std::random_access_iterator_tag iterator_category(const SliceIterator< T > &)
size_t size() const
Definition: Message.h:300
Message & putmsg(void *, int, int=0)
unsigned int numElems() const
Definition: Message.h:230
T * value_type(const SliceIterator< T > &)
Message & get(const T &cval)
Definition: Message.h:484
bool empty() const
Definition: Message.h:308
Message & getmsg(void *)
Message & setDelete(const bool c)
Definition: Message.h:339
Message & setCopy(const bool c)
Definition: Message.h:327
void * data()
Definition: Message.h:252
Inform & endl(Inform &inf)
Definition: Inform.cpp:42