HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Vector.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Vector of arbitrary size (C++)
7  *
8  * COMMENTS: From Numerical Recipes
9  * And contains a permutation vector class.
10  *
11  */
12 
13 #ifndef __UT_Vector_H__
14 #define __UT_Vector_H__
15 
16 #include "UT_API.h"
17 #include <stdlib.h>
18 #include <iosfwd>
19 #include <SYS/SYS_Types.h>
20 #include "UT_Assert.h"
21 #include "UT_ThreadedAlgorithm.h"
22 
23 #include "UT_VectorTypes.h"
24 
25 //////////////////////////////////////////////////////////////////////////////
26 //
27 // UT_VectorT
28 //
29 
30 template <typename T>
31 class UT_VectorT
32 {
33 public:
34 
35  typedef T value_type;
36 
37  /// Input the index range [nl..nh].
38  UT_VectorT() { myVector = 0; myOwnData = 0; }
39  UT_VectorT(exint nl, exint nh);
40  UT_VectorT(const UT_VectorT<T> &v);
41  ~UT_VectorT();
42 
43  /// Initialize nl, nh and allocate space.
44  void init(exint nl, exint nh);
45 
46  /// Steal from another vector, resulting vector has origin at 1.
47  void subvector(const UT_VectorT<T> &v, exint nl, exint nh);
48 
49  /// Assign from a float array into the designated subvector, performing a
50  /// deep copy.
51  void assign(const fpreal32 *data, exint nl, exint nh);
52  void assign(const fpreal64 *data, exint nl, exint nh);
53 
54  int isInit() const { return myVector ? 1 : 0; }
55 
56  /// Initialize to zeros.
57  void zero(exint nl, exint nh);
58  void zero()
59  { zero(myNL, myNH); }
60 
61  /// These methods allow one to read out and write into subvectors
62  /// of UT_Vector using our other vector classes.
63  /// Keep in mind that the usual UT_Vector? methods are 0 based,
64  /// while this class is usually 1 based.
65  void getSubvector2(UT_Vector2 &v, exint idx) const;
66  void setSubvector2(exint idx, const UT_Vector2 &v);
67  void getSubvector3(UT_Vector3 &v, exint idx) const;
68  void setSubvector3(exint idx, const UT_Vector3 &v);
69  void getSubvector4(UT_Vector4 &v, exint idx) const;
70  void setSubvector4(exint idx, const UT_Vector4 &v);
71 
72  /// Get the low index.
73  exint getNL() const { return myNL; }
74 
75  /// Get the high index.
76  exint getNH() const { return myNH; }
77 
78  /// Get dimension of this vector
79  exint length() const { return myNH - myNL + 1; }
80 
81  /// Determines if we have a large enough vector to make micro
82  /// threading worthwhile.
83  /// Experimentation shows with 4 procs on NT, 5000 is a cut off for good
84  /// behaviour on the simplistic addScaledVec method.
85  bool shouldMultiThread() const
86  {
87 #ifdef CELLBE
88  return false;
89 #else
90  return (myNH - myNL) > 4999;
91 #endif
92  }
93 
94  /// Determines the [start,end) interval that should be worked on
95  void getPartialRange(exint &start, exint &end, const UT_JobInfo &info) const;
96 
97  /// Determines the [startblock,endblock) interval that should be worked on,
98  /// where block 0 starts at myNL and all blocks are blocksize long, except
99  /// possibly the last.
100  void getPartialBlockRange(exint &startblock, exint &endblock,
101  const exint blocksize, const UT_JobInfo &info) const;
102 
103  /// Change the low index, and the high index will adjust itself.
104  void changeNL(exint nl);
105 
106  /// Change the indices in a very shallow manner, i.e. simply change
107  /// the index without shifting the data. This is highly dangerous, yet
108  /// very useful if used with care.
109  void setShallowNL(exint nl) { myNL = nl; }
110  void setShallowNH(exint nh) { myNH = nh; }
111 
112  /// For retrieving data
114  {
115  UT_ASSERT_P(i >= myNL && i <= myNH);
116  return myVector[i];
117  }
119  {
120  UT_ASSERT_P(i >= myNL && i <= myNH);
121  return myVector[i];
122  }
123 
124  /// Lp-norm
125  /// type: 0 L-infinity norm (ie. max abs)
126  /// 1 L1-norm (ie. sum of abs)
127  /// 2 L2-norm (ie. Euclidean distance)
128  T norm(int type=2) const;
129 
130  /// Square of L2-norm
131  T norm2() const;
132 
133  T distance2(const UT_VectorT<T> &v) const;
134 
135  /// Negate
139  const UT_VectorT<T> &, v)
140  void negPlusPartial(const UT_VectorT<T> &v, const UT_JobInfo &info);
141 
142  /// Add scaled vector
143  THREADED_METHOD2(UT_VectorT, shouldMultiThread(), addScaledVec,
144  T, s,
145  const UT_VectorT<T> &, v)
146  void addScaledVecPartial(T s, const UT_VectorT<T> &v,
147  const UT_JobInfo &info);
148 
149  /// Add scaled vector and compute the squared L2 norm
150  void addScaledVecNorm2(T s, const UT_VectorT<T> &v, fpreal64 *norm2);
151 
152  /// Add scaled vector and compute the squared L2 norm,
153  /// but only using components up to (but excluding) index normlimit
154  /// for the norm computation.
155  void addScaledVecNorm2UpTo(T s, const UT_VectorT<T> &v, fpreal64 *norm2, exint normlimit);
156 
157  /// Scale itself then add vector
158  THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), scaleAddVec,
159  T, s,
160  const UT_VectorT<T> &, v)
161  void scaleAddVecPartial(T s, const UT_VectorT<T> &v,
162  const UT_JobInfo &info);
163 
164  /// Multiply two sources together, save to this. Requires
165  /// that three vectors already have the same size.
166  THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), multAndSet,
167  const UT_VectorT<T> &, a,
168  const UT_VectorT<T> &, b)
169  void multAndSetPartial(const UT_VectorT<T> &a,
170  const UT_VectorT<T> &b,
171  const UT_JobInfo &info);
172 
173  /// Multiply two sources together, save to this. Requires
174  /// that three vectors already have the same size.
175  /// Computes dot(a*b, a)
176  /// This strange seeming pair is useful for jacobian preconditioners.
177  void multSetAndDotUpTo(const UT_VectorT<T> &a, const UT_VectorT<T> &b,
179 
180 
181  /// Divides two sources together, save to this. Requires
182  /// that three vectors already have the same size.
183  THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), divAndSet,
184  const UT_VectorT<T> &, a,
185  const UT_VectorT<T> &, b)
186  void divAndSetPartial(const UT_VectorT<T> &a,
187  const UT_VectorT<T> &b,
188  const UT_JobInfo &info);
189 
190  /// Inverts the source and saves to this.
191  /// Requires that the vectors match.
192  THREADED_METHOD1(UT_VectorT<T>, shouldMultiThread(), safeInvertAndSet,
193  const UT_VectorT<T> &, a)
194  void safeInvertAndSetPartial(const UT_VectorT<T> &a,
195  const UT_JobInfo &info);
196 
197  THREADED_METHOD1(UT_VectorT<T>, shouldMultiThread(), invertAndSet,
198  const UT_VectorT<T> &, a)
199  void invertAndSetPartial(const UT_VectorT<T> &a,
200  const UT_JobInfo &info);
201 
202  T dot(const UT_VectorT<T> &v) const;
203 
204  /// Multithreaded copy. operator= uses this.
205  THREADED_METHOD1(UT_VectorT<T>, shouldMultiThread(), copyFrom,
206  const UT_VectorT<T> &, v)
207  void copyFromPartial(const UT_VectorT<T> &v,
208  const UT_JobInfo &info);
209 
210  /// Operators
211  /// NOTE: operator= requires the destination be a matching size and
212  /// layout!
213  UT_VectorT &operator= (const UT_VectorT<T> &v);
214  UT_VectorT &operator+= (const UT_VectorT<T> &v);
215  UT_VectorT &operator-= (const UT_VectorT<T> &v);
216 
217  /// Componentwise multiplication & division.
218  UT_VectorT &operator*= (const UT_VectorT<T> &v);
219  UT_VectorT &operator/= (const UT_VectorT<T> &v);
220 
221  /// Scalar multiplication and division.
222  UT_VectorT &operator*= (T scalar);
223  UT_VectorT &operator/= (T scalar);
224 
225  /// Equals
226  /// upls is the number of representable fpreal64's to accept between
227  /// each individual component. If you deal in fpreal32's and assign into
228  /// this fpreal64 class, then you will want a ulps that is scaled by
229  /// 2^32. eg. for 50upls at fpreal32 precision, you will need 50*2^32
230  /// or approximately 1e+11.
231  bool isEqual(const UT_VectorT<T> &v, int64 ulps);
232 
233  /// Output
234  std::ostream &save(std::ostream &os) const;
235  friend std::ostream &operator<<(std::ostream &os, const UT_VectorT<T> &v)
236  { v.save(os); return os; }
237 
238  T *getData() const { return &myVector[myNL]; }
239 
240  bool hasNan() const;
241  void testForNan() const;
242 
243 protected:
244  /// A multithreaded norm function that returns the non-normalized
245  /// norm (Ie, squared in the L2 case.) and is mulithreaded.
247  fpreal64 *, result,
248  int, type)
249  void normInternalPartial(fpreal64 *result, int type, const UT_JobInfo &info) const;
250 
252  fpreal64 *, result,
253  const UT_VectorT<T> &, v)
254  void distance2InternalPartial(fpreal64 *result, const UT_VectorT<T> &v, const UT_JobInfo &info) const;
255 
256  /// A multithreaded dot method
257  THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), dotInternal,
258  fpreal64 *, result,
259  const UT_VectorT<T> &, v)
260  void dotInternalPartial(fpreal64 *result,
261  const UT_VectorT<T> &v,
262  const UT_JobInfo &info) const;
263 
264  THREADED_METHOD3(UT_VectorT, shouldMultiThread(), addScaledVecNorm2Internal,
265  T, s,
266  const UT_VectorT<T> &, v,
267  fpreal64 *, norm2)
268  void addScaledVecNorm2InternalPartial(T s, const UT_VectorT<T> &v,
269  fpreal64 *norm2, const UT_JobInfo &info);
270 
271  THREADED_METHOD4(UT_VectorT, shouldMultiThread(), addScaledVecNorm2UpToInternal,
272  T, s,
273  const UT_VectorT<T> &, v,
274  fpreal64 *, norm2,
275  exint, normlimit)
276  void addScaledVecNorm2UpToInternalPartial(T s, const UT_VectorT<T> &v,
277  fpreal64 *norm2, exint normlimit, const UT_JobInfo &info);
278 
279  THREADED_METHOD4(UT_VectorT<T>, shouldMultiThread(), multSetAndDotUpToInternal,
280  const UT_VectorT<T> &, a,
281  const UT_VectorT<T> &, b,
282  fpreal64 *, dot_aba,
283  exint, dotlimit)
284  void multSetAndDotUpToInternalPartial(
285  const UT_VectorT<T> &a, const UT_VectorT<T> &b,
286  fpreal64 *dot_aba, exint dotlimit,
287  const UT_JobInfo &info);
288 
289  static const exint PARALLEL_BLOCK_SIZE = 1024;
290 
291 private:
292  exint myNL, myNH;
293  int myOwnData;
294  T *myVector;
295 };
296 
297 
298 //////////////////////////////////////////////////////////////////////////////
299 //
300 // UT_Permutation
301 //
302 
303 
304 template <typename T>
306 {
307 public:
308  // Input the index range [nl..nh].
309  UT_PermutationT(exint nl, exint nh);
310  ~UT_PermutationT();
311 
312  // Assignment
313  UT_PermutationT(const UT_PermutationT<T> &p);
315 
316  // Initialize to zeros.
317  void zero();
318 
319  // Get the low index.
320  exint getNL() const { return myNL; }
321 
322  // Get the high index.
323  exint getNH() const { return myNH; }
324 
325  exint length() const { return myNH - myNL + 1; }
326 
327  // Change the low index, and the high index will adjust itself.
328  void changeNL(exint nl);
329 
330  // Change the indices in a very shallow manner, i.e. simply change
331  // the index without shifting the data. This is highly dangerous, yet
332  // very useful if used with care.
333  void setShallowNL(exint nl) { myNL = nl; }
334  void setShallowNH(exint nh) { myNH = nh; }
335 
336  // For retrieve data.
337  T &operator()(exint i)
338  {
339  UT_ASSERT_P(i >= myNL && i <= myNH);
340  return myVector[i];
341  }
342  T operator()(exint i) const
343  {
344  UT_ASSERT_P(i >= myNL && i <= myNH);
345  return myVector[i];
346  }
347 
348 private:
349  void clone(const UT_PermutationT<T> &p);
350  exint myNL, myNH;
351  T *myVector;
352 };
353 
354 #if defined( WIN32 ) || defined( LINUX ) || defined( MBSD ) || defined(GAMEOS)
355  #include "UT_Vector.C"
356 #endif
357 
358 
359 //////////////////////////////////////////////////////////////////////////////
360 //
361 // UT_Vector typedefs
362 //
363 
369 
370 //////////////////////////////////////////////////////////////////////////////
371 //
372 // UT_Vector inline binary operators
373 //
374 
375 // Free floating functions
376 inline fpreal64 dot(const UT_VectorD &v1, const UT_VectorD &v2);
377 inline fpreal64 distance2(const UT_VectorD &v1, const UT_VectorD &v2);
378 inline fpreal32 dot(const UT_VectorF &v1, const UT_VectorF &v2);
379 inline fpreal32 distance2(const UT_VectorF &v1, const UT_VectorF &v2);
380 
381 /// Dot product
382 inline fpreal64
383 dot(const UT_VectorD &v1, const UT_VectorD &v2)
384 {
385  return v1.dot(v2);
386 }
387 
388 /// Distance squared (L2) aka quadrance
389 inline fpreal64
391 {
392  return v1.distance2(v2);
393 }
394 
395 inline fpreal32
396 dot(const UT_VectorF &v1, const UT_VectorF &v2)
397 {
398  return v1.dot(v2);
399 }
400 
401 /// Distance squared (L2) aka quadrance
402 inline fpreal32
404 {
405  return v1.distance2(v2);
406 }
407 
408 // Overload for custom formatting of UT_VectorT<T> with UTformat.
409 template<typename T> inline size_t
410 format(char *buffer, size_t buffer_size, const UT_VectorT<T> &v);
411 
412 #endif
413 
bool hasNan() const
Definition: UT_Vector.C:928
void zero()
Definition: UT_Vector.h:58
int const UT_JobInfo &info const
Definition: UT_Vector.h:249
void getPartialRange(exint &start, exint &end, const UT_JobInfo &info) const
Determines the [start,end) interval that should be worked on.
Definition: UT_Vector.C:139
exint getNH() const
Definition: UT_Vector.h:323
void negPartial(const UT_JobInfo &info)
Negate.
Definition: UT_Vector.C:389
const UT_VectorT< T > fpreal64 * dot_aba
Definition: UT_Vector.h:285
void setShallowNH(exint nh)
Definition: UT_Vector.h:110
int isInit() const
Definition: UT_Vector.h:54
UT_VectorT< fpreal64 > UT_VectorD
Definition: UT_Vector.h:367
void init(exint nl, exint nh)
Initialize nl, nh and allocate space.
Definition: UT_Vector.C:110
UT_VectorT< fpreal64 > UT_Vector
Definition: UT_Vector.h:368
T & operator()(exint i)
For retrieving data.
Definition: UT_Vector.h:113
const GLdouble * v
Definition: glcorearb.h:836
GLuint start
Definition: glcorearb.h:474
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
exint getNH() const
Get the high index.
Definition: UT_Vector.h:76
void setShallowNL(exint nl)
Definition: UT_Vector.h:109
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
void getPartialBlockRange(exint &startblock, exint &endblock, const exint blocksize, const UT_JobInfo &info) const
Definition: UT_Vector.C:149
void addScaledVecNorm2(T s, const UT_VectorT< T > &v, fpreal64 *norm2)
Add scaled vector and compute the squared L2 norm.
Definition: UT_Vector.C:456
THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), normInternal, fpreal64 *, result, int, type) void normInternalPartial(fpreal64 *result
T * getData() const
Definition: UT_Vector.h:238
UT_VectorT< fpreal32 > UT_VectorF
Definition: UT_Vector.h:366
UT_PermutationT< int > UT_Permutation
Definition: UT_Vector.h:364
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
exint length() const
Definition: UT_Vector.h:325
3D Vector class.
T dot(const UT_VectorT< T > &v) const
Definition: UT_Vector.C:859
void multSetAndDotUpTo(const UT_VectorT< T > &a, const UT_VectorT< T > &b, fpreal64 *dot_aba, exint dotlimit)
Definition: UT_Vector.C:627
GLuint buffer
Definition: glcorearb.h:659
png_uint_32 i
Definition: png.h:2877
void testForNan() const
Definition: UT_Vector.C:940
void addScaledVecNorm2UpTo(T s, const UT_VectorT< T > &v, fpreal64 *norm2, exint normlimit)
Definition: UT_Vector.C:515
THREADED_METHOD4(UT_VectorT, shouldMultiThread(), addScaledVecNorm2UpToInternal, T, s, const UT_VectorT< T > &, v, fpreal64 *, norm2, exint, normlimit) void addScaledVecNorm2UpToInternalPartial(T s
THREADED_METHOD3(UT_VectorT, shouldMultiThread(), addScaledVecNorm2Internal, T, s, const UT_VectorT< T > &, v, fpreal64 *, norm2) void addScaledVecNorm2InternalPartial(T s
T & operator()(exint i)
Definition: UT_Vector.h:337
T value_type
Definition: UT_Vector.h:35
~UT_VectorT()
Definition: UT_Vector.C:56
void changeNL(exint nl)
Change the low index, and the high index will adjust itself.
Definition: UT_Vector.C:220
long long int64
Definition: SYS_Types.h:107
T norm(int type=2) const
Definition: UT_Vector.C:231
int64 exint
Definition: SYS_Types.h:116
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
double fpreal64
Definition: SYS_Types.h:192
UT_VectorT()
Input the index range [nl..nh].
Definition: UT_Vector.h:38
GLuint GLuint end
Definition: glcorearb.h:474
std::ostream & save(std::ostream &os) const
Output.
Definition: UT_Vector.C:902
void getSubvector4(UT_Vector4 &v, exint idx) const
Definition: UT_Vector.C:200
T distance2(const UT_VectorT< T > &v) const
Definition: UT_Vector.C:343
static const exint PARALLEL_BLOCK_SIZE
Definition: UT_Vector.h:289
T norm2() const
Square of L2-norm.
GLboolean * data
Definition: glcorearb.h:130
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
void setSubvector3(exint idx, const UT_Vector3 &v)
Definition: UT_Vector.C:191
T operator()(exint i) const
Definition: UT_Vector.h:342
THREADED_METHOD2(UT_VectorT, shouldMultiThread(), addScaledVec, T, s, const UT_VectorT< T > &, v) void addScaledVecPartial(T s
Add scaled vector.
UT_VectorT< fpreal > UT_VectorR
Definition: UT_Vector.h:365
void getSubvector2(UT_Vector2 &v, exint idx) const
Definition: UT_Vector.C:166
size_t format(char *buffer, size_t buffer_size, const UT_VectorT< T > &v)
const UT_VectorT< T > fpreal64 exint dotlimit
Definition: UT_Vector.h:285
void setShallowNL(exint nl)
Definition: UT_Vector.h:333
THREADED_METHOD1(UT_VectorT, shouldMultiThread(), negPlus, const UT_VectorT< T > &, v) void negPlusPartial(const UT_VectorT< T > &v
void setSubvector4(exint idx, const UT_Vector4 &v)
Definition: UT_Vector.C:210
void setSubvector2(exint idx, const UT_Vector2 &v)
Definition: UT_Vector.C:174
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
void getSubvector3(UT_Vector3 &v, exint idx) const
Definition: UT_Vector.C:182
GLfloat GLfloat v1
Definition: glcorearb.h:816
const UT_VectorT< T > fpreal64 exint normlimit
Definition: UT_Vector.h:276
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
void subvector(const UT_VectorT< T > &v, exint nl, exint nh)
Steal from another vector, resulting vector has origin at 1.
Definition: UT_Vector.C:64
exint getNL() const
Definition: UT_Vector.h:320
void assign(const fpreal32 *data, exint nl, exint nh)
Definition: UT_Vector.C:94
UT_VectorT & operator=(const UT_VectorT< T > &v)
Definition: UT_Vector.C:767
T operator()(exint i) const
Definition: UT_Vector.h:118
bool shouldMultiThread() const
Definition: UT_Vector.h:85
exint length() const
Get dimension of this vector.
Definition: UT_Vector.h:79
float fpreal32
Definition: SYS_Types.h:191
void setShallowNH(exint nh)
Definition: UT_Vector.h:334
#define THREADED_METHOD(CLASSNAME, DOMULTI, METHOD)
const UT_JobInfo & info
Definition: UT_Vector.h:140
bool isEqual(const UT_VectorT< T > &v, int64 ulps)
Definition: UT_Vector.C:914
exint getNL() const
Get the low index.
Definition: UT_Vector.h:73