1 /* Copyright (c) FFLAS-FFPACK
2  * Written by Bastien Vialla <bastien.vialla@lirmm.fr>
3  * ========LICENCE========
4  * This file is part of the library FFLAS-FFPACK.
5  *
6  * FFLAS-FFPACK is free software: you can redistribute it and/or modify
7  * it under the terms of the  GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  * ========LICENCE========
20  */
21 
22 #define __DLP_CHALLENGE
23 
24 #include <iostream>
25 #include <vector>
26 #include <sstream>
27 #include <cstdio>
28 #include <cstdlib>
29 
30 #include "gmpxx.h"
31 #include <givaro/zring.h>
32 #include <givaro/modular.h>
33 #include <givaro/modular-balanced.h>
34 #include <givaro/givinteger.h>
35 #include <recint/recint.h>
36 #include <givaro/givintprime.h>
37 
38 
39 #include "fflas-ffpack/fflas/fflas_sparse.h"
40 #include "fflas-ffpack/utils/args-parser.h"
41 #include "fflas-ffpack/field/rns-integer-mod.h"
42 #include "fflas-ffpack/fflas/fflas_sparse/read_sparse.h"
43 #include "fflas-ffpack/utils/timer.h"
44 #include "fflas-ffpack/utils/test-utils.h"
45 #include "fflas-ffpack/utils/flimits.h"
46 
47 #ifdef __FFLASFFPACK_USE_OPENMP
48 typedef FFLAS::OMPTimer TTimer;
49 #else
50 typedef FFLAS::Timer TTimer;
51 #endif
52 
53 using namespace RecInt;
54 using namespace std;
55 using namespace FFLAS;
56 using namespace Givaro;
57 
58 
main(int argc,char ** argv)59 int main(int argc, char **argv) {
60     using Field        = Modular<Integer>;
61     using FieldMat     = ZRing<double>;
62     using FieldComp    = FFPACK::RNSIntegerMod<FFPACK::rns_double>;
63     using FieldElement = RecInt::rmint<7>;
64     using FieldRec     = ZRing<FieldElement>;
65     using SparseMatrix = FFLAS::Sparse<FieldRec, FFLAS::SparseMatrix_t::HYB_ZO>;
66 
67     uint64_t seed = getSeed();
68     Integer q = -1;
69     int b = 128;
70     int blockSize = 1;
71     std::string matrixFile = "data/mat11.sms";
72     int nIter = 100;
73 
74     static Argument as[] = {
75         { 'q', "-q Q", "Set the field characteristic (-1 for random).",   TYPE_INTEGER , &q },
76         { 'b', "-b B", "Set the bitsize of the random characteristic.",   TYPE_INT , &b },
77         { 'k', "-k K", "Set the size of the block (1 by default).",       TYPE_INT, &blockSize },
78         { 'n', "-n N", "Set the size of the block (1 by default).",       TYPE_INT, &nIter },
79         { 'f', "-f FILE", "Set matrix file.",                             TYPE_STR, &matrixFile },
80         { 's', "-s seed", "Set seed for the random generator", TYPE_INT, &seed },
81         END_OF_ARGUMENTS };
82 
83     FFLAS::parseArguments(argc, argv, as);
84 
85     // Construct Givaro::Integer field
86     Field *F= chooseField<Field>(q,b,seed);
87     if (F==nullptr) exit(0);
88     Integer p;
89     F->cardinality(p);
90     cout << "Prime p: " << p << endl;
91 
92     RecInt::ruint<7> pRec;
93     // RecInt::mpz_to_ruint(pRec, FieldElement(p));
94     FieldElement::init_module(ruint<7>(p));
95     FieldRec Frec;
96     // Pointers for the matrix
97     index_t *row = nullptr, *col = nullptr;
98     typename FieldRec::Element_ptr dat;
99     index_t rowdim, coldim;
100     uint64_t nnz;
101 
102     // Read the matrix
103     readSmsFormat(matrixFile, Frec, row, col, dat, rowdim, coldim, nnz);
104     vector<index_t> rowCoo(nnz, 0);
105     for(size_t i = 0 ; i < rowdim ; ++i){
106         for(size_t j = row[i] ; j < row[i+1] ; ++j){
107             rowCoo[j] = i;
108         }
109     }
110 
111     // Build the matrix
112     SparseMatrix A;
113     FFLAS::sparse_init(Frec, A, rowCoo.data(), col, dat, rowdim, coldim, nnz);
114 
115     FFLAS::fflas_delete(row);
116     FFLAS::fflas_delete(col);
117     FFLAS::fflas_delete(dat);
118     rowCoo.resize(0);
119 
120     vector<FieldElement> x(coldim*blockSize, 1), y(rowdim*blockSize, 0);
121 
122     pfspmm(Frec, A, blockSize, x.data(), blockSize, 0, y.data(), blockSize);
123 
124     return 0;
125 }
126 
127 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
128 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
129