QDP++
qdp_outersubtype.h
Go to the documentation of this file.
1// -*- C++ -*-
5
6
7#ifndef QDP_OUTERSUBTYPE_H
8#define QDP_OUTERSUBTYPE_H
9
10#include "qdp_allocator.h"
11namespace QDP {
12
14
17
18template<class T>
19class OSubScalar: public QDPSubType<T, OScalar<T> >
20{
21 typedef OScalar<T> C;
22
23public:
24 //OSubScalar(OScalar<T>& a, const Subset& ss): F(a.getF()), s(ss) {}
25 OSubScalar(OScalar<T>& a, const Subset& ss): F(a.getF()), s(&(const_cast<Subset&>(ss))) {}
26 OSubScalar(const OSubScalar& a): F(a.F), s(a.s) {}
28
29 //---------------------------------------------------------
30 // Operators
31 // NOTE: all assignment-like operators except operator= are
32 // inherited from QDPSubType
33
34 inline
35 void operator=(const typename WordType<T>::Type_t& rhs)
36 {
37 this->assign(rhs);
38 }
39
40 inline
41 void operator=(const Zero& rhs)
42 {
43 this->assign(rhs);
44 }
45
46 template<class T1,class C1>
47 inline
48 void operator=(const QDPType<T1,C1>& rhs)
49 {
50 this->assign(rhs);
51 }
52
53 template<class T1,class C1>
54 inline
55 void operator=(const QDPExpr<T1,C1>& rhs)
56 {
57 this->assign(rhs);
58 }
59
60
61 inline
62 void operator=(const OSubScalar& rhs)
63 {
64 this->assign(rhs);
65 }
66
67
68private:
69 // Hide default constructor
70 OSubScalar() {}
71
72public:
73 T* getF() {return F;}
74 T* getF() const {return F;}
75 const Subset& subset() const {return *s;}
76 bool getOwnsMemory() const { return ownsMemory; }
77 bool getOwnsMemory() { return ownsMemory; }
78
79private:
80 T* F;
81 bool ownsMemory;
82 Subset* s;
83
84 //const Subset& s;
85};
86
87
88
89//-------------------------------------------------------------------------------------
91
94template<class T>
95class OSubLattice: public QDPSubType<T, OLattice<T> >
96{
97 typedef OLattice<T> C;
98
99public:
100 //OSubLattice(OLattice<T>& a, const Subset& ss): F(a.getF()), s(ss), ownsMemory(false) {}
101 OSubLattice(OLattice<T>& a, const Subset& ss): F(a.getF()), s(&(const_cast<Subset&>(ss))), ownsMemory(false) {}
103 ownsMemory = a.ownsMemory;
104 s = a.s;
105 if (a.ownsMemory) {
106 //std::cout << "OSubLattice copy ctor, must copy\n";
107 alloc_mem();
108 for( int i = 0 ; i < s->numSiteTable() ; ++i )
109 F[i] = a.F[i];
110 } else {
111 F = a.F;
112 }
113 }
114
115
116#if 1
117 OSubLattice(): s(NULL), F(NULL), ownsMemory(false) {}
118
119
120 void setSubset( const Subset& ss ) {
121 if (F && !ownsMemory)
122 QDP_error_exit("You try to set the subset on an OSubLattice that is a view of an OLattice!");
123 if (F) {
124 free_mem();
125 }
126 s = &(const_cast<Subset&>(ss));
127 alloc_mem();
128 ownsMemory = true;
129 }
130
131
132 OSubLattice(const Subset& ss , OLattice<T>& a): s(&(const_cast<Subset&>(ss))), ownsMemory(true) {
133 alloc_mem();
134 const int *tab = s->siteTable().slice();
135 for( int j = 0 ; j < s->numSiteTable() ; ++j ) {
136 int i = tab[j];
137 F[j] = a.elem(i);
138 }
139 }
140
141
142 void alloc_mem() {
143 //QDP_info("OSubLattice alloc for %d sites",s->numSiteTable());
144 try
145 {
146 F = (T*)QDP::Allocator::theQDPAllocator::Instance().allocate(sizeof(T)*s->numSiteTable(),QDP::Allocator::DEFAULT);
147 }
148 catch(std::bad_alloc)
149 {
150 QDPIO::cerr << "Allocation failed in OSubLattice" << std::endl;
152 QDP_abort(1);
153 }
154 }
155
157 if (ownsMemory) {
158 free_mem();
159 }
160 }
161
165#endif
166
167
168
169
170 //---------------------------------------------------------
171 // Operators
172 // NOTE: all assignment-like operators except operator= are
173 // inherited from QDPType
174
175 inline
176 void operator=(const typename WordType<T>::Type_t& rhs)
177 {
178 this->assign(rhs);
179 }
180
181 inline
182 void operator=(const Zero& rhs)
183 {
184 this->assign(rhs);
185 }
186
187 template<class T1,class C1>
188 inline
189 void operator=(const QDPType<T1,C1>& rhs)
190 {
191 this->assign(rhs);
192 }
193
194 template<class T1,class C1>
195 inline
196 void operator=(const QDPExpr<T1,C1>& rhs)
197 {
198 this->assign(rhs);
199 }
200
201
202 inline
203 void operator=(const OSubLattice& rhs)
204 {
205 this->assign(rhs);
206 }
207
208
209private:
210 // Hide default constructor
211 //OSubLattice() {}
212
213public:
214 bool getOwnsMemory() const { return ownsMemory; }
215 bool getOwnsMemory() { return ownsMemory; }
216 T* getF() {return F;}
217 T* getF() const {return F;}
218 //const Subset& subset() const {return s;}
219 const Subset& subset() const {return *s;}
220
221private:
222 T* F;
223 bool ownsMemory;
224 Subset* s;
225
226 //C& F;
227 //const Subset& s;
228};
229
230
231//-----------------------------------------------------------------------------
232// Traits class for returning the subset-ted class name of a outer grid class
233//-----------------------------------------------------------------------------
234
235template<class T>
237{
239};
240
241
242template<class T>
244{
246};
247
248
249//-----------------------------------------------------------------------------
250// Traits classes to support operations of simple scalars (floating constants,
251// etc.) on QDPTypes
252//-----------------------------------------------------------------------------
253
254template<class T>
256{
257 typedef typename WordType<T>::Type_t Type_t;
258};
259
260
261template<class T>
263{
264 typedef typename WordType<T>::Type_t Type_t;
265};
266
267
268// ------------------------------------------------------------
269// Get Single Precision Types of OuterSubType templates
270// ------------------------------------------------------------
271template<class T>
276
277
278template<class T>
283
284
285// ------------------------------------------------------------
286// Get Single Precision Types of OuterSubType templates
287// ------------------------------------------------------------
288template<class T>
293
294template<class T>
299
300
301//-----------------------------------------------------------------------------
302// Scalar Operations
303//-----------------------------------------------------------------------------
304
306template<class T>
307void zero_rep(OScalar<T>& dest, const Subset& s)
308{
309 zero_rep(dest.field().elem());
310}
311
313template<class T>
315{
316 zero_rep(dest.field().elem());
317}
318
320template<class T1, class T2>
321void copymask(OSubScalar<T2> dest, const OScalar<T1>& mask,
322 const OScalar<T2>& s1)
323{
324 copymask(dest.field().elem(), mask.elem(), s1.elem());
325}
326
327
328//-----------------------------------------------------------------------------
329// Random numbers
331
332template<class T>
334
336template<class T>
338{
339 OLattice<T>& d = dd.field();
340 const Subset& s = dd.subset();
341
342 OScalar<T> r1, r2;
343
344 random(r1(s));
345 random(r2(s));
346
347 fill_gaussian(d.elem(), r1.elem(), r2.elem());
348}
349
350} // namespace QDP
351
352#endif
void free(void *mem)
Free an aligned pointer, which was allocated by us.
Outer grid Lattice type.
Definition qdp_outer.h:264
T & elem(int i)
Definition qdp_outer.h:400
Outer grid Scalar class *‍/.
Definition qdp_outer.h:37
OLattice class narrowed to a subset.
void operator=(const OSubLattice &rhs)
void operator=(const Zero &rhs)
void operator=(const QDPType< T1, C1 > &rhs)
OSubLattice(OLattice< T > &a, const Subset &ss)
void operator=(const typename WordType< T >::Type_t &rhs)
void setSubset(const Subset &ss)
const Subset & subset() const
bool getOwnsMemory() const
OSubLattice(const Subset &ss, OLattice< T > &a)
void operator=(const QDPExpr< T1, C1 > &rhs)
OSubLattice(const OSubLattice &a)
OScalar class narrowed to a subset.
OSubScalar(OScalar< T > &a, const Subset &ss)
void operator=(const QDPExpr< T1, C1 > &rhs)
OSubScalar(const OSubScalar &a)
void operator=(const Zero &rhs)
void operator=(const OSubScalar &rhs)
void operator=(const QDPType< T1, C1 > &rhs)
const Subset & subset() const
bool getOwnsMemory() const
void operator=(const typename WordType< T >::Type_t &rhs)
Expression class for QDP.
Definition qdp_qdpexpr.h:16
void assign(const typename WordType< OScalar< T > >::Type_t &rhs)
QDPType - major type class/container for all QDP objects.
Definition qdp_qdptype.h:29
Subsets - controls how lattices are looped.
Definition qdp_subset.h:39
void fill_gaussian(IScalar< T > &d, IScalar< T > &r1, IScalar< T > &r2)
dest = gaussian
Definition qdp_inner.h:1861
void zero_rep(IScalar< T > &dest)
dest = 0
Definition qdp_inner.h:1841
void copymask(IScalar< T > &d, const IScalar< T1 > &mask, const IScalar< T > &s1)
dest = (mask) ? s1 : dest
Definition qdp_inner.h:1656
void gaussian(OSubScalar< T > &d)
dest = gaussian
Definition qdp_outer.h:1462
void random(OScalar< T > &d)
dest = random
StandardOutputStream cerr
Definition qdp_stdio.cc:22
Yet another random number generator.
void QDP_error_exit(const char *format,...)
Simple error display and abort routine.
Definition qdp_util.cc:93
void QDP_abort(int status)
Panic button.
Catch-alls for all memory allocators.
OSubLattice< typename DoublePrecType< T >::Type_t > Type_t
OSubScalar< typename DoublePrecType< T >::Type_t > Type_t
OSubLattice< typename SinglePrecType< T >::Type_t > Type_t
OSubScalar< typename SinglePrecType< T >::Type_t > Type_t
Find the underlying word type of a field.
Definition qdp_traits.h:29
Simple zero tag.
Definition qdp_traits.h:122