00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __UT_Vector_H__
00022 #define __UT_Vector_H__
00023
00024 #include "UT_API.h"
00025 #include <stdlib.h>
00026 #include <iostream.h>
00027 #include <SYS/SYS_Types.h>
00028 #include "UT_Assert.h"
00029 #include "UT_ThreadedAlgorithm.h"
00030
00031 #include "UT_VectorTypes.h"
00032
00033
00034
00035
00036
00037
00038 template <typename T>
00039 class UT_VectorT
00040 {
00041 public:
00042
00043 typedef T value_type;
00044
00045
00046 UT_VectorT() { myVector = 0; myOwnData = 0; }
00047 UT_VectorT(exint nl, exint nh);
00048 UT_VectorT(const UT_VectorT<T> &v);
00049 ~UT_VectorT();
00050
00051
00052 void init(exint nl, exint nh);
00053
00054
00055 void subvector(const UT_VectorT<T> &v, exint nl, exint nh);
00056
00057
00058
00059 void assign(const fpreal32 *data, exint nl, exint nh);
00060 void assign(const fpreal64 *data, exint nl, exint nh);
00061
00062 int isInit() const { return myVector ? 1 : 0; }
00063
00064
00065 void zero(exint nl, exint nh);
00066 void zero()
00067 { zero(myNL, myNH); }
00068
00069
00070
00071
00072
00073 void getSubvector2(UT_Vector2 &v, exint idx) const;
00074 void setSubvector2(exint idx, const UT_Vector2 &v);
00075 void getSubvector3(UT_Vector3 &v, exint idx) const;
00076 void setSubvector3(exint idx, const UT_Vector3 &v);
00077 void getSubvector4(UT_Vector4 &v, exint idx) const;
00078 void setSubvector4(exint idx, const UT_Vector4 &v);
00079
00080
00081 exint getNL() const { return myNL; }
00082
00083
00084 exint getNH() const { return myNH; }
00085
00086
00087 exint length() const { return myNH - myNL + 1; }
00088
00089
00090
00091
00092
00093 bool shouldMultiThread() const
00094 {
00095 #ifdef CELLBE
00096 return false;
00097 #else
00098 return (myNH - myNL) > 4999;
00099 #endif
00100 }
00101
00102
00103 void getPartialRange(exint &start, exint &end, const UT_JobInfo &info) const;
00104
00105
00106 void changeNL(exint nl);
00107
00108
00109
00110
00111 void setShallowNL(exint nl) { myNL = nl; }
00112 void setShallowNH(exint nh) { myNH = nh; }
00113
00114
00115 T &operator()(exint i)
00116 {
00117 UT_ASSERT_P(i >= myNL && i <= myNH);
00118 return myVector[i];
00119 }
00120 T operator()(exint i) const
00121 {
00122 UT_ASSERT_P(i >= myNL && i <= myNH);
00123 return myVector[i];
00124 }
00125
00126
00127
00128
00129
00130 T norm(int type=2) const;
00131
00132
00133 T norm2() const;
00134
00135 T distance2(const UT_VectorT<T> &v) const;
00136
00137
00138 THREADED_METHOD(UT_VectorT, shouldMultiThread(), neg)
00139 void negPartial(const UT_JobInfo &info);
00140 THREADED_METHOD1(UT_VectorT, shouldMultiThread(), negPlus,
00141 const UT_VectorT<T> &, v)
00142 void negPlusPartial(const UT_VectorT<T> &v, const UT_JobInfo &info);
00143
00144
00145 THREADED_METHOD2(UT_VectorT, shouldMultiThread(), addScaledVec,
00146 T, s,
00147 const UT_VectorT<T> &, v)
00148 void addScaledVecPartial(T s, const UT_VectorT<T> &v,
00149 const UT_JobInfo &info);
00150
00151
00152 THREADED_METHOD3(UT_VectorT, shouldMultiThread(), addScaledVecNorm2,
00153 T, s,
00154 const UT_VectorT<T> &, v,
00155 fpreal64 *, norm2)
00156 void addScaledVecNorm2Partial(T s, const UT_VectorT<T> &v,
00157 fpreal64 *norm2,
00158 const UT_JobInfo &info);
00159
00160
00161 THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), scaleAddVec,
00162 T, s,
00163 const UT_VectorT<T> &, v)
00164 void scaleAddVecPartial(T s, const UT_VectorT<T> &v,
00165 const UT_JobInfo &info);
00166
00167
00168
00169 THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), multAndSet,
00170 const UT_VectorT<T> &, a,
00171 const UT_VectorT<T> &, b)
00172 void multAndSetPartial(const UT_VectorT<T> &a,
00173 const UT_VectorT<T> &b,
00174 const UT_JobInfo &info);
00175
00176
00177
00178 THREADED_METHOD2(UT_VectorT<T>, shouldMultiThread(), divAndSet,
00179 const UT_VectorT<T> &, a,
00180 const UT_VectorT<T> &, b)
00181 void divAndSetPartial(const UT_VectorT<T> &a,
00182 const UT_VectorT<T> &b,
00183 const UT_JobInfo &info);
00184
00185
00186
00187 THREADED_METHOD1(UT_VectorT<T>, shouldMultiThread(), invertAndSet,
00188 const UT_VectorT<T> &, a)
00189 void invertAndSetPartial(const UT_VectorT<T> &a,
00190 const UT_JobInfo &info);
00191
00192 T dot(const UT_VectorT<T> &v) const;
00193
00194
00195 THREADED_METHOD1(UT_VectorT<T>, shouldMultiThread(), copyFrom,
00196 const UT_VectorT<T> &, v)
00197 void copyFromPartial(const UT_VectorT<T> &v,
00198 const UT_JobInfo &info);
00199
00200
00201
00202
00203 UT_VectorT &operator= (const UT_VectorT<T> &v);
00204 UT_VectorT &operator+= (const UT_VectorT<T> &v);
00205 UT_VectorT &operator-= (const UT_VectorT<T> &v);
00206
00207
00208 UT_VectorT &operator*= (const UT_VectorT<T> &v);
00209 UT_VectorT &operator/= (const UT_VectorT<T> &v);
00210
00211
00212 UT_VectorT &operator*= (T scalar);
00213 UT_VectorT &operator/= (T scalar);
00214
00215
00216
00217
00218
00219
00220
00221 bool isEqual(const UT_VectorT<T> &v, int64 ulps);
00222
00223
00224 ostream &save(ostream &os) const;
00225 friend ostream &operator<<(ostream &os, const UT_VectorT<T> &v)
00226 { v.save(os); return os; }
00227
00228 T *getData() const { return &myVector[myNL]; }
00229
00230 bool hasNan() const;
00231 void testForNan() const;
00232
00233 protected:
00234
00235
00236 THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), normInternal,
00237 fpreal64 *, result,
00238 int, type)
00239 void normInternalPartial(fpreal64 *result, int type, const UT_JobInfo &info) const;
00240
00241 THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), distance2Internal,
00242 fpreal64 *, result,
00243 const UT_VectorT<T> &, v)
00244 void distance2InternalPartial(fpreal64 *result, const UT_VectorT<T> &v, const UT_JobInfo &info) const;
00245
00246
00247 THREADED_METHOD2_CONST(UT_VectorT, shouldMultiThread(), dotInternal,
00248 fpreal64 *, result,
00249 const UT_VectorT<T> &, v)
00250 void dotInternalPartial(fpreal64 *result,
00251 const UT_VectorT<T> &v,
00252 const UT_JobInfo &info) const;
00253
00254 private:
00255 exint myNL, myNH;
00256 int myOwnData;
00257 T *myVector;
00258 };
00259
00260
00261
00262
00263
00264
00265
00266
00267 template <typename T>
00268 class UT_PermutationT
00269 {
00270 public:
00271
00272 UT_PermutationT(exint nl, exint nh);
00273 ~UT_PermutationT();
00274
00275
00276 UT_PermutationT(const UT_PermutationT<T> &p);
00277 UT_PermutationT<T> &operator=(const UT_PermutationT<T> &p);
00278
00279
00280 void zero();
00281
00282
00283 exint getNL() const { return myNL; }
00284
00285
00286 exint getNH() const { return myNH; }
00287
00288 exint length() const { return myNH - myNL + 1; }
00289
00290
00291 void changeNL(exint nl);
00292
00293
00294
00295
00296 void setShallowNL(exint nl) { myNL = nl; }
00297 void setShallowNH(exint nh) { myNH = nh; }
00298
00299
00300 T &operator()(exint i)
00301 {
00302 UT_ASSERT_P(i >= myNL && i <= myNH);
00303 return myVector[i];
00304 }
00305 T operator()(exint i) const
00306 {
00307 UT_ASSERT_P(i >= myNL && i <= myNH);
00308 return myVector[i];
00309 }
00310
00311 private:
00312 void clone(const UT_PermutationT<T> &p);
00313 exint myNL, myNH;
00314 T *myVector;
00315 };
00316
00317 #if defined( WIN32 ) || defined( LINUX ) || defined( MBSD ) || defined(GAMEOS)
00318 #include "UT_Vector.C"
00319 #endif
00320
00321
00322
00323
00324
00325
00326
00327 typedef UT_PermutationT<int> UT_Permutation;
00328 typedef UT_VectorT<fpreal> UT_VectorR;
00329 typedef UT_VectorT<fpreal32> UT_VectorF;
00330 typedef UT_VectorT<fpreal64> UT_VectorD;
00331 typedef UT_VectorT<fpreal64> UT_Vector;
00332
00333
00334
00335
00336
00337
00338
00339 inline fpreal64 dot(const UT_VectorD &v1, const UT_VectorD &v2);
00340 inline fpreal64 distance2(const UT_VectorD &v1, const UT_VectorD &v2);
00341 inline fpreal32 dot(const UT_VectorF &v1, const UT_VectorF &v2);
00342 inline fpreal32 distance2(const UT_VectorF &v1, const UT_VectorF &v2);
00343
00344
00345 inline fpreal64
00346 dot(const UT_VectorD &v1, const UT_VectorD &v2)
00347 {
00348 return v1.dot(v2);
00349 }
00350
00351
00352 inline fpreal64
00353 distance2(const UT_VectorD &v1, const UT_VectorD &v2)
00354 {
00355 return v1.distance2(v2);
00356 }
00357
00358 inline fpreal32
00359 dot(const UT_VectorF &v1, const UT_VectorF &v2)
00360 {
00361 return v1.dot(v2);
00362 }
00363
00364
00365 inline fpreal32
00366 distance2(const UT_VectorF &v1, const UT_VectorF &v2)
00367 {
00368 return v1.distance2(v2);
00369 }
00370
00371
00372 #endif
00373