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