/usr/include/linbox/blackbox/apply.h is in liblinbox-dev 1.3.2-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 | /* Copyright (C) LinBox
* Author: Zhendong Wan
* Modified by Pascal Giorgi
*
*
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*/
/* Reserve for possible optimal.
*/
#ifndef __LINBOX_apply_H
#define __LINBOX_apply_H
#include "linbox/linbox-config.h"
#include "linbox/integer.h"
#include "linbox/util/debug.h"
// #if defined(__LINBOX_field_multimod_field_H) && !defined(__LINBOX_blas_matrix_domain_H)
// #error "you need to include \"multimod-field.h\" before \"blas-domain.h\""
// #endif
#include "linbox/field/multimod-field.h"
#include "linbox/field/hom.h"
#include "linbox/randiter/multimod-randomprime.h"
#include "linbox/blackbox/sparse.h"
#include "linbox/matrix/blas-matrix.h"
#include "linbox/algorithms/lifting-container.h"
#include <vector>
#include "linbox/util/timer.h"
// #ifdef __LINBOX_BLAS_AVAILABLE
#include <fflas-ffpack/fflas/fflas.h>
// #endif
//#define DEBUG_CHUNK_SETUP
//#define DEBUG_CHUNK_APPLY
//#define DEBUG_CHUNK_APPLYM
//#define CHECK_APPLY
#define TIMING_APPLY
namespace LinBox
{
// general case, y = A x
template<class OutV, class Matrix, class InV>
inline OutV& apply (OutV& y, const Matrix& A, const InV& x)
{
return A. apply (y, x);
}
template<class OutV, class Matrix, class InV>
inline OutV& applyTranspose (OutV& y, const Matrix& A, const InV& x)
{
return A. applyTranspose (y, x);
}
template<class Domain>
class BlasApply {
public:
typedef typename Domain::Element Element;
typedef std::vector<Element> Vector;
BlasApply(const Domain& D) :
_domain(D), _MD(D)
{
_domain.characteristic(_prime);
_domain.init(_one,1UL);
_domain.init(_zero,0UL);
}
//#ifdef __LINBOX_BLAS_AVAILABLE
inline Vector& applyV(Vector &y,
const BlasMatrix<Domain> &A,
const Vector &x) const
{
if (( _prime > 0) && ( _prime < 67108863)) {
FFLAS::fgemv((typename Domain::Father_t) _domain, FFLAS::FflasNoTrans,
A.rowdim(), A.coldim(),
_one,
A.getPointer(), A.getStride(),
&x[0],1,
_zero,
&y[0],1);
}
else {
_MD.vectorMul (y, A, x);
}
return y;
}
inline Vector& applyVTrans(Vector &y,
BlasMatrix<Domain> &A,
const Vector &x) const
{
if (( _prime > 0) && ( _prime < 67108863)) {
FFLAS::fgemv((typename Domain::Father_t) _domain, FFLAS::FflasTrans,
A.rowdim(), A.coldim(),
_one,
A.getPointer(), A.getStride(),
&x[0],1,
_zero,
&y[0],1);
}
else {
TransposeMatrix<const BlasMatrix<Domain> > B(A);
_MD.vectorMul (y, B, x);
}
return y;
}
inline Vector& applyVspecial (Vector &y,
BlasMatrix<Domain> &A,
const Vector &x) const
{//toto
size_t m,n;
m = A.rowdim();
n = A.coldim();
linbox_check( x.size() == n);
double * At_dbl = new double[m*n];
for (size_t i=0;i<m;++i)
for (size_t j=0;j<n;++j)
_domain.convert(*(At_dbl+i+j*m), A.refEntry(i,j));
integer tmp;
bool use_neg=false;
size_t maxword=0;
for (size_t i=0;i<n;++i){
_domain.convert(tmp,x[i]);
if (tmp <0)
use_neg = true;
if ( maxword < tmp.size())
maxword= tmp.size();
}
if (use_neg)
maxword++;
double *xdbl= new double[n*maxword];
memset(xdbl, 0, sizeof(double)*n*maxword);
for (size_t i=0;i<n;++i){
_domain.convert(tmp,x[i]);
double * ptr= xdbl+i;
if (tmp == 0)
*ptr=0;
else {
if (tmp > 0) {
for (size_t j=0;j<tmp.size();++j){
*ptr= (double)x[i][j];
ptr+= n;
}
}
else {
size_t j=0;
for (;j<tmp.size();++j){
*ptr= double(0xFFFFFFFF^x[i][j]);
ptr+= n;
}
for (;j<maxword-1;++j){
*ptr= double(0xFFFFFFFF);
ptr+= n;
}
*ptr=1;
}
}
}
double *ydbl= new double[maxword*m];
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
(int) maxword,(int) m,(int) n, 1,
xdbl,(int) n, At_dbl, (int) m, 0, ydbl, (int) m);
delete At_dbl;
delete xdbl;
size_t rclen=maxword*4+5;
unsigned char* combined1 = new unsigned char[m*rclen];
unsigned char* combined2 = new unsigned char[m*rclen];
memset(combined1, 0, m*rclen);
memset(combined2, 0, m*rclen);
for (size_t i=0;i<m;++i){
unsigned char *ptr= combined1+i*rclen;
for (size_t j=0;j< maxword;j=j+2){
if (!use_neg || j< maxword-1){
long long mask = static_cast<long long>(ydbl[j*m+i]);
*(reinterpret_cast<long long*>(ptr) ) |= mask;
ptr+=4;
}
}
ptr= combined2+4+i*rclen;
for (size_t j=1;j< maxword;j=j+2){
if (!use_neg || j< maxword-1){
long long mask = static_cast<long long>(ydbl[j*m+i]);
*(reinterpret_cast<long long*>(ptr) ) |= mask;
ptr+=4;
}
}
}
for (size_t i=0; i<m; i++) {
LinBox::integer result, tmp2;
if (use_neg) {
result = -ydbl[(maxword-1)*m+i];
result <<= (maxword-1)*32;
}
else
result = 0;
importWords(tmp2, (size_t)rclen, -1, 1, 0, 0, combined1+i*rclen);
result += tmp2;
importWords(tmp2, (size_t)rclen, -1, 1, 0, 0, combined2+i*rclen);
result += tmp2;
_domain.init(y[i], result);
}
delete[] ydbl;
delete[] combined1;
delete[] combined2;
return y;
}// end of applyVspecial
private:
Domain _domain;
integer _prime;
Element _one,_zero;
MatrixDomain<Domain> _MD;
};
template <class Domain, class IMatrix>
class MatrixApplyDomain {
public:
typedef typename Domain::Element Element;
typedef std::vector<Element> Vector;
MatrixApplyDomain(const Domain& D, const IMatrix &Mat) :
_domain(D), _matM(Mat)
{}
void setup(LinBox::integer prime){}
Vector& applyV(Vector& y, Vector& x, Vector& z) const
{
return _matM.apply(y,x);
}
Vector& applyVTrans(Vector& y, Vector& x, Vector&z) const
{return _matM.applyTranspose(y,x);}
private:
Domain _domain;
const IMatrix &_matM;
};
// special function to split an integer matrix in q-adic representation in an array of double
template <class Domain, class IMatrix>
void create_MatrixQadic (const Domain &D, const IMatrix &Mat, double *chunks, size_t num_chunks, const integer shift=0);
// special function to split an integer vector in q-adic representation in an array of double
template <class Domain, class IVector>
void create_VectorQadic (const Domain &D, const IVector &V, double *chunks, size_t num_chunks);
// special function to split an integer matrix in an RNS representation in an array of double
template <class Domain, class IMatrix>
void create_MatrixRNS (const MultiModDouble& F, const Domain &D, const IMatrix &Mat, double *chunks);
// special function to split an integer vector in an RNS representation in an array of double
template <class Domain, class IVector>
void create_VectorRNS (const MultiModDouble& F, const Domain &D, const IVector &V, double *chunks);
// \brief optimizations for applying an integer matrix to a bounded integer vector
template <class Domain, class IMatrix>
class BlasMatrixApplyDomain {
enum ApplyChoice {Classic, MatrixQadic, VectorQadic, CRT};
public:
typedef typename Domain::Element Element;
typedef std::vector<Element> Vector;
typedef IMatrix Matrix;
BlasMatrixApplyDomain(const Domain& D, const IMatrix &Mat) :
_domain(D), _matM(Mat), _MD(D), _m(Mat.rowdim()), _n(Mat.coldim())
{
_switcher= Classic;_rns=NULL;
}
~BlasMatrixApplyDomain ()
{
if (_switcher==MatrixQadic) delete[] chunks;
if (_switcher==VectorQadic) {delete[] chunks;delete[] vchunks;}
if (_switcher== CRT) delete _rns;
//std::cout<<"time convert data = "<<_convert_data<<std::endl;
//std::cout<<"time apply = "<<_apply<<std::endl;
//std::cout<<"time convert result = "<<_convert_result<<std::endl;
}
ApplyChoice setup(LinBox::integer prime)
{ //setup
_domain.init(_prime,prime);
_apply.clear();
_convert_data.clear();
_convert_result.clear();
#ifdef __LINBOX_HAVE_BIG_ENDIAN
_switcher= Classic;
#else
// compute the magnitude in bit of the matrix
// check if at least one entry in the matrix is negative
LinBox::integer tmp=0, maxValue=0;
size_t maxBitSize = 0;
use_neg = false;
typename Matrix::ConstIterator it = _matM.Begin();
for (size_t i=0; i<_m*_n; i++, ++it) {
_domain.convert(tmp, *it);
if (tmp <0) {
use_neg = 1;
tmp=-tmp;
}
if (tmp> maxValue){
maxValue= tmp;
}
}
size_t bit, dbit;
bit=maxValue.bitsize();
dbit= maxValue.size_in_base(4)*2;
maxBitSize= ((dbit-bit)>0)? dbit: bit;
// Check Qadic matrix reprentation possibility
LinBox::integer maxChunkVal = 1;
maxChunkVal <<= 53;
maxChunkVal /= (prime-1) * _n;
chunk_size = maxChunkVal.bitsize();
use_chunks = (chunk_size >= 16);
//std::cout<<"max bit= "<<maxBitSize<<" "<<maxValue.size_in_base(4)*2<<"\n";std::cout<<"max value= "<<maxValue<<"\n";
if (use_chunks){//std::cout<<"Matrix Qadic\n";
_switcher= MatrixQadic;
}
else {
// Check Qadic vector representation possibility
maxChunkVal = 1;
maxChunkVal <<= 53;
maxChunkVal /= 2*maxValue * _n;
chunk_size = maxChunkVal.bitsize();
use_chunks = (chunk_size >= 16);
if (use_chunks){//std::cout<<"Vector Qadic\n";
_switcher= VectorQadic;
//std::cout<<"possible chunk size: "<<chunk_size<<std::endl;
}
else {
if (prime.bitsize()> 32){//std::cout<<"CRT\n";
_switcher= CRT;
}
else {//std::cout<<"Classic\n";
_switcher= Classic;
}
}
}
// set maximum size of chunk to 16
chunk_size = 16;
switch (_switcher) {
case MatrixQadic:
if (use_neg){
maxValue= maxValue<<1;
maxBitSize+=1;
}
// compute the number of chunk
if (maxValue*prime*_matM.coldim() < integer("9007199254740992")){
num_chunks=1;
use_neg=false;
}
else num_chunks =(maxBitSize) / chunk_size+ (((maxBitSize % chunk_size) > 0)? 1:0);
//num_chunks = 1;
if (num_chunks ==1) use_neg= false;
//if (use_neg) num_chunks++; //the leading chunk will be negative
//int n2 = _m*_n;
chunks = new double[_m*_n*num_chunks];
memset(chunks, 0, sizeof(double)*_m*_n*num_chunks);
shift= use_neg? maxValue : integer(0);
create_MatrixQadic(_domain, _matM, chunks, num_chunks, shift);
#ifdef DEBUG_CHUNK_SETUP
std::cout<<std::endl;
std::cout<<"max bit= "<<maxBitSize<<std::endl;
std::cout << num_chunks << " chunks of "<< chunk_size << " bits each" << std::endl;
if (!use_neg) std::cout << "not ";
std::cout << "using negative leading chunk" << std::endl;
std::cout << "shift is : "<<shift<<std::endl;
std::cout << "Contents of chunks: " << std::endl;
for (size_t i=0; i<num_chunks; i++) {
std::cout << "chunk " << i << std::endl;
for (size_t j=0; j<_m*_n; j++) {
std::cout << static_cast<long long>(chunks[i*_m*_n+j]);
if ((j+1)%_n) std::cout << ' '; else std::cout << std::endl;
}
}
#endif
break;
case VectorQadic:
num_chunks = (prime.bitsize() / chunk_size)+ (((prime.bitsize() % chunk_size) > 0)? 1:0);
// convert integer matrix to double matrix
chunks = new double[_m*_n];
memset(chunks, 0, sizeof(double)*_m*_n);
create_MatrixQadic (_domain, _matM, chunks, 1);
// if the matrix has negative entries
if (use_neg){
shift=maxValue;
double sh= (double) maxValue;
// shift the value of the entries in the matrix by ||A||=max(A_ij)
for (size_t i=0;i<_m*_n;++i)
chunks[i]+=sh;
}
// allocate memory for the vector chunks
vchunks = new double[_n*num_chunks];
break;
case Classic:
break;
case CRT:
if (use_neg){
maxValue= maxValue<<1;
maxBitSize+=1;
}
integer a_bound= maxValue*_n+1;
integer b_bound= sqrt(integer("9007199254740992")/_n); std::cout<<"max prime: "<<b_bound<<" max rns: "<<a_bound<<std::endl;
MultiModRandomPrime mmrp;
std::vector<integer> rns_basis = mmrp.createPrimes(b_bound, a_bound);
_rns = new MultiModDouble(rns_basis);
std::cout<<" CRT basi length= "<<_rns->size()<<std::endl;
// convert integer matrix to rns double matrix
chunks = new double[_m*_n*_rns->size()];
memset(chunks, 0, sizeof(double)*_m*_n*_rns->size());
create_MatrixRNS(*_rns, _domain, _matM, chunks);
// allocate memory for the rns vector
vchunks = new double[_n*_rns->size()];
// prepare special CRT
Element g, s, q, zero,one,two;
_q= _rns->getCRTmodulo();
_domain.init(q,_q);_domain.init(zero,0UL);_domain.init(one,1UL);_domain.init(two,2UL);
_domain.xgcd(g, _inv_q, s, q, _prime);
if (_domain.compare(_inv_q, zero)<0 ) _domain.addin(_inv_q,_prime);
_domain.mul(_pq,_prime,q);
_domain.sub(_h_pq,_pq, one);
_domain.divin(_h_pq, two);
break;
}
#endif
#ifdef DEBUG_CHUNK_APPLY
std::cout<<"A: \n";
_matM.write(std::cout);
#endif
return _switcher;
}
//#define DEBUG_CHUNK_APPLY
Vector& applyV(Vector& y, Vector& x, Vector &b) const
{ //applyV
#ifdef DEBUG_CHUNK_APPLY
std::cout << "x: ";
for (size_t i=0; i<x.size(); i++)
std::cout << x[i] << ' ';
std::cout << std::endl;
#endif
linbox_check( _n == x.size());
linbox_check( _m == y.size());
switch(_switcher) {//switch
case Classic:
_MD.vectorMul (y, _matM, x);
break;
case MatrixQadic:
{// mqadic
#if 0
temp fix
_MD.vectorMul (y, _matM, x);
break;
#endif
double* dx = new double[_n];
for (size_t i=0; i<_n; i++) {
_domain.convert(dx[i], x[i]);
}
if (num_chunks == 1) {
double *ctd = new double[_m];
cblas_dgemv(CblasRowMajor, CblasNoTrans, (int) _m, (int) _n,
1, chunks, (int) _n, dx, 1, 0, ctd, 1);
for (size_t i=0;i<_n;++i)
_domain.init(y[i],ctd[i]);
delete[] ctd;
delete[] dx;
}
else {
/*
* rc: number of vectors to recombine
* (the idea is that to compute a polynomial in the base 2^chunksize
* with <= 53 bits in each coefficient, we can instead OR nonoverlapping blocks
* of bits and then add them at the end, like this:
* AAAACCCCEEEEGGGG instead AAAA << 12 + BBBB << 10 + CCCC << 8 + ...
* + BBBBDDDDFFFF00 of
* also note that we need separate blocks for positive and negative entries)
*/
int rc = int(52 / chunk_size) + 1; //constant at 4 for now
/*
* rclen: number of bytes in each of these OR-ed vectors
* needs room to hold (max long long) << (num_chunks * chunksize)
*/
int rclen = (int)num_chunks*2 + 5;
unsigned char* combined = new unsigned char[rc*_n*rclen];
memset(combined, 0, rc*_n*rclen);
//order from major index to minor: combining index, component of sol'n, byte
//compute a product (chunk times x) for each chunk
double* ctd = new double[_n];
#ifdef DEBUG_CHUNK_APPLY
std::cout<<"- A chunk --------------------------\n";
for (size_t k=0;k<num_chunks;++k){
for (size_t i=0;i<_m;i++){
for (size_t j=0;j<_n;j++)
std::cout<<integer(*(chunks+j+i*_n+k*_m*_n))<<",";
std::cout<<std::endl;
}
std::cout<<"\n";
}
std::cout<<"- A.x chunk---------------------\n";
#endif
for (size_t i=0; i<num_chunks; i++) {
cblas_dgemv(CblasRowMajor, CblasNoTrans,
(int) _m, (int) _n, 1,
chunks + (_m*_n*i),(int) _n, dx, 1, 0, ctd, 1);
#ifdef DEBUG_CHUNK_APPLY
for (size_t j=0;j<_n;j++)
std::cout<<integer(*(ctd+j))<<",";
std::cout<<std::endl;
#endif
//if (!use_neg || i<num_chunks-1)
for (size_t j=0; j<_n; j++) {
// up to 53 bits will be ored-in, to be summed later
unsigned char* bitDest = combined;
bitDest += rclen*((i % rc)*_n+j);
long long mask = static_cast<long long>(ctd[j]);
bitDest += 2*i;
*(reinterpret_cast<long long*>(bitDest) ) |= mask;
}
}
delete[] dx;
for (size_t i=0; i<_n; i++) {
LinBox::integer result, tmp;
/*
if (use_neg) {
result = -ctd[i];
result <<= (num_chunks-1)*16;
}
else
*/
result = 0;
for (int j=0; j<rc; j++) {
unsigned char* thispos = combined + rclen*(j*_n+i);
importWords(tmp, (size_t)rclen, -1, 1, 0, 0, thispos);
result += tmp;
}
_domain.init(y[i], result);
}
// shift back the result
if (use_neg) {
Element acc;
_domain.init(acc,0);
for (size_t i=0;i<x.size();++i)
_domain.addin(acc,x[i]);
_domain.mulin(acc,shift);
for (size_t i=0;i<y.size();++i)
_domain.subin(y[i], acc);
}
delete[] combined;
delete[] ctd;
}
}
break;
case VectorQadic:
{
#ifdef TIMING_APPLY
Timer chrono;
chrono.clear();
chrono.start();
#endif
// fill vector chunks with zero
memset(vchunks, 0, sizeof(double)*_n*num_chunks);
// smallest distance of non-overlaping chunks results
size_t rc = (52 / chunk_size) + 1;
// number of bytes in a chunks
size_t chunk_byte= (chunk_size == 32)? 4: 2;
// number of byte to store
size_t rclen = num_chunks*chunk_byte + 5;
unsigned char* combined = new unsigned char[rclen];
double *ctd= new double[_m*num_chunks];
if (chunk_size >=32)
create_VectorQadic_32 (_domain, x, vchunks, num_chunks);
else
create_VectorQadic (_domain, x, vchunks, num_chunks);
#ifdef TIMING_APPLY
chrono.stop();
_convert_data+=chrono;
chrono.clear();
chrono.start();
#endif
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, (int) _m, (int) num_chunks, (int) _n, 1.,
chunks, (int) _n, vchunks, (int) num_chunks, 0., ctd, (int) num_chunks);
#ifdef TIMING_APPLY
chrono.stop();
_apply+=chrono;
chrono.clear();
chrono.start();
#endif
#ifdef DEBUG_CHUNK_APPLY
std::cout<<"- A ---------------------------\n";
for (size_t i=0;i<_m;i++){
for (size_t j=0;j<_n;j++)
std::cout<<integer(*(chunks+j+i*_n))<<",";
std::cout<<std::endl;
}
std::cout<<"- x chunk-----------------------\n";
for (size_t i=0;i<_n;i++){
for (size_t j=0;j<num_chunks;j++)
std::cout<<integer(*(vchunks+j+i*num_chunks))<<",";
std::cout<<std::endl;
}
std::cout<<"- A.x chunk---------------------\n";
for (size_t i=0;i<_m;i++){
for (size_t j=0;j<num_chunks;j++)
std::cout<<integer(*(ctd+j+i*num_chunks))<<",";
std::cout<<std::endl;
}
#endif
for (size_t i=0; i< _m; ++i){
integer result=0, val;
for (size_t k=0; k< rc;++k){
memset(combined, 0, rclen);
unsigned char* BitDest = combined+chunk_byte*k;
for (size_t j=k; j< num_chunks; j+=rc){
long long mask = static_cast<long long>(ctd[i*num_chunks+j]);
*(reinterpret_cast<long long*>(BitDest) ) |= mask;
BitDest+=rc*chunk_byte;
}
importWords(val, (size_t)rclen, -1, 1, 0, 0, combined);
result+=val;
}
_domain.init(y[i], result);
}
// shift back the result
if (use_neg) {
Element acc;
_domain.init(acc,0);
for (size_t i=0;i<x.size();++i)
_domain.addin(acc,x[i]);
_domain.mulin(acc,shift);
for (size_t i=0;i<y.size();++i)
_domain.subin(y[i], acc);
}
delete[] combined;
delete[] ctd;
#ifdef TIMING_APPLY
chrono.stop();
_convert_result+=chrono;
#endif
}
break;
case CRT:
{
#ifdef TIMING_APPLY
Timer chrono;
chrono.clear();
chrono.start();
#endif
size_t rns_size= _rns->size();
integer mod, hmod;
mod = _rns->getCRTmodulo ();
hmod = (mod-1)>>1;
// fill rns vector with zero
//memset(vchunks, 0, sizeof(double)*_n*rns_size);
// create rns vector
create_VectorRNS (*_rns, _domain, x, vchunks);
// allocate memory for the result
double *ctd= new double[_m*rns_size];
#ifdef TIMING_APPLY
chrono.stop();
_convert_data+=chrono;
chrono.clear();
chrono.start();
#endif
// perform multiplication componentwise
for (size_t i=0;i< rns_size; ++i)
cblas_dgemv(CblasRowMajor, CblasNoTrans, (int) _m, (int) _n,
1, chunks+i*_m*_n, (int) _n, vchunks+i*_n, 1, 0, ctd+i*_m, 1);
#ifdef TIMING_APPLY
chrono.stop();
_apply+=chrono;
chrono.clear();
chrono.start();
#endif
#ifdef DEBUG_CHUNK_APPLY
_rns->write(std::cout);
std::cout<<"- A RNS --------------------------\n";
for (size_t k=0;k<rns_size;++k){
for (size_t i=0;i<_m;i++){
for (size_t j=0;j<_n;j++)
std::cout<<integer(*(chunks+j+i*_n+k*_n*rns_size))<<",";
std::cout<<std::endl;
}
std::cout<<"\n";
}
std::cout<<"- x RNS-----------------------\n";
for (size_t i=0;i<_n;i++){
for (size_t j=0;j<rns_size;j++)
std::cout<<integer(*(vchunks+j+i*rns_size))<<",";
std::cout<<std::endl;
}
std::cout<<"- A.x RNS---------------------\n";
for (size_t i=0;i<_m;i++){
for (size_t j=0;j<rns_size;j++)
std::cout<<integer(*(ctd+j+i*rns_size))<<",";
std::cout<<std::endl;
}
#endif
// reconstruct the result using CRT
std::vector<double> tmp(rns_size);
integer res;
for (size_t j=0;j<_m;++j){
for (size_t i=0;i<rns_size;++i)
_rns->getBase(i).init(tmp[i], ctd[j+i*_m]);
_rns->convert(res, tmp);
_domain.init(y[j], res);
//if (y[j] > hmod) y[j]-=mod;
}
delete[] ctd;
#if 0
std::cout << "y mod q: ";
for (size_t i=0; i<y.size(); i++)
std::cout << y[i] << ' ';
std::cout << std::endl;
std::cout << "b: ";
for (size_t i=0; i<b.size(); i++)
std::cout << b[i] << ' ';
std::cout << std::endl;
std::cout<<"p: "<<_prime<<" q: "<<_q<<std::endl;
#endif
Element y_cur, b_cur;
// finish crt according to b
for (size_t i=0;i<_m;++i){
_domain.rem(b_cur, b[i], _prime);
_domain.sub(y_cur, b_cur, y[i]);//std::cout<<"(b-y): "<<y_cur<<std::endl;
_domain.mulin(y_cur, _inv_q);//std::cout<<"((b-y)/q): "<<y_cur<<std::endl;
_domain.remin(y_cur, _prime);//std::cout<<"((b-y)/q mod p): "<<y_cur<<std::endl;
_domain.axpyin(y[i],_q, y_cur);//std::cout<<"y+p((b-y)/q mod p): "<<y[i]<<std::endl;
if ( y[i] > _h_pq) y[i]-=_pq;
}
/*
std::cout << "y: ";
for (size_t i=0; i<y.size(); i++)
std::cout << y[i] << ' ';
std::cout << std::endl;
*/
#ifdef TIMING_APPLY
chrono.stop();
_convert_result+=chrono;
#endif
}
break;
}
#ifdef DEBUG_CHUNK_APPLY
std::cout << "y: ";
for (size_t i=0; i<y.size(); i++)
std::cout << y[i] << ' ';
std::cout << std::endl;
#endif
return y;
}
Vector& applyVTrans(Vector& y, Vector& x) const
{
TransposeMatrix<IMatrix> B(_matM);
return _MD.vectorMul (y, B, x);
}
IMatrix& applyM (IMatrix &Y, const IMatrix &X) const
{
linbox_check( _n == X.rowdim());
linbox_check( _m == Y.rowdim());
linbox_check( Y.coldim() == X.coldim());
if (!use_chunks){
_MD.mul (Y, _matM, X);
}
else{
size_t _k= X.coldim();
double* dX = new double[_n*_k];
for (size_t i=0; i<_n; i++)
for(size_t j=0;j<_k;++j)
_domain.convert(dX[i*_k+j], X.getEntry(i,j));
#ifdef DEBUG_CHUNK_APPLYM
cout << "X: ";
X.write(cout,_domain);
cout << endl;
#endif
if (num_chunks == 1) {
double *ctd = new double[_m*_k];
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
(int) _m,(int) _k,(int) _n, 1,
chunks,(int) _n, dX, (int) _k, 0, ctd, (int) _k);
for (size_t i=0;i<_m;++i)
for (size_t j=0;j<_k;++j)
_domain.init(Y.refEntry(i,j),ctd[i*_k+j]);
delete[] ctd;
delete[] dX;
}
else {
/*
* rc: number of vectors to recombine
*(the idea is that to compute a polynomial in the base 2^chunksize
* with <= 53 bits in each coefficient, we can instead OR nonoverlapping blocks
* of bits and then add them at the end, like this:
* AAAACCCCEEEEGGGG instead AAAA << 12 + BBBB << 10 + CCCC << 8 + ...
* + BBBBDDDDFFFF00 of
* also note that we need separate blocks for positive and negative entries)
*/
int rc = int(52 / chunk_size) + 1; //constant at 4 for now
//rclen: number of bytes in each of these OR-ed vectors
// needs room to hold (max long long) << (num_chunks * chunksize)
int rclen = (int)num_chunks*2 + 5;
// cout << "rc= " << rc << ", rclen = " << rclen << endl;
unsigned char* combined = new unsigned char[rc*_m*_k*rclen];
memset(combined, 0, rc*_m*_k*rclen);
//order from major index to minor: combining index, component of sol'n, byte
//compute a product (chunk times x) for each chunk
double* ctd = new double[_m*_k];
for (size_t i=0; i<num_chunks; i++) {
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
(int) _m,(int) _k,(int) _n, 1,
chunks+(_m*_n*i),(int) _n, dX, (int) _k, 0, ctd, (int) _k);
if (!use_neg || i<num_chunks-1)
for (size_t j=0; j<_m*_k; j++) {
// up to 53 bits will be ored-in, to be summed later
unsigned char* bitDest = combined;
bitDest += rclen*((i % rc)*_m*_k+j);
long long mask = static_cast<long long>(ctd[j]);
bitDest += 2*i;
*(reinterpret_cast<long long*>(bitDest) ) |= mask;
}
}
delete[] dX;
for (size_t i=0; i<_m*_k; i++) {
LinBox::integer result, tmp;
if (use_neg) {
result = -ctd[i];
result <<= (num_chunks-1)*16;
#ifdef DEBUG_CHUNK_APPLYM
cout << "rcneg: " << result << endl;
#endif
}
else
result = 0;
for (int j=0; j<rc; j++) {
unsigned char* thispos = combined + rclen*(j*_m*_k+i);
importWords(tmp, (size_t)rclen, -1, 1, 0, 0, thispos);
result += tmp;
#ifdef DEBUG_CHUNK_APPLYM
cout << "rc[" << j << "," << i << "]:" << tmp << endl;
#endif
}
#ifdef DEBUG_CHUNK_APPLYM
cout << "v2[" << i << "]:" << result << endl;
#endif
_domain.init(*(Y.getWritePointer()+i), result);
}
delete[] combined;
delete[] ctd;
}
}
return Y;
}
protected:
Domain _domain;
const IMatrix &_matM;
MatrixDomain<Domain> _MD;
size_t _m;
size_t _n;
// data initialize by setup
bool use_chunks;
bool use_neg;
size_t chunk_size;
size_t num_chunks;
double * chunks;
double * vchunks;
integer shift;
ApplyChoice _switcher;
MultiModDouble *_rns;
Element _prime, _q, _inv_q, _pq, _h_pq;
mutable Timer _apply, _convert_data, _convert_result;
};
// #if !defined (__INTEL_COMPILER) && !defined(__clang__)
// template<>
// #endif
template <class Domain>
class MatrixApplyDomain<Domain, BlasMatrix<Domain> > : public BlasMatrixApplyDomain<Domain, BlasMatrix<Domain> > {
public:
MatrixApplyDomain (const Domain &D, const BlasMatrix<Domain> &Mat) :
BlasMatrixApplyDomain<Domain, BlasMatrix<Domain> > (D,Mat)
{}
};
#if 0
// #ifndef __INTEL_COMPILER
// template<>
// #endif
template <class Domain>
class MatrixApplyDomain<Domain, BlasMatrix<Domain> > :
public BlasMatrixApplyDomain<Domain, BlasMatrix<Domain> > {
public:
MatrixApplyDomain (const Domain &D, const BlasMatrix<Domain> &Mat) :
BlasMatrixApplyDomain<Domain, BlasMatrix<Domain> > (D,Mat)
{}
};
#endif
/** \brief split an integer matrix into a padic chunk representation
*
*/
template <class Domain, class IMatrix>
void create_MatrixQadic (const Domain &D,
const IMatrix &Mat,
double *chunks,
size_t num_chunks,
const integer shift)
{
typename IMatrix::ConstIterator it= Mat.Begin();
size_t m,n,mn;
m = Mat.rowdim();
n = Mat.coldim();
mn = m*n;
size_t tmpsize, tmpbitsize, j;
if (num_chunks ==1)
for (size_t i=0; i<mn; ++i, ++it)
D.convert(*(chunks+i), *it);
else
for (size_t i=0; i<mn; ++i, ++it) {
integer tmp;
double* pdbl = chunks + i;
D.convert(tmp, *it);
tmp=tmp+shift;
tmpsize = tmp.size();
tmpbitsize = tmp.bitsize();
if (tmp ==0) {
*pdbl=0;
}
else
if (tmp > 0) {
//if (sizeof(long)==8 ) {
#if __LINBOX_SIZEOF_LONG == 8
// specialization for 64bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = double( tmp[j] & 0xFFFF);
*(pdbl+mn) = double((tmp[j] >> 16) & 0xFFFF);
*(pdbl+2*mn) = double((tmp[j] >> 32) & 0xFFFF);
*(pdbl+3*mn) = double((tmp[j] >> 48) & 0xFFFF);
pdbl += 4*mn;
}
if ((tmpbitsize - j*64) > 0 ) {
*pdbl = double(tmp[tmpsize-1]&0xFFFF);
pdbl+=mn;
}
if ((tmpbitsize - j*64) > 16 ) {
*pdbl = double((tmp[tmpsize-1] >> 16)& 0xFFFF);
pdbl+=mn;
}
if ((tmpbitsize - j*64) > 32 ) {
*pdbl = double((tmp[tmpsize-1] >> 32)& 0xFFFF);
pdbl+=mn;
}
if ((tmpbitsize - j*64) > 48 )
*pdbl = double((tmp[tmpsize-1] >> 48)& 0xFFFF);
//}
// else {
#else
// specialization for 32bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = double(tmp[j] & 0xFFFF);
*(pdbl+mn) = tmp[j] >> 16;
pdbl += 2*mn;
}
if ((tmpbitsize - j*32) > 16 ) {
*pdbl = double(tmp[tmpsize-1] & 0xFFFF);
*(pdbl+mn) = tmp[tmpsize-1] >> 16;
}
else {
*pdbl = double(tmp[tmpsize-1] & 0xFFFF);
}
//}
#endif
}
else {
++tmp;
#if __LINBOX_SIZEOF_LONG == 8
// specialization for 64bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = double(0xFFFF ^ ( tmp[j] & 0xFFFF));
*(pdbl+mn) = double(0xFFFF ^ ((tmp[j] >> 16) & 0xFFFF));
*(pdbl+2*mn) = double(0xFFFF ^ ((tmp[j] >> 32) & 0xFFFF));
*(pdbl+3*mn) = double(0xFFFF ^ ((tmp[j] >> 48) & 0xFFFF));
pdbl += 4*mn;
}
j=j<<2;
if ((tmpbitsize - j*64) > 0 ) {
*pdbl = double(0xFFFF ^ (tmp[tmpsize-1]&0xFFFF));
pdbl+=mn;
++j;
}
if ((tmpbitsize - j*64) > 16 ) {
*pdbl = double(0xFFFF ^ ((tmp[tmpsize-1] >> 16)& 0xFFFF));
pdbl+=mn;
++j;
}
if ((tmpbitsize - j*64) > 32 ) {
*pdbl = double(0xFFFF ^ ((tmp[tmpsize-1] >> 32)& 0xFFFF));
pdbl+=mn;
++j;
}
if ((tmpbitsize - j*64) > 48 ) {
*pdbl = double(0xFFFF ^ ((tmp[tmpsize-1] >> 48)& 0xFFFF));
++j;
}
for (; j<num_chunks-1; j++, pdbl += mn)
*pdbl = double(0xFFFF);
*pdbl = 1;
#else
// specialization for 32bits integer limb
for (j=0; j<tmpsize-1; j++) {
*pdbl = double(0xFFFF ^ (tmp[j] & 0xFFFF));
*(pdbl+mn) = double(0xFFFF ^ (tmp[j] >> 16));
pdbl += 2*mn;
}
j=j<<1;
if ((tmpbitsize -j*32) > 16) {
*pdbl = double(0xFFFF ^ (tmp[tmpsize-1] & 0xFFFF));
*(pdbl+mn) = double(0xFFFF ^ (tmp[tmpsize-1] >> 16));
pdbl += 2*mn;
j+=2;
}
else {
*pdbl = double(0xFFFF ^ (tmp[tmpsize-1] & 0xFFFF));
pdbl += mn;
j+=1;
}
for (; j<num_chunks-1; j++, pdbl += mn)
*pdbl = double(0xFFFF);
*pdbl = 1;
#endif
}
}
}
/** \brief split an integer vector into a padic chunk representation
*
*/
template <class Domain, class Vector>
void create_VectorQadic (const Domain &D,
const Vector &V,
double *chunks,
size_t num_chunks)
{
typename Vector::const_iterator it= V.begin();
size_t mn;
mn = V.size();
size_t tmpsize, tmpbitsize, j;
if (num_chunks ==1)
for (size_t i=0; i<mn; ++i, ++it)
D.convert(*(chunks+i), *it);
else
for (size_t i=0; i<mn; ++i, ++it) {
integer tmp;
double* pdbl = chunks + i*num_chunks;
D.convert(tmp, *it);
tmpsize = tmp.size();
tmpbitsize = tmp.bitsize();
if (tmp ==0)
*pdbl=0;
else
if (tmp > 0) {
#if __LINBOX_SIZEOF_LONG == 8
// specialization for 64bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = double( tmp[j] & 0xFFFF);
*(pdbl+1) = double((tmp[j] >> 16) & 0xFFFF);
*(pdbl+2) = double((tmp[j] >> 32) & 0xFFFF);
*(pdbl+3) = double((tmp[j] >> 48) & 0xFFFF);
pdbl += 4;
}
if ((tmpbitsize - j*64) > 0 ) {
*pdbl = double(tmp[tmpsize-1]&0xFFFF);
pdbl++;
}
if ((tmpbitsize - j*64) > 16 ) {
*pdbl = double((tmp[tmpsize-1] >> 16)& 0xFFFF);
pdbl++;
}
if ((tmpbitsize - j*64) > 32 ) {
*pdbl = double((tmp[tmpsize-1] >> 32)& 0xFFFF);
pdbl++;
}
if ((tmpbitsize - j*64) > 48 )
*pdbl = double((tmp[tmpsize-1] >> 48)& 0xFFFF);
#else
// specialization for 32bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = double(tmp[j] & 0xFFFF);
*(pdbl+1) = tmp[j] >> 16;
pdbl += 2;
}
if ((tmpbitsize - j*32) > 16 ) {
*pdbl = double(tmp[tmpsize-1] & 0xFFFF);
*(pdbl+1) = tmp[tmpsize-1] >> 16;
}
else {
*pdbl = double(tmp[tmpsize-1] & 0xFFFF);
}
#endif
}
else {
++tmp;
#if __LINBOX_SIZEOF_LONG == 8
// specialization for 64bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = (double) (0xFFFF ^ ( tmp[j] & 0xFFFF));
*(pdbl+1) = (double) (0xFFFF ^ ((tmp[j] >> 16) & 0xFFFF));
*(pdbl+2) = (double) (0xFFFF ^ ((tmp[j] >> 32) & 0xFFFF));
*(pdbl+3) = (double) (0xFFFF ^ ((tmp[j] >> 48) & 0xFFFF));
pdbl += 4 ;
}
j=j<<2;
if ((tmpbitsize - j*64) > 0 ) {
*pdbl = (double) (0xFFFF ^ (tmp[tmpsize-1]&0xFFFF));
pdbl++;
++j;
}
if ((tmpbitsize - j*64) > 16 ) {
*pdbl = (double) (0xFFFF ^ ((tmp[tmpsize-1] >> 16)& 0xFFFF));
pdbl++;
++j;
}
if ((tmpbitsize - j*64) > 32 ) {
*pdbl = (double) (0xFFFF ^ ((tmp[tmpsize-1] >> 32)& 0xFFFF));
pdbl++;
++j;
}
if ((tmpbitsize - j*64) > 48 ) {
*pdbl = (double) (0xFFFF ^ ((tmp[tmpsize-1] >> 48)& 0xFFFF));
++j;
}
for (; j<num_chunks-1; j++, pdbl += mn)
*pdbl = (double) (0xFFFF);
*pdbl = 1;
#else
// specialization for 32bits integer limb
for (j=0; j<tmpsize-1; j++) {
*pdbl = (double) (0xFFFF ^ (tmp[j] & 0xFFFF));
*(pdbl+1) = (double) (0xFFFF ^ (tmp[j] >> 16));
pdbl += 2;
}
j=j<<1;
if ((tmpbitsize -j*32) > 16) {
*pdbl = (double) (0xFFFF ^ (tmp[tmpsize-1] & 0xFFFF));
*(pdbl+1) = (double) (0xFFFF ^ (tmp[tmpsize-1] >> 16));
pdbl += 2;
j+=2;
}
else {
*pdbl = (double) (0xFFFF ^ (tmp[tmpsize-1] & 0xFFFF));
pdbl += 1;
j+=1;
}
for (; j<num_chunks-1; j++, pdbl += mn)
*pdbl = (double) (0xFFFF);
*pdbl = 1;
#endif
}
}
}
/** \brief split an integer vector into a padic chunk representation
*
*/
template <class Domain, class Vector>
void create_VectorQadic_32 (const Domain &D,
const Vector &V,
double *chunks,
size_t num_chunks)
{
typename Vector::const_iterator it= V.begin();
size_t mn;
mn = V.size();
size_t tmpsize, tmpbitsize, j;
if (num_chunks ==1)
for (size_t i=0; i<mn; ++i, ++it)
D.convert(*(chunks+i), *it);
else
for (size_t i=0; i<mn; ++i, ++it) {
integer tmp;
double* pdbl = chunks + i*num_chunks;
D.convert(tmp, *it);
tmpsize = tmp.size();
tmpbitsize = tmp.bitsize();
if (tmp ==0)
*pdbl=0;
else
if (tmp > 0) {
#if __LINBOX_SIZEOF_LONG == 8
// specialization for 64bits integer limbs
for (j=0; j<tmpsize-1; j++) {
*pdbl = double(tmp[j] & 0xFFFFFFFF);
*(pdbl+1) = double(tmp[j] >> 32);
pdbl += 2;
}
if ((tmpbitsize - j*64) > 0 ) {
*pdbl = double(tmp[tmpsize-1]&0xFFFFFFFF);
pdbl++;
}
#else
// specialization for 32bits integer limbs
for (j=0; j<tmpsize; j++) {
*pdbl = tmp[j] ;
pdbl += 1;
}
#endif
}
else {
++tmp;
#if __LINBOX_SIZEOF_LONG == 8
// specialization for 64bits integer limbs
for (j=0; j<tmpsize; j++) {
*pdbl = double(0xFFFFFFFF ^ ( tmp[j] & 0xFFFFFFFF));
*(pdbl+2) = double(0xFFFFFFFF ^ ( tmp[j] >> 32));
pdbl += 2 ;
}
j=j<<2;
if ((tmpbitsize - j*64) > 0 ) {
*pdbl = double(0xFFFFFFFF ^ (tmp[tmpsize-1]&0xFFFFFFFF));
pdbl++;
++j;
}
if ((tmpbitsize - j*64) > 32 ) {
*pdbl = double(0xFFFF ^ (tmp[tmpsize-1] >> 32));
pdbl++;
++j;
}
for (; j<num_chunks-1; j++, pdbl += mn)
*pdbl = double(0xFFFFFFFF);
*pdbl = 1;
#else
// specialization for 32bits integer limb
for (j=0; j<tmpsize-1; j++) {
*pdbl = double(0xFFFFFFFF ^ tmp[j]);
pdbl += 1;
}
j=j<<1;
for (; j<num_chunks-1; j++, pdbl += mn)
*pdbl = double(0xFFFF);
*pdbl = 1;
#endif
}
}
}
template <class Domain, class IMatrix>
void create_MatrixRNS (const MultiModDouble &F,
const Domain &D,
const IMatrix &Mat,
double *chunks)
{
size_t rns_size= F.size();
typename IMatrix::ConstIterator it = Mat.Begin();
size_t mn = Mat.rowdim()*Mat.coldim();
integer tmp;
for (size_t i=0; i< mn; ++i, ++it){
D.convert(tmp,*it);
for (size_t j=0;j< rns_size; ++j)
F.getBase(j).init(chunks[i+j*mn], tmp);
}
}
template <class Domain, class IVector>
void create_VectorRNS (const MultiModDouble &F,
const Domain &D,
const IVector &V,
double *chunks)
{
size_t rns_size= F.size();
typename IVector::const_iterator it= V.begin();
size_t mn = V.size();
integer tmp;
for (size_t i=0; i< mn; ++i, ++it){
D.convert(tmp, *it);
for (size_t j=0;j< rns_size; ++j)
F.getBase(j).init(chunks[i+j*mn], tmp);
}
}
} // end of namespace LinBox
#endif // __LINBOX_apply_H
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
|