Changeset 66
- Timestamp:
- 06/05/08 14:58:48 (6 months ago)
- Location:
- include/fflas-ffpack
- Files:
-
- 1 added
- 7 modified
-
fflas.h (modified) (2 diffs)
-
fflas_fgemm.inl (modified) (1 diff)
-
ffpack_frobenius.inl (modified) (1 diff)
-
modular-balanced.h (modified) (16 diffs)
-
modular-int.h (modified) (3 diffs)
-
modular-positive.h (modified) (4 diffs)
-
modular-randiter.h (modified) (3 diffs)
-
nonzero-randiter.h (added)
Legend:
- Unmodified
- Added
- Removed
-
include/fflas-ffpack/fflas.h
r62 r66 26 26 #include "linbox/field/modular-double.h" 27 27 #include "linbox/field/modular-float.h" 28 #include "linbox/field/modular-balanced-double.h" 29 #include "linbox/field/modular-balanced-float.h" 28 30 namespace LinBox { 29 31 #else … … 35 37 36 38 #ifndef __LINBOX_STRASSEN_OPTIMIZATION 37 #define WINOTHRESHOLD 200039 #define WINOTHRESHOLD 1000 38 40 #else 39 41 #define WINOTHRESHOLD __LINBOX_WINOTHRESHOLD -
include/fflas-ffpack/fflas_fgemm.inl
r62 r66 95 95 Bdd, dldb, betad, Cd, n, kmax,base ); 96 96 97 typename Field::Element tmp;98 97 MatD2MatF (F, C, ldc, Cd, n, m, n); 99 98 MatF2MatD (F, Cd, n, C, ldc, m, n); -
include/fflas-ffpack/ffpack_frobenius.inl
r58 r66 43 43 // Picking a random noc x N block vector U^T 44 44 typename Field::RandIter g (F); 45 typename Field::NonZeroRandIter nzg (F,g); 45 46 for (size_t i = 0; i < noc; ++i) 46 47 for (size_t j = 0; j < N; ++j) 47 48 g.random( *(K + i*ldk +j) ); 48 49 for (size_t i = 0; i < noc; ++i) 49 g.nonzerorandom (*(K + i*ldk +i));50 nzg.random (*(K + i*ldk +i)); 50 51 51 52 // Computing the bloc Krylov matrix [U AU .. A^(c-1) U]^T -
include/fflas-ffpack/modular-balanced.h
r62 r66 17 17 #include <math.h> 18 18 #include "fflas-ffpack/modular-randiter.h" 19 #include "fflas-ffpack/nonzero-randiter.h" 19 20 20 21 template <class Element> … … 26 27 protected: 27 28 double modulus; 28 double inv_modulus;29 29 double half_mod; 30 30 unsigned long lmodulus; 31 31 32 32 public: … … 34 34 typedef unsigned long FieldInt; 35 35 typedef ModularBalancedRandIter<double> RandIter; 36 typedef NonzeroRandIter<ModularBalanced<double>, ModularBalancedRandIter<double> > NonZeroRandIter; 36 37 37 38 const bool balanced; 38 39 39 ModularBalanced<double> (int p) : modulus((double)p), inv_modulus(1./(double)p), half_mod( (p-1.)/2), balanced(true) {} 40 41 ModularBalanced<double>(const ModularBalanced<double>& mf) : modulus(mf.modulus), inv_modulus(mf.inv_modulus), half_mod(mf.half_mod), balanced(true){} 40 ModularBalanced<double> (int p) : modulus((double)p), 41 half_mod( (p-1.)/2), 42 balanced(true) {} 43 44 ModularBalanced<double>(const ModularBalanced<double>& mf) 45 : modulus(mf.modulus), half_mod(mf.half_mod), balanced(true){} 42 46 43 47 const ModularBalanced<double> &operator=(const ModularBalanced<double> &F) { 44 48 modulus = F.modulus; 45 inv_modulus = F.inv_modulus;46 49 half_mod = F.half_mod; 47 50 return *this; … … 70 73 71 74 std::ostream &write (std::ostream &os) const { 72 return os << " intmod " << (int)modulus;75 return os << "double mod " << (int)modulus; 73 76 } 74 77 … … 96 99 } 97 100 98 inline Element& init(Element& x, double y =0) const {101 inline Element& init(Element& x, const double y =0) const { 99 102 double tmp; 100 103 … … 131 134 if ( x > half_mod ) return x -= modulus; 132 135 if ( x < -half_mod ) return x += modulus; 133 elsereturn x;136 return x; 134 137 } 135 138 … … 137 140 x = y - z; 138 141 if (x > half_mod) return x -= modulus; 139 elseif (x < -half_mod) return x += modulus;140 elsereturn x;142 if (x < -half_mod) return x += modulus; 143 return x; 141 144 } 142 145 143 146 inline Element &mul (Element &x, const Element &y, const Element &z) const { 144 147 double tmp= y*z; 145 //x= tmp - floor(tmp*inv_modulus)*modulus;146 148 return init (x, tmp); 147 149 } … … 187 189 const Element &y) const { 188 190 double tmp= a*x+y; 189 //return r = tmp- floor(tmp*inv_modulus)*modulus;190 191 return init( r, tmp); 191 192 192 } 193 193 … … 196 196 if ( x > half_mod ) return x -= modulus; 197 197 if ( x < -half_mod ) return x += modulus; 198 elsereturn x;198 return x; 199 199 } 200 200 … … 203 203 if ( x > half_mod ) return x -= modulus; 204 204 if ( x < -half_mod ) return x += modulus; 205 elsereturn x;205 return x; 206 206 } 207 207 … … 224 224 225 225 inline Element &axpyin (Element &r, const Element &a, const Element &x) const { 226 double tmp=r+a*x; 227 //return r= tmp- floor(tmp*inv_modulus)*modulus; 228 return init( r, tmp ); 226 r += a * x; 227 return init( r, r); 229 228 } 230 229 … … 238 237 protected: 239 238 float modulus; 240 float inv_modulus;241 239 float half_mod; 242 240 … … 246 244 typedef unsigned long FieldInt; 247 245 typedef ModularBalancedRandIter<float> RandIter; 246 typedef NonzeroRandIter<ModularBalanced<float>, RandIter> NonZeroRandIter; 248 247 249 248 const bool balanced; 250 249 251 ModularBalanced<float> (int p) : modulus((float)p), inv_modulus(1./(float)p),half_mod( (p-1.)/2), balanced(true) {}252 253 ModularBalanced<float>(const ModularBalanced<float>& mf) : modulus(mf.modulus), inv_modulus(mf.inv_modulus),half_mod(mf.half_mod), balanced(true){}250 ModularBalanced<float> (int p) : modulus((float)p), half_mod( (p-1.)/2), balanced(true) {} 251 252 ModularBalanced<float>(const ModularBalanced<float>& mf) : modulus(mf.modulus), half_mod(mf.half_mod), balanced(true){} 254 253 255 254 const ModularBalanced<float> &operator=(const ModularBalanced<float> &F) { 256 255 modulus = F.modulus; 257 inv_modulus = F.inv_modulus;258 256 half_mod = F.half_mod; 259 257 return *this; … … 361 359 362 360 inline Element &mul (Element &x, const Element &y, const Element &z) const { 363 float tmp= y*z; 364 //x= tmp - floor(tmp*inv_modulus)*modulus; 365 return init (x, tmp); 361 x = y*z; 362 return init (x, x); 366 363 } 367 364 368 365 inline Element &div (Element &x, const Element &y, const Element &z) const { 369 Element temp; 370 inv (temp, z); 371 return mul (x, y, temp); 366 inv (x, z); 367 return mulin (x, y); 372 368 } 373 369 … … 405 401 const Element &x, 406 402 const Element &y) const { 407 float tmp= a*x+y; 408 //return r = tmp- floor(tmp*inv_modulus)*modulus; 409 return init( r, tmp); 403 r = a * x + y; 404 return init( r, r); 410 405 411 406 } … … 443 438 444 439 inline Element &axpyin (Element &r, const Element &a, const Element &x) const { 445 float tmp=r+a*x; 446 //return r= tmp- floor(tmp*inv_modulus)*modulus; 447 return init( r, tmp ); 440 r += a * x; 441 return init( r, r); 448 442 } 449 443 static inline float getMaxModulus() -
include/fflas-ffpack/modular-int.h
r62 r66 18 18 #include <sys/time.h> 19 19 #include "fflas-ffpack/modular-randiter.h" 20 #include "fflas-ffpack/nonzero-randiter.h" 20 21 template< class Element > 21 22 class Modular; … … 26 27 typedef unsigned long long uint64; 27 28 typedef unsigned long FieldInt; 28 29 29 30 protected: 30 31 31 int modulus; 32 33 double modulusinv; 34 35 36 int _two64; 32 int modulus; 33 double modulusinv; 34 int _two64; 37 35 38 36 public: 39 37 40 typedef int Element; 41 typedef ModularRandIter<int> RandIter; 42 43 const bool balanced; 44 45 46 Modular (int value) : modulus(value), balanced(false) { 47 modulusinv = 1 / ((double) value); 48 _two64 = (int) ((uint64) (-1) % (uint64) value); 49 _two64 += 1; 50 if (_two64 >= value) _two64 -= value; 51 } 52 53 Modular(const Modular<int>& mf) : modulus(mf.modulus),modulusinv(mf.modulusinv),_two64(mf._two64), balanced(false){} 54 55 const Modular &operator=(const Modular<int> &F) { 56 modulus = F.modulus; 57 modulusinv = F.modulusinv; 58 _two64 = F._two64; 59 return *this; 60 } 61 62 63 FieldInt &cardinality (FieldInt &c) const{ 64 return c = modulus; 65 } 66 67 FieldInt &characteristic (FieldInt &c) const { 68 return c = modulus; 69 } 70 71 FieldInt &convert (FieldInt &x, const Element &y) const { 72 return x = y; 73 } 38 typedef int Element; 39 typedef ModularRandIter<int> RandIter; 40 typedef NonzeroRandIter<Modular<int>,RandIter> NonZeroRandIter; 41 42 const bool balanced; 43 44 45 Modular (int value) : modulus(value), balanced(false) { 46 modulusinv = 1 / ((double) value); 47 _two64 = (int) ((uint64) (-1) % (uint64) value); 48 _two64 += 1; 49 if (_two64 >= value) _two64 -= value; 50 } 51 52 Modular(const Modular<int>& mf) : modulus(mf.modulus),modulusinv(mf.modulusinv),_two64(mf._two64), balanced(false){} 53 54 const Modular &operator=(const Modular<int> &F) { 55 modulus = F.modulus; 56 modulusinv = F.modulusinv; 57 _two64 = F._two64; 58 return *this; 59 } 60 61 62 FieldInt &cardinality (FieldInt &c) const{ 63 return c = modulus; 64 } 65 66 FieldInt &characteristic (FieldInt &c) const { 67 return c = modulus; 68 } 69 70 FieldInt &convert (FieldInt &x, const Element &y) const { 71 return x = y; 72 } 74 73 Element &convert (Element &x, const Element &y) const { 75 74 return x = y; 76 75 } 77 76 78 double & convert (double &x, const Element &y) const {79 return x = (double) y;80 }77 double & convert (double &x, const Element &y) const { 78 return x = (double) y; 79 } 81 80 82 81 float & convert (float &x, const Element &y) const { … … 84 83 } 85 84 86 std::ostream &write (std::ostream &os) const { 87 return os << "int mod " << modulus; 85 std::ostream &write (std::ostream &os) const { 86 return os << "int mod " << modulus; 87 } 88 89 std::istream &read (std::istream &is) { 90 is >> modulus; 91 modulusinv = 1 /((double) modulus ); 92 _two64 = (int) ((uint64) (-1) % (uint64) modulus); 93 _two64 += 1; 94 if (_two64 >= modulus) _two64 -= modulus; 95 96 return is; 97 } 98 99 std::ostream &write (std::ostream &os, const Element &x) const { 100 return os << x; 101 } 102 103 std::istream &read (std::istream &is, Element &x) const { 104 FieldInt tmp; 105 is >> tmp; 106 init(x,tmp); 107 return is; 108 } 109 110 111 template<class Element1> 112 Element &init (Element & x, const Element1 &y) const { 113 x = y % modulus; 114 if (x < 0) x += modulus; 115 return x; 116 } 117 118 Element &init (Element &x, const double &y) const { 119 double z = fmod(y, (double)modulus); 120 if (z < 0) z += (double)modulus; 121 z += 0.5; 122 return x = static_cast<long>(z); //rounds towards 0 123 } 124 Element &init (Element &x, const float &y) const { 125 float z = fmod(y, (float)modulus); 126 if (z < 0) z += (float)modulus; 127 z += 0.5; 128 return x = static_cast<long>(z); //rounds towards 0 129 } 130 131 Element &init (Element &x, const FieldInt &y) const { 132 x = y % modulus; 133 if (x < 0) x += modulus; 134 return x; 135 } 136 137 inline Element& init(Element& x, int y =0) const { 138 x = y % modulus; 139 if ( x < 0 ) x += modulus; 140 return x; 141 } 142 143 inline Element& init(Element& x, long y) const { 144 x = y % modulus; 145 if ( x < 0 ) x += modulus; 146 return x; 147 } 148 149 inline Element& assign(Element& x, const Element& y) const { 150 return x = y; 151 } 152 153 154 inline bool areEqual (const Element &x, const Element &y) const { 155 return x == y; 156 } 157 158 inline bool isZero (const Element &x) const { 159 return x == 0; 160 } 161 162 inline bool isOne (const Element &x) const { 163 return x == 1; 164 } 165 166 inline Element &add (Element &x, const Element &y, const Element &z) const { 167 x = y + z; 168 if ( x >= modulus ) x -= modulus; 169 return x; 170 } 171 172 inline Element &sub (Element &x, const Element &y, const Element &z) const { 173 x = y - z; 174 if (x < 0) x += modulus; 175 return x; 176 } 177 178 inline Element &mul (Element &x, const Element &y, const Element &z) const { 179 int q; 180 181 q = (int) ((((double) y)*((double) z)) * modulusinv); // q could be off by (+/-) 1 182 x = (int) (y*z - q*modulus); 183 184 185 if (x >= modulus) 186 x -= modulus; 187 else if (x < 0) 188 x += modulus; 189 190 return x; 191 } 192 193 inline Element &div (Element &x, const Element &y, const Element &z) const { 194 Element temp; 195 inv (temp, z); 196 return mul (x, y, temp); 197 } 198 199 inline Element &neg (Element &x, const Element &y) const { 200 if(y == 0) return x=0; 201 else return x = modulus-y; 202 } 203 204 inline Element &inv (Element &x, const Element &y) const { 205 int d, t; 206 XGCD(d, x, t, y, modulus); 207 if (x < 0) 208 x += modulus; 209 return x; 210 211 } 212 213 inline Element &axpy (Element &r, 214 const Element &a, 215 const Element &x, 216 const Element &y) const { 217 int q; 218 219 q = (int) (((((double) a) * ((double) x)) + (double)y) * modulusinv); // q could be off by (+/-) 1 220 r = (int) (a * x + y - q*modulus); 221 222 223 if (r >= modulus) 224 r -= modulus; 225 else if (r < 0) 226 r += modulus; 227 228 return r; 229 230 } 231 232 inline Element &addin (Element &x, const Element &y) const { 233 x += y; 234 if ( x >= modulus ) x -= modulus; 235 return x; 236 } 237 238 inline Element &subin (Element &x, const Element &y) const { 239 x -= y; 240 if (x < 0) x += modulus; 241 return x; 242 } 243 244 inline Element &mulin (Element &x, const Element &y) const { 245 return mul(x,x,y); 246 } 247 248 inline Element &divin (Element &x, const Element &y) const { 249 return div(x,x,y); 250 } 251 252 inline Element &negin (Element &x) const { 253 if (x == 0) return x; 254 else return x = modulus - x; 255 } 256 257 inline Element &invin (Element &x) const { 258 return inv (x, x); 259 } 260 261 inline Element &axpyin (Element &r, const Element &a, const Element &x) const { 262 int q; 263 264 q = (int) (((((double) a) * ((double) x)) + (double) r) * modulusinv); // q could be off by (+/-) 1 265 r = (int) (a * x + r - q*modulus); 266 267 268 if (r >= modulus) 269 r -= modulus; 270 else if (r < 0) 271 r += modulus; 272 273 return r; 274 } 275 276 private: 277 278 static void XGCD(int& d, int& s, int& t, int a, int b) { 279 int u, v, u0, v0, u1, v1, u2, v2, q, r; 280 281 int aneg = 0, bneg = 0; 282 283 if (a < 0) { 284 a = -a; 285 aneg = 1; 88 286 } 89 90 std::istream &read (std::istream &is) { 91 is >> modulus; 92 modulusinv = 1 /((double) modulus ); 93 _two64 = (int) ((uint64) (-1) % (uint64) modulus); 94 _two64 += 1; 95 if (_two64 >= modulus) _two64 -= modulus; 96 97 return is; 287 288 if (b < 0) { 289 b = -b; 290 bneg = 1; 98 291 } 99 100 std::ostream &write (std::ostream &os, const Element &x) const { 101 return os << x; 292 293 u1 = 1; v1 = 0; 294 u2 = 0; v2 = 1; 295 u = a; v = b; 296 297 while (v != 0) { 298 q = u / v; 299 r = u % v; 300 u = v; 301 v = r; 302 u0 = u2; 303 v0 = v2; 304 u2 = u1 - q*u2; 305 v2 = v1- q*v2; 306 u1 = u0; 307 v1 = v0; 102 308 } 103 104 std::istream &read (std::istream &is, Element &x) const { 105 FieldInt tmp; 106 is >> tmp; 107 init(x,tmp); 108 return is; 109 } 110 111 112 template<class Element1> 113 Element &init (Element & x, const Element1 &y) const { 114 x = y % modulus; 115 if (x < 0) x += modulus; 116 return x; 117 } 118 119 Element &init (Element &x, const double &y) const { 120 double z = fmod(y, (double)modulus); 121 if (z < 0) z += (double)modulus; 122 z += 0.5; 123 return x = static_cast<long>(z); //rounds towards 0 124 } 125 Element &init (Element &x, const float &y) const { 126 float z = fmod(y, (float)modulus); 127 if (z < 0) z += (float)modulus; 128 z += 0.5; 129 return x = static_cast<long>(z); //rounds towards 0 <
