OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
CompressedBrickIterator.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 COMPRESSED_BRICK_ITERATOR_H
12 #define COMPRESSED_BRICK_ITERATOR_H
13 
14 // include files
15 #include "Field/BrickIterator.h"
16 
17 #include <iostream>
18 
19 template<class T, unsigned Dim>
21 
23 
24 // Global function that lets us fake member templates.
25 template<class T, unsigned D1, unsigned D2>
28  const NDIndex<D1>&, const NDIndex<D2>&);
29 
31 
32 
33 
34 
35 
36 
38 //
39 // A version of BrickIterator that can do compression.
40 // If it detects that the block it is pointing to is constant,
41 // it can point to a single value and set its strides to zero.
42 // The location it uses for that single value must be passed in
43 // to the ctor.
44 //
46 
47 template<class T, unsigned Dim>
49 {
50 public:
51  // Replicate all the ctors from BrickIterator.
52 
53  // Construct w/
54  // a pointer to allocated memory
55  // owned NDIndex
56  // alloc NDIndex
57  // Location to store the compressed value.
59  T& compressed)
60  : BrickIterator<T,Dim>(t,c,a), CompressedData(&compressed)
61  {
62  if (!t) {
64  for (unsigned d=0; d<Dim; d++)
66  }
67  }
68 
69  // Construct w/
70  // a pointer to allocated memory
71  // sizes
72  // Location to store the compressed value.
73  CompressedBrickIterator(T* t, const vec<int,Dim>& v, T& compressed)
74  : BrickIterator<T,Dim>(t,v),CompressedData(&compressed)
75  {
76  if (!t) {
78  for (unsigned d=0; d<Dim; d++)
80  }
81  }
82 
83  // Construct with just a location for the compressed data.
85 
86  // Null ctor for array allocations.
87  // Not functional. You have to overwrite this with a real one to use it.
89 
90  // Construct with just a domain and a compressed value.
91  // This makes it compressed.
92  CompressedBrickIterator(const NDIndex<Dim>& a, T& compressed);
93 
94  // Construct with a regular BrickIterator
95  // and a place to store the compressed value.
97  : BrickIterator<T,Dim>(x), CompressedData(&compressed) {}
98 
99  // return true if it is currently compressed, false otherwise.
100  bool IsCompressed() const
101  {
103  }
104 
105  // Check and see if it can compress.
106  bool CanCompress(const T&) const;
107 
108  // Make it compress to a given value.
109  void Compress(T& val);
110 
111  // Try to sparsify a CompressedBrickIterator.
112  // Return true on success, false otherwise.
113  // If it is already compressed it quickly returns true.
114  bool TryCompress() { return TryCompress(**this); }
115 
116  // Here is a version that lets the user specify a value
117  // to try sparsifying on.
118  bool TryCompress(T val);
119 
120  // Since this has a potentially self-referential pointer,
121  // we need the copy ctor and assignment operator to deal with it.
124 
126 
127  // put data into a message to send to another node
128  // ... for putMessage, the second argument
129  // is used for an optimization for when the entire brick is being
130  // sent. If that is so, do not copy data into a new buffer, just
131  // put the pointer into the message. USE WITH CARE. The default is
132  // tohave putMessage make a copy of the data in the brick before adding
133  // it to the message. In many situations, this is required since the
134  // data referred to by the iterator is not contiguous. getMessage
135  // has no such option, it always does the most efficient thing it can.
136  Message& putMessage(Message& m, bool makecopy = true);
137 
138  // get data out from a message
140 
141  // Permute the order of the loops (given by the first NDIndex)
142  // to correspond to the order in the second NDIndex.
143  // Obviously this would be better done as a member template,
144  // but we can fake it with a global function and some accessor functions.
146  permute(NDIndex<Dim>& current, NDIndex<1>& permuted) const
147  { return ::permute(*this,current,permuted); }
149  permute(NDIndex<Dim>& current, NDIndex<2>& permuted) const
150  { return ::permute(*this,current,permuted); }
152  permute(NDIndex<Dim>& current, NDIndex<3>& permuted) const
153  { return ::permute(*this,current,permuted); }
155  permute(NDIndex<Dim>& current, NDIndex<4>& permuted) const
156  { return ::permute(*this,current,permuted); }
158  permute(NDIndex<Dim>& current, NDIndex<5>& permuted) const
159  { return ::permute(*this,current,permuted); }
161  permute(NDIndex<Dim>& current, NDIndex<6>& permuted) const
162  { return ::permute(*this,current,permuted); }
163  // The global function permute needs some special accessor functions.
165  void SetCount(int d, int count) { BrickCounter<Dim>::Counts[d] = count; }
167  void SetStride(int d, int stride) { BrickIterator<T,Dim>::Strides[d] = stride; }
168  int GetStride(int d) const { return BrickIterator<T,Dim>::Strides[d]; }
169  T& GetCompressedData() const { return *CompressedData; }
170  void SetCompressedData(T *newData)
171  {
172  CompressedData = newData;
173  }
174 
175 private:
176  // If you are able to be compressed, put the constant value here.
178 
179 };
180 
181 
183 
184 #endif // COMPRESSED_BRICK_ITERATOR_H
185 
186 /***************************************************************************
187  * $RCSfile: CompressedBrickIterator.h,v $ $Author: adelmann $
188  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:26 $
189  * IPPL_VERSION_ID: $Id: CompressedBrickIterator.h,v 1.1.1.1 2003/01/23 07:40:26 adelmann Exp $
190  ***************************************************************************/
191 
const unsigned Dim
#define X(arg)
Definition: fftpack.cpp:112
CompressedBrickIterator< T, D2 > permute(const CompressedBrickIterator< T, D1 > &, const NDIndex< D1 > &, const NDIndex< D2 > &)
std::complex< double > a
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
bool CanCompress(const T &) const
CompressedBrickIterator< T, 3 > permute(NDIndex< Dim > &current, NDIndex< 3 > &permuted) const
CompressedBrickIterator< T, 5 > permute(NDIndex< Dim > &current, NDIndex< 5 > &permuted) const
CompressedBrickIterator< T, 6 > permute(NDIndex< Dim > &current, NDIndex< 6 > &permuted) const
CompressedBrickIterator< T, 1 > permute(NDIndex< Dim > &current, NDIndex< 1 > &permuted) const
void SetStride(int d, int stride)
Message & getMessage(Message &m)
CompressedBrickIterator(const BrickIterator< T, Dim > &x, T &compressed)
CompressedBrickIterator< T, 4 > permute(NDIndex< Dim > &current, NDIndex< 4 > &permuted) const
CompressedBrickIterator(T *t, const vec< int, Dim > &v, T &compressed)
void SetCount(int d, int count)
CompressedBrickIterator(T *t, const NDIndex< Dim > &c, const NDIndex< Dim > &a, T &compressed)
Message & putMessage(Message &m, bool makecopy=true)
CompressedBrickIterator< T, 2 > permute(NDIndex< Dim > &current, NDIndex< 2 > &permuted) const
const CompressedBrickIterator< T, Dim > & operator=(const CompressedBrickIterator< T, Dim > &rhs)