OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
LSIndex.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef LSINDEX_H
12 #define LSINDEX_H
13 
14 // include files
15 #include "Index/SOffset.h"
16 #include "FieldLayout/Vnode.h"
17 #include "Utility/RefCounted.h"
18 #include "Utility/Vec.h"
19 
20 #include <vector>
21 
22 /***********************************************************************
23  *
24  * LSIndex represents a set of single-point indices for a Field, just
25  * for a single vnode. SIndex contains a list of these LSIndex objects.
26  * Expressions involving sparse indices are constrained to have the same
27  * number of indices in the corresponding LSIndex objects, even though
28  * the indices themselves do not have to be the same.
29  *
30  ***********************************************************************/
31 
32 template<unsigned int Dim>
33 class LSIndex : public RefCounted {
34 
35 public:
36  // useful typedefs
37  typedef std::vector< SOffset<Dim> > container_t;
38  typedef typename container_t::iterator iterator;
39  typedef typename container_t::const_iterator const_iterator;
40  typedef typename container_t::size_type size_type;
41 
42 public:
43  // constructors
45  : compressed(false), VN(vn)
46  {
47  Strides[0] = 1;
48  for (unsigned int d=1; d < Dim; ++d)
49  Strides[d] = Strides[d-1] * vn->getDomain()[d-1].length();
50  }
51  LSIndex(const LSIndex<Dim>& lsi)
53  VN(lsi.VN), Strides(lsi.Strides) { }
54 
55  // destructor
56  ~LSIndex() { }
57 
58  // change this LSIndex to store the values in the given LSIndex instead
59  LSIndex& operator=(const LSIndex& lsi) {
60  VN = lsi.VN;
61  IndexList = lsi.IndexList;
62  compressed = lsi.compressed;
63  Strides = lsi.Strides;
64  return *this;
65  }
66 
67  // are we compressed?
68  bool IsCompressed() const { return compressed; }
69 
70  // compress this list
71  void Compress(bool docompress) {
72  clear();
73  compressed = docompress;
74  }
75 
76  // check to see if the given point would go here
77  bool contains(const SOffset<Dim> &indx) {
78  return indx.inside( (VN->getDomain()) );
79  }
80 
81  // add a new point ... to do explicit checking for errors, call contains()
82  // before adding a point.
83  void addIndex(const SOffset<Dim> &indx) {
84  compressed = false;
85  IndexList.push_back(indx);
86  }
87 
88  // return the Nth index
89  SOffset<Dim>& getIndex(unsigned int n) {
90  if (compressed) {
91  int mval = n;
92  for (unsigned int d=(Dim-1); d >= 1; --d) {
93  int dval = mval / Strides[d];
94  mval -= dval * Strides[d];
95  CompressedPoint[d] = dval + VN->getDomain()[d].first();
96  }
97  CompressedPoint[0] = mval + VN->getDomain()[0].first();
98 
99  //Inform dbgmsg("LSIndex::getIndex", INFORM_ALL_NODES);
100  //dbgmsg << "For dom=" << VN->getDomain() << ": mapped n=" << n;
101  //dbgmsg << " to SOffset=" << CompressedPoint << endl;
102 
103  return CompressedPoint;
104  }
105 
106  // if we're here, not compressed, so just return Nth point
107  return IndexList[n];
108  }
109 
110  // return a copy of the Nth index
111  SOffset<Dim> getIndex(unsigned int n) const {
112  if (compressed) {
113  SOffset<Dim> retval;
114  int mval = n;
115  for (unsigned int d=(Dim-1); d >= 1; --d) {
116  int dval = mval / Strides[d];
117  mval -= dval * Strides[d];
118  retval[d] = dval + VN->getDomain()[d].first();
119  }
120  retval[0] = mval + VN->getDomain()[0].first();
121 
122  //Inform dbgmsg("LSIndex::getIndex", INFORM_ALL_NODES);
123  //dbgmsg << "For dom=" << VN->getDomain() << ": mapped n=" << n;
124  //dbgmsg << " to SOffset=" << retval << endl;
125 
126  return retval;
127  }
128 
129  // if we're here, not compressed, so just return Nth point
130  return IndexList[n];
131  }
132 
133  // remove the given point if we have it. Just move the last element
134  // up, the order does not matter
135  void removeIndex(const SOffset<Dim> &indx) {
136  iterator loc = (*this).find(indx);
137  if (loc != end()) {
138  *loc = IndexList.back();
139  IndexList.pop_back();
140  }
141  }
142 
143  // clear out the existing indices
144  void clear() { IndexList.erase(IndexList.begin(), IndexList.end()); }
145 
146  // reserve enough space to hold at least n points
147  void reserve(size_type n) { IndexList.reserve(n); }
148 
149  //
150  // container methods
151  //
152 
153  // return begin/end iterators
154  iterator begin() { return IndexList.begin(); }
155  iterator end() { return IndexList.end(); }
156  const_iterator begin() const { return IndexList.begin(); }
157  const_iterator end() const { return IndexList.end(); }
158 
159  // return size information about the number of index points here
160  size_type capacity() const { return IndexList.capacity(); }
161  size_type size() const {
162  return (compressed ? VN->getDomain().size() : IndexList.size());
163  }
164 
165  // return an iterator to the given point, if we have it; otherwise
166  // return the end iterator
167  iterator find(const SOffset<Dim>& indx) {
168  for (iterator a = begin(); a != end(); ++a)
169  if (*a == indx)
170  return a;
171  return end();
172  }
173 
174  // just return a boolean indicating if we have the given point
175  bool hasIndex(const SOffset<Dim>& indx) const {
176  for (const_iterator a = begin(); a != end(); ++a)
177  if (*a == indx)
178  return true;
179  return false;
180  }
181 
182  //
183  // vnode information methods
184  //
185 
186  // return the local domain of vnode this LSIndex contains points for
187  const NDIndex<Dim>& getDomain() const { return VN->getDomain(); }
188 
189  // return the local procssor of vnode this LSIndex contains points for
190  int getNode() const { return VN->getNode(); }
191 
192 private:
193  // vnode on which this LSIndex stores points
195 
196  // list of points
198 
199  // are we compressed? If so, IndexList should be empty, and all points
200  // in the domain assumed to be here.
203 
204  // strides for computing mapping from N --> (i,j,k)
206 };
207 
208 #endif // LSINDEX_H
209 
210 /***************************************************************************
211  * $RCSfile: LSIndex.h,v $ $Author: adelmann $
212  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:27 $
213  * IPPL_VERSION_ID: $Id: LSIndex.h,v 1.1.1.1 2003/01/23 07:40:27 adelmann Exp $
214  ***************************************************************************/
215 
bool inside(const NDIndex< Dim > &) const
Definition: SOffset.h:254
void removeIndex(const SOffset< Dim > &indx)
Definition: LSIndex.h:135
void addIndex(const SOffset< Dim > &indx)
Definition: LSIndex.h:83
void reserve(size_type n)
Definition: LSIndex.h:147
iterator begin()
Definition: LSIndex.h:154
LSIndex & operator=(const LSIndex &lsi)
Definition: LSIndex.h:59
container_t IndexList
Definition: LSIndex.h:197
const_iterator begin() const
Definition: LSIndex.h:156
SOffset< Dim > CompressedPoint
Definition: LSIndex.h:202
container_t::const_iterator const_iterator
Definition: LSIndex.h:39
iterator find(const SOffset< Dim > &indx)
Definition: LSIndex.h:167
Vnode< Dim > * VN
Definition: LSIndex.h:194
LSIndex(Vnode< Dim > *vn)
Definition: LSIndex.h:44
void Compress(bool docompress)
Definition: LSIndex.h:71
SOffset< Dim > getIndex(unsigned int n) const
Definition: LSIndex.h:111
size_type capacity() const
Definition: LSIndex.h:160
bool compressed
Definition: LSIndex.h:201
bool IsCompressed() const
Definition: LSIndex.h:68
size_type size() const
Definition: LSIndex.h:161
const NDIndex< Dim > & getDomain() const
Definition: LSIndex.h:187
std::vector< SOffset< Dim > > container_t
Definition: LSIndex.h:37
const NDIndex< Dim > & getDomain() const
Definition: Vnode.h:71
SOffset< Dim > & getIndex(unsigned int n)
Definition: LSIndex.h:89
container_t::iterator iterator
Definition: LSIndex.h:38
void clear()
Definition: LSIndex.h:144
container_t::size_type size_type
Definition: LSIndex.h:40
Definition: Vnode.h:22
int getNode() const
Definition: LSIndex.h:190
~LSIndex()
Definition: LSIndex.h:56
vec< int, Dim > Strides
Definition: LSIndex.h:205
std::string::iterator iterator
Definition: MSLang.h:16
bool hasIndex(const SOffset< Dim > &indx) const
Definition: LSIndex.h:175
const unsigned Dim
LSIndex(const LSIndex< Dim > &lsi)
Definition: LSIndex.h:51
const_iterator end() const
Definition: LSIndex.h:157
bool contains(const SOffset< Dim > &indx)
Definition: LSIndex.h:77
iterator end()
Definition: LSIndex.h:155